Class: GenericAuth::Engine
- Inherits:
-
Object
- Object
- GenericAuth::Engine
- Includes:
- Singleton
- Defined in:
- lib/generic_auth/engine.rb
Instance Method Summary collapse
- #activities(role) ⇒ Object
- #add_activity(role, activity, contexts = []) ⇒ Object
- #add_context(role, activity, &block) ⇒ Object
- #add_role(role) ⇒ Object
- #clear_activities(role) ⇒ Object
- #clear_contexts(role, activity) ⇒ Object
- #clear_roles ⇒ Object
- #contexts(role, activity) ⇒ Object
-
#initialize ⇒ Engine
constructor
A new instance of Engine.
- #permitted?(role, activity, object, *args) ⇒ Boolean
- #remove_activity(role, activity) ⇒ Object
- #remove_context(role, activity, index) ⇒ Object
- #remove_role(role) ⇒ Object
- #roles ⇒ Object
- #user ⇒ Object
- #user=(user = nil) ⇒ Object
- #wrap_class(klass) ⇒ Object
Constructor Details
Instance Method Details
#activities(role) ⇒ Object
61 62 63 64 |
# File 'lib/generic_auth/engine.rb', line 61 def activities role require_role_symbol role @roles[role].keys end |
#add_activity(role, activity, contexts = []) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/generic_auth/engine.rb', line 42 def add_activity role, activity, contexts = [] require_activity_symbol activity require_defined_role role @roles[role][activity] = contexts end |
#add_context(role, activity, &block) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/generic_auth/engine.rb', line 66 def add_context role, activity, &block require_defined_activity role, activity raise ArgumentError, 'Invalid callback, callback must respond_to? call' unless block.respond_to?(:call) @roles[role][activity] << block end |
#add_role(role) ⇒ Object
22 23 24 25 26 |
# File 'lib/generic_auth/engine.rb', line 22 def add_role role require_no_role role @roles[role] = {} end |
#clear_activities(role) ⇒ Object
56 57 58 59 |
# File 'lib/generic_auth/engine.rb', line 56 def clear_activities role require_defined_role role @roles[role] = {} if @roles[role] end |
#clear_contexts(role, activity) ⇒ Object
79 80 81 82 83 |
# File 'lib/generic_auth/engine.rb', line 79 def clear_contexts role, activity require_role_symbol role require_activity_symbol activity @roles[role][activity] = [] if @roles[role] && @roles[role][activity] end |
#clear_roles ⇒ Object
28 29 30 |
# File 'lib/generic_auth/engine.rb', line 28 def clear_roles @roles = {} end |
#contexts(role, activity) ⇒ Object
85 86 87 88 89 |
# File 'lib/generic_auth/engine.rb', line 85 def contexts role, activity require_defined_role role require_activity_symbol activity @roles[role][activity] end |
#permitted?(role, activity, object, *args) ⇒ Boolean
91 92 93 94 95 96 97 98 |
# File 'lib/generic_auth/engine.rb', line 91 def permitted? role, activity, object, *args require_defined_role role return false unless @roles[role][activity] return true if @roles[role][activity].empty? @roles[role][activity].all? do |context| context.call(object, *args) end end |
#remove_activity(role, activity) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/generic_auth/engine.rb', line 49 def remove_activity role, activity require_defined_role role require_activity_symbol activity @roles[role].delete(activity) if @roles[role][activity] end |
#remove_context(role, activity, index) ⇒ Object
73 74 75 76 77 |
# File 'lib/generic_auth/engine.rb', line 73 def remove_context role, activity, index require_defined_activity role, activity @roles[role][activity].delete_at(index) if @roles[role][activity] end |
#remove_role(role) ⇒ Object
32 33 34 35 36 |
# File 'lib/generic_auth/engine.rb', line 32 def remove_role role require_role_symbol role @roles.delete(role) if @roles[role] end |
#roles ⇒ Object
38 39 40 |
# File 'lib/generic_auth/engine.rb', line 38 def roles @roles.keys end |
#user ⇒ Object
18 19 20 |
# File 'lib/generic_auth/engine.rb', line 18 def user @user end |
#user=(user = nil) ⇒ Object
12 13 14 15 16 |
# File 'lib/generic_auth/engine.rb', line 12 def user= user=nil user = GenericAuth::Guest.new if user.nil? raise ArgumentError, 'User must respond_to? roles' unless user.respond_to?(:roles) @user = user end |
#wrap_class(klass) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/generic_auth/engine.rb', line 100 def wrap_class(klass) wrapped = klass.to_s.camelize.constantize raise 'Class must respond_to? generic_auth_on with an array of methods(activites) to authorize' unless wrapped.respond_to?(:generic_auth_on) activities = wrapped.send(:generic_auth_on) activities.each do |activity| old = wrapped.instance_method(activity) wrapped.send(:define_method, activity) do |*args| GenericAuth::Guard. activity, self, *args old.bind(self).(*args) end end end |