Class: Controlist::Permissions::OrderedPackage

Inherits:
Object
  • Object
show all
Defined in:
lib/controlist/permissions/ordered_package.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*permissions) ⇒ OrderedPackage

Returns a new instance of OrderedPackage.



8
9
10
11
12
13
14
15
16
17
# File 'lib/controlist/permissions/ordered_package.rb', line 8

def initialize(*permissions)
  permissions.compact!
  @list_create = {}
  @list_read = {}
  @list_update = {}
  @list_delete = {}
  @permissions = permissions
  @permissions.freeze # avoid bypassing add_permissions/remove_permissions
  add_permissions *permissions
end

Instance Attribute Details

#list_createObject (readonly)

Returns the value of attribute list_create.



6
7
8
# File 'lib/controlist/permissions/ordered_package.rb', line 6

def list_create
  @list_create
end

#list_deleteObject (readonly)

Returns the value of attribute list_delete.



6
7
8
# File 'lib/controlist/permissions/ordered_package.rb', line 6

def list_delete
  @list_delete
end

#list_readObject (readonly)

Returns the value of attribute list_read.



6
7
8
# File 'lib/controlist/permissions/ordered_package.rb', line 6

def list_read
  @list_read
end

#list_updateObject (readonly)

Returns the value of attribute list_update.



6
7
8
# File 'lib/controlist/permissions/ordered_package.rb', line 6

def list_update
  @list_update
end

#permissionsObject (readonly)

Returns the value of attribute permissions.



6
7
8
# File 'lib/controlist/permissions/ordered_package.rb', line 6

def permissions
  @permissions
end

Instance Method Details

#add_permissions(*permissions) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/controlist/permissions/ordered_package.rb', line 19

def add_permissions(*permissions)
  @permissions += permissions
  @permissions.freeze
  permissions.each do |permission|
    operations = permission.operations
    add @list_create, permission if operations.nil? || operations.include?(CREATE)
    add @list_read, permission if operations.nil? ||  operations.include?(READ)
    add @list_update, permission if operations.nil? ||  operations.include?(UPDATE)
    add @list_delete, permission if operations.nil? ||  operations.include?(DELETE)
  end
end

#has_permission(klass, operation) ⇒ Object



43
44
45
46
47
48
# File 'lib/controlist/permissions/ordered_package.rb', line 43

def has_permission(klass, operation)
  permission_list = instance_variable_get("@list_#{operation}")
  if permission_list && (permissions = permission_list[klass])
    permissions.any?(&:is_allowed)
  end
end

#remove_permissions(*permissions) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/controlist/permissions/ordered_package.rb', line 31

def remove_permissions(*permissions)
  @permissions -= permissions
  @permissions.freeze
  permissions.each do |permission|
    operations = permission.operations
    remove @list_create, permission if operations.nil? || operations.include?(CREATE)
    remove @list_read, permission if operations.nil? ||  operations.include?(READ)
    remove @list_update, permission if operations.nil? ||  operations.include?(UPDATE)
    remove @list_delete, permission if operations.nil? ||  operations.include?(DELETE)
  end
end