Module: Consent
- Defined in:
- lib/consent.rb,
lib/consent/dsl.rb,
lib/consent/view.rb,
lib/consent/rspec.rb,
lib/consent/action.rb,
lib/consent/ability.rb,
lib/consent/railtie.rb,
lib/consent/subject.rb,
lib/consent/version.rb,
lib/consent/reloader.rb,
lib/consent/permission.rb,
lib/consent/permissions.rb,
lib/generators/consent/permissions_generator.rb
Overview
Consent makes defining permissions easier by providing a clean, concise DSL for authorization so that all abilities do not have to be in your ‘Ability` class.
Defined Under Namespace
Modules: Rspec Classes: Ability, Action, DSL, Permission, Permissions, PermissionsGenerator, Railtie, Reloader, Subject, View
Constant Summary collapse
- FULL_ACCESS =
%w[1 true].freeze
- VERSION =
'0.6.0'
Class Method Summary collapse
-
.default_views ⇒ Hash<Symbol,Consent::View>
Default views available to every permission.
-
.define(key, label, options = {}, &block) ⇒ Object
Defines a subject with the given key, label and options.
-
.find_action(subject_key, action_key) ⇒ Consent::Action?
Finds an action within a subject context.
-
.find_subjects(subject_key) ⇒ Array<Consent::Subject>
Finds all subjects defined with the given key.
-
.find_view(subject_key, view_key) ⇒ Consent::View?
Finds a view within a subject context.
-
.load_subjects!(paths, mechanism = :require) ⇒ Object
Loads all permission (ruby) files from the given directory and using the given mechanism (default: :require).
-
.permissions(permissions) ⇒ Consent::Permissions
Maps a permissions hash to a Consent::Permissions.
-
.subjects ⇒ Array<Consent::Subject>
Subjects defined in Consent.
Class Method Details
.default_views ⇒ Hash<Symbol,Consent::View>
26 27 28 |
# File 'lib/consent.rb', line 26 def self.default_views @default_views ||= {} end |
.define(key, label, options = {}, &block) ⇒ Object
Defines a subject with the given key, label and options
i.e:
Consent.define :users, "User management" do
view :department, "Same department only" do |user|
{ department_id: user.department_id }
end
action :read, "Can view users"
action :update, "Can edit existing user", views: :department
end
87 88 89 90 91 92 |
# File 'lib/consent.rb', line 87 def self.define(key, label, = {}, &block) defaults = .fetch(:defaults, {}) subjects << Subject.new(key, label).tap do |subject| DSL.build(subject, defaults, &block) end end |
.find_action(subject_key, action_key) ⇒ Consent::Action?
Finds an action within a subject context
49 50 51 52 53 54 55 |
# File 'lib/consent.rb', line 49 def self.find_action(subject_key, action_key) Consent.find_subjects(subject_key) .map(&:actions).flatten .find do |action| action.key.eql?(action_key) end end |
.find_subjects(subject_key) ⇒ Array<Consent::Subject>
Finds all subjects defined with the given key
40 41 42 43 44 |
# File 'lib/consent.rb', line 40 def self.find_subjects(subject_key) @subjects.find_all do |subject| subject.key.eql?(subject_key) end end |
.find_view(subject_key, view_key) ⇒ Consent::View?
Finds a view within a subject context
60 61 62 63 64 65 |
# File 'lib/consent.rb', line 60 def self.find_view(subject_key, view_key) views = Consent.find_subjects(subject_key) .map(&:views) .reduce({}, &:merge) views[view_key] end |
.load_subjects!(paths, mechanism = :require) ⇒ Object
Loads all permission (ruby) files from the given directory and using the given mechanism (default: :require)
72 73 74 75 |
# File 'lib/consent.rb', line 72 def self.load_subjects!(paths, mechanism = :require) = paths.map { |dir| File.join(dir, '*.rb') } Dir[*].each(&Kernel.method(mechanism)) end |
.permissions(permissions) ⇒ Consent::Permissions
Maps a permissions hash to a Consent::Permissions
97 98 99 |
# File 'lib/consent.rb', line 97 def self.() Permissions.new() end |
.subjects ⇒ Array<Consent::Subject>
Subjects defined in Consent
33 34 35 |
# File 'lib/consent.rb', line 33 def self.subjects @subjects ||= [] end |