Module: Ekylibre::Access

Defined in:
lib/ekylibre/access.rb,
lib/ekylibre/access/right.rb

Defined Under Namespace

Classes: Right

Class Method Summary collapse

Class Method Details

.add_right(resource, interaction, options = {}) ⇒ Object

Add an access right


25
26
27
28
29
30
31
# File 'lib/ekylibre/access.rb', line 25

def add_right(resource, interaction, options = {})
  right = Right.new(resource, interaction, options)
  # @rights << right unless @rights.include?(right)
  @resources ||= {}.with_indifferent_access
  @resources[right.resource] ||= {}.with_indifferent_access
  @resources[right.resource][right.interaction] = right
end

.all_rightsObject

Returns a hash for all known rights


69
70
71
72
73
74
# File 'lib/ekylibre/access.rb', line 69

def all_rights
  @resources.each_with_object({}) do |pair, hash|
    hash[pair.first.to_s] = pair.second.keys.map(&:to_s)
    hash
  end
end

.config_fileObject


7
8
9
# File 'lib/ekylibre/access.rb', line 7

def config_file
  Rails.root.join('config', 'rights.yml')
end

.find(resource, interaction) ⇒ Object

Find a given right by resource and interaction


41
42
43
44
# File 'lib/ekylibre/access.rb', line 41

def find(resource, interaction)
  return @resources[resource][interaction] if @resources[resource]
  nil
end

.human_interaction_name(interaction) ⇒ Object

Returns the translated name of an interaction (with a resource)


52
53
54
# File 'lib/ekylibre/access.rb', line 52

def human_interaction_name(interaction)
  "access.interactions.#{interaction}".t
end

.human_resource_name(resource) ⇒ Object

Returns the translated name of a resource


47
48
49
# File 'lib/ekylibre/access.rb', line 47

def human_resource_name(resource)
  "access.resources.#{resource}".t
end

.interactionsObject

Returns list of interactions


57
58
59
60
61
# File 'lib/ekylibre/access.rb', line 57

def interactions
  @resources.collect do |_name, interactions|
    interactions.keys
  end.flatten.uniq.sort.map(&:to_sym)
end

.interactions_of(resource) ⇒ Object

Returns interactions of a given resource


64
65
66
# File 'lib/ekylibre/access.rb', line 64

def interactions_of(resource)
  @resources[resource].keys
end

.load_file(file, origin = :unknown) ⇒ Object

Load a right definition file


12
13
14
15
16
17
18
# File 'lib/ekylibre/access.rb', line 12

def load_file(file, origin = :unknown)
  YAML.load_file(file).each do |resource, interactions|
    interactions.each do |interaction, options|
      add_right(resource, interaction, options.symbolize_keys.merge(origin: origin))
    end
  end
end

.remove_right(resource, interaction) ⇒ Object

Remove an access right


34
35
36
37
38
# File 'lib/ekylibre/access.rb', line 34

def remove_right(resource, interaction)
  right = find(resource, interaction)
  # @rights.delete(right)
  @resources[resource].delete(right)
end

.resourcesObject


20
21
22
# File 'lib/ekylibre/access.rb', line 20

def resources
  @resources.deep_symbolize_keys
end

.rights_of(action) ⇒ Object


76
77
78
79
80
81
82
83
84
# File 'lib/ekylibre/access.rb', line 76

def rights_of(action)
  list = []
  @resources.each do |_resource, interactions|
    interactions.each do |_interaction, right|
      list << right.name if right.actions.include?(action)
    end
  end
  list
end