Sha256: c83532d84398337a760dd7b6a98ead5a791154f02b854626b6aabf29746c7ad6

Contents?: true

Size: 1.4 KB

Versions: 35

Compression:

Stored size: 1.4 KB

Contents

require "json"
require "virtus"

module Ddr::Auth
  module Roles
    #
    # The assignment of a role to an agent within a scope.
    #
    class Role
      extend Deprecation
      include Virtus.value_object
      include Ddr::Models::Validatable

      self.validator = RoleValidator

      DEFAULT_SCOPE = Roles::RESOURCE_SCOPE

      values do
        attribute :agent,     RoleAttribute
        attribute :role_type, RoleAttribute
        attribute :scope,     RoleAttribute, default: DEFAULT_SCOPE
      end

      class << self
        def build(*args)
          Deprecation.warn(Role, "`build` is deprecated; use `new` instead.")
          new(*args)
        end

        def from_json(json)
          new JSON.parse(json)
        end
      end

      def initialize(*args)
        super
        validate!
      end

      def to_s
        to_h.to_s
      end

      def to_json
        JSON.dump(to_h)
      end

      def validate!
        if invalid?
          raise Ddr::Models::Error, "Invalid Role: #{errors.full_messages.join('; ')}"
        end
      end

      def in_resource_scope?
        scope == Roles::RESOURCE_SCOPE
      end

      def in_policy_scope?
        scope == Roles::POLICY_SCOPE
      end

      # Returns the permissions associated with the role
      # @return [Array<Symbol>] the permissions
      def permissions
        Roles.type_map[role_type].permissions
      end

    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
ddr-models-3.0.8 lib/ddr/auth/roles/role.rb
ddr-models-3.0.7 lib/ddr/auth/roles/role.rb
ddr-models-3.0.6 lib/ddr/auth/roles/role.rb
ddr-models-3.0.5 lib/ddr/auth/roles/role.rb
ddr-models-3.0.4 lib/ddr/auth/roles/role.rb
ddr-models-3.0.3 lib/ddr/auth/roles/role.rb
ddr-models-3.0.2 lib/ddr/auth/roles/role.rb
ddr-models-3.0.1 lib/ddr/auth/roles/role.rb
ddr-models-3.0.0 lib/ddr/auth/roles/role.rb
ddr-models-3.0.0.rc6 lib/ddr/auth/roles/role.rb
ddr-models-3.0.0.rc5 lib/ddr/auth/roles/role.rb
ddr-models-3.0.0.rc4 lib/ddr/auth/roles/role.rb
ddr-models-3.0.0.rc3 lib/ddr/auth/roles/role.rb
ddr-models-3.0.0.rc2 lib/ddr/auth/roles/role.rb
ddr-models-3.0.0.rc1 lib/ddr/auth/roles/role.rb
ddr-models-3.0.0.beta.22 lib/ddr/auth/roles/role.rb
ddr-models-3.0.0.beta.21 lib/ddr/auth/roles/role.rb
ddr-models-3.0.0.beta.20 lib/ddr/auth/roles/role.rb
ddr-models-3.0.0.beta.18 lib/ddr/auth/roles/role.rb
ddr-models-3.0.0.beta.17 lib/ddr/auth/roles/role.rb