Skip to content

Commit

Permalink
Merge pull request #1 from mnaamani/permissions
Browse files Browse the repository at this point in the history
Permissions Layer for Versioned store
  • Loading branch information
mnaamani authored Jan 10, 2020
2 parents b45c1e6 + 8c27944 commit 816b796
Show file tree
Hide file tree
Showing 9 changed files with 1,907 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Generated by Cargo
# will have compiled files and executables
**/target/

# Cargo lock file for native runtime - only used for cargo test
./Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# JetBrains IDEs
.idea

# Vim
.*.sw*
43 changes: 43 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[package]
name = 'substrate-versioned-store-permissions-module'
version = '1.0.0'
authors = ['Mokhtar Naamani <mokhtar.naamani@gmail.com>']
edition = '2018'

[dependencies]
hex-literal = '0.1.0'
serde = { version = '1.0', optional = true }
serde_derive = { version = '1.0', optional = true }
codec = { package = 'parity-scale-codec', version = '1.0.0', default-features = false, features = ['derive'] }
rstd = { package = 'sr-std', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = '0e3001a1ad6fa3d1ba7da7342a8d0d3b3facb2f3'}
runtime-primitives = { package = 'sr-primitives', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = '0e3001a1ad6fa3d1ba7da7342a8d0d3b3facb2f3'}
srml-support = { package = 'srml-support', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = '0e3001a1ad6fa3d1ba7da7342a8d0d3b3facb2f3'}
srml-support-procedural = { package = 'srml-support-procedural', git = 'https://github.com/paritytech/substrate.git', rev = '0e3001a1ad6fa3d1ba7da7342a8d0d3b3facb2f3'}
system = { package = 'srml-system', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = '0e3001a1ad6fa3d1ba7da7342a8d0d3b3facb2f3'}
timestamp = { package = 'srml-timestamp', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = '0e3001a1ad6fa3d1ba7da7342a8d0d3b3facb2f3'}
runtime-io = { package = 'sr-io', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = '0e3001a1ad6fa3d1ba7da7342a8d0d3b3facb2f3'}

[dependencies.versioned-store]
default_features = false
package ='substrate-versioned-store'
git = 'https://github.com/joystream/substrate-versioned-store-module'
rev = 'd0c68722405355404840512edf3064d5ced3e1fe'

[dev-dependencies]
runtime-io = { package = 'sr-io', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = '0e3001a1ad6fa3d1ba7da7342a8d0d3b3facb2f3'}
primitives = { package = 'substrate-primitives', git = 'https://github.com/paritytech/substrate.git', rev = '0e3001a1ad6fa3d1ba7da7342a8d0d3b3facb2f3'}

[features]
default = ['std']
std = [
'serde',
'serde_derive',
'codec/std',
'rstd/std',
'runtime-io/std',
'runtime-primitives/std',
'srml-support/std',
'system/std',
'timestamp/std',
'versioned-store/std',
]
28 changes: 28 additions & 0 deletions src/constraint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use codec::{Decode, Encode};
use rstd::collections::btree_set::BTreeSet;

/// Reference to a specific property of a specific class.
#[derive(Encode, Decode, Eq, PartialEq, Ord, PartialOrd, Clone, Debug)]
pub struct PropertyOfClass<ClassId, PropertyIndex> {
pub class_id: ClassId,
pub property_index: PropertyIndex,
}

/// The type of constraint imposed on referencing a class via class property of type "Internal".
#[derive(Encode, Decode, Eq, PartialEq, Clone, Debug)]
pub enum ReferenceConstraint<ClassId: Ord, PropertyIndex: Ord> {
/// No property can reference the class.
NoReferencingAllowed,

/// Any property of any class may reference the class.
NoConstraint,

/// Only a set of properties of specific classes can reference the class.
Restricted(BTreeSet<PropertyOfClass<ClassId, PropertyIndex>>),
}

impl<ClassId: Ord, PropertyIndex: Ord> Default for ReferenceConstraint<ClassId, PropertyIndex> {
fn default() -> Self {
ReferenceConstraint::NoReferencingAllowed
}
}
57 changes: 57 additions & 0 deletions src/credentials.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use codec::{Decode, Encode};
use rstd::collections::btree_set::BTreeSet;
use rstd::prelude::*;

#[derive(Encode, Decode, Eq, PartialEq, Clone, Debug)]
pub struct CredentialSet<Credential>(BTreeSet<Credential>);

impl<Credential> From<Vec<Credential>> for CredentialSet<Credential>
where
Credential: Ord,
{
fn from(v: Vec<Credential>) -> CredentialSet<Credential> {
let mut set = CredentialSet(BTreeSet::new());
for credential in v.into_iter() {
set.insert(credential);
}
set
}
}

/// Default CredentialSet set is just an empty set.
impl<Credential: Ord> Default for CredentialSet<Credential> {
fn default() -> Self {
CredentialSet(BTreeSet::new())
}
}

impl<Credential: Ord> CredentialSet<Credential> {
pub fn new() -> Self {
Self(BTreeSet::new())
}

pub fn insert(&mut self, value: Credential) -> bool {
self.0.insert(value)
}

pub fn contains(&self, value: &Credential) -> bool {
self.0.contains(value)
}

pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}

/// Type, derived from dispatchable call, identifies the caller
#[derive(Encode, Decode, Eq, PartialEq, Ord, PartialOrd, Clone, Debug)]
pub enum AccessLevel<Credential> {
/// ROOT origin
System,
/// Caller identified as the entity maintainer
EntityMaintainer, // Maybe enclose EntityId?
/// Verified Credential
Credential(Credential),
/// In cases where a signed extrinsic doesn't provide a Credential
Unspecified,
}
Loading

0 comments on commit 816b796

Please sign in to comment.