restricted_access 0.0.2 → 0.1.1
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.
- checksums.yaml +4 -4
- data/README-v0.0.2.md +178 -0
- data/README.md +79 -62
- data/lib/generators/restricted_access/USAGE +1 -1
- data/lib/generators/restricted_access/install_generator.rb +5 -10
- data/lib/generators/restricted_access/templates/restricted_access.erb +3 -17
- data/lib/restricted_access.rb +6 -35
- data/lib/restricted_access/access.rb +2 -3
- data/lib/restricted_access/configuration.rb +1 -3
- data/lib/restricted_access/controller.rb +25 -1
- data/lib/restricted_access/helper.rb +4 -3
- data/lib/restricted_access/models.rb +37 -0
- data/lib/restricted_access/orm/active_record.rb +4 -0
- data/lib/restricted_access/orm/mongoid.rb +6 -0
- data/lib/restricted_access/version.rb +1 -1
- data/restricted_access.gemspec +2 -5
- metadata +8 -46
- data/lib/restricted_access/model.rb +0 -19
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a09963a023398671ce46a2bb029db73ec5d1ad41
|
|
4
|
+
data.tar.gz: fecea55f47f5b79637cbd1a88d178bb46328fb29
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2a0997f4e9dbe5c21eabe6bb8c9e70163721fab8e5b0d4ddce723d4b0bad94dc0ae0ec2b692993de4cdb021337d894fd54740d31ad140024df9ec1e05073201e
|
|
7
|
+
data.tar.gz: c775fc0a5e586b611b9a93fe987ef0bdde98da3bf7a0a5c2ec250192ac48f8b27130df445d84f0b432f7f98455485acf79cb35e0f88a11a955de94fd1c9d6856
|
data/README-v0.0.2.md
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# RestrictedAccess
|
|
2
|
+
|
|
3
|
+
An access rights management tool.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
gem 'restricted_access'
|
|
10
|
+
|
|
11
|
+
And then execute:
|
|
12
|
+
|
|
13
|
+
$ bundle
|
|
14
|
+
|
|
15
|
+
Or install it yourself as:
|
|
16
|
+
|
|
17
|
+
$ gem install restricted_access
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
The gem is currently working only with Mongoid.
|
|
22
|
+
|
|
23
|
+
It depends on [Devise](https://github.com/plataformatec/devise) & [mongoid-enum](https://github.com/thetron/mongoid-enum).
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
Generate this initializer with
|
|
27
|
+
```
|
|
28
|
+
rails g restricted_access:install admin --levels=mini normal super --controller_scope=backoffice
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
model_name is the name of the model concerned with the access restriction.
|
|
32
|
+
|
|
33
|
+
Give the available levels of access to the --levels options
|
|
34
|
+
|
|
35
|
+
Give your controllers scope name to the --controller_scope options (default: nil)
|
|
36
|
+
|
|
37
|
+
This will generate the restricted_access.rb initializer
|
|
38
|
+
|
|
39
|
+
```ruby
|
|
40
|
+
RestrictedAccess.configure do |config|
|
|
41
|
+
|
|
42
|
+
config.accesses = [ { level: :mini,
|
|
43
|
+
label: 'Some description for this access level',
|
|
44
|
+
power: 0 },
|
|
45
|
+
{ level: :normal,
|
|
46
|
+
label: 'Some description for this access level',
|
|
47
|
+
power: 1 },
|
|
48
|
+
{ level: :super,
|
|
49
|
+
label: 'Some description for this access level',
|
|
50
|
+
power: 2}
|
|
51
|
+
]
|
|
52
|
+
config.resource = :admin
|
|
53
|
+
config.controller_scope = :backoffice
|
|
54
|
+
|
|
55
|
+
end
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
You can customize the accesses with a label (optional) and define different power (the higher has more rights).
|
|
59
|
+
|
|
60
|
+
The `config.resource` and `config.controller_scope` are useful only in Rails, defining some methods in controllers and helpers (see below).
|
|
61
|
+
|
|
62
|
+
### RestrictedAccess::Model
|
|
63
|
+
|
|
64
|
+
Include the RestrictedAccess::Model module in your related model
|
|
65
|
+
|
|
66
|
+
```ruby
|
|
67
|
+
class Admin
|
|
68
|
+
include Mongoid::Document
|
|
69
|
+
include RestrictedAccess::Model
|
|
70
|
+
|
|
71
|
+
end
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
The module enhances the model with some methods and attributes.
|
|
75
|
+
|
|
76
|
+
Every model has now a :level attribute (Symbol type), by default the first defined in your initializer. You can set it like any attributes.
|
|
77
|
+
|
|
78
|
+
```ruby
|
|
79
|
+
admin = Admin.first
|
|
80
|
+
admin.update(level: :super)
|
|
81
|
+
|
|
82
|
+
admin2 = Admin.last
|
|
83
|
+
admin.update(level: :mini)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
The level defines its access rights.
|
|
87
|
+
|
|
88
|
+
Each instance has a `:access` method, returning a `RestrictedAccess::Access` instance.
|
|
89
|
+
|
|
90
|
+
```ruby
|
|
91
|
+
admin.access
|
|
92
|
+
=> #<RestrictedAccess::Access:0x007fc255d36098 @level=:super, @label="", @power=2>
|
|
93
|
+
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
The `RestrictedAccess::Access` class include comparable, so you can do such things :
|
|
97
|
+
|
|
98
|
+
```ruby
|
|
99
|
+
admin.access > admin2.access
|
|
100
|
+
=> true
|
|
101
|
+
|
|
102
|
+
RestrictedAccess.accesses.max
|
|
103
|
+
=> #<RestrictedAccess::Access:0x007fc255d36098 @level=:super, @label="", @power=2>
|
|
104
|
+
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Thanks to the [mongoid-enum](https://github.com/thetron/mongoid-enum) gem, some methods to check rights.
|
|
108
|
+
|
|
109
|
+
```ruby
|
|
110
|
+
admin.mini?
|
|
111
|
+
=> false
|
|
112
|
+
|
|
113
|
+
admin.super?
|
|
114
|
+
=> true
|
|
115
|
+
|
|
116
|
+
Admin::LEVEL
|
|
117
|
+
=> [:mini, :normal, :super]
|
|
118
|
+
|
|
119
|
+
# scopes
|
|
120
|
+
Admin.mini # => Mongoid::Criteria
|
|
121
|
+
Admin.super # => Mongoid::Criteria
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### RestrictedAccess::Controller
|
|
125
|
+
|
|
126
|
+
If you provided a `config.resource` and `config.controller_scope` in the initializer you can include the `RestrictedAccess::Controller` in your controller.
|
|
127
|
+
|
|
128
|
+
```ruby
|
|
129
|
+
class Backoffice::BaseController < ApplicationController
|
|
130
|
+
include RestrictedAccess::Controller
|
|
131
|
+
end
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Every inherited controller has now a few more methods:
|
|
135
|
+
|
|
136
|
+
* `:restrict_access`, which redirect to the `#{controller_scope}_root_path`. Set controller_scope to nil if you just want to redirect to root_path.
|
|
137
|
+
|
|
138
|
+
* `:prevent_#{level}_access`, which calls `:restrict_access` if the `:current_#{resource_name}` doesn't have enough access right. If you use Devise, you already have a `:current_#{resource_name}` method, if you don't use Devise, just implement it.
|
|
139
|
+
|
|
140
|
+
```ruby
|
|
141
|
+
class Backoffice::AdminsController < Backoffice::BaseController
|
|
142
|
+
before_action :prevent_normal_access, except: [:index]
|
|
143
|
+
# mini & normal admins will only be able to access index view
|
|
144
|
+
end
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### RestrictedAccess::Helper
|
|
148
|
+
|
|
149
|
+
```ruby
|
|
150
|
+
module Backoffice::AdminHelper
|
|
151
|
+
include RestrictedAccess::Helper
|
|
152
|
+
end
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
If you provided a `config.resource` option, you can include the `RestrictedAccess::Helper` in one of your helpers.
|
|
157
|
+
|
|
158
|
+
It provides a `:available_for` method in the views, allowing you to hide some part of the view.
|
|
159
|
+
|
|
160
|
+
```html
|
|
161
|
+
<!-- this div won't be seen be admins lower than super -->
|
|
162
|
+
<%= available_for :super do %>
|
|
163
|
+
<div>
|
|
164
|
+
I have something to hide here.
|
|
165
|
+
</div>
|
|
166
|
+
<%- end %>
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
## Contributing
|
|
173
|
+
|
|
174
|
+
1. Fork it ( http://github.com/<my-github-username>/restricted_access/fork )
|
|
175
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
176
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
177
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
178
|
+
5. Create new Pull Request
|
data/README.md
CHANGED
|
@@ -6,7 +6,7 @@ An access rights management tool.
|
|
|
6
6
|
|
|
7
7
|
Add this line to your application's Gemfile:
|
|
8
8
|
|
|
9
|
-
gem 'restricted_access'
|
|
9
|
+
gem 'restricted_access'
|
|
10
10
|
|
|
11
11
|
And then execute:
|
|
12
12
|
|
|
@@ -16,64 +16,63 @@ Or install it yourself as:
|
|
|
16
16
|
|
|
17
17
|
$ gem install restricted_access
|
|
18
18
|
|
|
19
|
-
##
|
|
19
|
+
## Previous version of the README (for version 0.0.2)
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
can be found [here](https://github.com/4nt1/restricted_access/blob/develop/README-v0.0.2.md)
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
## Usage
|
|
24
24
|
|
|
25
|
+
Generate the config file with the generator, pass the resources as an argument.
|
|
25
26
|
|
|
26
|
-
Generate this initializer with
|
|
27
27
|
```
|
|
28
|
-
rails g restricted_access:install
|
|
28
|
+
rails g restricted_access:install -r user admin
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
## Active Record
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
You have to run a migration to add the `:level` field to your model.
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
```
|
|
36
|
+
$ rails g migration AddLevelToAdmin
|
|
37
|
+
```
|
|
38
|
+
You can edit it this way :
|
|
38
39
|
|
|
39
40
|
```ruby
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
power: 0 },
|
|
45
|
-
{ level: :normal,
|
|
46
|
-
label: 'Some description for this access level',
|
|
47
|
-
power: 1 },
|
|
48
|
-
{ level: :super,
|
|
49
|
-
label: 'Some description for this access level',
|
|
50
|
-
power: 2}
|
|
51
|
-
]
|
|
52
|
-
config.resource = :admin
|
|
53
|
-
config.controller_scope = :backoffice
|
|
54
|
-
|
|
41
|
+
class AddLevelToAdmins < ActiveRecord::Migration
|
|
42
|
+
def change
|
|
43
|
+
add_column :admins, :level, :integer
|
|
44
|
+
end
|
|
55
45
|
end
|
|
46
|
+
|
|
56
47
|
```
|
|
57
48
|
|
|
58
|
-
|
|
49
|
+
## Mongoid support
|
|
59
50
|
|
|
60
|
-
|
|
51
|
+
You need the [mongoid-enum](https://github.com/thetron/mongoid-enum) gem if you use mongoid.
|
|
52
|
+
Just ad it to your gemfile.
|
|
61
53
|
|
|
62
|
-
|
|
54
|
+
```
|
|
55
|
+
gem 'mongoid-enum'
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
I didn't ship it as a dependency as it's useless for active_record users, and I didn't want to copy/paste some nice code done by someone else.
|
|
63
59
|
|
|
64
|
-
|
|
60
|
+
### Models
|
|
61
|
+
|
|
62
|
+
You must define the different levels of accesses in your model.
|
|
65
63
|
|
|
66
64
|
```ruby
|
|
67
65
|
class Admin
|
|
68
66
|
include Mongoid::Document
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
access_levels mini: 0, normal: 1, super: 2
|
|
71
68
|
end
|
|
72
69
|
```
|
|
73
70
|
|
|
74
71
|
The module enhances the model with some methods and attributes.
|
|
75
72
|
|
|
76
|
-
Every model has now a :level attribute
|
|
73
|
+
Every model has now a :level attribute, by default the first defined by the class method.
|
|
74
|
+
|
|
75
|
+
You can set it like any attributes.
|
|
77
76
|
|
|
78
77
|
```ruby
|
|
79
78
|
admin = Admin.first
|
|
@@ -89,7 +88,7 @@ Each instance has a `:access` method, returning a `RestrictedAccess::Access` ins
|
|
|
89
88
|
|
|
90
89
|
```ruby
|
|
91
90
|
admin.access
|
|
92
|
-
=> #<RestrictedAccess::Access:0x007fc255d36098 @level=:super, @
|
|
91
|
+
=> #<RestrictedAccess::Access:0x007fc255d36098 @level=:super, @power=2>
|
|
93
92
|
|
|
94
93
|
```
|
|
95
94
|
|
|
@@ -99,12 +98,11 @@ The `RestrictedAccess::Access` class include comparable, so you can do such thin
|
|
|
99
98
|
admin.access > admin2.access
|
|
100
99
|
=> true
|
|
101
100
|
|
|
102
|
-
|
|
103
|
-
=> #<RestrictedAccess::Access:0x007fc255d36098 @level=:super, @
|
|
101
|
+
Admin.accesses.max
|
|
102
|
+
=> #<RestrictedAccess::Access:0x007fc255d36098 @level=:super, @power=2>
|
|
104
103
|
|
|
105
104
|
```
|
|
106
|
-
|
|
107
|
-
Thanks to the [mongoid-enum](https://github.com/thetron/mongoid-enum) gem, some methods to check rights.
|
|
105
|
+
As the `:level` attribute is an enum, you get this kind of methods :
|
|
108
106
|
|
|
109
107
|
```ruby
|
|
110
108
|
admin.mini?
|
|
@@ -113,53 +111,72 @@ admin.mini?
|
|
|
113
111
|
admin.super?
|
|
114
112
|
=> true
|
|
115
113
|
|
|
116
|
-
Admin::LEVEL
|
|
117
|
-
=> [:mini, :normal, :super]
|
|
118
|
-
|
|
119
114
|
# scopes
|
|
120
|
-
Admin.mini # =>
|
|
121
|
-
|
|
115
|
+
Admin.mini # => Mongoid::Criteria
|
|
116
|
+
# or
|
|
117
|
+
Admin.super # => ActiveRecord::Relation
|
|
122
118
|
```
|
|
123
119
|
|
|
124
|
-
###
|
|
120
|
+
### Controllers
|
|
121
|
+
|
|
122
|
+
Controllers inheriting from ApplicationController have now a few more methods:
|
|
123
|
+
|
|
124
|
+
* `:prevent_#{level}_#{resource_name}_access`, which calls `:restrict_#{current_resource.level}_#{resource_name}_access` if the `:current_#{resource_name}` doesn't have enough access right.
|
|
125
125
|
|
|
126
|
-
If you
|
|
126
|
+
If you use Devise, you already have a `:current_#{resource_name}` method, if you don't use Devise, just implement the method.
|
|
127
127
|
|
|
128
128
|
```ruby
|
|
129
|
-
class
|
|
130
|
-
|
|
129
|
+
class FrontController < ApplicationController
|
|
130
|
+
before_action :prevent_normal_admin_access, except: [:index]
|
|
131
|
+
# mini & normal admins will only be able to access index view
|
|
131
132
|
end
|
|
132
133
|
```
|
|
133
134
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
* `:restrict_access`, which redirect to the `#{controller_scope}_root_path`. Set controller_scope to nil if you just want to redirect to root_path.
|
|
135
|
+
* `:restrict_#{level}_#{resource_name}_access` which redirect to the `root_path`.
|
|
137
136
|
|
|
138
|
-
|
|
137
|
+
You can override the method in your controller to do something else.
|
|
139
138
|
|
|
140
139
|
```ruby
|
|
141
|
-
class
|
|
142
|
-
before_action :
|
|
143
|
-
#
|
|
140
|
+
class FrontController < ApplicationController
|
|
141
|
+
before_action :prevent_normal_admin_access, except: [:index]
|
|
142
|
+
# Some code ...
|
|
143
|
+
|
|
144
|
+
private
|
|
145
|
+
|
|
146
|
+
def restrict_mini_admin_access
|
|
147
|
+
redirect_to some_path, notice: 'Nope dude' and return
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def restrict_normal_admin_access
|
|
151
|
+
redirect_to some_other_path, notice: 'Nope dude' and return
|
|
152
|
+
end
|
|
144
153
|
end
|
|
145
154
|
```
|
|
146
155
|
|
|
147
|
-
|
|
156
|
+
You can also define a more global `:restrict_#{resource_name}_access` method which applies to all concerned resources.
|
|
148
157
|
|
|
149
158
|
```ruby
|
|
150
|
-
|
|
151
|
-
|
|
159
|
+
class FrontController < ApplicationController
|
|
160
|
+
|
|
161
|
+
private
|
|
162
|
+
# this method as a bigger precedency than the other one
|
|
163
|
+
def restrict_admin_access
|
|
164
|
+
redirect_to some_path, notice: 'Nope dude' and return
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def restrict_normal_admin_access
|
|
168
|
+
redirect_to some_other_path, notice: 'Nope dude' and return
|
|
169
|
+
end
|
|
152
170
|
end
|
|
153
171
|
```
|
|
154
172
|
|
|
173
|
+
### Helpers
|
|
155
174
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
It provides a `:available_for` method in the views, allowing you to hide some part of the view.
|
|
175
|
+
The gem provides a `:available_for` method in the views, allowing you to hide some part of the view.
|
|
159
176
|
|
|
160
177
|
```html
|
|
161
178
|
<!-- this div won't be seen be admins lower than super -->
|
|
162
|
-
<%= available_for :super do %>
|
|
179
|
+
<%= available_for :super, :admin do %>
|
|
163
180
|
<div>
|
|
164
181
|
I have something to hide here.
|
|
165
182
|
</div>
|
|
@@ -171,7 +188,7 @@ It provides a `:available_for` method in the views, allowing you to hide some pa
|
|
|
171
188
|
|
|
172
189
|
## Contributing
|
|
173
190
|
|
|
174
|
-
1. Fork it ( http://github.com
|
|
191
|
+
1. Fork it ( http://github.com/4nt1/restricted_access/fork )
|
|
175
192
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
176
193
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
177
194
|
4. Push to the branch (`git push origin my-new-feature`)
|
|
@@ -1,19 +1,14 @@
|
|
|
1
|
+
require 'rails/generators/base'
|
|
2
|
+
require 'securerandom'
|
|
1
3
|
module RestrictedAccess
|
|
2
4
|
module Generators
|
|
3
|
-
class InstallGenerator < Rails::Generators::
|
|
4
|
-
include Rails::Generators::ResourceHelpers
|
|
5
|
+
class InstallGenerator < Rails::Generators::Base
|
|
5
6
|
source_root File.expand_path('../templates', __FILE__)
|
|
6
|
-
argument :resource_name, type: :string, default: 'user'
|
|
7
|
-
class_option :levels, type: :array, default: ['normal', 'super'], desc: "List of the differents access levels"
|
|
8
|
-
class_option :controller_scope, type: :string, desc: "Scope of the concerned controllers"
|
|
9
7
|
|
|
10
8
|
desc "Creates a RestrictedAccess initializer."
|
|
9
|
+
class_option :orm
|
|
11
10
|
|
|
12
|
-
|
|
13
|
-
@levels = options.levels
|
|
14
|
-
@resource_name = resource_name
|
|
15
|
-
@controller_scope = options.controller_scope
|
|
16
|
-
end
|
|
11
|
+
class_option :resources, type: :array, desc: "List of the differents resources", aliases: '-r'
|
|
17
12
|
|
|
18
13
|
def copy_initializer
|
|
19
14
|
template "restricted_access.erb", "config/initializers/restricted_access.rb"
|
|
@@ -1,18 +1,4 @@
|
|
|
1
|
-
RestrictedAccess.configure do |
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
{ level: :<%= level %>,
|
|
5
|
-
label: '',
|
|
6
|
-
power: <%= index %> }<% if index + 1 < @levels.count %>,<%- end %>
|
|
7
|
-
<%- end %>
|
|
8
|
-
]
|
|
9
|
-
|
|
10
|
-
config.resource = :<%= @resource_name %>
|
|
11
|
-
|
|
12
|
-
<% if @controller_scope %>
|
|
13
|
-
config.controller_scope = :<%= @controller_scope %>
|
|
14
|
-
<% else %>
|
|
15
|
-
config.controller_scope = nil
|
|
16
|
-
<%- end %>
|
|
17
|
-
|
|
1
|
+
RestrictedAccess.configure do |conf|
|
|
2
|
+
require 'restricted_access/orm/<%= options[:orm] %>'
|
|
3
|
+
conf.resources = <%= options[:resources].map { |r| r.downcase.singularize.to_sym } %>
|
|
18
4
|
end
|
data/lib/restricted_access.rb
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
require "mongoid/enum"
|
|
2
1
|
require "restricted_access/version"
|
|
3
2
|
require 'restricted_access/configuration'
|
|
4
3
|
require 'restricted_access/access'
|
|
5
|
-
require 'restricted_access/
|
|
6
|
-
require 'restricted_access/controller'
|
|
4
|
+
require 'restricted_access/models'
|
|
7
5
|
require 'restricted_access/helper'
|
|
8
6
|
|
|
9
7
|
module RestrictedAccess
|
|
@@ -15,41 +13,14 @@ module RestrictedAccess
|
|
|
15
13
|
|
|
16
14
|
def configure
|
|
17
15
|
yield(configuration)
|
|
18
|
-
|
|
16
|
+
require 'restricted_access/controller'
|
|
17
|
+
ActionController::Base.send(:include, RestrictedAccess::Controller)
|
|
18
|
+
ActionView::Base.send(:include, RestrictedAccess::Helper)
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
def
|
|
22
|
-
@
|
|
23
|
-
Access.new(a[:level], a[:label], a[:power])
|
|
24
|
-
end
|
|
21
|
+
def resources
|
|
22
|
+
@resources ||= configuration.resources
|
|
25
23
|
end
|
|
26
24
|
|
|
27
|
-
def resource
|
|
28
|
-
@resource ||= configuration.resource
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
def controller_scope
|
|
32
|
-
@controller_scope ||= configuration.controller_scope
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
def define_dynamic_methods
|
|
36
|
-
# on Access class
|
|
37
|
-
accesses.map(&:level).each do |level|
|
|
38
|
-
Access.define_singleton_method level do
|
|
39
|
-
RestrictedAccess.accesses.find {|a| a.level == level}
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
RestrictedAccess::Controller.class_eval do
|
|
43
|
-
define_method "prevent_#{level}_access" do
|
|
44
|
-
restrict_access if send("current_#{RestrictedAccess.resource}").access <= RestrictedAccess::Access.send(level)
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
define_method :restrict_access do
|
|
48
|
-
_scope = RestrictedAccess.controller_scope.present? ? "#{RestrictedAccess.controller_scope}_" : nil
|
|
49
|
-
redirect_to send("#{_scope}root_path"), notice: 'You do not have access to this page' and return
|
|
50
|
-
end
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
end
|
|
54
25
|
end
|
|
55
26
|
end
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
module RestrictedAccess
|
|
2
2
|
class Access
|
|
3
3
|
include Comparable
|
|
4
|
-
attr_accessor :level, :
|
|
4
|
+
attr_accessor :level, :power
|
|
5
5
|
|
|
6
6
|
def <=>(access)
|
|
7
7
|
power <=> access.power
|
|
8
8
|
end
|
|
9
9
|
|
|
10
|
-
def initialize(level,
|
|
10
|
+
def initialize(level, power)
|
|
11
11
|
@level = level
|
|
12
|
-
@label = label
|
|
13
12
|
@power = power
|
|
14
13
|
end
|
|
15
14
|
|
|
@@ -1,6 +1,30 @@
|
|
|
1
1
|
module RestrictedAccess
|
|
2
2
|
module Controller
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
RestrictedAccess.resources.each do |resource_name|
|
|
5
|
+
klass = resource_name.to_s.classify.constantize
|
|
6
|
+
if klass.accesses.present?
|
|
7
|
+
klass.accesses.each do |access|
|
|
8
|
+
|
|
9
|
+
define_method "prevent_#{access.level}_#{resource_name}_access" do
|
|
10
|
+
current_resource = send("current_#{resource_name}")
|
|
11
|
+
if current_resource && current_resource.access <= klass.send(:access, access.level)
|
|
12
|
+
if respond_to?("restrict_#{resource_name}_access", true)
|
|
13
|
+
send("restrict_#{resource_name}_access")
|
|
14
|
+
else
|
|
15
|
+
send("restrict_#{current_resource.level}_#{resource_name}_access"
|
|
16
|
+
)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
define_method "restrict_#{access.level}_#{resource_name}_access" do
|
|
22
|
+
redirect_to root_path, notice: 'You do not have access to this page' and return
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
5
26
|
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
end
|
|
6
30
|
end
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
module RestrictedAccess
|
|
2
2
|
module Helper
|
|
3
|
-
def available_for(level, &block)
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
def available_for(level, resource_name, &block)
|
|
4
|
+
klass = resource_name.to_s.classify.constantize
|
|
5
|
+
access = klass.send(level)
|
|
6
|
+
capture(&block) if access && send("current_#{resource_name}") && send("current_#{resource_name}").access >= access
|
|
6
7
|
end
|
|
7
8
|
end
|
|
8
9
|
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
module RestrictedAccess
|
|
2
|
+
module Models
|
|
3
|
+
extend ActiveSupport::Concern
|
|
4
|
+
|
|
5
|
+
module ClassMethods
|
|
6
|
+
|
|
7
|
+
attr_reader :accesses
|
|
8
|
+
|
|
9
|
+
def access_levels(accesses)
|
|
10
|
+
@accesses = accesses.map { |k, v| Access.new(k, v) }
|
|
11
|
+
|
|
12
|
+
# define level store field
|
|
13
|
+
if defined?(Mongoid::Document)
|
|
14
|
+
enum :level, accesses.keys
|
|
15
|
+
elsif defined?(ActiveRecord::Base)
|
|
16
|
+
enum level: accesses
|
|
17
|
+
else
|
|
18
|
+
raise 'Your ORM is not recognized.'
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def access(level_name)
|
|
24
|
+
@accesses.find { |a| a.level == level_name }
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def access
|
|
29
|
+
self.class.access(level.to_sym)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def authorized_accesses
|
|
33
|
+
self.class.accesses.select { |a| a <= access }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
end
|
|
37
|
+
end
|
data/restricted_access.gemspec
CHANGED
|
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
|
|
|
8
8
|
spec.version = RestrictedAccess::VERSION
|
|
9
9
|
spec.authors = ["4nt1"]
|
|
10
10
|
spec.email = ["antoinemary@hotmail.fr"]
|
|
11
|
-
spec.summary = %q{An access rights management tool
|
|
12
|
-
spec.description = %q{An access rights management tool
|
|
11
|
+
spec.summary = %q{An access rights management tool}
|
|
12
|
+
spec.description = %q{An access rights management tool. Define routes and part of views that can be accessed by resources.}
|
|
13
13
|
spec.homepage = "https://github.com/4nt1/restricted_access"
|
|
14
14
|
spec.license = "MIT"
|
|
15
15
|
|
|
@@ -19,7 +19,4 @@ Gem::Specification.new do |spec|
|
|
|
19
19
|
spec.require_paths = ["lib"]
|
|
20
20
|
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.5"
|
|
22
|
-
spec.add_development_dependency "rake"
|
|
23
|
-
spec.add_dependency "mongoid"
|
|
24
|
-
spec.add_dependency "mongoid-enum"
|
|
25
22
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: restricted_access
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- 4nt1
|
|
@@ -24,49 +24,8 @@ dependencies:
|
|
|
24
24
|
- - "~>"
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
26
|
version: '1.5'
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
|
30
|
-
requirements:
|
|
31
|
-
- - ">="
|
|
32
|
-
- !ruby/object:Gem::Version
|
|
33
|
-
version: '0'
|
|
34
|
-
type: :development
|
|
35
|
-
prerelease: false
|
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
-
requirements:
|
|
38
|
-
- - ">="
|
|
39
|
-
- !ruby/object:Gem::Version
|
|
40
|
-
version: '0'
|
|
41
|
-
- !ruby/object:Gem::Dependency
|
|
42
|
-
name: mongoid
|
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
|
44
|
-
requirements:
|
|
45
|
-
- - ">="
|
|
46
|
-
- !ruby/object:Gem::Version
|
|
47
|
-
version: '0'
|
|
48
|
-
type: :runtime
|
|
49
|
-
prerelease: false
|
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
-
requirements:
|
|
52
|
-
- - ">="
|
|
53
|
-
- !ruby/object:Gem::Version
|
|
54
|
-
version: '0'
|
|
55
|
-
- !ruby/object:Gem::Dependency
|
|
56
|
-
name: mongoid-enum
|
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
|
58
|
-
requirements:
|
|
59
|
-
- - ">="
|
|
60
|
-
- !ruby/object:Gem::Version
|
|
61
|
-
version: '0'
|
|
62
|
-
type: :runtime
|
|
63
|
-
prerelease: false
|
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
-
requirements:
|
|
66
|
-
- - ">="
|
|
67
|
-
- !ruby/object:Gem::Version
|
|
68
|
-
version: '0'
|
|
69
|
-
description: An access rights management tool intended to work with Devise
|
|
27
|
+
description: An access rights management tool. Define routes and part of views that
|
|
28
|
+
can be accessed by resources.
|
|
70
29
|
email:
|
|
71
30
|
- antoinemary@hotmail.fr
|
|
72
31
|
executables: []
|
|
@@ -76,6 +35,7 @@ files:
|
|
|
76
35
|
- ".gitignore"
|
|
77
36
|
- Gemfile
|
|
78
37
|
- LICENSE.txt
|
|
38
|
+
- README-v0.0.2.md
|
|
79
39
|
- README.md
|
|
80
40
|
- Rakefile
|
|
81
41
|
- lib/generators/restricted_access/USAGE
|
|
@@ -86,7 +46,9 @@ files:
|
|
|
86
46
|
- lib/restricted_access/configuration.rb
|
|
87
47
|
- lib/restricted_access/controller.rb
|
|
88
48
|
- lib/restricted_access/helper.rb
|
|
89
|
-
- lib/restricted_access/
|
|
49
|
+
- lib/restricted_access/models.rb
|
|
50
|
+
- lib/restricted_access/orm/active_record.rb
|
|
51
|
+
- lib/restricted_access/orm/mongoid.rb
|
|
90
52
|
- lib/restricted_access/version.rb
|
|
91
53
|
- restricted_access.gemspec
|
|
92
54
|
homepage: https://github.com/4nt1/restricted_access
|
|
@@ -112,5 +74,5 @@ rubyforge_project:
|
|
|
112
74
|
rubygems_version: 2.2.2
|
|
113
75
|
signing_key:
|
|
114
76
|
specification_version: 4
|
|
115
|
-
summary: An access rights management tool
|
|
77
|
+
summary: An access rights management tool
|
|
116
78
|
test_files: []
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
module RestrictedAccess
|
|
2
|
-
module Model
|
|
3
|
-
extend ActiveSupport::Concern
|
|
4
|
-
|
|
5
|
-
included do |base|
|
|
6
|
-
include Mongoid::Enum
|
|
7
|
-
enum :level, RestrictedAccess.accesses.map(&:level)
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
def access
|
|
11
|
-
RestrictedAccess.accesses.find {|a| a.level == level}
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
def authorized_accesses
|
|
15
|
-
RestrictedAccess.accesses.select {|a| a <= access}
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
end
|
|
19
|
-
end
|