Sha256: cc31613e074353d2a89dbef8385b34bdfeb67574fd6b505b6fa6b0ff6c2cbdd7
Contents?: true
Size: 1.95 KB
Versions: 1
Compression:
Stored size: 1.95 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. # @param [Array<Symbol>] scopes that the role may be granted in. # @return [void] def role( name, inherits: nil, scopes: nil ) roles[ name.to_sym ] = { inherits: Array( inherits ), scopes: Array( scopes ) } 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, Set.new ).to_a 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 ) raise "No roles defined for #{ name }" unless self.roles.present? roles.each do |name| name = name.to_sym if name == :all expanded.merge( self.roles.keys ) next end 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
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
shamu-0.0.24 | lib/shamu/security/roles.rb |