the_role 1.7.0 → 2.0.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.
- data/.travis.yml +5 -0
- data/README.md +139 -114
- data/app/assets/stylesheets/the_role.css.scss +47 -0
- data/app/controllers/admin/role_sections_controller.rb +8 -6
- data/app/controllers/admin/roles_controller.rb +9 -6
- data/app/controllers/the_role_controller.rb +18 -0
- data/app/models/concerns/role_model.rb +124 -0
- data/app/models/concerns/the_role_base.rb +30 -0
- data/app/models/concerns/the_role_user_model.rb +45 -0
- data/app/views/admin/roles/_role.html.haml +54 -53
- data/app/views/admin/roles/_sidebar.html.haml +4 -1
- data/app/views/admin/roles/edit.html.haml +2 -2
- data/app/views/admin/roles/index.haml +2 -2
- data/app/views/admin/roles/new.html.haml +18 -17
- data/lib/generators/the_role/USAGE +19 -0
- data/lib/generators/the_role/templates/the_role.rb +5 -0
- data/lib/generators/the_role/the_role_generator.rb +23 -0
- data/lib/the_role/config.rb +20 -0
- data/lib/the_role/hash.rb +23 -30
- data/lib/the_role/param_helper.rb +5 -11
- data/lib/the_role/version.rb +1 -1
- data/lib/the_role.rb +15 -14
- data/the_role.gemspec +2 -2
- metadata +20 -18
- data/app/assets/javascripts/admin_the_role.js +0 -4
- data/app/assets/javascripts/bootstrap-alert.js +0 -90
- data/app/assets/javascripts/bootstrap-dropdown.js +0 -100
- data/app/assets/stylesheets/admin_the_role.css +0 -6
- data/app/assets/stylesheets/custom.css +0 -3
- data/app/assets/stylesheets/headers.css +0 -12
- data/app/assets/stylesheets/reset.css +0 -117
- data/app/assets/stylesheets/role_base.css +0 -1739
- data/lib/the_role/engine.rb +0 -16
- data/lib/the_role/modules/base.rb +0 -30
- data/lib/the_role/modules/controller_requires.rb +0 -17
- data/lib/the_role/modules/role_model.rb +0 -121
- data/lib/the_role/modules/user_model.rb +0 -31
data/lib/the_role/engine.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
require 'the_role'
|
2
|
-
require 'rails'
|
3
|
-
|
4
|
-
module TheRole
|
5
|
-
class Engine < Rails::Engine
|
6
|
-
initializer "TheRole precompile hook", :group => :all do |app|
|
7
|
-
app.config.assets.precompile += %w( admin_the_role.js admin_the_role.css )
|
8
|
-
end
|
9
|
-
|
10
|
-
config.to_prepare do
|
11
|
-
Role.send :include, TheRole::RoleModel if the_class_exists? :Role
|
12
|
-
User.send :include, TheRole::UserModel if the_class_exists? :User
|
13
|
-
ApplicationController.send :include, TheRole::Requires if the_class_exists? :ApplicationController
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
module TheRole
|
2
|
-
module Base
|
3
|
-
def has_section? section_name
|
4
|
-
hash = role_hash
|
5
|
-
section_name = param_prepare 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 = param_prepare(section_name)
|
13
|
-
rule_name = param_prepare(rule_name)
|
14
|
-
return true if hash['system'] and hash['system']['administrator']
|
15
|
-
return true if hash['moderator'] and hash['moderator'][section_name]
|
16
|
-
return false unless hash[section_name]
|
17
|
-
return false unless hash[section_name].key? rule_name
|
18
|
-
hash[section_name][rule_name]
|
19
|
-
end
|
20
|
-
|
21
|
-
def moderator? section_name
|
22
|
-
section_name = param_prepare(section_name)
|
23
|
-
has_role? section_name, 'any_crazy_name'
|
24
|
-
end
|
25
|
-
|
26
|
-
def admin?
|
27
|
-
has_role? 'any_crazy_name', 'any_crazy_name'
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
module TheRole
|
2
|
-
module Requires
|
3
|
-
private
|
4
|
-
def role_access_denied
|
5
|
-
flash[:error] = t('the_role.access_denied')
|
6
|
-
redirect_to root_path
|
7
|
-
end
|
8
|
-
|
9
|
-
def role_required
|
10
|
-
role_access_denied unless current_user.has_role?(controller_name, action_name)
|
11
|
-
end
|
12
|
-
|
13
|
-
def owner_required
|
14
|
-
role_access_denied unless current_user.owner?(@ownership_checking_object)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,121 +0,0 @@
|
|
1
|
-
module TheRole
|
2
|
-
module RoleModel
|
3
|
-
include TheRole::Base
|
4
|
-
include TheRole::ParamHelper
|
5
|
-
|
6
|
-
def role_hash; to_hash; end
|
7
|
-
alias_method :has?, :has_role?
|
8
|
-
|
9
|
-
def has_section? section_name
|
10
|
-
section_name = param_prepare(section_name)
|
11
|
-
to_hash.key? section_name
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.included(base)
|
15
|
-
base.class_eval do
|
16
|
-
attr_accessible :name, :title, :description, :the_role
|
17
|
-
|
18
|
-
has_many :users
|
19
|
-
validates :name, :presence => true, :uniqueness => true
|
20
|
-
validates :title, :presence => true, :uniqueness => true
|
21
|
-
validates :description, :presence => true
|
22
|
-
|
23
|
-
before_create do
|
24
|
-
self.name = param_prepare(name)
|
25
|
-
self.the_role = {}.to_json if self.the_role.blank?
|
26
|
-
end
|
27
|
-
|
28
|
-
# C
|
29
|
-
|
30
|
-
def create_section section_name = nil
|
31
|
-
return false unless section_name
|
32
|
-
role = to_hash
|
33
|
-
section_name = param_prepare(section_name)
|
34
|
-
return false if section_name.blank?
|
35
|
-
return true if role[section_name]
|
36
|
-
role[section_name] = {}
|
37
|
-
update_attributes(:the_role => role.to_json)
|
38
|
-
end
|
39
|
-
|
40
|
-
def create_rule section_name, rule_name
|
41
|
-
return false if rule_name.blank?
|
42
|
-
return false unless create_section(section_name)
|
43
|
-
role = to_hash
|
44
|
-
rule_name = param_prepare(rule_name)
|
45
|
-
section_name = param_prepare(section_name)
|
46
|
-
return true if role[section_name][rule_name]
|
47
|
-
role[section_name][rule_name] = false
|
48
|
-
update_attributes(:the_role => role.to_json)
|
49
|
-
end
|
50
|
-
|
51
|
-
# R
|
52
|
-
|
53
|
-
def to_hash
|
54
|
-
begin JSON.load(the_role) rescue {} end
|
55
|
-
end
|
56
|
-
|
57
|
-
def to_json
|
58
|
-
the_role
|
59
|
-
end
|
60
|
-
|
61
|
-
# U
|
62
|
-
|
63
|
-
# source_hash will be reset to false
|
64
|
-
# except true items from new_role_hash
|
65
|
-
# all keys will become 'strings'
|
66
|
-
# look at lib/the_role/hash.rb to find definition of *underscorify_keys* method
|
67
|
-
def update_role new_role_hash
|
68
|
-
new_role_hash = new_role_hash.try(:to_hash) || {}
|
69
|
-
new_role = new_role_hash.underscorify_keys
|
70
|
-
role = to_hash.underscorify_keys.deep_reset(false)
|
71
|
-
role.deep_merge! new_role
|
72
|
-
update_attributes(:the_role => role.to_json)
|
73
|
-
end
|
74
|
-
|
75
|
-
def rule_on section_name, rule_name
|
76
|
-
role = to_hash
|
77
|
-
rule_name = param_prepare(rule_name)
|
78
|
-
section_name = param_prepare(section_name)
|
79
|
-
return false unless role[section_name]
|
80
|
-
return false unless role[section_name].key? rule_name
|
81
|
-
return true if role[section_name][rule_name]
|
82
|
-
role[section_name][rule_name] = true
|
83
|
-
update_attributes(:the_role => role.to_json)
|
84
|
-
end
|
85
|
-
|
86
|
-
def rule_off section_name, rule_name
|
87
|
-
role = to_hash
|
88
|
-
rule_name = param_prepare(rule_name)
|
89
|
-
section_name = param_prepare(section_name)
|
90
|
-
return false unless role[section_name]
|
91
|
-
return false unless role[section_name].key? rule_name
|
92
|
-
return true unless role[section_name][rule_name]
|
93
|
-
role[section_name][rule_name] = false
|
94
|
-
update_attributes(:the_role => role.to_json)
|
95
|
-
end
|
96
|
-
|
97
|
-
# D
|
98
|
-
|
99
|
-
def delete_section section_name = nil
|
100
|
-
return false unless section_name
|
101
|
-
role = to_hash
|
102
|
-
section_name = param_prepare(section_name)
|
103
|
-
return false if section_name.blank?
|
104
|
-
return false unless role[section_name]
|
105
|
-
role.delete section_name
|
106
|
-
update_attributes(:the_role => role.to_json)
|
107
|
-
end
|
108
|
-
|
109
|
-
def delete_rule section_name, rule_name
|
110
|
-
role = to_hash
|
111
|
-
rule_name = param_prepare(rule_name)
|
112
|
-
section_name = param_prepare(section_name)
|
113
|
-
return false unless role[section_name]
|
114
|
-
return false unless role[section_name].key? rule_name
|
115
|
-
role[section_name].delete rule_name
|
116
|
-
update_attributes(:the_role => role.to_json)
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
120
|
-
end
|
121
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
module TheRole
|
2
|
-
module UserModel
|
3
|
-
include TheRole::Base
|
4
|
-
include TheRole::ParamHelper
|
5
|
-
def role_hash; @role_hash ||= role.to_hash; end
|
6
|
-
|
7
|
-
# FALSE if object is nil
|
8
|
-
# If object is a USER - check for youself
|
9
|
-
# Check for owner field - :user_id
|
10
|
-
# Check for owner _object_ if owner field is not :user_id
|
11
|
-
def owner? obj
|
12
|
-
return false unless obj
|
13
|
-
return true if admin?
|
14
|
-
|
15
|
-
section_name = obj.class.to_s.tableize
|
16
|
-
return true if moderator?(section_name)
|
17
|
-
|
18
|
-
return id == obj.id if obj.is_a?(User)
|
19
|
-
return id == obj[:user_id] if obj[:user_id]
|
20
|
-
return id == obj[:user][:id] if obj[:user]
|
21
|
-
false
|
22
|
-
end
|
23
|
-
|
24
|
-
def self.included(base)
|
25
|
-
base.class_eval do
|
26
|
-
belongs_to :role
|
27
|
-
after_save { |user| user.instance_variable_set(:@role_hash, nil) }
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|