role_based_authorization 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
@@ -112,37 +112,39 @@ module RoleBasedAuthorization
112
112
  # :user, :controller, :action:: self explanatory
113
113
  # :ids:: id to be used to retrieve relevant objects
114
114
  def authorize_action? opts = {}
115
- if defined?(logged_in?) && !logged_in?
115
+ if respond_to?(:logged_in?) && !logged_in?
116
116
  AUTHORIZATION_LOGGER.info("returning false (not logged in)")
117
117
  return false
118
118
  end
119
119
 
120
- opts[:ids] ||= {}
121
- opts[:ids].reverse_merge!( opts.reject { |k,v| k.to_s !~ /(_id\Z)|(\Aid\Z)/ } )
120
+ user, ids, controller, action = *opts.values_at(:user, :ids, :controller, :action)
122
121
 
123
- if opts[:user].nil? && defined?(current_user)
124
- opts[:user] = current_user
122
+ ids ||= {}
123
+ ids.reverse_merge!( opts.reject { |key,value| key.to_s !~ /(_id\Z)|(\Aid\Z)/ } )
124
+
125
+ if user.nil? && respond_to?(:current_user)
126
+ user = current_user
125
127
  end
126
128
 
127
- if opts[:controller].nil? && defined?(controller_name)
128
- opts[:controller] = controller_name
129
+ if controller.nil? && respond_to?(:controller_name)
130
+ controller = controller_name
129
131
  end
130
132
 
131
133
  AUTHORIZATION_LOGGER.info("user %s requested access to method %s:%s using ids:%s" %
132
- [ opts[:user] && opts[:user].description + "(id:#{opts[:user].id} role:#{opts[:user].role})" || 'none',
133
- opts[:controller],
134
- opts[:action],
135
- opts[:ids].inspect])
134
+ [ user && user.description + "(id:#{user.id} role:#{user.role})" || 'none',
135
+ controller,
136
+ action,
137
+ ids.inspect])
136
138
 
137
139
  rules = self.class.role_auth_rules
138
140
  AUTHORIZATION_LOGGER.debug("current set of rules: %s" % [rules.inspect])
139
141
 
140
- ([opts[:controller]] | ['application']).each do |controller|
141
- if( !controller.blank? && rules[controller].nil? )
142
+ ([controller] | ['application']).each do |current_controller|
143
+ if( !current_controller.blank? && rules[current_controller].nil? )
142
144
  # tries to load the controller. Rails automagically loads classes if their name
143
145
  # is used anywhere. By trying to constantize the name of the controller, we
144
146
  # force rails to load it.
145
- controller_klass = (controller.to_s+'_controller').camelize.constantize
147
+ controller_klass = (current_controller.to_s+'_controller').camelize.constantize
146
148
  end
147
149
 
148
150
  AUTHORIZATION_LOGGER.debug("current controller: %s" % [controller])
@@ -150,10 +152,12 @@ module RoleBasedAuthorization
150
152
  [:all, opts[:action]].each do |action|
151
153
  AUTHORIZATION_LOGGER.debug('current action: %s' % [action])
152
154
  action = action.to_sym
153
- raise "Action should be a symbol -- not a #{action.class.name}!" if action!=:all && action.class!=Symbol
155
+ action_class = action.class
156
+
157
+ raise "Action should be a symbol -- not a #{action_class.name}!" if action_class != Symbol
154
158
 
155
- next if rules[controller].nil? || rules[controller][action].nil?
156
- if rules[controller][action].find { |rule| rule.match(opts[:user], opts[:ids]) }
159
+ rules_for_this_action = rules[controller] && rules[controller][action]
160
+ if rules_for_this_action != nil && rules_for_this_action.find { |rule| rule.match(user, ids) }
157
161
  AUTHORIZATION_LOGGER.info('returning true (access granted)')
158
162
  return true
159
163
  end
@@ -205,7 +209,7 @@ module RoleBasedAuthorization
205
209
  def authorized?
206
210
  authorize_action? :controller => controller_name,
207
211
  :action => action_name,
208
- :ids => params.reject { |k,v| k.to_s !~ /(_id\Z)|(\Aid\Z)/ },
212
+ :ids => params.reject { |key,value| key.to_s !~ /(_id\Z)|(\Aid\Z)/ },
209
213
  :user => current_user
210
214
  end
211
215
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{role_based_authorization}
8
- s.version = "0.1.3"
8
+ s.version = "0.1.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Roberto Esposito"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: role_based_authorization
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roberto Esposito