the_role 2.1.1 → 2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +27 -3
- data/.ruby-gemset.example +1 -0
- data/.ruby-version.example +1 -0
- data/.rvmrc.example +1 -1
- data/.travis.yml +2 -2
- data/README.md +46 -47
- data/app/assets/javascripts/the_role_editinplace.js.coffee +32 -0
- data/app/controllers/admin/role_sections_controller.rb +2 -2
- data/app/controllers/admin/roles_controller.rb +2 -2
- data/app/controllers/concerns/controller.rb +23 -0
- data/{lib/generators/the_role/templates → app/models/_templates_}/role.rb +1 -1
- data/app/models/concerns/base.rb +37 -0
- data/app/models/concerns/role.rb +134 -0
- data/app/models/concerns/user.rb +57 -0
- data/app/views/admin/roles/_role.html.haml +90 -69
- data/app/views/admin/roles/_sidebar.html.haml +16 -9
- data/app/views/admin/roles/new.html.haml +11 -10
- data/config/locales/en.yml +1 -0
- data/config/locales/es.yml +28 -28
- data/config/locales/ru.yml +1 -1
- data/db/migrate/20111025025129_create_roles.rb +7 -1
- data/docs/2.1.1.png +0 -0
- data/lib/generators/the_role/USAGE +1 -1
- data/lib/generators/the_role/templates/the_role.rb +7 -2
- data/lib/generators/the_role/the_role_generator.rb +8 -5
- data/lib/tasks/roles.rake +1 -1
- data/lib/the_role/activerecord.rb +11 -0
- data/lib/the_role/config.rb +7 -0
- data/lib/the_role/version.rb +1 -1
- data/lib/the_role.rb +19 -5
- data/spec/dummy_app/.gitignore +18 -0
- data/spec/dummy_app/.rspec +1 -0
- data/spec/dummy_app/Gemfile +32 -0
- data/spec/dummy_app/README.md +39 -0
- data/spec/dummy_app/Rakefile +6 -0
- data/spec/dummy_app/app/assets/images/.keep +0 -0
- data/spec/dummy_app/app/assets/javascripts/application.js +17 -0
- data/spec/dummy_app/app/assets/stylesheets/app/style.css.scss +23 -0
- data/spec/dummy_app/app/assets/stylesheets/application.css +3 -0
- data/spec/dummy_app/app/controllers/application_controller.rb +38 -0
- data/spec/dummy_app/app/controllers/concerns/.keep +0 -0
- data/spec/dummy_app/app/controllers/pages_controller.rb +75 -0
- data/spec/dummy_app/app/controllers/users_controller.rb +32 -0
- data/spec/dummy_app/app/controllers/welcome_controller.rb +13 -0
- data/spec/dummy_app/app/helpers/application_helper.rb +2 -0
- data/spec/dummy_app/app/mailers/.keep +0 -0
- data/spec/dummy_app/app/models/.keep +0 -0
- data/spec/dummy_app/app/models/concerns/.keep +0 -0
- data/spec/dummy_app/app/models/page.rb +24 -0
- data/spec/dummy_app/app/models/role.rb +4 -0
- data/spec/dummy_app/app/models/user.rb +21 -0
- data/spec/dummy_app/app/views/layouts/_current_user_info.html.haml +17 -0
- data/spec/dummy_app/app/views/layouts/application.html.haml +68 -0
- data/spec/dummy_app/app/views/pages/_form.html.haml +19 -0
- data/spec/dummy_app/app/views/pages/edit.html.haml +7 -0
- data/spec/dummy_app/app/views/pages/index.html.haml +27 -0
- data/spec/dummy_app/app/views/pages/manage.html.haml +23 -0
- data/spec/dummy_app/app/views/pages/my.html.haml +10 -0
- data/spec/dummy_app/app/views/pages/new.html.haml +5 -0
- data/spec/dummy_app/app/views/pages/show.html.haml +15 -0
- data/spec/dummy_app/app/views/users/edit.html.haml +34 -0
- data/spec/dummy_app/app/views/welcome/index.html.haml +38 -0
- data/spec/dummy_app/app/views/welcome/profile.html.haml +2 -0
- data/spec/dummy_app/bin/bundle +3 -0
- data/spec/dummy_app/bin/rails +4 -0
- data/spec/dummy_app/bin/rake +4 -0
- data/spec/dummy_app/config/application.rb +23 -0
- data/spec/dummy_app/config/boot.rb +4 -0
- data/spec/dummy_app/config/database.yml +17 -0
- data/spec/dummy_app/config/environment.rb +5 -0
- data/spec/dummy_app/config/environments/development.rb +29 -0
- data/spec/dummy_app/config/environments/production.rb +80 -0
- data/spec/dummy_app/config/environments/test.rb +36 -0
- data/spec/dummy_app/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy_app/config/initializers/devise.rb +227 -0
- data/spec/dummy_app/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy_app/config/initializers/inflections.rb +16 -0
- data/spec/dummy_app/config/initializers/mime_types.rb +5 -0
- data/spec/dummy_app/config/initializers/secret_token.rb +12 -0
- data/spec/dummy_app/config/initializers/session_store.rb +3 -0
- data/spec/dummy_app/config/initializers/the_role.rb +6 -0
- data/spec/dummy_app/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy_app/config/locales/devise.en.yml +57 -0
- data/spec/dummy_app/config/locales/en.yml +7 -0
- data/spec/dummy_app/config/locales/ru.yml +4 -0
- data/spec/dummy_app/config/routes.rb +20 -0
- data/spec/dummy_app/config.ru +4 -0
- data/spec/dummy_app/db/migrate/20120212061952_devise_create_users.rb +63 -0
- data/spec/dummy_app/db/migrate/20120212063249_create_roles.rb +18 -0
- data/spec/dummy_app/db/migrate/20120314061307_create_pages.rb +14 -0
- data/spec/dummy_app/db/schema.rb +59 -0
- data/spec/dummy_app/db/seeds.rb +85 -0
- data/spec/dummy_app/lib/assets/.keep +0 -0
- data/spec/dummy_app/lib/tasks/.keep +0 -0
- data/spec/dummy_app/lib/tasks/assets.rake +15 -0
- data/spec/dummy_app/lib/tasks/db_bootstrap.rake +16 -0
- data/spec/dummy_app/log/.keep +0 -0
- data/spec/dummy_app/public/404.html +58 -0
- data/spec/dummy_app/public/422.html +58 -0
- data/spec/dummy_app/public/500.html +57 -0
- data/spec/dummy_app/public/favicon.ico +0 -0
- data/spec/dummy_app/public/robots.txt +5 -0
- data/spec/dummy_app/spec/controllers/admin_roles_controller_spec.rb +52 -0
- data/spec/dummy_app/spec/controllers/pages_controller_spec.rb +141 -0
- data/spec/dummy_app/spec/controllers/welcome_controller_spec.rb +66 -0
- data/spec/dummy_app/spec/factories/page.rb +6 -0
- data/spec/dummy_app/spec/factories/role.rb +55 -0
- data/spec/dummy_app/spec/factories/user.rb +11 -0
- data/spec/dummy_app/spec/models/hash_spec.rb +272 -0
- data/spec/dummy_app/spec/models/param_process_spec.rb +44 -0
- data/spec/dummy_app/spec/models/role_spec.rb +218 -0
- data/spec/dummy_app/spec/models/user_spec.rb +164 -0
- data/spec/dummy_app/spec/routing/admin_roles_routing_spec.rb +41 -0
- data/spec/dummy_app/spec/routing/pages_routing_spec.rb +35 -0
- data/spec/dummy_app/spec/spec_helper.rb +68 -0
- data/spec/dummy_app/vendor/assets/javascripts/.keep +0 -0
- data/spec/dummy_app/vendor/assets/stylesheets/.keep +0 -0
- data/the_role.gemspec +2 -1
- data/the_role.yml.teamocil.example +11 -0
- metadata +213 -16
- data/app/assets/javascripts/the_role.js.coffee +0 -31
- data/app/assets/stylesheets/the_role/bootstrap_sass.css.scss +0 -20
- data/app/assets/stylesheets/the_role.css.scss +0 -47
- data/app/controllers/the_role_controller.rb +0 -18
- data/app/models/concerns/role_model.rb +0 -125
- data/app/models/concerns/the_role_base.rb +0 -35
- data/app/models/concerns/the_role_user_model.rb +0 -54
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 77b00593bcd32d2fee50327a0c3fab22fbadf482
|
4
|
+
data.tar.gz: 269eb319746ed8f82864259c20ba46439ea59390
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 88afca09c44fe32449d4d8ca1d7bdfa60d9ad13f2d02fdf43c3fbb29931ec99377c9e6e4bf0c632c739f40f0858ec58d55eb59bffde8cbcb1e114cba33a6f518
|
7
|
+
data.tar.gz: cf2b3f534ad0d7a949590a563d722e9048161a91560c3db73940b08cd5db4ba72691055fab7c86d1c596ed408fa99a22cf8a5c0389a53cb3a6905c0d796e2b0b
|
data/.gitignore
CHANGED
@@ -1,6 +1,30 @@
|
|
1
1
|
*.gem
|
2
|
+
*.rbc
|
3
|
+
|
2
4
|
.bundle
|
3
|
-
|
4
|
-
|
5
|
+
.config
|
6
|
+
.yardoc
|
7
|
+
|
5
8
|
.rvmrc
|
6
|
-
|
9
|
+
.ruby-gemset
|
10
|
+
.ruby-version
|
11
|
+
|
12
|
+
_yardoc
|
13
|
+
coverage
|
14
|
+
Gemfile.lock
|
15
|
+
InstalledFiles
|
16
|
+
lib/bundler/man
|
17
|
+
spec/dummy_app/public/assets
|
18
|
+
|
19
|
+
tmp
|
20
|
+
doc
|
21
|
+
pkg
|
22
|
+
rdoc
|
23
|
+
|
24
|
+
test/tmp
|
25
|
+
spec/reports
|
26
|
+
test/version_tmp
|
27
|
+
|
28
|
+
.DS_Store
|
29
|
+
.LSOverride
|
30
|
+
.AppleDouble
|
@@ -0,0 +1 @@
|
|
1
|
+
the_role
|
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.0.0-p353
|
data/.rvmrc.example
CHANGED
@@ -1 +1 @@
|
|
1
|
-
rvm use ruby-
|
1
|
+
rvm use ruby-2.0.0-p353@the_role --create
|
data/.travis.yml
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
language: ruby
|
2
2
|
rvm:
|
3
3
|
- 1.9.3
|
4
|
-
gemfile: spec/
|
5
|
-
script: "cd spec/
|
4
|
+
gemfile: spec/dummy_app/Gemfile
|
5
|
+
script: "cd spec/dummy_app && rake db:bootstrap RAILS_ENV=test && rspec --format documentation"
|
data/README.md
CHANGED
@@ -41,7 +41,9 @@ puts following yields into your layout:
|
|
41
41
|
gem 'the_role', '~> 2.0.0'
|
42
42
|
```
|
43
43
|
|
44
|
-
|
44
|
+
## If you have any questions
|
45
|
+
|
46
|
+
Please before ask anything try to launch and play with **[Dummy App](spec/dummy_app)** in spec folder. Maybe example of integration will be better than any documentation. Thank you!
|
45
47
|
|
46
48
|
### Instalation
|
47
49
|
|
@@ -68,10 +70,8 @@ gem 'the_role', '~> 2.0.0'
|
|
68
70
|
## Install
|
69
71
|
|
70
72
|
```ruby
|
71
|
-
#
|
72
|
-
|
73
|
-
# You can find required components below
|
74
|
-
gem 'bootstrap-sass', '~> 2.3.1.0'
|
73
|
+
# You can use any Bootstrap 3 version (CSS, LESS, SCSS)
|
74
|
+
gem 'bootstrap-sass', github: 'thomas-mcdonald/bootstrap-sass'
|
75
75
|
|
76
76
|
gem "the_role", "~> 2.0.0"
|
77
77
|
```
|
@@ -110,7 +110,9 @@ end
|
|
110
110
|
|
111
111
|
```ruby
|
112
112
|
class User < ActiveRecord::Base
|
113
|
-
include
|
113
|
+
include TheRole::User
|
114
|
+
# or following alias for AR:
|
115
|
+
# has_role
|
114
116
|
|
115
117
|
# has_many :pages
|
116
118
|
end
|
@@ -124,6 +126,16 @@ Generate Role model
|
|
124
126
|
bundle exec rails g the_role install
|
125
127
|
```
|
126
128
|
|
129
|
+
or you can create Role model manually:
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
class Role < ActiveRecord::Base
|
133
|
+
include TheRole::Role
|
134
|
+
# or following alias for AR:
|
135
|
+
# acts_as_role
|
136
|
+
end
|
137
|
+
```
|
138
|
+
|
127
139
|
install TheRole migrations
|
128
140
|
|
129
141
|
```ruby
|
@@ -158,22 +170,35 @@ User.first.update( role: Role.with_name(:admin) )
|
|
158
170
|
|
159
171
|
```ruby
|
160
172
|
class ApplicationController < ActionController::Base
|
161
|
-
include
|
173
|
+
include TheRole::Controller
|
162
174
|
|
163
175
|
protect_from_forgery
|
164
176
|
|
165
|
-
# your Access Denied processor
|
166
177
|
def access_denied
|
167
|
-
|
178
|
+
flash[:error] = t('the_role.access_denied')
|
179
|
+
redirect_to(:back)
|
168
180
|
end
|
181
|
+
end
|
182
|
+
```
|
183
|
+
### Configuration
|
184
|
+
|
185
|
+
create the_role config:
|
186
|
+
|
187
|
+
```
|
188
|
+
bundle exec rails g the_role setup
|
189
|
+
```
|
169
190
|
|
170
|
-
|
171
|
-
# 2) LOGIN_REQUIRE => require_login for Sorcery
|
172
|
-
# 3) LOGIN_REQUIRE => user_require_method for other Authentication solution
|
191
|
+
**config/initializers/the_role.rb**
|
173
192
|
|
174
|
-
|
175
|
-
|
176
|
-
|
193
|
+
```ruby
|
194
|
+
TheRole.configure do |config|
|
195
|
+
config.layout = :application
|
196
|
+
config.default_user_role = :user
|
197
|
+
config.access_denied_method = :access_denied # define it in ApplicationController
|
198
|
+
config.login_required_method = :authenticate_user! # devise auth method
|
199
|
+
|
200
|
+
# config.first_user_should_be_admin = false
|
201
|
+
# config.destroy_strategy = :restrict_with_exception # can be nil
|
177
202
|
end
|
178
203
|
```
|
179
204
|
|
@@ -209,43 +234,17 @@ end
|
|
209
234
|
**application.css**
|
210
235
|
|
211
236
|
```
|
212
|
-
//= require
|
213
|
-
//= require the_role
|
237
|
+
//= require bootstrap
|
214
238
|
```
|
215
239
|
|
216
|
-
|
217
|
-
|
218
|
-
```
|
219
|
-
bootstrap/variables
|
220
|
-
bootstrap/mixins
|
221
|
-
bootstrap/reset
|
222
|
-
|
223
|
-
bootstrap/scaffolding
|
224
|
-
bootstrap/grid
|
225
|
-
bootstrap/layouts
|
226
|
-
|
227
|
-
bootstrap/navs
|
228
|
-
bootstrap/wells
|
229
|
-
bootstrap/forms
|
230
|
-
bootstrap/close
|
231
|
-
bootstrap/tables
|
232
|
-
bootstrap/navbar
|
233
|
-
bootstrap/dropdowns
|
240
|
+
**application.js**
|
234
241
|
|
235
|
-
bootstrap/alerts
|
236
|
-
bootstrap/buttons
|
237
|
-
bootstrap/button-groups
|
238
242
|
```
|
243
|
+
//= require jquery
|
244
|
+
//= require jquery_ujs
|
239
245
|
|
240
|
-
|
241
|
-
|
242
|
-
config/initializers/the_role.rb
|
243
|
-
|
244
|
-
```ruby
|
245
|
-
TheRole.configure do |config|
|
246
|
-
config.layout = :application # default Layout for TheRole UI
|
247
|
-
config.default_user_role = nil # set default role (name)
|
248
|
-
end
|
246
|
+
//= require bootstrap
|
247
|
+
//= require the_role_editinplace
|
249
248
|
```
|
250
249
|
|
251
250
|
## Understanding
|
@@ -0,0 +1,32 @@
|
|
1
|
+
showForm = (item) ->
|
2
|
+
holder = item.parents('.holder')
|
3
|
+
a_item = holder.children('span.a')
|
4
|
+
b_item = holder.children('span.b')
|
5
|
+
|
6
|
+
a_item.hide().off 'click'
|
7
|
+
b_item.css('visibility', 'visible')
|
8
|
+
|
9
|
+
holder.find('.btn-warning').click ->
|
10
|
+
item = $ @
|
11
|
+
hideForm item
|
12
|
+
item.parents('form')[0].reset()
|
13
|
+
|
14
|
+
holder.find('.btn-success').click ->
|
15
|
+
$(@).parents('form')[0].submit()
|
16
|
+
|
17
|
+
b_item.find('input').keypress (event) ->
|
18
|
+
ENTER = 13
|
19
|
+
form = $(event.target).parents('form')
|
20
|
+
form.submit() if event.which is ENTER
|
21
|
+
|
22
|
+
hideForm = (item) ->
|
23
|
+
holder = item.parents('.holder')
|
24
|
+
a_item = holder.children('span.a')
|
25
|
+
b_item = holder.children('span.b')
|
26
|
+
|
27
|
+
a_item.show()
|
28
|
+
b_item.css('visibility', 'hidden')
|
29
|
+
holder.find('.btn').off('click')
|
30
|
+
a_item.click -> showForm item
|
31
|
+
|
32
|
+
$ -> $('span.a', 'h3, h5').click -> showForm $(@)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module TheRole
|
2
|
+
module Controller
|
3
|
+
def login_required
|
4
|
+
send TheRole.config.login_required_method
|
5
|
+
end
|
6
|
+
|
7
|
+
def role_access_denied
|
8
|
+
send TheRole.config.access_denied_method
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def role_required
|
14
|
+
role_access_denied unless current_user.has_role?(controller_path, action_name)
|
15
|
+
end
|
16
|
+
|
17
|
+
def owner_required
|
18
|
+
# TheRole: You should define OWNER CHECK OBJECT
|
19
|
+
# When editable object was found
|
20
|
+
role_access_denied unless current_user.owner?(@owner_check_object)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module TheRole
|
2
|
+
module Base
|
3
|
+
def has_section? section_name
|
4
|
+
hash = role_hash
|
5
|
+
section_name = TheRoleParam.process section_name
|
6
|
+
return true if hash[section_name]
|
7
|
+
false
|
8
|
+
end
|
9
|
+
|
10
|
+
def has_role? section_name, rule_name
|
11
|
+
hash = role_hash
|
12
|
+
section_name = TheRoleParam.process(section_name)
|
13
|
+
rule_name = TheRoleParam.process(rule_name)
|
14
|
+
|
15
|
+
return true if hash.try(:[], 'system').try(:[], 'administrator')
|
16
|
+
return true if hash.try(:[], 'moderator').try(:[], section_name)
|
17
|
+
|
18
|
+
return false unless hash[section_name]
|
19
|
+
return false unless hash[section_name].key? rule_name
|
20
|
+
hash[section_name][rule_name]
|
21
|
+
end
|
22
|
+
|
23
|
+
def any_role? roles_hash = {}
|
24
|
+
roles_hash.each_pair{|section, action| return true if has_role?(section, action)}
|
25
|
+
false
|
26
|
+
end
|
27
|
+
|
28
|
+
def moderator? section_name
|
29
|
+
section_name = TheRoleParam.process(section_name)
|
30
|
+
has_role? section_name, 'any_crazy_name'
|
31
|
+
end
|
32
|
+
|
33
|
+
def admin?
|
34
|
+
has_role? 'any_crazy_name', 'any_crazy_name'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,134 @@
|
|
1
|
+
module TheRole
|
2
|
+
module Role
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
include TheRole::Base
|
6
|
+
|
7
|
+
def role_hash;
|
8
|
+
to_hash;
|
9
|
+
end
|
10
|
+
|
11
|
+
alias_method :has?, :has_role?
|
12
|
+
alias_method :any?, :any_role?
|
13
|
+
|
14
|
+
def has_section? section_name
|
15
|
+
to_hash.key? TheRoleParam.process(section_name)
|
16
|
+
end
|
17
|
+
|
18
|
+
included do
|
19
|
+
has_many :users, dependent: TheRole.config.destroy_strategy
|
20
|
+
validates :name, presence: true, uniqueness: true
|
21
|
+
validates :title, presence: true, uniqueness: true
|
22
|
+
validates :description, presence: true
|
23
|
+
|
24
|
+
before_save do
|
25
|
+
self.name = TheRoleParam.process(name)
|
26
|
+
|
27
|
+
rules_set = self.the_role
|
28
|
+
self.the_role = {}.to_json if rules_set.blank? # blank
|
29
|
+
self.the_role = rules_set.to_json if rules_set.is_a?(Hash) # Hash
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
module ClassMethods
|
34
|
+
def with_name name
|
35
|
+
::Role.where(name: name).first
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# C
|
40
|
+
|
41
|
+
def create_section section_name = nil
|
42
|
+
return false unless section_name
|
43
|
+
role = to_hash
|
44
|
+
section_name = TheRoleParam.process(section_name)
|
45
|
+
return false if section_name.blank?
|
46
|
+
return true if role[section_name]
|
47
|
+
role[section_name] = {}
|
48
|
+
update(the_role: role)
|
49
|
+
end
|
50
|
+
|
51
|
+
def create_rule section_name, rule_name
|
52
|
+
return false if rule_name.blank?
|
53
|
+
return false unless create_section(section_name)
|
54
|
+
role = to_hash
|
55
|
+
rule_name = TheRoleParam.process(rule_name)
|
56
|
+
section_name = TheRoleParam.process(section_name)
|
57
|
+
return true if role[section_name][rule_name]
|
58
|
+
role[section_name][rule_name] = false
|
59
|
+
update(the_role: role)
|
60
|
+
end
|
61
|
+
|
62
|
+
# R
|
63
|
+
|
64
|
+
def to_hash
|
65
|
+
#return the hash if activerecord parsed the json from postgresql
|
66
|
+
return the_role if the_role.kind_of? Hash
|
67
|
+
begin
|
68
|
+
JSON.load(the_role) rescue {}
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def to_json
|
73
|
+
the_role
|
74
|
+
end
|
75
|
+
|
76
|
+
# U
|
77
|
+
|
78
|
+
# source_hash will be reset to false
|
79
|
+
# except true items from new_role_hash
|
80
|
+
# all keys will become 'strings'
|
81
|
+
# look at lib/the_role/hash.rb to find definition of *underscorify_keys* method
|
82
|
+
def update_role new_role_hash
|
83
|
+
new_role_hash = new_role_hash.try(:to_hash) || {}
|
84
|
+
new_role = new_role_hash.underscorify_keys
|
85
|
+
role = to_hash.underscorify_keys.deep_reset(false)
|
86
|
+
role.deep_merge! new_role
|
87
|
+
update(the_role: role)
|
88
|
+
end
|
89
|
+
|
90
|
+
def rule_on section_name, rule_name
|
91
|
+
role = to_hash
|
92
|
+
rule_name = TheRoleParam.process(rule_name)
|
93
|
+
section_name = TheRoleParam.process(section_name)
|
94
|
+
return false unless role[section_name]
|
95
|
+
return false unless role[section_name].key? rule_name
|
96
|
+
return true if role[section_name][rule_name]
|
97
|
+
role[section_name][rule_name] = true
|
98
|
+
update(the_role: role)
|
99
|
+
end
|
100
|
+
|
101
|
+
def rule_off section_name, rule_name
|
102
|
+
role = to_hash
|
103
|
+
rule_name = TheRoleParam.process(rule_name)
|
104
|
+
section_name = TheRoleParam.process(section_name)
|
105
|
+
return false unless role[section_name]
|
106
|
+
return false unless role[section_name].key? rule_name
|
107
|
+
return true unless role[section_name][rule_name]
|
108
|
+
role[section_name][rule_name] = false
|
109
|
+
update(the_role: role)
|
110
|
+
end
|
111
|
+
|
112
|
+
# D
|
113
|
+
|
114
|
+
def delete_section section_name = nil
|
115
|
+
return false unless section_name
|
116
|
+
role = to_hash
|
117
|
+
section_name = TheRoleParam.process(section_name)
|
118
|
+
return false if section_name.blank?
|
119
|
+
return false unless role[section_name]
|
120
|
+
role.delete section_name
|
121
|
+
update(the_role: role)
|
122
|
+
end
|
123
|
+
|
124
|
+
def delete_rule section_name, rule_name
|
125
|
+
role = to_hash
|
126
|
+
rule_name = TheRoleParam.process(rule_name)
|
127
|
+
section_name = TheRoleParam.process(section_name)
|
128
|
+
return false unless role[section_name]
|
129
|
+
return false unless role[section_name].key? rule_name
|
130
|
+
role[section_name].delete rule_name
|
131
|
+
update(the_role: role)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module TheRole
|
2
|
+
module User
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
include TheRole::Base
|
6
|
+
|
7
|
+
included do
|
8
|
+
belongs_to :role
|
9
|
+
before_validation :set_default_role, on: :create
|
10
|
+
after_save { |user| user.instance_variable_set(:@role_hash, nil) }
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
def with_role name
|
15
|
+
::Role.where(name: name).first.users
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def role_hash;
|
20
|
+
@role_hash ||= role.try(:to_hash) || {}
|
21
|
+
end
|
22
|
+
|
23
|
+
# FALSE if object is nil
|
24
|
+
# If object is a USER - check for youself
|
25
|
+
# Check for owner field - :user_id
|
26
|
+
# Check for owner _object_ if owner field is not :user_id
|
27
|
+
def owner? obj
|
28
|
+
return false unless obj
|
29
|
+
return true if admin?
|
30
|
+
|
31
|
+
section_name = obj.class.to_s.tableize
|
32
|
+
return true if moderator?(section_name)
|
33
|
+
|
34
|
+
# obj is User, simple way to define user_id
|
35
|
+
return id == obj.id if obj.is_a?(self.class)
|
36
|
+
|
37
|
+
# few ways to define user_id
|
38
|
+
return id == obj.user_id if obj.respond_to? :user_id
|
39
|
+
return id == obj[:user_id] if obj[:user_id]
|
40
|
+
return id == obj[:user][:id] if obj[:user]
|
41
|
+
false
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def set_default_role
|
47
|
+
unless role
|
48
|
+
default_role = ::Role.find_by_name(TheRole.config.default_user_role)
|
49
|
+
self.role = default_role if default_role
|
50
|
+
end
|
51
|
+
|
52
|
+
if self.class.count.zero? && TheRole.config.first_user_should_be_admin
|
53
|
+
self.role = TheRole.create_admin_role!
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|