role_authorization 0.5.5 → 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,8 +1,62 @@
1
- Features
1
+ Overview
2
2
  --------
3
3
 
4
- Requirements
5
- ------------
4
+ Role Authorization is a gem for Rails 3.x applications that provides role based access control.
5
+
6
+ * You must use the "allow {}" method in a controller for it to filter access. Access is based on a whitelist. The current_user is checked against each defined rule for the action trying to be accessed and if no rules pass the user is denied. Once on rule passes the user is given access.
7
+
8
+
9
+ You can also enable view_security which will dynamically hide form_for, link_to and button_to outputs if the current_user does not have access to it (hide meaning not render at all).
10
+
6
11
 
7
12
  Usage
8
- -----
13
+ -------
14
+
15
+ # config/initializers/01_roles.rb
16
+ RoleAuthorization::Roles.configure do
17
+ roles([
18
+ :all,
19
+ :developer
20
+ ],
21
+ :area => [
22
+ :area_worker
23
+ ])
24
+ end
25
+
26
+ Define your roles in an initializer. The :all role is special and grants access no matter what. :developer is considered a "global" role in that a User may be #enroll into it (user.enroll(:developer) ).
27
+
28
+ The :area key defines a scoped role. In this case you may have many Areas in your application and each Area may have many workers. If you need to account for this in authorization you can do: user.enroll(:area_worker, Area.find(1)). The second option to enroll defines a scope. In this case a user would have the role of area_worker but only in Area 1.
29
+
30
+
31
+
32
+ # controllers
33
+ allow do
34
+ all :only => [:index]
35
+ role :area_worker, :scope => proc {Area.find(params[:area_id])}, :only => [:edit, :update]
36
+ role :area_worker, :scope => :area, :only => [:new, :create]
37
+ end
38
+
39
+
40
+ Here we use a given rule, all to let anyone and everyone view the index action. The next rule allows a user with the area_worker role in that specified area to access the edit/update actions. Notice the use of proc which will be instance_evaled on the controller instance. (Useful if using inherited_resources). The next rule allows any area_worker in any area access to the new/create actions.
41
+
42
+
43
+ # defining your own rules lib/rules/*.rb
44
+ RoleAuthorization::Rules.define :logged_in do
45
+ controller_instance.logged_in?
46
+ end
47
+
48
+ Define a rule (the name you specify is the method you will use in the controller) and give it a block to execute. The block must *not* use return but instead softly return true or false. You have access to the controller_instance variable as well as the options variable. options contains the options passed to the rule in a controller. For example:
49
+
50
+
51
+ # controller
52
+ allow do
53
+ logged_in :only => [:index], :resource => proc {Area.find(1)}
54
+ end
55
+
56
+ # rule
57
+ RoleAuthorization::Rules.define :logged_in do
58
+ resource = controller_instance.instance_eval(&options[:resource])
59
+ controller_instance.logged_in?(resource)
60
+ end
61
+
62
+
@@ -12,7 +12,7 @@ module RoleAuthorization
12
12
  @global_roles = {}
13
13
  @object_roles = []
14
14
  @groups = Hash.new
15
- @creations = Hash.new
15
+ @creations = Hash.new(Array.new)
16
16
  @nicknames = Hash.new {|hash, key| key}
17
17
 
18
18
  self
@@ -72,6 +72,28 @@ module RoleAuthorization
72
72
  def group(group_name)
73
73
  RoleAuthorization::Roles.manager.groups[group_name.to_sym]
74
74
  end
75
+
76
+ def roles(scope = nil, creations = nil)
77
+ scoped_roles = if scope.nil? || scope.to_sym == :global
78
+ RoleAuthorization::Roles.manager.global_roles
79
+ else
80
+ scope = if scope.is_a?(Class)
81
+ scope.class.to_s.downcase.to_sym
82
+ else
83
+ scope.to_s.downcase.to_sym
84
+ end
85
+
86
+ RoleAuthorization::Roles.manager.object_roles[scope]
87
+ end
88
+
89
+ if creation.nil?
90
+ scoped_roles
91
+ else
92
+ creations.map do |creation|
93
+ scoped_roles & RoleAuthorization::Roles.creations[creation]
94
+ end.flatten.uniq
95
+ end
96
+ end
75
97
  end
76
98
  end
77
99
  end
@@ -1,3 +1,3 @@
1
1
  module RoleAuthorization
2
- VERSION = "0.5.5"
2
+ VERSION = "0.5.6"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: role_authorization
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.5
4
+ version: 0.5.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-07-08 00:00:00.000000000 -04:00
12
+ date: 2011-07-25 00:00:00.000000000 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
17
- requirement: &15561940 !ruby/object:Gem::Requirement
17
+ requirement: &21550160 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *15561940
25
+ version_requirements: *21550160
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rr
28
- requirement: &15561520 !ruby/object:Gem::Requirement
28
+ requirement: &21549620 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *15561520
36
+ version_requirements: *21549620
37
37
  description: A gem for handling authorization in rails using roles
38
38
  email:
39
39
  - machinist@asceth.com