Class: CommandTower::Authorization::Entity
- Inherits:
-
Object
- Object
- CommandTower::Authorization::Entity
- Defined in:
- lib/command_tower/authorization/entity.rb
Instance Attribute Summary collapse
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#except ⇒ Object
readonly
Returns the value of attribute except.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#only ⇒ Object
readonly
Returns the value of attribute only.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Class Method Summary collapse
- .create_entity(name:, controller:, only: nil, except: nil, source: :host) ⇒ Object
- .entities ⇒ Object
- .entities_reset! ⇒ Object
Instance Method Summary collapse
-
#authorized?(user:) ⇒ Boolean
This is a custom method that can get overridden by a child class for custom authorization logic beyond grouping.
- #humanize ⇒ Object
-
#initialize(name:, controller:, only: nil, except: nil, source: :host) ⇒ Entity
constructor
A new instance of Entity.
-
#matches?(controller:, method:) ⇒ Boolean
controller will be the class object method will be the string of the route method.
Constructor Details
#initialize(name:, controller:, only: nil, except: nil, source: :host) ⇒ Entity
Returns a new instance of Entity.
35 36 37 38 39 40 41 42 43 |
# File 'lib/command_tower/authorization/entity.rb', line 35 def initialize(name:, controller:, only: nil, except: nil, source: :host) @controller = controller @except = except.nil? ? nil : Array(except).map(&:to_sym) @only = only.nil? ? nil : Array(only).map(&:to_sym) @source = source.to_sym @name = name validate! end |
Instance Attribute Details
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
34 35 36 |
# File 'lib/command_tower/authorization/entity.rb', line 34 def controller @controller end |
#except ⇒ Object (readonly)
Returns the value of attribute except.
34 35 36 |
# File 'lib/command_tower/authorization/entity.rb', line 34 def except @except end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
34 35 36 |
# File 'lib/command_tower/authorization/entity.rb', line 34 def name @name end |
#only ⇒ Object (readonly)
Returns the value of attribute only.
34 35 36 |
# File 'lib/command_tower/authorization/entity.rb', line 34 def only @only end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
34 35 36 |
# File 'lib/command_tower/authorization/entity.rb', line 34 def source @source end |
Class Method Details
.create_entity(name:, controller:, only: nil, except: nil, source: :host) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/command_tower/authorization/entity.rb', line 7 def create_entity(name:, controller:, only: nil, except: nil, source: :host) if entities[name] raise Error, "Authorization entity [#{name}] already exists. Hosts must not redefine CommandTower-owned entities." end entity = new(name:, controller:, only:, except:, source:) if source.to_sym == :host ct_controllers = entities.values.select { |existing| existing.source == :command_tower }.map(&:controller) if ct_controllers.include?(entity.controller) raise Error, "Host entity [#{name}] redefines CommandTower-owned controller [#{entity.controller}]" end end entities[name] = entity entities[name] end |
.entities ⇒ Object
25 26 27 |
# File 'lib/command_tower/authorization/entity.rb', line 25 def entities @entities ||= ActiveSupport::HashWithIndifferentAccess.new end |
.entities_reset! ⇒ Object
29 30 31 |
# File 'lib/command_tower/authorization/entity.rb', line 29 def entities_reset! @entities = ActiveSupport::HashWithIndifferentAccess.new end |
Instance Method Details
#authorized?(user:) ⇒ Boolean
This is a custom method that can get overridden by a child class for custom authorization logic beyond grouping
74 75 76 |
# File 'lib/command_tower/authorization/entity.rb', line 74 def (user:) true end |
#humanize ⇒ Object
45 46 47 |
# File 'lib/command_tower/authorization/entity.rb', line 45 def humanize "name:[#{name}]; controller:[#{controller}]; only:[#{only}]; except:[#{except}]" end |
#matches?(controller:, method:) ⇒ Boolean
controller will be the class object method will be the string of the route method
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/command_tower/authorization/entity.rb', line 51 def matches?(controller:, method:) # Return early if the controller does not match the existing entity controller return nil if @controller != controller # We are in the correct controller # if inclusions are not present, the check is on the entire contoller and we can return true if only.nil? && except.nil? return true end ## `only` or `except` is present at this point if only # If method is included in only, accept otherwise return reject return only.include?(method.to_sym) else # If method is included in except, reject otherwise return accept return !except.include?(method.to_sym) end end |