Class: PolicyMachineStorageAdapter::InMemory
- Inherits:
-
Object
- Object
- PolicyMachineStorageAdapter::InMemory
- Defined in:
- lib/policy_machine_storage_adapters/in_memory.rb
Defined Under Namespace
Classes: PersistedPolicyElement
Constant Summary collapse
- POLICY_ELEMENT_TYPES =
%w(user user_attribute object object_attribute operation policy_class)
Instance Method Summary collapse
-
#add_association(user_attribute, operation_set, object_attribute, policy_machine_uuid) ⇒ Object
Add the given association to the policy map.
-
#assign(src, dst) ⇒ Object
Assign src to dst in policy machine.
-
#associations_with(operation) ⇒ Object
Return all associations in which the given operation is included Returns an array of arrays.
-
#connected?(src, dst) ⇒ Boolean
Determine if there is a path from src to dst in the policy machine.
-
#delete(element) ⇒ Object
Remove a persisted policy element.
-
#element_in_machine?(pe) ⇒ Boolean
Determine if the given node is in the policy machine or not.
-
#policy_classes_for_object_attribute(object_attribute) ⇒ Object
Return array of all policy classes which contain the given object_attribute (or object).
-
#transaction ⇒ Object
Execute the passed-in block transactionally: any error raised out of the block causes all the block’s changes to be rolled back.
-
#unassign(src, dst) ⇒ Object
Disconnect two policy elements in the machine.
-
#update(element, changes_hash) ⇒ Object
Update a persisted policy element.
-
#user_attributes_for_user(user) ⇒ Object
Return array of all user attributes which contain the given user.
Instance Method Details
#add_association(user_attribute, operation_set, object_attribute, policy_machine_uuid) ⇒ Object
Add the given association to the policy map. If an association between user_attribute and object_attribute already exists, then replace it with that given in the arguments.
106 107 108 109 110 111 112 |
# File 'lib/policy_machine_storage_adapters/in_memory.rb', line 106 def add_association(user_attribute, operation_set, object_attribute, policy_machine_uuid) # TODO: scope by policy machine uuid associations[user_attribute.unique_identifier + object_attribute.unique_identifier] = [user_attribute, operation_set, object_attribute] true end |
#assign(src, dst) ⇒ Object
Assign src to dst in policy machine
44 45 46 47 48 49 50 |
# File 'lib/policy_machine_storage_adapters/in_memory.rb', line 44 def assign(src, dst) assert_persisted_policy_element(src) assert_persisted_policy_element(dst) assignments << [src, dst] true end |
#associations_with(operation) ⇒ Object
Return all associations in which the given operation is included Returns an array of arrays. Each sub-array is of the form
- user_attribute, operation_set, object_attribute
118 119 120 121 122 123 124 |
# File 'lib/policy_machine_storage_adapters/in_memory.rb', line 118 def associations_with(operation) matching = associations.values.select do |assoc| assoc[1].include?(operation) end matching.map{ |m| [m[0], m[1], m[2]] } end |
#connected?(src, dst) ⇒ Boolean
Determine if there is a path from src to dst in the policy machine
55 56 57 58 59 60 61 62 63 |
# File 'lib/policy_machine_storage_adapters/in_memory.rb', line 55 def connected?(src, dst) assert_persisted_policy_element(src) assert_persisted_policy_element(dst) return true if src == dst distances = dijkstra(src, dst) distances.nil? ? false : true end |
#delete(element) ⇒ Object
Remove a persisted policy element
84 85 86 87 88 |
# File 'lib/policy_machine_storage_adapters/in_memory.rb', line 84 def delete(element) assignments.delete_if{ |assgn| assgn.include?(element) } associations.delete_if { |_,assoc| assoc.include?(element) } policy_elements.delete(element) end |
#element_in_machine?(pe) ⇒ Boolean
Determine if the given node is in the policy machine or not.
99 100 101 |
# File 'lib/policy_machine_storage_adapters/in_memory.rb', line 99 def element_in_machine?(pe) policy_elements.member?( pe ) end |
#policy_classes_for_object_attribute(object_attribute) ⇒ Object
Return array of all policy classes which contain the given object_attribute (or object). Return empty array if no such policy classes found.
129 130 131 132 133 |
# File 'lib/policy_machine_storage_adapters/in_memory.rb', line 129 def policy_classes_for_object_attribute(object_attribute) find_all_of_type_policy_class.select do |pc| connected?(object_attribute, pc) end end |
#transaction ⇒ Object
Execute the passed-in block transactionally: any error raised out of the block causes all the block’s changes to be rolled back.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/policy_machine_storage_adapters/in_memory.rb', line 147 def transaction old_state = dup instance_variables.each do |var| value = instance_variable_get(var) if (value.respond_to?(:dup)) old_state.instance_variable_set(var, value.dup) end end begin yield rescue Exception instance_variables.each do |var| value = old_state.instance_variable_get(var) instance_variable_set(var, value) end raise end end |
#unassign(src, dst) ⇒ Object
Disconnect two policy elements in the machine
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/policy_machine_storage_adapters/in_memory.rb', line 68 def unassign(src, dst) assert_persisted_policy_element(src) assert_persisted_policy_element(dst) assignment = assignments.find{|assgn| assgn[0] == src && assgn[1] == dst} if assignment assignments.delete(assignment) true else false end end |
#update(element, changes_hash) ⇒ Object
Update a persisted policy element
93 94 95 |
# File 'lib/policy_machine_storage_adapters/in_memory.rb', line 93 def update(element, changes_hash) element.send(:extra_attributes).merge!(changes_hash) end |
#user_attributes_for_user(user) ⇒ Object
Return array of all user attributes which contain the given user. Return empty array if no such user attributes are found.
138 139 140 141 142 |
# File 'lib/policy_machine_storage_adapters/in_memory.rb', line 138 def user_attributes_for_user(user) find_all_of_type_user_attribute.select do |user_attribute| connected?(user, user_attribute) end end |