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
@@ -1,125 +0,0 @@
|
|
1
|
-
module RoleModel
|
2
|
-
extend ActiveSupport::Concern
|
3
|
-
|
4
|
-
include TheRoleBase
|
5
|
-
|
6
|
-
def role_hash; to_hash; end
|
7
|
-
alias_method :has?, :has_role?
|
8
|
-
alias_method :any?, :any_role?
|
9
|
-
|
10
|
-
def has_section? section_name
|
11
|
-
to_hash.key? TheRoleParam.process(section_name)
|
12
|
-
end
|
13
|
-
|
14
|
-
included do
|
15
|
-
has_many :users
|
16
|
-
validates :name, presence: true, uniqueness: true
|
17
|
-
validates :title, presence: true, uniqueness: true
|
18
|
-
validates :description, presence: true
|
19
|
-
|
20
|
-
before_save do
|
21
|
-
self.name = TheRoleParam.process(name)
|
22
|
-
|
23
|
-
rules_set = self.the_role
|
24
|
-
self.the_role = {}.to_json if rules_set.blank? # blank
|
25
|
-
self.the_role = rules_set.to_json if rules_set.is_a?(Hash) # Hash
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
module ClassMethods
|
30
|
-
def with_name name
|
31
|
-
where(name: name).first
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
# C
|
36
|
-
|
37
|
-
def create_section section_name = nil
|
38
|
-
return false unless section_name
|
39
|
-
role = to_hash
|
40
|
-
section_name = TheRoleParam.process(section_name)
|
41
|
-
return false if section_name.blank?
|
42
|
-
return true if role[section_name]
|
43
|
-
role[section_name] = {}
|
44
|
-
update(the_role: role)
|
45
|
-
end
|
46
|
-
|
47
|
-
def create_rule section_name, rule_name
|
48
|
-
return false if rule_name.blank?
|
49
|
-
return false unless create_section(section_name)
|
50
|
-
role = to_hash
|
51
|
-
rule_name = TheRoleParam.process(rule_name)
|
52
|
-
section_name = TheRoleParam.process(section_name)
|
53
|
-
return true if role[section_name][rule_name]
|
54
|
-
role[section_name][rule_name] = false
|
55
|
-
update(the_role: role)
|
56
|
-
end
|
57
|
-
|
58
|
-
# R
|
59
|
-
|
60
|
-
def to_hash
|
61
|
-
begin JSON.load(the_role) rescue {} end
|
62
|
-
end
|
63
|
-
|
64
|
-
def to_json
|
65
|
-
the_role
|
66
|
-
end
|
67
|
-
|
68
|
-
# U
|
69
|
-
|
70
|
-
# source_hash will be reset to false
|
71
|
-
# except true items from new_role_hash
|
72
|
-
# all keys will become 'strings'
|
73
|
-
# look at lib/the_role/hash.rb to find definition of *underscorify_keys* method
|
74
|
-
def update_role new_role_hash
|
75
|
-
new_role_hash = new_role_hash.try(:to_hash) || {}
|
76
|
-
new_role = new_role_hash.underscorify_keys
|
77
|
-
role = to_hash.underscorify_keys.deep_reset(false)
|
78
|
-
role.deep_merge! new_role
|
79
|
-
update(the_role: role)
|
80
|
-
end
|
81
|
-
|
82
|
-
def rule_on section_name, rule_name
|
83
|
-
role = to_hash
|
84
|
-
rule_name = TheRoleParam.process(rule_name)
|
85
|
-
section_name = TheRoleParam.process(section_name)
|
86
|
-
return false unless role[section_name]
|
87
|
-
return false unless role[section_name].key? rule_name
|
88
|
-
return true if role[section_name][rule_name]
|
89
|
-
role[section_name][rule_name] = true
|
90
|
-
update(the_role: role)
|
91
|
-
end
|
92
|
-
|
93
|
-
def rule_off section_name, rule_name
|
94
|
-
role = to_hash
|
95
|
-
rule_name = TheRoleParam.process(rule_name)
|
96
|
-
section_name = TheRoleParam.process(section_name)
|
97
|
-
return false unless role[section_name]
|
98
|
-
return false unless role[section_name].key? rule_name
|
99
|
-
return true unless role[section_name][rule_name]
|
100
|
-
role[section_name][rule_name] = false
|
101
|
-
update(the_role: role)
|
102
|
-
end
|
103
|
-
|
104
|
-
# D
|
105
|
-
|
106
|
-
def delete_section section_name = nil
|
107
|
-
return false unless section_name
|
108
|
-
role = to_hash
|
109
|
-
section_name = TheRoleParam.process(section_name)
|
110
|
-
return false if section_name.blank?
|
111
|
-
return false unless role[section_name]
|
112
|
-
role.delete section_name
|
113
|
-
update(the_role: role)
|
114
|
-
end
|
115
|
-
|
116
|
-
def delete_rule section_name, rule_name
|
117
|
-
role = to_hash
|
118
|
-
rule_name = TheRoleParam.process(rule_name)
|
119
|
-
section_name = TheRoleParam.process(section_name)
|
120
|
-
return false unless role[section_name]
|
121
|
-
return false unless role[section_name].key? rule_name
|
122
|
-
role[section_name].delete rule_name
|
123
|
-
update(the_role: role)
|
124
|
-
end
|
125
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
module TheRoleBase
|
2
|
-
def has_section? section_name
|
3
|
-
hash = role_hash
|
4
|
-
section_name = TheRoleParam.process section_name
|
5
|
-
return true if hash[section_name]
|
6
|
-
false
|
7
|
-
end
|
8
|
-
|
9
|
-
def has_role? section_name, rule_name
|
10
|
-
hash = role_hash
|
11
|
-
section_name = TheRoleParam.process(section_name)
|
12
|
-
rule_name = TheRoleParam.process(rule_name)
|
13
|
-
|
14
|
-
return true if hash.try(:[], 'system').try(:[], 'administrator')
|
15
|
-
return true if hash.try(:[], 'moderator').try(:[], section_name)
|
16
|
-
|
17
|
-
return false unless hash[section_name]
|
18
|
-
return false unless hash[section_name].key? rule_name
|
19
|
-
hash[section_name][rule_name]
|
20
|
-
end
|
21
|
-
|
22
|
-
def any_role? roles_hash = {}
|
23
|
-
roles_hash.each_pair{|section, action| return true if has_role?(section, action)}
|
24
|
-
false
|
25
|
-
end
|
26
|
-
|
27
|
-
def moderator? section_name
|
28
|
-
section_name = TheRoleParam.process(section_name)
|
29
|
-
has_role? section_name, 'any_crazy_name'
|
30
|
-
end
|
31
|
-
|
32
|
-
def admin?
|
33
|
-
has_role? 'any_crazy_name', 'any_crazy_name'
|
34
|
-
end
|
35
|
-
end
|
@@ -1,54 +0,0 @@
|
|
1
|
-
module TheRoleUserModel
|
2
|
-
extend ActiveSupport::Concern
|
3
|
-
|
4
|
-
include TheRoleBase
|
5
|
-
|
6
|
-
included do
|
7
|
-
belongs_to :role
|
8
|
-
before_validation :set_default_role, on: :create
|
9
|
-
after_save { |user| user.instance_variable_set(:@role_hash, nil) }
|
10
|
-
end
|
11
|
-
|
12
|
-
module ClassMethods
|
13
|
-
def with_role name
|
14
|
-
Role.where(name: name).first.users
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def role_hash; @role_hash ||= role.try(:to_hash) || {} end
|
19
|
-
|
20
|
-
# FALSE if object is nil
|
21
|
-
# If object is a USER - check for youself
|
22
|
-
# Check for owner field - :user_id
|
23
|
-
# Check for owner _object_ if owner field is not :user_id
|
24
|
-
def owner? obj
|
25
|
-
return false unless obj
|
26
|
-
return true if admin?
|
27
|
-
|
28
|
-
section_name = obj.class.to_s.tableize
|
29
|
-
return true if moderator?(section_name)
|
30
|
-
|
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
|
36
|
-
return id == obj[:user_id] if obj[:user_id]
|
37
|
-
return id == obj[:user][:id] if obj[:user]
|
38
|
-
|
39
|
-
false
|
40
|
-
end
|
41
|
-
|
42
|
-
private
|
43
|
-
|
44
|
-
def set_default_role
|
45
|
-
unless role
|
46
|
-
default_role = Role.where(name: TheRole.config.default_user_role).first
|
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
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|