Sha256: f23a52954ba72c61e933702f450985e1a27a56bcfb1ea207403b9628e6207cec

Contents?: true

Size: 1.63 KB

Versions: 13

Compression:

Stored size: 1.63 KB

Contents

module Shamu
  module Security

    # Mixin for {Policy} and {Support} classes to define security roles
    # including inheritance.
    module Roles
      extend ActiveSupport::Concern

      class_methods do

        # @return [Hash] the named roles defined on the class.
        def roles
          @roles ||= {}
        end

        # Define a named role.
        #
        # @param [Symbol] name of the role.
        # @param [Array<Symbol>] inherits additional roles that are
        #     automatically inherited when the named role is granted.
        # @return [void]
        def role( name, inherits: nil )
          roles[ name.to_sym ] = { inherits: Array( inherits ) }
        end

        # Expand the given roles to include the roles that they have inherited.
        # @param [Array<Symbol>] roles
        # @return [Array<Symbol>] the expanded roles.
        def expand_roles( *roles )
          expand_roles_into( roles, [] )
        end

        # @param [Symbol] the role to check.
        # @return [Boolean] true if the role has been defined.
        def role_defined?( role )
          roles.key?( role.to_sym )
        end

        private

          def expand_roles_into( roles, expanded )
            roles.each do |name|
              name = name.to_sym

              next unless role = self.roles[ name ]
              expanded << name

              role[ :inherits ].each do |inherited|
                next if expanded.include?( inherited )

                expanded << inherited
                expand_roles_into( [ inherited ], expanded )
              end
            end

            expanded
          end

      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
shamu-0.0.18 lib/shamu/security/roles.rb
shamu-0.0.17 lib/shamu/security/roles.rb
shamu-0.0.15 lib/shamu/security/roles.rb
shamu-0.0.14 lib/shamu/security/roles.rb
shamu-0.0.13 lib/shamu/security/roles.rb
shamu-0.0.11 lib/shamu/security/roles.rb
shamu-0.0.9 lib/shamu/security/roles.rb
shamu-0.0.8 lib/shamu/security/roles.rb
shamu-0.0.7 lib/shamu/security/roles.rb
shamu-0.0.5 lib/shamu/security/roles.rb
shamu-0.0.4 lib/shamu/security/roles.rb
shamu-0.0.3 lib/shamu/security/roles.rb
shamu-0.0.2 lib/shamu/security/roles.rb