Class: Hippo::Access::LockedFields

Inherits:
Object
  • Object
show all
Defined in:
lib/hippo/access/locked_fields.rb

Class Method Summary collapse

Class Method Details

.definitionsObject



35
36
37
# File 'lib/hippo/access/locked_fields.rb', line 35

def definitions
    @definitions
end

.lock(klass, field, role, only = nil) ⇒ Object

Lock a given class and attribute to a given role

Parameters:

  • klass (Hippo::Model)
  • field (Symbol)
  • only (:read, :write) (defaults to: nil)


31
32
33
# File 'lib/hippo/access/locked_fields.rb', line 31

def lock(klass, field, role, only=nil)
    @definitions[klass][field] << { role: role, only: only }
end

.roles_needed_for(klass, attribute, access_type) ⇒ Array<Role>

An empty array indicates that the field is not locked and the Model should be used for determining access

Parameters:

  • klass (Hippo::Model)
  • field (Symbol)
  • access_type (:read, :write)

Returns:

  • (Array<Role>)

    Roles that are allowed to access the field.



17
18
19
20
21
22
23
24
25
# File 'lib/hippo/access/locked_fields.rb', line 17

def roles_needed_for(klass, attribute, access_type)
    if @definitions.has_key?(klass) && @definitions[klass].has_key?(attribute)
        @definitions[klass][attribute].each_with_object([]) do | grant, result |
            result.push(grant[:role]) if grant[:only].nil? || grant[:only] == access_type
        end
    else
        []
    end
end