role_based_authorization 0.1.12 → 0.1.13
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.
|
1
|
+
0.1.13
|
@@ -5,22 +5,7 @@ module RoleBasedAuthorization
|
|
5
5
|
def role_auth_rules
|
6
6
|
@@rules||={}
|
7
7
|
@@rules
|
8
|
-
end
|
9
|
-
|
10
|
-
# Returns true if one of the given rules matches the
|
11
|
-
# given options. rules must be an hash with a list of rules for
|
12
|
-
# each action
|
13
|
-
def find_matching_rule rules, options
|
14
|
-
user,actions,ids = *options.values_at(:user, :actions, :ids)
|
15
|
-
|
16
|
-
return actions.find do |action|
|
17
|
-
AUTHORIZATION_LOGGER.debug('current action: %s' % [action])
|
18
|
-
action = action.to_sym
|
19
|
-
rules_for_action = rules[action]
|
20
|
-
rules_for_action && rules_for_action.find { |rule| rule.match(user, ids) }
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
8
|
+
end
|
24
9
|
|
25
10
|
# Defines the DSL for the authorization system. The syntax is:
|
26
11
|
# permit :actions => [list of actions],
|
@@ -2,7 +2,7 @@
|
|
2
2
|
module RoleBasedAuthorization
|
3
3
|
# AuthorizationLogger instance that is used throughout the plugin for logging
|
4
4
|
# events.
|
5
|
-
AUTHORIZATION_LOGGER = AuthorizationLogger.new(File.join(RAILS_ROOT,'log','authorization.log'))
|
5
|
+
AUTHORIZATION_LOGGER = AuthorizationLogger.new(File.join(RAILS_ROOT,'log','authorization.log'))
|
6
6
|
|
7
7
|
# Fires when the module is included into the controllers. It adds all class methods
|
8
8
|
# defined in the ClassAdditions sub-module and the authorize_action? and if_authorized?
|
@@ -15,6 +15,33 @@ module RoleBasedAuthorization
|
|
15
15
|
helper_method :if_authorized?
|
16
16
|
end
|
17
17
|
end
|
18
|
+
|
19
|
+
# Returns true if one of the given rules matches the
|
20
|
+
# given options. rules must be an hash with a list of rules for
|
21
|
+
# each action
|
22
|
+
def RoleBasedAuthorization.find_matching_rule rules, options
|
23
|
+
user,actions,ids = *options.values_at(:user, :actions, :ids)
|
24
|
+
|
25
|
+
return actions.find do |action|
|
26
|
+
AUTHORIZATION_LOGGER.debug('current action: %s' % [action])
|
27
|
+
action = action.to_sym
|
28
|
+
rules_for_action = rules[action]
|
29
|
+
rules_for_action && rules_for_action.find { |rule| rule.match(user, ids) }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
# Returns an hash options amenable to be passed to authorize_action?. It takes either
|
35
|
+
# an option hash, or a path string
|
36
|
+
def RoleBasedAuthorization.cleanup_options(opts)
|
37
|
+
path_cleanup_regexp = %r{(#{ActionController::Base.relative_url_root})?}
|
38
|
+
|
39
|
+
url_options = (opts.class == String) && ActionController::Routing::Routes.recognize_path(opts.gsub(path_cleanup_regexp,''))
|
40
|
+
url_options ||= opts.dup
|
41
|
+
|
42
|
+
url_options
|
43
|
+
end
|
44
|
+
|
18
45
|
|
19
46
|
# Returns true if one of the rules defined for this controller matches
|
20
47
|
# the given options
|
@@ -33,7 +60,7 @@ module RoleBasedAuthorization
|
|
33
60
|
(controller.to_s+'_controller').camelize.constantize if( !controller.blank? && rules_for_controller.nil? )
|
34
61
|
|
35
62
|
|
36
|
-
rules_for_controller &&
|
63
|
+
rules_for_controller && RoleBasedAuthorization.find_matching_rule(rules_for_controller, options)
|
37
64
|
end
|
38
65
|
end
|
39
66
|
|
@@ -85,21 +112,7 @@ module RoleBasedAuthorization
|
|
85
112
|
# if_authorized?( edit_item_path ) { |opts| link_to('yyy', opts) }
|
86
113
|
|
87
114
|
def if_authorized? opts, &block
|
88
|
-
|
89
|
-
|
90
|
-
url_options = nil
|
91
|
-
if opts.class == String
|
92
|
-
path = opts
|
93
|
-
|
94
|
-
|
95
|
-
url_options = ActionController::Routing::Routes.recognize_path(path.gsub(cleanup_url_regexp,''))
|
96
|
-
else
|
97
|
-
url_options = opts.dup
|
98
|
-
end
|
99
|
-
|
100
|
-
if authorize_action? url_options
|
101
|
-
block.call(opts)
|
102
|
-
end
|
115
|
+
block.call(opts) if authorize_action?(RoleBasedAuthorization.cleanup_options(opts))
|
103
116
|
end
|
104
117
|
|
105
118
|
# Returns true if the current user is authorized to perform the current action
|