Class: Permit::Base
- Inherits:
-
Object
- Object
- Permit::Base
- Includes:
- Util
- Defined in:
- lib/cancan-permits/permit/base_permit.rb
Instance Attribute Summary collapse
-
#ability ⇒ Object
readonly
Returns the value of attribute ability.
-
#strategy ⇒ Object
readonly
this can be used to customize the strategy used by owns to determine ownership, fx to support alternative ORMs.
-
#user_permissions ⇒ Object
readonly
Returns the value of attribute user_permissions.
Instance Method Summary collapse
- #can(action, subject, conditions = nil, &block) ⇒ Object
- #cannot(action, subject, conditions = nil, &block) ⇒ Object
-
#initialize(ability, options = {}) ⇒ Base
constructor
A new instance of Base.
- #licenses(*names) ⇒ Object
- #load_role_rules ⇒ Object
- #load_rules(user) ⇒ Object
- #load_user_rules(user) ⇒ Object
- #owns(user, clazz, ownership_relation = :user_id, user_id_attribute = :id, strategy_used = nil) ⇒ Object
- #permit?(user, options = {}) ⇒ Boolean
Methods included from Util
Constructor Details
#initialize(ability, options = {}) ⇒ Base
Returns a new instance of Base.
62 63 64 65 66 67 |
# File 'lib/cancan-permits/permit/base_permit.rb', line 62 def initialize ability, = {} @ability = ability @strategy = [:strategy] || Permits::Ability.strategy || :default @user_permissions = ::PermissionsLoader. [:user_permissions_file] @role_permissions = ::PermissionsLoader.load_permits [:permits_file] end |
Instance Attribute Details
#ability ⇒ Object (readonly)
Returns the value of attribute ability.
6 7 8 |
# File 'lib/cancan-permits/permit/base_permit.rb', line 6 def ability @ability end |
#strategy ⇒ Object (readonly)
this can be used to customize the strategy used by owns to determine ownership, fx to support alternative ORMs
7 8 9 |
# File 'lib/cancan-permits/permit/base_permit.rb', line 7 def strategy @strategy end |
#user_permissions ⇒ Object (readonly)
Returns the value of attribute user_permissions.
9 10 11 |
# File 'lib/cancan-permits/permit/base_permit.rb', line 9 def @user_permissions end |
Instance Method Details
#can(action, subject, conditions = nil, &block) ⇒ Object
76 77 78 |
# File 'lib/cancan-permits/permit/base_permit.rb', line 76 def can(action, subject, conditions = nil, &block) rules << rule_class.new(true, action, subject, conditions, block) end |
#cannot(action, subject, conditions = nil, &block) ⇒ Object
80 81 82 |
# File 'lib/cancan-permits/permit/base_permit.rb', line 80 def cannot(action, subject, conditions = nil, &block) rules << rule_class.new(false, action, subject, conditions, block) end |
#licenses(*names) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/cancan-permits/permit/base_permit.rb', line 11 def licenses *names names.to_strings.each do |name| begin module_name = "#{name.camelize}License" clazz = module_name.constantize rescue raise "License #{module_name} is not defined" end begin clazz.new(self).enforce! rescue raise "License #{clazz} could not be enforced using #{self.inspect}" end end end |
#load_role_rules ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/cancan-permits/permit/base_permit.rb', line 33 def load_role_rules return if ! || .empty? name ||= self.class.to_s.gsub(/Permit$/, "").underscore.to_sym [name].can_statement do || instance_eval end [name].cannot_statement do || instance_eval end end |
#load_rules(user) ⇒ Object
28 29 30 31 |
# File 'lib/cancan-permits/permit/base_permit.rb', line 28 def load_rules user load_role_rules load_user_rules user end |
#load_user_rules(user) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/cancan-permits/permit/base_permit.rb', line 46 def load_user_rules user return if ! || .empty? raise "#load_user_rules expects the user to have an email property: #{user.inspect}" if !user || !user.respond_to?(:email) id = user.email return nil if id.strip.empty? [id].can_statement do || instance_eval end [id].cannot_statement do || instance_eval end end |
#owns(user, clazz, ownership_relation = :user_id, user_id_attribute = :id, strategy_used = nil) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/cancan-permits/permit/base_permit.rb', line 84 def owns(user, clazz, ownership_relation = :user_id, user_id_attribute = :id, strategy_used = nil) begin strategy_used = strategy_used || self.strategy user_id = user.send :"#{user_id_attribute}" rescue raise ArgumentError, "ERROR (owns) - The user of class #{user.class} does not respond to ##{user_id_attribute}" end # puts "can #{clazz} manage ownership: #{ownership_relation.inspect} => #{user_id.inspect} ???" # puts "Using strategy: #{strategy_used}" begin case strategy_used when :string can :manage, clazz, ownership_relation => user_id.to_s when :default can :manage, clazz, ownership_relation => user_id else raise "Trying to use unknown ownership strategy: #{strategy}" end rescue Exception => e puts e.inspect end end |
#permit?(user, options = {}) ⇒ Boolean
69 70 71 72 73 74 |
# File 'lib/cancan-permits/permit/base_permit.rb', line 69 def permit?(user, = {}) if == :in_role return true if !role_match? user end false end |