Class: Strongbolt::Capability

Inherits:
Base
  • Object
show all
Defined in:
lib/strongbolt/capability.rb

Constant Summary collapse

Actions =
%w[find create update destroy].freeze
DEFAULT_MODELS =
['Strongbolt::UserGroup',
'Strongbolt::Role',
'Strongbolt::Capability',
'Strongbolt::UsersTenant'].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Bolted

included

Class Attribute Details

.modelsObject

List all the models to be used in capabilities



30
31
32
# File 'lib/strongbolt/capability.rb', line 30

def self.models
  @models ||= DEFAULT_MODELS
end

Class Method Details

.actions_from_list(actions) ⇒ Object

Virtual setter of actions



131
132
133
134
135
136
137
138
# File 'lib/strongbolt/capability.rb', line 131

def self.actions_from_list(actions)
  # Transform actions array
  if actions.respond_to?(:to_sym) && actions.to_sym == :all
    Actions # All actions
  else
    [*actions] # Transform into an array
  end
end

.add_models(models) ⇒ Object



38
39
40
41
42
# File 'lib/strongbolt/capability.rb', line 38

def self.add_models(models)
  @models ||= DEFAULT_MODELS
  @models |= [*models]
  @models.sort!
end

.from_hash(hash) ⇒ Object

Create a set capabilities from a hash which has:

model: "ModelName",
require_ownership: true,
require_tenant_access: false,
actions: [:find, :update]

Actions can be either one operation, an array of operations, or :all meaning all operations



118
119
120
121
122
123
124
125
126
# File 'lib/strongbolt/capability.rb', line 118

def self.from_hash(hash)
  hash.symbolize_keys!
  actions_from_list(hash[:actions]).map do |action|
    new model: hash[:model],
        require_ownership: hash[:require_ownership],
        require_tenant_access: hash[:require_tenant_access],
        action: action
  end
end

.to_hashObject

Group by model, ownership and tenant access and tells whether each action is set or not in a hash



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/strongbolt/capability.rb', line 86

def self.to_hash
  hash = {}
  all.ordered.each do |capability|
    key = {
      model: capability.model,
      require_ownership: capability.require_ownership,
      require_tenant_access: capability.require_tenant_access
    }

    hash[key] ||= {
      find: false,
      create: false,
      update: false,
      destroy: false
    }

    hash[key][capability.action.to_sym] = true
  end
  hash
end

.to_tableObject

Group by model, ownership and tenant access and tells whether each action is set or not



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/strongbolt/capability.rb', line 57

def self.to_table
  table = []
  all.ordered.each do |capability|
    if table.last.nil? ||
       !(table.last[:model] == capability.model &&
         table.last[:require_ownership] == capability.require_ownership &&
         table.last[:require_tenant_access] == capability.require_tenant_access)

      table << {
        model: capability.model,
        require_ownership: capability.require_ownership,
        require_tenant_access: capability.require_tenant_access,
        find: false,
        create: false,
        update: false,
        destroy: false
      }
    end

    table.last[capability.action.to_sym] = true
  end
  table
end