Module: Authorule
- Extended by:
- ActiveSupport::Autoload
- Defined in:
- lib/authorule.rb,
lib/authorule/rule.rb,
lib/authorule/railtie.rb,
lib/authorule/version.rb,
lib/authorule/rule_base.rb,
lib/authorule/permission.rb,
lib/authorule/permission_holder.rb,
lib/authorule/permission_accessors.rb
Defined Under Namespace
Modules: PermissionAccessors, PermissionHolder, Rule Classes: DuplicatePermission, Permission, PermissionResolutionError, Railtie, RuleBase
Constant Summary collapse
- VERSION =
"1.2.0"
- @@permission_classes =
{}
Class Method Summary collapse
-
.available_permissions ⇒ Hash<Symbol,Array<Permission>>
Retrieves all available permissions, organized by their kind, into a hash.
- .register(kind, klass) ⇒ Object
-
.resolve(target, action = nil) ⇒ Object
Resolves a target.
Class Method Details
.available_permissions ⇒ Hash<Symbol,Array<Permission>>
Retrieves all available permissions, organized by their kind, into a hash.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/authorule.rb', line 62 def self. = {} .each do |kind, klass| next unless klass.list_block [kind] = [] objects = klass.list_block.call objects.each do |object| [kind] << klass.new(object) end end end |
.register(kind, klass) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/authorule.rb', line 25 def self.register(kind, klass) kind = kind.to_sym unless klass < Permission raise ArgumentError, "class #{klass.name} cannot be registered as a permission kind: it should be derived from Authorule::Permission" end if Authorule.[kind] raise DuplicatePermission, "another permission class has already been registered for kind :#{kind}" end [kind] = klass end |
.resolve(target, action = nil) ⇒ Object
Resolves a target. Tries all registered permission classes with a resolve block, and runs the target through the block. If anythin is returned, it is passed into the constructor of that permission class.
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/authorule.rb', line 42 def self.resolve(target, action = nil) return target if target.is_a?(Authorule::Permission) .values.each do |klass| next unless klass.resolve_block resolved = klass.resolve_block.call(target) return klass.new(resolved, action) if resolved end # If we got here, no schema returned a matching permission. raise PermissionResolutionError, "target #{target} could not be resolved into a permission" end |