1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#![cfg_attr(not(feature = "std"), no_std)]

use codec::{Decode, Encode};
#[cfg(feature = "std")]
use serde::{Serialize, Deserialize};
use frame_support::{
  decl_module,
  traits::Get
};
use sp_runtime::RuntimeDebug;
use sp_std::{
  collections::btree_set::BTreeSet,
  prelude::*
};
use frame_system::{self as system};

use pallet_utils::SpaceId;

pub mod default_permissions;

#[derive(Encode, Decode, Ord, PartialOrd, Clone, Eq, PartialEq, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub enum SpacePermission {
  /// Create, update, delete, grant and revoke roles in this space.
  ManageRoles,

  /// Act on behalf of this space within this space.
  RepresentSpaceInternally,
  /// Act on behalf of this space outside of this space.
  RepresentSpaceExternally,

  /// Update this space.
  UpdateSpace,

  // Related to subspaces in this space:
  CreateSubspaces,
  UpdateOwnSubspaces,
  DeleteOwnSubspaces,
  HideOwnSubspaces,

  UpdateAnySubspace,
  DeleteAnySubspace,
  HideAnySubspace,

  // Related to posts in this space:
  CreatePosts,
  UpdateOwnPosts,
  DeleteOwnPosts,
  HideOwnPosts,

  UpdateAnyPost,
  DeleteAnyPost,
  HideAnyPost,

  // Related to comments in this space:
  CreateComments,
  UpdateOwnComments,
  DeleteOwnComments,
  HideOwnComments,

  // NOTE: It was made on purpose that it's not possible to update or delete not own comments.
  // Instead it's possible to allow to hide and block comments.
  HideAnyComment,

  /// Upvote any post or comment in this space.
  Upvote,
  /// Downvote any post or comment in this space.
  Downvote,
  /// Share any post or comment from this space to another outer space.
  Share,

  /// Override permissions per subspace in this space.
  OverrideSubspacePermissions,
  /// Override permissions per post in this space.
  OverridePostPermissions,

  // Related to the moderation pallet:

  /// Suggest new entity status in space (whether it's blocked or allowed)
  SuggestEntityStatus,
  /// Update entity status in space
  UpdateEntityStatus,

  // Related to space settings:

  /// Allows to update space settings across different pallets.
  UpdateSpaceSettings,
}

pub type SpacePermissionSet = BTreeSet<SpacePermission>;

/// These are a set of built-in roles which can be given different permissions within a given space.
/// For example: everyone can comment (`CreateComments`), but only followers can post 
/// (`CreatePosts`).
#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct SpacePermissions {

  /// None represents a set of permissions which is not capable of being performed by anyone.
  /// For example, if you want to create a space similar to Twitter, you would set the permissions 
  /// for `UpdateOwnPosts`, `UpdateOwnComments`, and `Downvote` to `none`.
  pub none: Option<SpacePermissionSet>,

  /// Everyone represents a set of permissions which are capable of being performed by every account
  /// in a given space.
  pub everyone: Option<SpacePermissionSet>,

  /// Follower represents a set of permissions which are capable of being performed by every account
  /// that follows a given space.
  pub follower: Option<SpacePermissionSet>,

  /// Space owner represents a set of permissions which are capable of being performed by an account
  /// that is a current owner of a given space.
  pub space_owner: Option<SpacePermissionSet>,
}

impl Default for SpacePermissions {
  fn default() -> SpacePermissions {
    SpacePermissions {
      none: None,
      everyone: None,
      follower: None,
      space_owner: None,
    }
  }
}

#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug)]
pub struct SpacePermissionsContext {
  pub space_id: SpaceId,
  pub is_space_owner: bool,
  pub is_space_follower: bool,
  pub space_perms: Option<SpacePermissions>
}

/// The pallet's configuration trait.
pub trait Trait: system::Trait {
  type DefaultSpacePermissions: Get<SpacePermissions>;
}

decl_module! {
  pub struct Module<T: Trait> for enum Call where origin: T::Origin {
    const DefaultSpacePermissions: SpacePermissions = T::DefaultSpacePermissions::get();
  }
}

impl SpacePermission {
  fn is_present_in_role(&self, perms_opt: Option<SpacePermissionSet>) -> bool {
    if let Some(perms) = perms_opt {
      if perms.contains(self) {
        return true
      }
    }
    false
  }
}

impl<T: Trait> Module<T> {

  fn get_overrides_or_defaults(
    overrides: Option<SpacePermissionSet>,
    defaults: Option<SpacePermissionSet>
  ) -> Option<SpacePermissionSet> {

    if overrides.is_some() {
      overrides
    } else {
      defaults
    }
  }

  fn resolve_space_perms(
    space_perms: Option<SpacePermissions>,
  ) -> SpacePermissions {

    let defaults = T::DefaultSpacePermissions::get();
    let overrides = space_perms.unwrap_or_default();

    SpacePermissions {
      none: Self::get_overrides_or_defaults(overrides.none, defaults.none),
      everyone: Self::get_overrides_or_defaults(overrides.everyone, defaults.everyone),
      follower: Self::get_overrides_or_defaults(overrides.follower, defaults.follower),
      space_owner: Self::get_overrides_or_defaults(overrides.space_owner, defaults.space_owner)
    }
  }

  pub fn has_user_a_space_permission(
    ctx: SpacePermissionsContext,
    permission: SpacePermission,
  ) -> Option<bool> {

    let perms_by_role = Self::resolve_space_perms(ctx.space_perms);

    // Check if this permission is forbidden:
    if permission.is_present_in_role(perms_by_role.none) {
      return Some(false)
    }

    let is_space_owner = ctx.is_space_owner;
    let is_follower = is_space_owner || ctx.is_space_follower;

    if
      permission.is_present_in_role(perms_by_role.everyone) ||
      is_follower && permission.is_present_in_role(perms_by_role.follower) ||
      is_space_owner && permission.is_present_in_role(perms_by_role.space_owner)
    {
      return Some(true)
    }

    None
  }

  pub fn override_permissions(mut overrides: SpacePermissions) -> SpacePermissions {
    overrides.none = overrides.none.map(
      |mut none_permissions_set| {
        none_permissions_set.extend(T::DefaultSpacePermissions::get().none.unwrap_or_default());
        none_permissions_set
      }
    );

    overrides
  }
}