Class: Checken::PermissionGroup

Inherits:
Object
  • Object
show all
Includes:
Concerns::HasParents
Defined in:
lib/checken/permission_group.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = {}
  @permissions = {}
  @defined_rules = {}
end

Instance Attribute Details

#defined_rulesObject (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

#descriptionObject

Returns the value of attribute description.



12
13
14
# File 'lib/checken/permission_group.rb', line 12

def description
  @description
end

#groupObject (readonly)

Returns the value of attribute group.



14
15
16
# File 'lib/checken/permission_group.rb', line 14

def group
  @group
end

#groupsObject (readonly)

Returns the value of attribute groups.



16
17
18
# File 'lib/checken/permission_group.rb', line 16

def groups
  @groups
end

#keyObject (readonly)

Returns the value of attribute key.



15
16
17
# File 'lib/checken/permission_group.rb', line 15

def key
  @key
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/checken/permission_group.rb', line 11

def name
  @name
end

#permissionsObject (readonly)

Returns the value of attribute permissions.



17
18
19
# File 'lib/checken/permission_group.rb', line 17

def permissions
  @permissions
end

#schemaObject (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 [](group_or_permission_key)
  @groups[group_or_permission_key.to_sym] || @permissions[group_or_permission_key.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 group_or_permission(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 add_permission(key)
  key = key.to_sym
  if group_or_permission(key).nil?
    @permissions[key] = Permission.new(self, key)
  else
    raise Error, "Group or permission with key of #{key} already exists"
  end
end

#all_defined_rulesArray<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_permissionsArray<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 all_permissions
  array = []
  @permissions.each { |_, permission| array << permission }
  @groups.each do |_, group|
    group.all_permissions.each do |permission|
      array << permission
    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(options = {}, &block)
  dsl = DSL::GroupDSL.new(self, options)
  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 find_permissions_from_path(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)
  last_group_or_permission = 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 last_group_or_permission.permissions.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 last_group_or_permission.all_permissions
    else
      last_group_or_permission = last_group_or_permission.group_or_permission(part)
      if last_group_or_permission.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 last_group_or_permission.nil?
        raise PermissionNotFoundError, "No permission found matching '#{path}'"
      end
    end
  end

  if last_group_or_permission.is_a?(Permission)
    [last_group_or_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 group_or_permission(key)
  key = key.to_sym
  @groups[key] || @permissions[key]
end

#update_schemaObject



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