monarchy 0.2.1 → 0.3.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/.rspec +0 -1
- data/Gemfile +8 -10
- data/Gemfile.lock +37 -29
- data/README.md +2 -0
- data/lib/monarchy/acts_as_hierarchy.rb +32 -7
- data/lib/monarchy/acts_as_resource.rb +15 -12
- data/lib/monarchy/acts_as_user.rb +59 -43
- data/lib/monarchy/membership.rb +24 -11
- data/lib/monarchy/version.rb +1 -1
- data/lib/monarchy.rb +2 -1
- data/monarchy.gemspec +3 -2
- metadata +22 -9
- data/.lock-ClosureTreeMonarchyHierarchy-1609118677 +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c41ca61cf044e6e4c4ae5a3a2230c6495e662005
|
4
|
+
data.tar.gz: 283d4f647c876b35712a7ff094bd5bcf4e4cfe9d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d79f2aac6d3ebb18b1a361a7ed26c0ae3156d2876de19b4b4a447862e7f05fae6bb29b9d47c8be3d5c17e7eb998350cf286b380588bcfd0ce3c8a1cbab58a471
|
7
|
+
data.tar.gz: d2f3d22b828220881e0f39b55798f6ec686d87bf44ef463b8033570eee9177dfa62f8d24c590ebc65c0a5249563e338ded43fc7733a6b5d1c17e938c56941237
|
data/.gitignore
CHANGED
data/.rspec
CHANGED
data/Gemfile
CHANGED
@@ -15,17 +15,15 @@ gemspec
|
|
15
15
|
# To use a debugger
|
16
16
|
# gem 'byebug', group: [:development, :test]
|
17
17
|
|
18
|
-
gem 'configurations', '~> 2.2.0'
|
19
|
-
|
20
18
|
group :development, :test do
|
21
|
-
gem 'sqlite3'
|
22
|
-
gem 'rubocop'
|
19
|
+
gem 'sqlite3', '1.3.11'
|
20
|
+
gem 'rubocop', '0.40.0'
|
23
21
|
gem 'pry-rails', '0.3.4'
|
24
|
-
gem 'shoulda-matchers'
|
25
|
-
gem 'rspec-rails'
|
26
|
-
gem 'factory_girl_rails', '4.
|
27
|
-
gem 'ffaker', '2.
|
28
|
-
gem 'database_cleaner', '1.
|
22
|
+
gem 'shoulda-matchers', '3.1.1'
|
23
|
+
gem 'rspec-rails', '3.4.2'
|
24
|
+
gem 'factory_girl_rails', '4.7.0'
|
25
|
+
gem 'ffaker', '2.2.0'
|
26
|
+
gem 'database_cleaner', '1.5.3'
|
29
27
|
end
|
30
28
|
|
31
|
-
gem 'simplecov', require: false, group: :test
|
29
|
+
gem 'simplecov', '0.11.2', require: false, group: :test
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
monarchy (0.
|
4
|
+
monarchy (0.3.0)
|
5
|
+
active_record_union (= 1.1.1)
|
5
6
|
closure_tree (= 6.0.0)
|
6
7
|
configurations (= 2.2.0)
|
7
8
|
rails (~> 4.2, >= 4.2.4)
|
@@ -28,6 +29,8 @@ GEM
|
|
28
29
|
erubis (~> 2.7.0)
|
29
30
|
rails-dom-testing (~> 1.0, >= 1.0.5)
|
30
31
|
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
32
|
+
active_record_union (1.1.1)
|
33
|
+
activerecord (>= 4.0)
|
31
34
|
activejob (4.2.6)
|
32
35
|
activesupport (= 4.2.6)
|
33
36
|
globalid (>= 0.3.0)
|
@@ -51,18 +54,18 @@ GEM
|
|
51
54
|
activerecord (>= 4.1.0)
|
52
55
|
with_advisory_lock (>= 3.0.0)
|
53
56
|
coderay (1.1.1)
|
54
|
-
concurrent-ruby (1.0.
|
57
|
+
concurrent-ruby (1.0.2)
|
55
58
|
configurations (2.2.0)
|
56
|
-
database_cleaner (1.
|
59
|
+
database_cleaner (1.5.3)
|
57
60
|
diff-lcs (1.2.5)
|
58
61
|
docile (1.1.5)
|
59
62
|
erubis (2.7.0)
|
60
|
-
factory_girl (4.
|
63
|
+
factory_girl (4.7.0)
|
61
64
|
activesupport (>= 3.0.0)
|
62
|
-
factory_girl_rails (4.
|
63
|
-
factory_girl (~> 4.
|
65
|
+
factory_girl_rails (4.7.0)
|
66
|
+
factory_girl (~> 4.7.0)
|
64
67
|
railties (>= 3.0.0)
|
65
|
-
ffaker (2.
|
68
|
+
ffaker (2.2.0)
|
66
69
|
globalid (0.3.6)
|
67
70
|
activesupport (>= 4.1.0)
|
68
71
|
i18n (0.7.0)
|
@@ -72,14 +75,14 @@ GEM
|
|
72
75
|
mail (2.6.4)
|
73
76
|
mime-types (>= 1.16, < 4)
|
74
77
|
method_source (0.8.2)
|
75
|
-
mime-types (3.
|
78
|
+
mime-types (3.1)
|
76
79
|
mime-types-data (~> 3.2015)
|
77
|
-
mime-types-data (3.2016.
|
80
|
+
mime-types-data (3.2016.0521)
|
78
81
|
mini_portile2 (2.0.0)
|
79
|
-
minitest (5.
|
82
|
+
minitest (5.9.0)
|
80
83
|
nokogiri (1.6.7.2)
|
81
84
|
mini_portile2 (~> 2.0.0.rc2)
|
82
|
-
parser (2.3.0
|
85
|
+
parser (2.3.1.0)
|
83
86
|
ast (~> 2.2)
|
84
87
|
powerpack (0.1.1)
|
85
88
|
pry (0.10.3)
|
@@ -116,8 +119,11 @@ GEM
|
|
116
119
|
rake (>= 0.8.7)
|
117
120
|
thor (>= 0.18.1, < 2.0)
|
118
121
|
rainbow (2.1.0)
|
119
|
-
rake (
|
120
|
-
rspec (
|
122
|
+
rake (11.1.2)
|
123
|
+
rspec (3.4.0)
|
124
|
+
rspec-core (~> 3.4.0)
|
125
|
+
rspec-expectations (~> 3.4.0)
|
126
|
+
rspec-mocks (~> 3.4.0)
|
121
127
|
rspec-core (3.4.4)
|
122
128
|
rspec-support (~> 3.4.0)
|
123
129
|
rspec-expectations (3.4.0)
|
@@ -135,13 +141,13 @@ GEM
|
|
135
141
|
rspec-mocks (~> 3.4.0)
|
136
142
|
rspec-support (~> 3.4.0)
|
137
143
|
rspec-support (3.4.1)
|
138
|
-
rubocop (0.
|
139
|
-
parser (>= 2.3.0
|
144
|
+
rubocop (0.40.0)
|
145
|
+
parser (>= 2.3.1.0, < 3.0)
|
140
146
|
powerpack (~> 0.1)
|
141
147
|
rainbow (>= 1.99.1, < 3.0)
|
142
148
|
ruby-progressbar (~> 1.7)
|
143
149
|
unicode-display_width (~> 1.0, >= 1.0.1)
|
144
|
-
ruby-progressbar (1.
|
150
|
+
ruby-progressbar (1.8.1)
|
145
151
|
shoulda-matchers (3.1.1)
|
146
152
|
activesupport (>= 4.0.0)
|
147
153
|
simplecov (0.11.2)
|
@@ -162,7 +168,7 @@ GEM
|
|
162
168
|
thread_safe (0.3.5)
|
163
169
|
tzinfo (1.2.2)
|
164
170
|
thread_safe (~> 0.1)
|
165
|
-
unicode-display_width (1.0.
|
171
|
+
unicode-display_width (1.0.5)
|
166
172
|
with_advisory_lock (3.0.0)
|
167
173
|
activerecord (>= 3.2)
|
168
174
|
thread_safe
|
@@ -172,19 +178,21 @@ PLATFORMS
|
|
172
178
|
|
173
179
|
DEPENDENCIES
|
174
180
|
bundler (~> 1.10)
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
ffaker (= 2.1.0)
|
181
|
+
database_cleaner (= 1.5.3)
|
182
|
+
factory_girl_rails (= 4.7.0)
|
183
|
+
ffaker (= 2.2.0)
|
179
184
|
monarchy!
|
180
185
|
pry-rails (= 0.3.4)
|
181
|
-
rake (~>
|
182
|
-
rspec (
|
183
|
-
rspec-rails
|
184
|
-
rubocop
|
185
|
-
shoulda-matchers
|
186
|
-
simplecov
|
187
|
-
sqlite3
|
186
|
+
rake (~> 11.1.2)
|
187
|
+
rspec (= 3.4.0)
|
188
|
+
rspec-rails (= 3.4.2)
|
189
|
+
rubocop (= 0.40.0)
|
190
|
+
shoulda-matchers (= 3.1.1)
|
191
|
+
simplecov (= 0.11.2)
|
192
|
+
sqlite3 (= 1.3.11)
|
193
|
+
|
194
|
+
RUBY VERSION
|
195
|
+
ruby 2.3.0p0
|
188
196
|
|
189
197
|
BUNDLED WITH
|
190
|
-
1.12.
|
198
|
+
1.12.1
|
data/README.md
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
[![Build Status](https://travis-ci.org/Exelord/Monarchy.svg?branch=master)](https://travis-ci.org/Exelord/Monarchy)
|
2
|
+
[![Dependency Status](https://gemnasium.com/badges/github.com/Exelord/Monarchy.svg)](https://gemnasium.com/github.com/Exelord/Monarchy)
|
3
|
+
[![Gitter](https://badges.gitter.im/Exelord/Monarchy.svg)](https://gitter.im/Exelord/Monarchy?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
2
4
|
![Monarchy Logo](monarchy.png?raw=true)
|
3
5
|
|
4
6
|
## Documentation
|
@@ -5,21 +5,46 @@ module Monarchy
|
|
5
5
|
|
6
6
|
module ClassMethods
|
7
7
|
def acts_as_hierarchy
|
8
|
+
extend Monarchy::ActsAsHierarchy::SupportMethods
|
8
9
|
has_closure_tree dependent: :destroy
|
9
10
|
|
10
|
-
has_many :members, class_name: 'Monarchy::Member'
|
11
|
+
has_many :members, class_name: 'Monarchy::Member', dependent: :destroy
|
11
12
|
belongs_to :resource, polymorphic: true, dependent: :destroy
|
12
13
|
|
13
|
-
|
14
|
+
include_scopes
|
14
15
|
|
15
|
-
|
16
|
+
validates :resource_type, presence: true
|
17
|
+
validates :resource_id, uniqueness: { scope: [:resource_type] }, presence: true
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
19
|
-
module
|
20
|
-
|
21
|
-
|
22
|
-
|
21
|
+
module SupportMethods
|
22
|
+
private
|
23
|
+
|
24
|
+
def include_scopes
|
25
|
+
scope :in, (lambda do |resource|
|
26
|
+
where(monarchy_hierarchies: { parent_id: resource.hierarchy.self_and_descendant_ids })
|
27
|
+
end)
|
28
|
+
|
29
|
+
scope :accessible_for, (lambda do |user|
|
30
|
+
where(id: accessible_roots(user)).union(where(id: accessible_leaves(user)))
|
31
|
+
end)
|
32
|
+
end
|
33
|
+
|
34
|
+
def accessible_roots(user)
|
35
|
+
joins('INNER JOIN "monarchy_hierarchy_hierarchies" ON '\
|
36
|
+
'"monarchy_hierarchies"."id" = "monarchy_hierarchy_hierarchies"."ancestor_id"')
|
37
|
+
.joins('INNER JOIN "monarchy_members" ON '\
|
38
|
+
'"monarchy_members"."hierarchy_id" = "monarchy_hierarchy_hierarchies"."descendant_id"')
|
39
|
+
.where(monarchy_members: { user_id: user.id }).distinct
|
40
|
+
end
|
41
|
+
|
42
|
+
def accessible_leaves(user)
|
43
|
+
joins('INNER JOIN "monarchy_hierarchy_hierarchies" ON '\
|
44
|
+
'"monarchy_hierarchies"."id" = "monarchy_hierarchy_hierarchies"."descendant_id"')
|
45
|
+
.joins('INNER JOIN "monarchy_members" ON '\
|
46
|
+
'"monarchy_members"."hierarchy_id" = "monarchy_hierarchy_hierarchies"."ancestor_id"')
|
47
|
+
.where(monarchy_members: { user_id: user.id }).distinct
|
23
48
|
end
|
24
49
|
end
|
25
50
|
end
|
@@ -5,44 +5,48 @@ module Monarchy
|
|
5
5
|
|
6
6
|
module ClassMethods
|
7
7
|
def acts_as_resource(options = {})
|
8
|
+
extend Monarchy::ActsAsResource::SupportMethods
|
9
|
+
|
8
10
|
parent_as(options[:parent_as]) if options[:parent_as]
|
9
11
|
|
10
12
|
after_create :ensure_hierarchy
|
11
13
|
|
12
|
-
has_many :members, through: :hierarchy
|
14
|
+
has_many :members, through: :hierarchy, class_name: 'Monarchy::Member'
|
15
|
+
has_many :users, through: :members, class_name: 'User'
|
13
16
|
has_one :hierarchy, as: :resource, dependent: :destroy, class_name: 'Monarchy::Hierarchy'
|
14
17
|
|
15
18
|
include_scopes
|
16
19
|
|
17
20
|
include Monarchy::ActsAsResource::InstanceMethods
|
18
21
|
end
|
22
|
+
end
|
19
23
|
|
24
|
+
module SupportMethods
|
20
25
|
private
|
21
26
|
|
22
27
|
def parent_as(name)
|
23
28
|
define_method "#{name}=" do |value|
|
24
|
-
super(value)
|
25
29
|
self.parent = value
|
30
|
+
super(value)
|
31
|
+
end
|
32
|
+
|
33
|
+
define_method "#{name}_id=" do |id|
|
34
|
+
class_name = name.to_s.camelize.safe_constantize
|
35
|
+
self.parent = class_name.find(id)
|
36
|
+
super(id)
|
26
37
|
end
|
27
38
|
end
|
28
39
|
|
29
|
-
# rubocop:disable MethodLength
|
30
40
|
def include_scopes
|
31
41
|
scope :in, (lambda do |resource|
|
32
|
-
joins(:hierarchy).where(monarchy_hierarchies: { parent_id: resource.hierarchy.
|
42
|
+
joins(:hierarchy).where(monarchy_hierarchies: { parent_id: resource.hierarchy.self_and_descendant_ids })
|
33
43
|
end)
|
34
44
|
|
35
45
|
scope :accessible_for, (lambda do |user|
|
36
|
-
joins(:hierarchy)
|
37
|
-
.joins('INNER JOIN "monarchy_hierarchy_hierarchies" ON '\
|
38
|
-
'"monarchy_hierarchies"."id" = "monarchy_hierarchy_hierarchies"."ancestor_id"')
|
39
|
-
.joins('INNER JOIN "monarchy_members" ON '\
|
40
|
-
'"monarchy_members"."hierarchy_id" = "monarchy_hierarchy_hierarchies"."descendant_id"')
|
41
|
-
.where(monarchy_members: { user_id: user.id }).uniq
|
46
|
+
joins(:hierarchy).where(monarchy_hierarchies: { id: Monarchy::Hierarchy.accessible_for(user) })
|
42
47
|
end)
|
43
48
|
end
|
44
49
|
end
|
45
|
-
# rubocop:enable MethodLength
|
46
50
|
|
47
51
|
module InstanceMethods
|
48
52
|
def parent
|
@@ -63,7 +67,6 @@ module Monarchy
|
|
63
67
|
|
64
68
|
def children=(array)
|
65
69
|
hierarchy.update(children: hierarchies_for(array)) if hierarchy
|
66
|
-
|
67
70
|
@children = array
|
68
71
|
end
|
69
72
|
|
@@ -5,55 +5,79 @@ module Monarchy
|
|
5
5
|
|
6
6
|
module ClassMethods
|
7
7
|
def acts_as_user
|
8
|
-
has_many :members, class_name: 'Monarchy::Member'
|
8
|
+
has_many :members, class_name: 'Monarchy::Member', dependent: :destroy
|
9
|
+
has_many :hierarchies, through: :members, class_name: 'Monarchy::Hierarchy'
|
9
10
|
|
10
11
|
include Monarchy::ActsAsUser::InstanceMethods
|
11
12
|
end
|
12
13
|
end
|
13
14
|
|
14
15
|
module InstanceMethods
|
15
|
-
def
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
.order(level: :desc).first
|
16
|
+
def roles_for(resource, inheritence = true)
|
17
|
+
return [] unless resource.hierarchy
|
18
|
+
accessible_roles_for(resource, inheritence).group_by(&:level).values.first || []
|
19
|
+
end
|
20
|
+
|
21
|
+
def member_for(resource)
|
22
|
+
resource.hierarchy.members.where(monarchy_members: { user_id: id }).first
|
23
23
|
end
|
24
24
|
|
25
25
|
def grant(role_name, resource)
|
26
26
|
ActiveRecord::Base.transaction do
|
27
|
-
Monarchy::Member.create(build_members(resource.hierarchy.memberless_ancestors_for(self)))
|
28
27
|
grant_or_create_member(role_name, resource)
|
29
28
|
end
|
30
29
|
end
|
31
30
|
|
32
|
-
def member_for(resource)
|
33
|
-
resource.hierarchy.members.where("monarchy_members.user_id": id).first
|
34
|
-
end
|
35
|
-
|
36
31
|
def revoke_access(resource)
|
37
32
|
self_and_descendant_ids = resource.hierarchy.self_and_descendant_ids
|
38
|
-
|
39
|
-
ActiveRecord::Base.transaction do
|
40
|
-
members_roles_for(self_and_descendant_ids).destroy_all
|
41
|
-
try_revoke_ancestors_for(resource)
|
42
|
-
end
|
33
|
+
members_for(self_and_descendant_ids).destroy_all
|
43
34
|
end
|
44
35
|
|
45
36
|
def revoke_role(role_name, resource)
|
46
|
-
|
37
|
+
revoking_role(role_name, resource)
|
38
|
+
end
|
47
39
|
|
48
|
-
|
49
|
-
|
50
|
-
else
|
51
|
-
members_roles.joins(:role).where("monarchy_roles.name": role_name).destroy_all
|
52
|
-
end
|
40
|
+
def revoke_role!(role_name, resource)
|
41
|
+
revoking_role(role_name, resource, true)
|
53
42
|
end
|
54
43
|
|
55
44
|
private
|
56
45
|
|
46
|
+
def accessible_roles_for(resource, inheritnce)
|
47
|
+
accessible_roles = inheritnce ? resource_and_inheritence_roles(resource) : resource_roles(resource)
|
48
|
+
accessible_roles.present? ? accessible_roles : descendant_role(resource)
|
49
|
+
end
|
50
|
+
|
51
|
+
def resource_and_inheritence_roles(resource)
|
52
|
+
hierarchy_ids = resource.hierarchy.self_and_ancestors_ids
|
53
|
+
Monarchy::Role.joins(:members)
|
54
|
+
.where("((monarchy_roles.inherited = 't' "\
|
55
|
+
"AND monarchy_members.hierarchy_id IN (#{hierarchy_ids.join(',')})) "\
|
56
|
+
"OR (monarchy_members.hierarchy_id = #{resource.hierarchy.id})) "\
|
57
|
+
"AND monarchy_members.user_id = #{id}")
|
58
|
+
.distinct.order(level: :desc)
|
59
|
+
end
|
60
|
+
|
61
|
+
def resource_roles(resource)
|
62
|
+
Monarchy::Role.joins(:members)
|
63
|
+
.where('monarchy_members.hierarchy_id': resource.hierarchy.id, 'monarchy_members.user_id': id)
|
64
|
+
.distinct.order(level: :desc)
|
65
|
+
end
|
66
|
+
|
67
|
+
def descendant_role(resource)
|
68
|
+
descendant_ids = resource.hierarchy.descendant_ids
|
69
|
+
children_access = members_for(descendant_ids).present?
|
70
|
+
children_access ? [default_role] : []
|
71
|
+
end
|
72
|
+
|
73
|
+
def revoking_role(role_name, resource, force = false)
|
74
|
+
member = member_for(resource)
|
75
|
+
member_roles = member.members_roles
|
76
|
+
|
77
|
+
return revoke_access(resource) if last_role?(member_roles, role_name) && force
|
78
|
+
member_roles.joins(:role).where(monarchy_roles: { name: role_name }).destroy_all
|
79
|
+
end
|
80
|
+
|
57
81
|
def grant_or_create_member(role_name, resource)
|
58
82
|
role = Monarchy::Role.find_by(name: role_name)
|
59
83
|
member = member_for(resource)
|
@@ -61,35 +85,27 @@ module Monarchy
|
|
61
85
|
if member
|
62
86
|
Monarchy::MembersRole.create(member: member, role: role)
|
63
87
|
else
|
64
|
-
member = Monarchy::Member.create(
|
88
|
+
member = Monarchy::Member.create(user: self, hierarchy: resource.hierarchy, roles: [role])
|
65
89
|
end
|
66
90
|
|
67
91
|
member
|
68
92
|
end
|
69
93
|
|
70
|
-
def
|
71
|
-
|
72
|
-
end
|
73
|
-
|
74
|
-
def equal_role_name?(role, role_name)
|
75
|
-
role.name == role_name.to_s
|
94
|
+
def members_for(hierarchy_ids)
|
95
|
+
Monarchy::Member.where(hierarchy_id: hierarchy_ids, user_id: id)
|
76
96
|
end
|
77
97
|
|
78
|
-
def
|
79
|
-
|
80
|
-
members_roles.count == 1 && equal_role_name?(members_roles.first.role, role_name)
|
98
|
+
def default_role
|
99
|
+
@default_role ||= Monarchy::Role.find_by(name: Monarchy.configuration.default_role.name)
|
81
100
|
end
|
82
101
|
|
83
|
-
def
|
84
|
-
|
85
|
-
|
102
|
+
def last_role?(member_roles, role_name = nil)
|
103
|
+
role_name ||= default_role.name
|
104
|
+
member_roles.count == 1 && member_roles.first.role.name == role_name.to_s
|
86
105
|
end
|
87
106
|
|
88
|
-
def
|
89
|
-
|
90
|
-
members_roles = members_roles_for(hierarchy.self_and_descendant_ids)
|
91
|
-
only_this_role(members_roles) ? members_roles.destroy_all : break
|
92
|
-
end
|
107
|
+
def default_role?(role_name)
|
108
|
+
default_role.name.to_s == role_name.to_s
|
93
109
|
end
|
94
110
|
end
|
95
111
|
end
|
data/lib/monarchy/membership.rb
CHANGED
@@ -3,21 +3,29 @@ module Monarchy
|
|
3
3
|
class Member < ActiveRecord::Base
|
4
4
|
self.table_name = 'monarchy_members'
|
5
5
|
|
6
|
-
has_many :members_roles, dependent: :destroy
|
7
|
-
has_many :roles, through: :members_roles
|
6
|
+
has_many :members_roles, dependent: :destroy, class_name: 'Monarchy::MembersRole'
|
7
|
+
has_many :roles, through: :members_roles, class_name: 'Monarchy::Role'
|
8
8
|
|
9
9
|
belongs_to :user
|
10
|
-
belongs_to :hierarchy
|
10
|
+
belongs_to :hierarchy, class_name: 'Monarchy::Hierarchy'
|
11
11
|
|
12
|
-
delegate :resource, to: :hierarchy
|
13
|
-
delegate :resource=, to: :hierarchy
|
12
|
+
delegate :resource, :resource=, :resource_id, :resource_type, to: :hierarchy
|
14
13
|
|
15
14
|
validates :user_id, uniqueness: { scope: :hierarchy_id }
|
16
15
|
validates :user, presence: true
|
17
|
-
|
16
|
+
|
17
|
+
validate :hierarchy_or_resource
|
18
18
|
|
19
19
|
before_create :set_default_role
|
20
20
|
|
21
|
+
scope :accessible_for, (lambda do |user|
|
22
|
+
where(hierarchy: Monarchy::Hierarchy.accessible_for(user))
|
23
|
+
end)
|
24
|
+
|
25
|
+
def resource=(resource)
|
26
|
+
self.hierarchy = resource.hierarchy unless hierarchy
|
27
|
+
end
|
28
|
+
|
21
29
|
private
|
22
30
|
|
23
31
|
def set_default_role
|
@@ -25,23 +33,28 @@ module Monarchy
|
|
25
33
|
roles << Monarchy::Role.find_or_create_by(
|
26
34
|
name: Monarchy.configuration.default_role.name,
|
27
35
|
inherited: Monarchy.configuration.default_role.inherited,
|
28
|
-
level: Monarchy.configuration.default_role.level
|
36
|
+
level: Monarchy.configuration.default_role.level
|
37
|
+
)
|
29
38
|
self.roles = roles.uniq
|
30
39
|
end
|
40
|
+
|
41
|
+
def hierarchy_or_resource
|
42
|
+
errors.add(:base, 'Specify a resource or a hierarchy') unless hierarchy
|
43
|
+
end
|
31
44
|
end
|
32
45
|
|
33
46
|
class Role < ActiveRecord::Base
|
34
47
|
self.table_name = 'monarchy_roles'
|
35
48
|
|
36
|
-
has_many :members_roles, dependent: :destroy
|
37
|
-
has_many :members, through: :members_roles
|
49
|
+
has_many :members_roles, dependent: :destroy, class_name: 'Monarchy::MembersRole'
|
50
|
+
has_many :members, through: :members_roles, class_name: 'Monarchy::Member'
|
38
51
|
end
|
39
52
|
|
40
53
|
class MembersRole < ActiveRecord::Base
|
41
54
|
self.table_name = 'monarchy_members_roles'
|
42
55
|
|
43
|
-
belongs_to :member
|
44
|
-
belongs_to :role
|
56
|
+
belongs_to :member, class_name: 'Monarchy::Member'
|
57
|
+
belongs_to :role, class_name: 'Monarchy::Role'
|
45
58
|
|
46
59
|
validates :role_id, uniqueness: { scope: :member_id }
|
47
60
|
end
|
data/lib/monarchy/version.rb
CHANGED
data/lib/monarchy.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
require 'closure_tree'
|
3
3
|
require 'configurations'
|
4
|
+
require 'active_record_union'
|
4
5
|
|
5
6
|
require 'monarchy/acts_as_hierarchy'
|
6
7
|
require 'monarchy/acts_as_resource'
|
7
8
|
require 'monarchy/acts_as_user'
|
8
9
|
require 'monarchy/membership'
|
9
|
-
require
|
10
|
+
require 'monarchy/engine'
|
10
11
|
|
11
12
|
module Monarchy
|
12
13
|
include Configurations
|
data/monarchy.gemspec
CHANGED
@@ -28,8 +28,9 @@ Gem::Specification.new do |s|
|
|
28
28
|
s.add_runtime_dependency 'rails', '~> 4.2', '>= 4.2.4'
|
29
29
|
s.add_dependency 'closure_tree', '6.0.0'
|
30
30
|
s.add_dependency 'configurations', '2.2.0'
|
31
|
+
s.add_dependency 'active_record_union', '1.1.1'
|
31
32
|
|
32
33
|
s.add_development_dependency 'bundler', '~> 1.10'
|
33
|
-
s.add_development_dependency 'rake', '~>
|
34
|
-
s.add_development_dependency 'rspec', '
|
34
|
+
s.add_development_dependency 'rake', '~> 11.1'
|
35
|
+
s.add_development_dependency 'rspec', '3.4.0'
|
35
36
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: monarchy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Exelord
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-05-
|
11
|
+
date: 2016-05-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -58,6 +58,20 @@ dependencies:
|
|
58
58
|
- - '='
|
59
59
|
- !ruby/object:Gem::Version
|
60
60
|
version: 2.2.0
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: active_record_union
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - '='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 1.1.1
|
68
|
+
type: :runtime
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - '='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 1.1.1
|
61
75
|
- !ruby/object:Gem::Dependency
|
62
76
|
name: bundler
|
63
77
|
requirement: !ruby/object:Gem::Requirement
|
@@ -78,28 +92,28 @@ dependencies:
|
|
78
92
|
requirements:
|
79
93
|
- - "~>"
|
80
94
|
- !ruby/object:Gem::Version
|
81
|
-
version: '
|
95
|
+
version: '11.1'
|
82
96
|
type: :development
|
83
97
|
prerelease: false
|
84
98
|
version_requirements: !ruby/object:Gem::Requirement
|
85
99
|
requirements:
|
86
100
|
- - "~>"
|
87
101
|
- !ruby/object:Gem::Version
|
88
|
-
version: '
|
102
|
+
version: '11.1'
|
89
103
|
- !ruby/object:Gem::Dependency
|
90
104
|
name: rspec
|
91
105
|
requirement: !ruby/object:Gem::Requirement
|
92
106
|
requirements:
|
93
|
-
- -
|
107
|
+
- - '='
|
94
108
|
- !ruby/object:Gem::Version
|
95
|
-
version:
|
109
|
+
version: 3.4.0
|
96
110
|
type: :development
|
97
111
|
prerelease: false
|
98
112
|
version_requirements: !ruby/object:Gem::Requirement
|
99
113
|
requirements:
|
100
|
-
- -
|
114
|
+
- - '='
|
101
115
|
- !ruby/object:Gem::Version
|
102
|
-
version:
|
116
|
+
version: 3.4.0
|
103
117
|
description: Hierarchical access management system with roles inheritance.
|
104
118
|
email:
|
105
119
|
- ''
|
@@ -108,7 +122,6 @@ extensions: []
|
|
108
122
|
extra_rdoc_files: []
|
109
123
|
files:
|
110
124
|
- ".gitignore"
|
111
|
-
- ".lock-ClosureTreeMonarchyHierarchy-1609118677"
|
112
125
|
- ".rspec"
|
113
126
|
- ".rubocop.yml"
|
114
127
|
- ".travis.yml"
|
File without changes
|