Sha256: e3e818eb6f14ec70e2f196957d306b0f90f6812da8e99db9f1dc31a12ccf514d

Contents?: true

Size: 1.26 KB

Versions: 34

Compression:

Stored size: 1.26 KB

Contents

module Ddr::Auth
  module Roles
    class RoleSetManager

      attr_reader :object
      attr_accessor :role_set

      def initialize(object)
        @object = object
        load
      end

      def grant(*roles)
        granted = RoleSet.new(roles: roles)
        role_set.merge(granted)
        persist
      end

      def granted?(role)
        if role.is_a?(Role)
          role_set.include?(role)
        else
          granted? Role.new(role)
        end
      end

      def revoke(*roles)
        revoked = RoleSet.new(roles: roles)
        role_set.roles -= revoked.roles
        persist
      end

      def revoke_all
        role_set.clear
        persist
      end

      def replace(*roles)
        self.role_set = RoleSet.new(roles: roles)
        persist
      end

      protected

      def respond_to_missing?(name, include_all=false)
        role_set.respond_to?(name, include_all)
      end

      def method_missing(name, *args, &block)
        if role_set.respond_to?(name)
          return role_set.send(name, *args, &block)
        end
        super
      end

      private

      def persist
        object.access_roles = role_set.to_json
      end

      def load
        self.role_set = RoleSet.from_json(object.access_roles)
      end

    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

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