the_role 2.1.0 → 2.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +10 -1
- data/app/assets/javascripts/.keep +0 -0
- data/app/assets/javascripts/the_role.js.coffee +31 -0
- data/app/controllers/admin/roles_controller.rb +11 -2
- data/app/models/concerns/the_role_user_model.rb +11 -2
- data/app/views/admin/roles/_role.html.haml +31 -9
- data/config/locales/en.yml +8 -8
- data/config/locales/pl.yml +42 -0
- data/config/locales/ru.yml +1 -1
- data/config/routes.rb +1 -0
- data/lib/tasks/roles.rake +2 -23
- data/lib/the_role/config.rb +6 -3
- data/lib/the_role/version.rb +1 -1
- data/lib/the_role.rb +13 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 27c23d0e82c72b3aa2f3f2c846d62424f50bb349
|
4
|
+
data.tar.gz: e7f1057d85c4ec5b0a8d60a227d7504810eedde9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fab50cb0f7259c5a0902f1b2647078e978171eacb42e33a4bde402cf6bb86a5f4fcd814a2d3e27a975bbf3af0ea70278b9b4ebafd76832e1856d359fe21b70f7
|
7
|
+
data.tar.gz: f69efe7d0fe26e0b7d6a5423932e8c9f316f416b3fc0b9c6651da6990e80025cb9af1ea1cecfc0461ab905f8666a6b301fcd6f0c7cd2b37d2d491218765a2717
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -52,7 +52,7 @@ gem 'the_role', '~> 2.0.0'
|
|
52
52
|
|
53
53
|
### Understanding
|
54
54
|
|
55
|
-
* [TheRole instead CanCan?](#therole-instead-cancan)
|
55
|
+
* [TheRole instead of CanCan?](#therole-instead-of-cancan)
|
56
56
|
* [What does it mean semantic?](#what-does-it-mean-semantic)
|
57
57
|
* [Virtual sections and rules](#virtual-sections-and-rules)
|
58
58
|
* [Using with Views](#using-with-views)
|
@@ -385,6 +385,9 @@ Has user got an access to **rule** of **section** (action of controller)?
|
|
385
385
|
@user.has_role?(:pages, :show) => true | false
|
386
386
|
@user.has_role?(:blogs, :new) => true | false
|
387
387
|
@user.has_role?(:articles, :edit) => true | false
|
388
|
+
|
389
|
+
# return true if one of roles is true
|
390
|
+
@user.any_role?(pages: :show, posts: :show) => true | false
|
388
391
|
```
|
389
392
|
|
390
393
|
Is user **Owner** of object?
|
@@ -406,6 +409,9 @@ Is user **Owner** of object?
|
|
406
409
|
@role.has?(:pages, :show) => true | false
|
407
410
|
@role.moderator?(:pages) => true | false
|
408
411
|
@role.admin? => true | false
|
412
|
+
|
413
|
+
# return true if one of roles is true
|
414
|
+
@role.any?(pages: :show, posts: :show) => true | false
|
409
415
|
```
|
410
416
|
|
411
417
|
#### CREATE
|
@@ -470,6 +476,7 @@ new_role_hash = {
|
|
470
476
|
|
471
477
|
#### Changelog
|
472
478
|
|
479
|
+
* 2.1.0 - User#any_role? & Role#any?
|
473
480
|
* 2.0.3 - create role fix, cleanup
|
474
481
|
* 2.0.2 - code cleanup, readme
|
475
482
|
* 2.0.1 - code cleanup
|
@@ -492,6 +499,8 @@ new_role_hash = {
|
|
492
499
|
|
493
500
|
**zh_CN** by @doabit & @linjunpop
|
494
501
|
|
502
|
+
**PL** by @egb3
|
503
|
+
|
495
504
|
### MIT-LICENSE
|
496
505
|
|
497
506
|
##### Copyright (c) 2012 [Ilya N.Zykin]
|
File without changes
|
@@ -0,0 +1,31 @@
|
|
1
|
+
showForm = (event) ->
|
2
|
+
item = $ event
|
3
|
+
|
4
|
+
a_item = item.children('span.a')
|
5
|
+
b_item = item.children('span.b')
|
6
|
+
|
7
|
+
a_item.hide().off 'click'
|
8
|
+
b_item.css('visibility', 'visible')
|
9
|
+
|
10
|
+
item.find('.btn-warning').click ->
|
11
|
+
hideForm(event)
|
12
|
+
event.parentNode.reset()
|
13
|
+
|
14
|
+
item.find('.btn-success').click -> event.parentNode.submit()
|
15
|
+
|
16
|
+
b_item.find('input').keypress (e) ->
|
17
|
+
ENTER = 13
|
18
|
+
event.parentNode.submit() if e.which is ENTER
|
19
|
+
|
20
|
+
hideForm = (event) ->
|
21
|
+
item = $ event
|
22
|
+
a_item = item.children('span.a')
|
23
|
+
b_item = item.children('span.b')
|
24
|
+
|
25
|
+
a_item.show()
|
26
|
+
b_item.css('visibility', 'hidden')
|
27
|
+
item.find('.btn').off('click')
|
28
|
+
a_item.click -> showForm(event)
|
29
|
+
|
30
|
+
$ ->
|
31
|
+
$('span.a', '.title, .name, .description').click -> showForm @parentNode
|
@@ -5,8 +5,8 @@ class Admin::RolesController < ApplicationController
|
|
5
5
|
before_filter :login_required
|
6
6
|
before_filter :role_required
|
7
7
|
|
8
|
-
before_filter :role_find, only: [:edit, :update, :destroy]
|
9
|
-
before_filter :owner_required, only: [:edit, :update, :destroy]
|
8
|
+
before_filter :role_find, only: [:edit, :update, :destroy, :change]
|
9
|
+
before_filter :owner_required, only: [:edit, :update, :destroy, :change]
|
10
10
|
|
11
11
|
def index
|
12
12
|
@roles = Role.all.order('created_at ASC')
|
@@ -38,6 +38,15 @@ class Admin::RolesController < ApplicationController
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
+
def change
|
42
|
+
if @role.update_attributes!(role_params)
|
43
|
+
flash[:notice] = t 'the_role.role_updated'
|
44
|
+
redirect_to_edit
|
45
|
+
else
|
46
|
+
render :action => :edit
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
41
50
|
def destroy
|
42
51
|
@role.destroy
|
43
52
|
flash[:notice] = t 'the_role.role_deleted'
|
@@ -28,9 +28,14 @@ module TheRoleUserModel
|
|
28
28
|
section_name = obj.class.to_s.tableize
|
29
29
|
return true if moderator?(section_name)
|
30
30
|
|
31
|
-
|
31
|
+
# obj is User, simple way to define user_id
|
32
|
+
return id == obj.id if obj.is_a?(User)
|
33
|
+
|
34
|
+
# few ways to define user_id
|
35
|
+
return id == obj.user_id if obj.respond_to? :user_id
|
32
36
|
return id == obj[:user_id] if obj[:user_id]
|
33
37
|
return id == obj[:user][:id] if obj[:user]
|
38
|
+
|
34
39
|
false
|
35
40
|
end
|
36
41
|
|
@@ -39,7 +44,11 @@ module TheRoleUserModel
|
|
39
44
|
def set_default_role
|
40
45
|
unless role
|
41
46
|
default_role = Role.where(name: TheRole.config.default_user_role).first
|
42
|
-
self.role
|
47
|
+
self.role = default_role if default_role
|
48
|
+
end
|
49
|
+
|
50
|
+
if User.count.zero? && TheRole.config.first_user_should_be_admin
|
51
|
+
self.role = TheRole.create_admin
|
43
52
|
end
|
44
53
|
end
|
45
54
|
end
|
@@ -1,16 +1,38 @@
|
|
1
1
|
- content_for :role_main do
|
2
2
|
.well
|
3
|
-
|
4
|
-
%
|
5
|
-
|
3
|
+
= form_for(role, :url => change_admin_role_path(role)) do |f|
|
4
|
+
%h3.name
|
5
|
+
%b= t '.role_name'
|
6
|
+
%span.a= role.name
|
7
|
+
%span.b{style: 'visibility:hidden'}
|
8
|
+
= f.text_field :name
|
9
|
+
.btn-group
|
10
|
+
%a.btn.btn-warning
|
11
|
+
%i.icon-repeat
|
12
|
+
%a.btn.btn-success
|
13
|
+
%i.icon-ok
|
6
14
|
|
7
|
-
|
8
|
-
|
9
|
-
|
15
|
+
%h5.title
|
16
|
+
%b= t '.title'
|
17
|
+
%span.a= role.title
|
18
|
+
%span.b{style: 'visibility:hidden'}
|
19
|
+
= f.text_field :title
|
20
|
+
.btn-group
|
21
|
+
%a.btn.btn-warning
|
22
|
+
%i.icon-repeat
|
23
|
+
%a.btn.btn-success
|
24
|
+
%i.icon-ok
|
10
25
|
|
11
|
-
|
12
|
-
|
13
|
-
|
26
|
+
%h5.description
|
27
|
+
%b= t '.role_description'
|
28
|
+
%span.a= role.description
|
29
|
+
%span.b{style: 'visibility:hidden'}
|
30
|
+
= f.text_field :description
|
31
|
+
.btn-group
|
32
|
+
%a.btn.btn-warning
|
33
|
+
%i.icon-repeat
|
34
|
+
%a.btn.btn-success
|
35
|
+
%i.icon-ok
|
14
36
|
|
15
37
|
- role.to_hash.each_pair do |section, rules|
|
16
38
|
.section
|
data/config/locales/en.yml
CHANGED
@@ -5,15 +5,15 @@ en:
|
|
5
5
|
role_deleted: Role deleted
|
6
6
|
section_created: Section created
|
7
7
|
section_not_created: Section not created
|
8
|
-
section_rule_created: "Section
|
9
|
-
section_rule_not_created: "Section
|
10
|
-
section_rule_on: "Section
|
11
|
-
section_rule_off: "Section
|
12
|
-
state_not_changed: "Section
|
8
|
+
section_rule_created: "Section rule created"
|
9
|
+
section_rule_not_created: "Section rule not created"
|
10
|
+
section_rule_on: "Section rule is enable"
|
11
|
+
section_rule_off: "Section rule is disable"
|
12
|
+
state_not_changed: "Section rule not changed"
|
13
13
|
section_deleted: Section deleted
|
14
14
|
section_not_deleted: Section not deleted
|
15
|
-
section_rule_deleted: "Section
|
16
|
-
section_rule_not_deleted: "Section
|
15
|
+
section_rule_deleted: "Section rule deleted"
|
16
|
+
section_rule_not_deleted: "Section rule not deleted"
|
17
17
|
|
18
18
|
admin:
|
19
19
|
roles:
|
@@ -39,4 +39,4 @@ en:
|
|
39
39
|
sidebar:
|
40
40
|
roles_list: Roles list
|
41
41
|
new_role: Create new role
|
42
|
-
delete_role_confirm: 'Are you sure? It can dangerous!'
|
42
|
+
delete_role_confirm: 'Are you sure? It can be dangerous!'
|
@@ -0,0 +1,42 @@
|
|
1
|
+
pl:
|
2
|
+
the_role:
|
3
|
+
role_created: Rola dodana
|
4
|
+
role_updated: Rola zaktualizowana
|
5
|
+
role_deleted: Role skasowana
|
6
|
+
section_created: Sekcja utworzona
|
7
|
+
section_not_created: Sekcja nie utworzona
|
8
|
+
section_rule_created: "Reguła utworzona"
|
9
|
+
section_rule_not_created: "Reguła nie utworzona"
|
10
|
+
section_rule_on: "Reguła uruchomiona"
|
11
|
+
section_rule_off: "Reguła wyłączona"
|
12
|
+
state_not_changed: "Reguła nie zmieniona"
|
13
|
+
section_deleted: Sekcja skasowana
|
14
|
+
section_not_deleted: Sekcja nie skasowana
|
15
|
+
section_rule_deleted: "Reguła skasowana"
|
16
|
+
section_rule_not_deleted: "Reguła nie skasowana"
|
17
|
+
|
18
|
+
admin:
|
19
|
+
roles:
|
20
|
+
new:
|
21
|
+
create: Utwórz nową rolę
|
22
|
+
name: Nazwa roli
|
23
|
+
title: Tytuł roli (text)
|
24
|
+
description: Opis roli (text)
|
25
|
+
role:
|
26
|
+
role_name: 'Nazwa roli:'
|
27
|
+
title: 'Tytuł:'
|
28
|
+
role_description: 'Opis roli:'
|
29
|
+
delete_section: Skasuj sekcje
|
30
|
+
section_delete_confirm: 'Czy aby napewno chcesz skasować sekcję?'
|
31
|
+
rule_delete_confirm: 'Czy aby na pewno chcesz usunąć rolę?'
|
32
|
+
enable: Włącz
|
33
|
+
disable: Wyłącz
|
34
|
+
delete_rule: Skasuj regułę
|
35
|
+
new_section_placeholder: Nazwa nowej sekcji
|
36
|
+
create_section: Utwórz nową sekcję
|
37
|
+
new_rule_placeholder: Nazwa nowej reguły
|
38
|
+
create_rule: Utwórz nową regułę
|
39
|
+
sidebar:
|
40
|
+
roles_list: Lista ról
|
41
|
+
new_role: Utwórz nową rolę
|
42
|
+
delete_role_confirm: 'Jesteś pewien? To może być niebezpieczne!'
|
data/config/locales/ru.yml
CHANGED
data/config/routes.rb
CHANGED
data/lib/tasks/roles.rake
CHANGED
@@ -8,20 +8,7 @@ namespace :db do
|
|
8
8
|
puts 'TheRole'
|
9
9
|
puts '~'*40
|
10
10
|
|
11
|
-
|
12
|
-
role = Role.create(
|
13
|
-
name: :admin,
|
14
|
-
title: "Role for admin",
|
15
|
-
description:"This user can do anything"
|
16
|
-
)
|
17
|
-
|
18
|
-
role.create_rule(:system, :administrator)
|
19
|
-
role.rule_on(:system, :administrator)
|
20
|
-
|
21
|
-
puts "Admin role created"
|
22
|
-
else
|
23
|
-
puts "Admin role exists"
|
24
|
-
end
|
11
|
+
TheRole.create_admin
|
25
12
|
|
26
13
|
puts "Now you can makes any user as Admin:"
|
27
14
|
puts "> rails c"
|
@@ -33,15 +20,7 @@ namespace :db do
|
|
33
20
|
desc 'create roles'
|
34
21
|
task :test => :environment do
|
35
22
|
# ADMIN
|
36
|
-
|
37
|
-
name: :admin,
|
38
|
-
title: "Role for admin",
|
39
|
-
description:"This user can do anything"
|
40
|
-
)
|
41
|
-
|
42
|
-
role.create_rule(:system, :administrator)
|
43
|
-
role.rule_on(:system, :administrator)
|
44
|
-
|
23
|
+
Rake::Task["db:roles:admin"].invoke
|
45
24
|
puts 'Administrator'
|
46
25
|
|
47
26
|
# MODERATOR
|
data/lib/the_role/config.rb
CHANGED
@@ -10,11 +10,14 @@ module TheRole
|
|
10
10
|
# Configuration class
|
11
11
|
class Configuration
|
12
12
|
include ActiveSupport::Configurable
|
13
|
-
config_accessor :layout,
|
13
|
+
config_accessor :layout,
|
14
|
+
:default_user_role,
|
15
|
+
:first_user_should_be_admin
|
14
16
|
end
|
15
17
|
|
16
18
|
configure do |config|
|
17
|
-
config.layout
|
18
|
-
config.default_user_role
|
19
|
+
config.layout = :application
|
20
|
+
config.default_user_role = nil
|
21
|
+
config.first_user_should_be_admin = false
|
19
22
|
end
|
20
23
|
end
|
data/lib/the_role/version.rb
CHANGED
data/lib/the_role.rb
CHANGED
@@ -6,6 +6,19 @@ require 'the_role/version'
|
|
6
6
|
require 'the_role/param_helper'
|
7
7
|
|
8
8
|
module TheRole
|
9
|
+
class << self
|
10
|
+
def create_admin
|
11
|
+
admin_role = Role.where(name: :admin).first_or_create(
|
12
|
+
name: :admin,
|
13
|
+
title: "Role for admin",
|
14
|
+
description:"This user can do anything"
|
15
|
+
)
|
16
|
+
admin_role.create_rule(:system, :administrator)
|
17
|
+
admin_role.rule_on(:system, :administrator)
|
18
|
+
admin_role
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
9
22
|
class Engine < Rails::Engine
|
10
23
|
# initializer "TheRole precompile hook", group: :all do |app|
|
11
24
|
# app.config.assets.precompile += %w( x.js y.css )
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: the_role
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ilya N. Zykin [the-teacher]
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-09-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: haml
|
@@ -38,6 +38,8 @@ files:
|
|
38
38
|
- Gemfile
|
39
39
|
- README.md
|
40
40
|
- Rakefile
|
41
|
+
- app/assets/javascripts/.keep
|
42
|
+
- app/assets/javascripts/the_role.js.coffee
|
41
43
|
- app/assets/stylesheets/the_role.css.scss
|
42
44
|
- app/assets/stylesheets/the_role/bootstrap_sass.css.scss
|
43
45
|
- app/controllers/admin/role_sections_controller.rb
|
@@ -53,6 +55,7 @@ files:
|
|
53
55
|
- app/views/admin/roles/new.html.haml
|
54
56
|
- config/locales/en.yml
|
55
57
|
- config/locales/es.yml
|
58
|
+
- config/locales/pl.yml
|
56
59
|
- config/locales/ru.yml
|
57
60
|
- config/locales/zh_CN.yml
|
58
61
|
- config/routes.rb
|