Sha256: 69a5cc5bce026f4130a32eec21714da35a8c54907cd86fd33aef68ff9fc3d0ba

Contents?: true

Size: 1.53 KB

Versions: 9

Compression:

Stored size: 1.53 KB

Contents

module Hippo
    module Access

        class LockedFields

            @definitions = Hash.new{ |fields,klass|
                fields[klass] = Hash.new{ |grants,field| grants[field] = [] }
            }
            class << self

                # @param klass [Hippo::Model]
                # @param field [Symbol]
                # @param access_type [:read, :write]
                # @return [Array<Role>] Roles that are allowed to access the field.
                # An empty array indicates that the field is not locked and the Model
                # should be used for determining access
                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

                # Lock a given class and attribute to a given role
                # @param klass [Hippo::Model]
                # @param field [Symbol]
                # @param only [:read, :write]
                def lock(klass, field, role, only=nil)
                    @definitions[klass][field] << { role: role, only: only }
                end

                def definitions
                    @definitions
                end
            end

        end

    end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
hippo-fw-0.9.9 lib/hippo/access/locked_fields.rb
hippo-fw-0.9.8 lib/hippo/access/locked_fields.rb
hippo-fw-0.9.7 lib/hippo/access/locked_fields.rb
hippo-fw-0.9.6 lib/hippo/access/locked_fields.rb
hippo-fw-0.9.5 lib/hippo/access/locked_fields.rb
hippo-fw-0.9.4 lib/hippo/access/locked_fields.rb
hippo-fw-0.9.3 lib/hippo/access/locked_fields.rb
hippo-fw-0.9.2 lib/hippo/access/locked_fields.rb
hippo-fw-0.9.1 lib/hippo/access/locked_fields.rb