Class: Checken::PermissionGroup
- Inherits:
-
Object
- Object
- Checken::PermissionGroup
- Includes:
- Concerns::HasParents
- Defined in:
- lib/checken/permission_group.rb
Instance Attribute Summary collapse
-
#defined_rules ⇒ Object
readonly
Returns the value of attribute defined_rules.
-
#description ⇒ Object
Returns the value of attribute description.
-
#group ⇒ Object
readonly
Returns the value of attribute group.
-
#groups ⇒ Object
readonly
Returns the value of attribute groups.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#name ⇒ Object
Returns the value of attribute name.
-
#permissions ⇒ Object
readonly
Returns the value of attribute permissions.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Instance Method Summary collapse
-
#[](group_or_permission_key) ⇒ Checken::PermissionGroup, ...
Return a group or permission matching the given key.
-
#add_group(key) ⇒ Checken::PermissionGroup
Adds a new sub group to this group.
-
#add_permission(key) ⇒ Checken::Permission
Adds a permission to the group.
-
#all_defined_rules ⇒ Array<Checken::Rule>
Return an array of all defined rules on this and all upper groups.
-
#all_permissions ⇒ Array<Checken::Permission>
Return all permissions in this group and all the permissions in its sub groups.
-
#define_rule(key, *required_object_types, &block) ⇒ Checken::Rule
Define a new global rule that can be used by any permissions.
-
#dsl(options = {}, &block) ⇒ Checken::DSL::GroupDSL
Execute the given block within the group DSL.
-
#find_permissions_from_path(path) ⇒ Object
Find permissions from a path.
-
#group_or_permission(key) ⇒ Cheken::PermissionGroup, Checken::Permission
Return a group or a permission that matches the given key.
-
#initialize(schema, group, key = nil) ⇒ PermissionGroup
constructor
Create a new permission group.
- #update_schema ⇒ Object
Methods included from Concerns::HasParents
#parents, #path, #path_with_namespace, #root
Constructor Details
#initialize(schema, group, key = nil) ⇒ PermissionGroup
Create a new permission group
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/checken/permission_group.rb', line 32 def initialize(schema, group, key = nil) if group && key.nil? raise Error, "Cannot create a new non-root permission group without a key" elsif group.nil? && key raise Error, "Cannot create a new root permission group with a key" end @schema = schema @group = group @key = key.to_sym if key @groups = {} = {} @defined_rules = {} end |
Instance Attribute Details
#defined_rules ⇒ Object (readonly)
Returns the value of attribute defined_rules.
18 19 20 |
# File 'lib/checken/permission_group.rb', line 18 def defined_rules @defined_rules end |
#description ⇒ Object
Returns the value of attribute description.
12 13 14 |
# File 'lib/checken/permission_group.rb', line 12 def description @description end |
#group ⇒ Object (readonly)
Returns the value of attribute group.
14 15 16 |
# File 'lib/checken/permission_group.rb', line 14 def group @group end |
#groups ⇒ Object (readonly)
Returns the value of attribute groups.
16 17 18 |
# File 'lib/checken/permission_group.rb', line 16 def groups @groups end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
15 16 17 |
# File 'lib/checken/permission_group.rb', line 15 def key @key end |
#name ⇒ Object
Returns the value of attribute name.
11 12 13 |
# File 'lib/checken/permission_group.rb', line 11 def name @name end |
#permissions ⇒ Object (readonly)
Returns the value of attribute permissions.
17 18 19 |
# File 'lib/checken/permission_group.rb', line 17 def end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
13 14 15 |
# File 'lib/checken/permission_group.rb', line 13 def schema @schema end |
Instance Method Details
#[](group_or_permission_key) ⇒ Checken::PermissionGroup, ...
Return a group or permission matching the given key
24 25 26 |
# File 'lib/checken/permission_group.rb', line 24 def []() @groups[.to_sym] || [.to_sym] end |
#add_group(key) ⇒ Checken::PermissionGroup
Adds a new sub group to this group
59 60 61 62 63 64 65 66 |
# File 'lib/checken/permission_group.rb', line 59 def add_group(key) key = key.to_sym if (key).nil? @groups[key] = PermissionGroup.new(@schema, self, key) else raise Error, "Group or permission with key of #{key} already exists" end end |
#add_permission(key) ⇒ Checken::Permission
Adds a permission to the group
72 73 74 75 76 77 78 79 |
# File 'lib/checken/permission_group.rb', line 72 def (key) key = key.to_sym if (key).nil? [key] = Permission.new(self, key) else raise Error, "Group or permission with key of #{key} already exists" end end |
#all_defined_rules ⇒ Array<Checken::Rule>
Return an array of all defined rules on this and all upper groups
100 101 102 103 104 105 106 |
# File 'lib/checken/permission_group.rb', line 100 def all_defined_rules if @group @defined_rules.merge(@group.all_defined_rules) else @defined_rules end end |
#all_permissions ⇒ Array<Checken::Permission>
Return all permissions in this group and all the permissions in its sub groups
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/checken/permission_group.rb', line 149 def array = [] .each { |_, | array << } @groups.each do |_, group| group..each do || array << end end array end |
#define_rule(key, *required_object_types, &block) ⇒ Checken::Rule
Define a new global rule that can be used by any permissions
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/checken/permission_group.rb', line 86 def define_rule(key, *required_object_types, &block) if all_defined_rules[key.to_sym] raise Checken::Error, "Rule #{key} has already been defined" else rule = Rule.new(key, &block) required_object_types.each { |rot| rule.required_object_types << rot } @defined_rules[key.to_sym] = rule rule end end |
#dsl(options = {}, &block) ⇒ Checken::DSL::GroupDSL
Execute the given block within the group DSL
163 164 165 166 167 |
# File 'lib/checken/permission_group.rb', line 163 def dsl( = {}, &block) dsl = DSL::GroupDSL.new(self, ) dsl.instance_eval(&block) dsl end |
#find_permissions_from_path(path) ⇒ Object
Find permissions from a path
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 |
# File 'lib/checken/permission_group.rb', line 109 def (path) unless path.is_a?(String) && path.length > 0 raise PermissionNotFoundError, "Must provide a permission path" end path = parse_namespace(path) path_parts = path.split('.').map(&:to_sym) = self while part = path_parts.shift if part == :* if path_parts.empty? # We're at the end of the path, that's an acceptable place for a wildcard. # Return all the permissions in the final group. return ..values else raise Error, "Wildcards must be placed at the end of a permission path" end elsif part == :** && path_parts[0] == :* # If we get a **.* wildcard, we should find permissions in the sub groups too. return . else = .(part) if .is_a?(Permission) && !path_parts.empty? raise Error, "Permission found too early in the path. Permission key should always be at the end of the path." elsif .nil? raise PermissionNotFoundError, "No permission found matching '#{path}'" end end end if .is_a?(Permission) [] else raise Error, "Last part of path was not a permission. Last part of permission must be a path" end end |
#group_or_permission(key) ⇒ Cheken::PermissionGroup, Checken::Permission
Return a group or a permission that matches the given key
50 51 52 53 |
# File 'lib/checken/permission_group.rb', line 50 def (key) key = key.to_sym @groups[key] || [key] end |
#update_schema ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/checken/permission_group.rb', line 169 def update_schema return if path.nil? schema.update_schema( { path_with_namespace => { type: :group, name: name, description: description, group: group&.path } } ) end |