role_authorization 0.1.6 → 0.2.0

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.
Files changed (32) hide show
  1. data/Gemfile.lock +1 -1
  2. data/lib/rails/role_authorization.rb +9 -1
  3. data/lib/role_authorization/active_record.rb +7 -0
  4. data/lib/role_authorization/{allow_group.rb → controller/allow_group.rb} +0 -0
  5. data/lib/role_authorization/controller/mapper.rb +44 -0
  6. data/lib/role_authorization/{ruleset.rb → controller/ruleset.rb} +3 -5
  7. data/lib/role_authorization/controller.rb +117 -0
  8. data/lib/role_authorization/roles/manager.rb +84 -0
  9. data/lib/role_authorization/roles/role.rb +66 -0
  10. data/lib/role_authorization/roles/role_group.rb +16 -0
  11. data/lib/role_authorization/roles.rb +14 -0
  12. data/lib/role_authorization/rules/defaults.rb +25 -0
  13. data/lib/role_authorization/rules/rule.rb +33 -0
  14. data/lib/role_authorization/rules.rb +12 -0
  15. data/lib/role_authorization/user.rb +121 -0
  16. data/lib/role_authorization/version.rb +1 -1
  17. data/lib/role_authorization/view_security.rb +114 -0
  18. data/lib/role_authorization.rb +61 -1
  19. metadata +16 -17
  20. data/lib/role_authorization/base.rb +0 -116
  21. data/lib/role_authorization/exts/controller.rb +0 -126
  22. data/lib/role_authorization/exts/model.rb +0 -126
  23. data/lib/role_authorization/exts/session.rb +0 -52
  24. data/lib/role_authorization/exts/user.rb +0 -58
  25. data/lib/role_authorization/exts/view.rb +0 -77
  26. data/lib/role_authorization/mapper.rb +0 -76
  27. data/lib/role_authorization/rules/access.rb +0 -88
  28. data/lib/role_authorization/rules/basic.rb +0 -22
  29. data/lib/role_authorization/rules/custom.rb +0 -32
  30. data/lib/role_authorization/rules/object_role.rb +0 -51
  31. data/lib/role_authorization/rules/resource.rb +0 -106
  32. data/lib/role_authorization/rules/user.rb +0 -70
@@ -1,58 +0,0 @@
1
- module RoleAuthorization
2
- module Exts
3
- module User
4
- def self.included(base)
5
- base.send :extend, ClassMethods
6
- base.send :include, InstanceMethods
7
-
8
- base.class_eval do
9
- has_many :user_roles, :dependent => :delete_all
10
- has_many :roles, :through => :user_roles
11
- end
12
- end
13
-
14
- module ClassMethods
15
- def enroll(user_id, role_name)
16
- user = find_by_id(user_id.to_i)
17
- user.enroll(role_name) unless user.nil?
18
- end # enroll
19
-
20
- def withdraw(user_id, role_name)
21
- user = find_by_id(user_id.to_i)
22
- user.withdraw(role_name) unless user.nil?
23
- end # withdraw
24
- end # ClassMethods
25
-
26
- module InstanceMethods
27
- # has_object_role? simply needs to return true or false whether a user has a role or not.
28
- # It may be a good idea to have "admin" roles return true always
29
- # Return false always for anonymous users
30
- def has_object_role?(role, object)
31
- return false if self.anonymous?
32
-
33
- @object_user_roles ||= roles.all(:conditions => ["roleable_type IS NOT NULL and roleable_id IS NOT NULL"])
34
- result = @object_user_roles.detect do |r|
35
- r.roleable_type == object.class.to_s && r.roleable_id == object.id && r.name == role.to_s
36
- end
37
- !result.nil?
38
- end
39
-
40
- # adds a role to the user
41
- def enroll(role_name)
42
- role_id = role_name.is_a?(Integer) ? role_name : Role.find_by_name(role_name.to_s).try(:id)
43
- user_roles.create(:role_id => role_id) if !role_id.nil? && self.user_roles.find_by_role_id(role_id).nil?
44
- end
45
-
46
- def withdraw(role_name)
47
- role_id = role_name.is_a?(Integer) ? role_name : Role.find_by_name(role_name.to_s).try(:id)
48
- UserRole.delete_all(["user_id = ? AND role_id = ?", self.id, role_id]) unless role_id.nil?
49
- end
50
-
51
- def admin?
52
- return true if roles.include?(Role.get(:all))
53
- false
54
- end
55
- end # InstanceMethods
56
- end
57
- end
58
- end
@@ -1,77 +0,0 @@
1
- module RoleAuthorization
2
- module Exts
3
- module View
4
- def self.included(base)
5
- base.class_eval do
6
- alias_method :link_to_open, :link_to
7
- alias_method :link_to, :link_to_secured
8
-
9
- alias_method :button_to_open, :button_to
10
- alias_method :button_to, :button_to_secured
11
-
12
- alias_method :form_for_open, :form_for
13
- alias_method :form_for, :form_for_secured
14
- end
15
- end
16
-
17
- def form_for_secured(record_or_name_or_array, *args, &proc)
18
- options = args.last.is_a?(Hash) ? args.last : {}
19
-
20
- url = url_for(options[:url] || record_or_name_or_array)
21
-
22
- method = (options[:html] && options[:html].has_key?(:method)) ? options[:html][:method] : :post
23
-
24
- if authorized?(url, method)
25
- return form_for_open(record_or_name_or_array, *args, &proc)
26
- else
27
- return ""
28
- end
29
- end
30
-
31
- def link_to_secured(name, options = {}, html_options = {})
32
- url = url_for(options)
33
-
34
- method = (html_options && html_options.has_key?(:method)) ? html_options[:method] : :get
35
-
36
- if authorized?(url, method)
37
- return link_to_open(name, url, html_options)
38
- else
39
- return ""
40
- end
41
- end
42
-
43
- def button_to_secured(name, options = {}, html_options = {})
44
- url = url_for(options)
45
-
46
- method = (html_options && html_options.has_key?(:method)) ? html_options[:method] : :post
47
-
48
- if authorized?(url, method)
49
- return button_to_open(name, url, html_options)
50
- else
51
- return ""
52
- end
53
- end
54
-
55
- def role(*user_roles, &block)
56
- if block_given? && !session[:access_rights].blank? && !(user_roles & session[:access_rights]).empty?
57
- capture_haml(&block)
58
- end
59
- end
60
-
61
- def permitted_to?(url, method, &block)
62
- capture_haml(&block) if block_given? && authorized?(url, method)
63
- end
64
-
65
- def link_to_or_show(name, options = {}, html_options = nil)
66
- lnk = link_to(name, options, html_options)
67
- lnk.length == 0 ? name : lnk
68
- end
69
-
70
- def links(*lis)
71
- rvalue = []
72
- lis.each{|link| rvalue << link if link.length > 0 }
73
- rvalue.join(' | ')
74
- end
75
- end # View
76
- end
77
- end
@@ -1,76 +0,0 @@
1
- module RoleAuthorization
2
- class Mapper
3
- def initialize(controller_klass)
4
- @controller_klass = controller_klass
5
- @rules = Hash.new do |h,k|
6
- h[k] = Hash.new do |h1,k1|
7
- h1[k1] = Array.new
8
- end
9
- end
10
- self
11
- end
12
-
13
- def to_s
14
- output = []
15
- @rules.each_pair do |action, rules|
16
- output << "Action :#{action}"
17
- output << " allow roles #{rules[:roles].inspect}" unless rules[:roles].nil? || rules[:roles].empty?
18
- rules[:rules].map {|rule| output << " #{rule.to_s}"} if rules.has_key?(:rules)
19
- output << ""
20
- output << ""
21
- end
22
-
23
- output.join("\n")
24
- end
25
-
26
- # special role rules
27
- def all(options={}, &block)
28
- options.assert_valid_keys(:only)
29
- rule(:all, :role, options, &block)
30
- end
31
-
32
- def role(user_role, options={}, &block)
33
- options.assert_valid_keys(:check, :only)
34
- rule(user_role, :role, options, &block)
35
- end
36
-
37
- def authorized?(controller_instance, controller, action, id = nil)
38
- rules = @rules[action]
39
-
40
- return false if rules.empty?
41
- return true if rules[:roles].include?(:all)
42
- unless controller_instance.session[:access_rights].nil?
43
- return true if !(rules[:roles] & controller_instance.session[:access_rights]).empty?
44
- end
45
-
46
- if rules.has_key?(:rules)
47
- rules[:rules].each do |rule|
48
- return true if rule.authorized?(controller_instance, controller, action, id)
49
- end
50
- end
51
-
52
- return false
53
- end
54
-
55
- private
56
-
57
- # rule method
58
- def rule(user_role, type, options={}, &block)
59
- actions = [options.delete(:only) || [:all]].flatten.collect {|v| v.to_sym}
60
-
61
- case type
62
- when :role
63
- irule = nil
64
- role_or_type = user_role
65
- else
66
- irule = "RoleAuthorization::Rules::#{type.to_s.camelize}".constantize.new(@controller_klass, options.merge(:role => user_role), &block)
67
- role_or_type = type
68
- end
69
-
70
- actions.each do |action|
71
- @rules[action][:roles] << role_or_type if irule.nil?
72
- @rules[action][:rules] << irule unless irule.nil?
73
- end
74
- end
75
- end
76
- end
@@ -1,88 +0,0 @@
1
- module RoleAuthorization
2
- # define our rule helper in Mapper
3
- class Mapper
4
- def access(options={}, &block)
5
- options.assert_valid_keys(:resource, :only, :no_send)
6
- rule(:access, :access, options, &block)
7
- end
8
- end
9
-
10
- module Rules
11
- class Access < Basic
12
- def initialize(controller, options, &block)
13
- @controller_klass = controller
14
- @options = {:no_send => false}.merge(options)
15
- @block = block
16
- @mapper = nil
17
-
18
- unless @block.nil?
19
- @mapper = RoleAuthorization::Mapper.new(@controller_klass)
20
- @mapper.instance_eval(&@block)
21
- end
22
- self
23
- end
24
-
25
- def to_s
26
- output = ["allow current_user with access role of requested #{[controller_name.singularize, @options[:check]].compact.join('.')}"]
27
- output << @mapper.to_s
28
- end
29
-
30
- def authorized?(controller_instance, controller, action, id)
31
- object = find_object(controller_instance, controller, action, id)
32
- unless object.nil?
33
- return true if controller_instance.accessible?(object.access_role)
34
- end
35
-
36
- if !@mapper.nil? && object.try(:access_role).nil?
37
- return true if @mapper.authorized?(controller_instance, controller, action, object)
38
- end
39
- return false
40
- end
41
-
42
- def find_object(controller_instance, controller, action, id)
43
- object = nil
44
- instance_found = false
45
-
46
- if id.is_a?(Integer) || id.is_a?(String)
47
- # id is a parameter passed in
48
- # we use the :resource option to find the right instance variable
49
- object = controller_instance.instance_variable_get('@' + @options[:resource].to_s) rescue nil
50
- instance_found = true unless object.nil?
51
-
52
- if object.nil? && controller_instance.instance_variable_get('@' + controller)
53
- collection = controller_instance.instance_variable_get('@' + controller)
54
- object = collection.detect {|item| item.andand.id == id.to_i}
55
- end
56
-
57
- if object.nil?
58
- model = controller.singularize.camelize.constantize
59
- if model.respond_to?(:to_param_column)
60
- finder = "find_by_#{model.to_param_column}".to_sym
61
- else
62
- finder = :find_by_id
63
- id = id.to_i
64
- end
65
-
66
- object = model.send(finder, id)
67
- end
68
-
69
- unless object.nil?
70
- if @options.has_key?(:resource) && !@options[:no_send] && !instance_found && object.respond_to?(@options[:resource])
71
- object = object.send(@options[:resource])
72
- end
73
- end
74
- elsif id.is_a?(ActiveRecord::Base) && @options.has_key?(:resource)
75
- # id is already a model record so this is a nested rule
76
-
77
- # first try to find it as an instance variable
78
- object = controller_instance.instance_variable_get('@' + @options[:resource].to_s) rescue nil
79
-
80
- # next we call id's method to find it
81
- object = id.send(@options[:resource]) if object.nil?
82
- end
83
-
84
- return object
85
- end
86
- end # Access
87
- end
88
- end
@@ -1,22 +0,0 @@
1
- module RoleAuthorization
2
- module Rules
3
- class Basic
4
- def initialize(controller, options, &block)
5
- @controller_klass = controller
6
- self
7
- end
8
-
9
- def to_s
10
- "deny all (basic rule)"
11
- end
12
-
13
- def controller_name
14
- @controller_klass.to_s.gsub('Controller', '')
15
- end
16
-
17
- def authorized?(controller_instance, controller, action, id)
18
- return false
19
- end
20
- end
21
- end
22
- end
@@ -1,32 +0,0 @@
1
- module RoleAuthorization
2
- # define our rule helper in Mapper
3
- class Mapper
4
- def custom(options={}, &block)
5
- options.assert_valid_keys(:only, :description)
6
- rule(:custom, :custom, options, &block)
7
- end
8
- end
9
-
10
- module Rules
11
- class Custom < Basic
12
- def initialize(controller, options, &block)
13
- @controller_klass = controller
14
- @options = options
15
- @block = block
16
- self
17
- end
18
-
19
- def to_s
20
- "allow when custom rule (#{@options[:description]}) returns true"
21
- end
22
-
23
- def authorized?(controller_instance, controller, action, id)
24
- unless @block.nil?
25
- result = @block.call(controller_instance)
26
- return true unless result == false || result.nil?
27
- end
28
- return false
29
- end
30
- end
31
- end
32
- end
@@ -1,51 +0,0 @@
1
- module RoleAuthorization
2
- # define our rule helper in Mapper
3
- class Mapper
4
- def object_role(user_role, options={}, &block)
5
- options.assert_valid_keys(:only, :resource, :type)
6
- rule(user_role, :object_role, options, &block)
7
- end
8
- end
9
-
10
- module Rules
11
- class ObjectRole < Basic
12
- def initialize(controller, options, &block)
13
- @controller_klass = controller
14
- @options = options
15
- self
16
- end
17
-
18
- def to_s
19
- if @options[:resource]
20
- "allow when current_user has the role (#{@options[:role]}) for a specific object (#{@options[:resource]})"
21
- else
22
- "allow when current_user has the role (#{@options[:role]}) for any object of type #{@options[:type]}"
23
- end
24
- end
25
-
26
- def authorized?(controller_instance, controller, action, id)
27
- object = @options[:resource].nil? ? nil : find_object(controller_instance) if @options[:resource]
28
-
29
- if object
30
- return true if controller_instance.current_user.has_object_role?(object, @options[:role])
31
- elsif @options[:type].constantize.respond_to?(:enrolled)
32
- return true if @options[:type].constantize.enrolled(@options[:role]).include?(controller_instance.current_user)
33
- end
34
-
35
- return false
36
- end
37
-
38
- def find_object(controller_instance)
39
- # try to find as instance variable
40
- object = controller_instance.instance_variable_get("@#{@options[:resource]}".to_sym) rescue nil
41
-
42
- # try to find based on params
43
- if object.nil? && !controller_instance.params["#{@options[:resource]}_id"].blank?
44
- object = @options[:type].constantize.find_by_id(controller_instance.params["#{@options[:resource]}_id"])
45
- end
46
-
47
- object
48
- end
49
- end
50
- end
51
- end
@@ -1,106 +0,0 @@
1
- module RoleAuthorization
2
- # define our rule helper in Mapper
3
- class Mapper
4
- def resource(user_role, options={}, &block)
5
- options.assert_valid_keys(:resource, :only, :no_send)
6
- rule(user_role, :resource, options, &block)
7
- end
8
- end
9
-
10
- module Rules
11
- class Resource < Basic
12
- def initialize(controller, options, &block)
13
- @controller_klass = controller
14
- @options = {:no_send => false}.merge(options)
15
- @block = block
16
- @mapper = nil
17
-
18
- unless @block.nil?
19
- @mapper = RoleAuthorization::Mapper.new(@controller_klass)
20
- @mapper.instance_eval(&@block)
21
- end
22
- self
23
- end
24
-
25
- def to_s
26
- output = ["allow current_user with role :#{@options[:role]} of requested resource #{@options[:resource]}"]
27
- output << @mapper.to_s
28
- end
29
-
30
- def authorized?(controller_instance, controller, action, id)
31
- object = find_object(controller_instance, controller, action, id)
32
- return true if controller_instance.current_user.has_object_role?(@options[:role], object) unless object.nil?
33
-
34
- unless @mapper.nil?
35
- return true if @mapper.authorized?(controller_instance, controller, action, object)
36
- end
37
-
38
- return false
39
- end
40
-
41
- def find_object(controller_instance, controller, action, id)
42
- object = nil
43
- instance_found = false
44
-
45
- if id.is_a?(Integer) || id.is_a?(String)
46
- # id is a parameter passed in
47
- # we use the :resource option to find the right instance variable
48
- object = controller_instance.instance_variable_get('@' + @options[:resource].to_s) rescue nil
49
- instance_found = true unless object.nil?
50
-
51
- if controller_instance.instance_variable_defined?('@' + controller)
52
- collection = controller_instance.instance_variable_get('@' + controller)
53
- object = collection.detect {|item| item.andand.id == id.to_i}
54
- end
55
-
56
- if object.nil?
57
- model = controller.singularize.camelize.constantize
58
- if model.respond_to?(:to_param_column)
59
- finder = "find_by_#{model.to_param_column}".to_sym
60
- else
61
- finder = :find_by_id
62
- id = id.to_i
63
- end
64
-
65
- object = model.send(finder, id)
66
- end
67
-
68
- unless object.nil?
69
- if @options.has_key?(:resource) && !@options[:no_send] && !instance_found && object.respond_to?(@options[:resource])
70
- object = object.send(@options[:resource])
71
- end
72
- end
73
- elsif id.is_a?(ActiveRecord::Base) && @options.has_key?(:resource)
74
- # id is already a model record so this is a nested rule
75
-
76
- # first try to find it as an instance variable
77
- object = controller_instance.instance_variable_get('@' + @options[:resource].to_s) rescue nil
78
-
79
- if id.respond_to?("#{@options[:resource]}_id") && controller_instance.instance_variable_defined?('@' + @options[:resource].to_s.pluralize)
80
- collection = controller_instance.instance_variable_get('@' + @options[:resource].to_s.pluralize)
81
- object = collection.detect {|item| item.andand.id == id.send("#{@options[:resource]}_id")}
82
- end
83
-
84
- # next we call id's method to find it
85
- object = id.send(@options[:resource]) if object.nil?
86
- elsif id.nil?
87
- # no id means we must be using an association or parent resource for this rule
88
-
89
- if @options.has_key?(:resource)
90
- object_base = @options[:resource].to_s
91
- object_id = controller_instance.params["#{object_base}_id".to_sym]
92
-
93
- unless object_id.nil?
94
- object = controller_instance.instance_variable_get('@' + object_base) rescue nil
95
- object = nil unless object.id == object_id
96
-
97
- object = object_base.to_s.camelize.constantize.find_by_id(object_id.to_i) if object.nil?
98
- end
99
- end
100
- end
101
-
102
- return object
103
- end # find object
104
- end
105
- end
106
- end
@@ -1,70 +0,0 @@
1
- module RoleAuthorization
2
- # define our rule helper in Mapper
3
- class Mapper
4
- def user(options={}, &block)
5
- options.assert_valid_keys(:check, :only, :resource, :association)
6
- rule(:user, :user, options, &block)
7
- end
8
- end
9
-
10
- module Rules
11
- class User < Basic
12
- def initialize(controller, options, &block)
13
- @controller_klass = controller
14
- @options = options
15
- self
16
- end
17
-
18
- def to_s
19
- "allow when current_user.id == #{[@options[:resource], @options[:association], @options[:check]].compact.join('.')}"
20
- end
21
-
22
- def authorized?(controller_instance, controller, action, id)
23
- object = find_object(controller_instance, controller, action, id)
24
-
25
- unless object.nil?
26
- [object].flatten.each do |obj|
27
- return true if controller_instance.current_user.owns?(obj.send(@options[:check]))
28
- end
29
- end
30
-
31
- return false
32
- end
33
-
34
- def find_object(controller_instance, controller, action, id)
35
- object = nil
36
-
37
- if id.nil? && !@options[:resource].nil?
38
- if controller_instance.instance_variable_defined?('@' + @options[:resource].to_s)
39
- object = controller_instance.instance_variable_get('@' + @options[:resource].to_s)
40
- end
41
- model = @options[:resource].to_s.camelize.constantize
42
- elsif id.is_a?(Integer) || id.is_a?(String)
43
- if controller_instance.instance_variable_defined?('@' + controller)
44
- collection = controller_instance.instance_variable_get('@' + controller)
45
- object = collection.detect {|item| item.andand.id == id.to_i}
46
- end
47
- model = controller.singularize.camelize.constantize
48
- elsif id.is_a?(ActiveRecord::Base) && @options.has_key?(:check)
49
- object = id
50
- end
51
-
52
- if object.nil?
53
- if model.respond_to?(:to_param_column)
54
- finder = "find_by_#{model.to_param_column}".to_sym
55
- else
56
- finder = :find_by_id
57
- id = id.to_i
58
- end
59
- object = model.send(finder, id)
60
- end
61
-
62
- unless object.nil? || @options[:check].nil?
63
- object = @options[:association].nil? ? object : object.send(@options[:association])
64
- end
65
-
66
- object
67
- end
68
- end # User
69
- end
70
- end