monarchy 2.0.7 → 2.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github_changelog_generator +1 -1
- data/CHANGELOG.md +9 -2
- data/Gemfile.lock +2 -2
- data/docs/acts_as_hierarchy.md +19 -1
- data/docs/acts_as_resource.md +18 -0
- data/dummy/db/schema.rb +13 -19
- data/lib/monarchy/acts_as_hierarchy.rb +21 -5
- data/lib/monarchy/acts_as_resource.rb +2 -2
- data/lib/monarchy/version.rb +1 -1
- data/lib/monarchy.rb +2 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9a877a7ac740060484a3f64d5d0f9fbb7fcf310b
|
4
|
+
data.tar.gz: f26a5bd2af7906227bb78e9a35937e9bfd2ebe4a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8727e6be4d706c8117fc1dae73f19bff9cab7e57269c2c85e942f14b4dbf7e9e761a065ecd8d031fe13c39ce1b6961073dd8e4d6e4937e2c558253bc910668e0
|
7
|
+
data.tar.gz: 2fd0924823fba6765740c972246a1db28cb358be122c290a28957451b85bc886e306ad89d1c6797436f0a74e763de5f0c70255fb10fdcf0b6dce8c84974c1b8f
|
data/.github_changelog_generator
CHANGED
@@ -1 +1 @@
|
|
1
|
-
future-release=2.0.
|
1
|
+
future-release=2.0.8
|
data/CHANGELOG.md
CHANGED
@@ -1,7 +1,14 @@
|
|
1
1
|
# Change Log
|
2
2
|
|
3
|
-
## [2.0.
|
4
|
-
[Full Changelog](https://github.com/Exelord/Monarchy/compare/v2.0.
|
3
|
+
## [2.0.8](https://github.com/Exelord/Monarchy/tree/2.0.8) (2017-01-12)
|
4
|
+
[Full Changelog](https://github.com/Exelord/Monarchy/compare/v2.0.7...2.0.8)
|
5
|
+
|
6
|
+
**Merged pull requests:**
|
7
|
+
|
8
|
+
- Feature parent access [\#67](https://github.com/Exelord/Monarchy/pull/67) ([Exelord](https://github.com/Exelord))
|
9
|
+
|
10
|
+
## [v2.0.7](https://github.com/Exelord/Monarchy/tree/v2.0.7) (2017-01-09)
|
11
|
+
[Full Changelog](https://github.com/Exelord/Monarchy/compare/v2.0.6...v2.0.7)
|
5
12
|
|
6
13
|
**Merged pull requests:**
|
7
14
|
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
monarchy (2.0.
|
4
|
+
monarchy (2.0.8)
|
5
5
|
active_record_union (= 1.2.0)
|
6
6
|
activerecord (>= 4.2.7.1)
|
7
7
|
closure_tree (= 6.2.0)
|
@@ -74,7 +74,7 @@ GEM
|
|
74
74
|
globalid (0.3.7)
|
75
75
|
activesupport (>= 4.1.0)
|
76
76
|
i18n (0.7.0)
|
77
|
-
json (2.0.
|
77
|
+
json (2.0.3)
|
78
78
|
loofah (2.0.3)
|
79
79
|
nokogiri (>= 1.5.9)
|
80
80
|
mail (2.6.4)
|
data/docs/acts_as_hierarchy.md
CHANGED
@@ -16,9 +16,27 @@ You can select all hierarchies accessible for specific user by using a scope: `a
|
|
16
16
|
``` ruby
|
17
17
|
Monarchy.hierarchy_class.accessible_for(current_user) # returns [hierarchy1, hierarchy2, hierarchy5]
|
18
18
|
```
|
19
|
+
|
20
|
+
#### Options
|
19
21
|
Optionally you can pass extra allowed roles which should be inherited for this request
|
20
22
|
``` ruby
|
21
|
-
Monarchy.hierarchy_class.accessible_for(current_user, [:blocked, :visitors]) # returns [hierarchy1, hierarchy2, hierarchy5, hierarchy6]
|
23
|
+
Monarchy.hierarchy_class.accessible_for(current_user, { inherited_roles: [:blocked, :visitors] }) # returns [hierarchy1, hierarchy2, hierarchy5, hierarchy6]
|
24
|
+
```
|
25
|
+
|
26
|
+
You can also determine if read (default role) access in resource should allow to access theirs children
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
Monarchy.hierarchy_class.accessible_for(current_user, { parent_access: true })
|
30
|
+
# (GRANTED) hierarchy1
|
31
|
+
# |
|
32
|
+
# |
|
33
|
+
# (GRANTED) hierarchy2 hierarchy4 (GRANTED cuz it's a parent of granted resource)
|
34
|
+
# | |
|
35
|
+
# | |
|
36
|
+
# | hierarchy5 (NOT granted - it's not a child of granted resource)
|
37
|
+
# |
|
38
|
+
# |
|
39
|
+
# hierarchy3 (member role - GRANTED)
|
22
40
|
```
|
23
41
|
|
24
42
|
### .in(resource, true)
|
data/docs/acts_as_resource.md
CHANGED
@@ -69,11 +69,29 @@ You can select all resources accessible for specyfic user by using scope: `acces
|
|
69
69
|
``` ruby
|
70
70
|
Resource.accessible_for(current_user) # returns [resource1, resource2, resource5]
|
71
71
|
```
|
72
|
+
|
73
|
+
#### Options
|
72
74
|
Optionally you can pass extra allowed roles which should be inherited for this request
|
73
75
|
``` ruby
|
74
76
|
Resource.accessible_for(current_user, [:blocked, :visitors]) # returns [resource1, resource2, resource5, resource6]
|
75
77
|
```
|
76
78
|
|
79
|
+
You can also determine if read (default role) access in resource should allow to access theirs children
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
Resource.accessible_for(current_user, { parent_access: true })
|
83
|
+
# (GRANTED) project1
|
84
|
+
# |
|
85
|
+
# |
|
86
|
+
# (GRANTED) project2 project4 (GRANTED cuz it's a parent of granted resource)
|
87
|
+
# | |
|
88
|
+
# | |
|
89
|
+
# | project5 (NOT granted - it's not a child of granted resource)
|
90
|
+
# |
|
91
|
+
# |
|
92
|
+
# project3 (member role - GRANTED)
|
93
|
+
```
|
94
|
+
|
77
95
|
### .in(resource, true)
|
78
96
|
You can select all resources scoped into another by using scope: `in`:
|
79
97
|
|
data/dummy/db/schema.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
# encoding: UTF-8
|
2
1
|
# This file is auto-generated from the current state of the database. Instead
|
3
2
|
# of editing this file, please use the migrations feature of Active Record to
|
4
3
|
# incrementally modify your database, and then regenerate this schema definition.
|
@@ -26,41 +25,37 @@ ActiveRecord::Schema.define(version: 20160818205534) do
|
|
26
25
|
t.string "resource_type", null: false
|
27
26
|
t.datetime "created_at", null: false
|
28
27
|
t.datetime "updated_at", null: false
|
28
|
+
t.index ["parent_id"], name: "index_monarchy_hierarchies_on_parent_id"
|
29
|
+
t.index ["resource_id"], name: "index_monarchy_hierarchies_on_resource_id"
|
29
30
|
end
|
30
31
|
|
31
|
-
add_index "monarchy_hierarchies", ["parent_id"], name: "index_monarchy_hierarchies_on_parent_id"
|
32
|
-
add_index "monarchy_hierarchies", ["resource_id"], name: "index_monarchy_hierarchies_on_resource_id"
|
33
|
-
|
34
32
|
create_table "monarchy_hierarchy_hierarchies", id: false, force: :cascade do |t|
|
35
33
|
t.integer "ancestor_id", null: false
|
36
34
|
t.integer "descendant_id", null: false
|
37
35
|
t.integer "generations", null: false
|
36
|
+
t.index ["ancestor_id", "descendant_id", "generations"], name: "hierarchy_anc_desc_idx", unique: true
|
37
|
+
t.index ["descendant_id"], name: "hierarchy_desc_idx"
|
38
38
|
end
|
39
39
|
|
40
|
-
add_index "monarchy_hierarchy_hierarchies", ["ancestor_id", "descendant_id", "generations"], name: "hierarchy_anc_desc_idx", unique: true
|
41
|
-
add_index "monarchy_hierarchy_hierarchies", ["descendant_id"], name: "hierarchy_desc_idx"
|
42
|
-
|
43
40
|
create_table "monarchy_members", force: :cascade do |t|
|
44
41
|
t.integer "user_id"
|
45
42
|
t.integer "hierarchy_id"
|
46
43
|
t.datetime "created_at", null: false
|
47
44
|
t.datetime "updated_at", null: false
|
45
|
+
t.index ["hierarchy_id"], name: "index_monarchy_members_on_hierarchy_id"
|
46
|
+
t.index ["user_id"], name: "index_monarchy_members_on_user_id"
|
48
47
|
end
|
49
48
|
|
50
|
-
add_index "monarchy_members", ["hierarchy_id"], name: "index_monarchy_members_on_hierarchy_id"
|
51
|
-
add_index "monarchy_members", ["user_id"], name: "index_monarchy_members_on_user_id"
|
52
|
-
|
53
49
|
create_table "monarchy_members_roles", force: :cascade do |t|
|
54
50
|
t.integer "role_id"
|
55
51
|
t.integer "member_id"
|
56
52
|
t.datetime "created_at", null: false
|
57
53
|
t.datetime "updated_at", null: false
|
54
|
+
t.index ["member_id"], name: "index_monarchy_members_roles_on_member_id"
|
55
|
+
t.index ["role_id", "member_id"], name: "index_monarchy_members_roles_on_role_id_and_member_id", unique: true
|
56
|
+
t.index ["role_id"], name: "index_monarchy_members_roles_on_role_id"
|
58
57
|
end
|
59
58
|
|
60
|
-
add_index "monarchy_members_roles", ["member_id"], name: "index_monarchy_members_roles_on_member_id"
|
61
|
-
add_index "monarchy_members_roles", ["role_id", "member_id"], name: "index_monarchy_members_roles_on_role_id_and_member_id", unique: true
|
62
|
-
add_index "monarchy_members_roles", ["role_id"], name: "index_monarchy_members_roles_on_role_id"
|
63
|
-
|
64
59
|
create_table "monarchy_roles", force: :cascade do |t|
|
65
60
|
t.string "name", null: false
|
66
61
|
t.integer "level", default: 0, null: false
|
@@ -68,13 +63,12 @@ ActiveRecord::Schema.define(version: 20160818205534) do
|
|
68
63
|
t.boolean "inherited", default: false, null: false
|
69
64
|
t.datetime "created_at", null: false
|
70
65
|
t.datetime "updated_at", null: false
|
66
|
+
t.index ["inherited"], name: "index_monarchy_roles_on_inherited"
|
67
|
+
t.index ["inherited_role_id"], name: "index_monarchy_roles_on_inherited_role_id"
|
68
|
+
t.index ["level"], name: "index_monarchy_roles_on_level"
|
69
|
+
t.index ["name"], name: "index_monarchy_roles_on_name", unique: true
|
71
70
|
end
|
72
71
|
|
73
|
-
add_index "monarchy_roles", ["inherited"], name: "index_monarchy_roles_on_inherited"
|
74
|
-
add_index "monarchy_roles", ["inherited_role_id"], name: "index_monarchy_roles_on_inherited_role_id"
|
75
|
-
add_index "monarchy_roles", ["level"], name: "index_monarchy_roles_on_level"
|
76
|
-
add_index "monarchy_roles", ["name"], name: "index_monarchy_roles_on_name", unique: true
|
77
|
-
|
78
72
|
create_table "projects", force: :cascade do |t|
|
79
73
|
t.string "name"
|
80
74
|
t.datetime "created_at", null: false
|
@@ -23,25 +23,37 @@ module Monarchy
|
|
23
23
|
module SupportMethods
|
24
24
|
private
|
25
25
|
|
26
|
+
# rubocop:disable all
|
26
27
|
def include_scopes
|
27
28
|
scope :in, (lambda do |hierarchy, descendants = true|
|
28
29
|
Monarchy::Validators.hierarchy(hierarchy)
|
29
30
|
where(id: descendants ? hierarchy.descendants : hierarchy.children)
|
30
31
|
end)
|
31
32
|
|
32
|
-
scope :accessible_for, (lambda do |user,
|
33
|
+
scope :accessible_for, (lambda do |user, options = {}|
|
33
34
|
Monarchy::Validators.user(user)
|
34
35
|
user_id = user.id
|
35
|
-
|
36
|
+
|
37
|
+
custom_options = accessible_for_options(options)
|
38
|
+
where(id: accessible_roots_ids(user_id, custom_options[:parent_access])
|
39
|
+
.union_all(accessible_leaves_ids(user_id, custom_options[:inherited_roles])))
|
36
40
|
end)
|
37
41
|
end
|
42
|
+
# rubocop:enable all
|
38
43
|
|
39
|
-
def accessible_roots_ids(user_id)
|
40
|
-
unscoped.joins('INNER JOIN monarchy_hierarchy_hierarchies ON ' \
|
44
|
+
def accessible_roots_ids(user_id, parent_access)
|
45
|
+
accessible_roots = unscoped.joins('INNER JOIN monarchy_hierarchy_hierarchies ON ' \
|
41
46
|
'monarchy_hierarchies.id = monarchy_hierarchy_hierarchies.ancestor_id')
|
42
|
-
|
47
|
+
.joins('INNER JOIN (SELECT hierarchy_id FROM monarchy_members ' \
|
43
48
|
"WHERE monarchy_members.user_id = #{user_id}) as members ON " \
|
44
49
|
'members.hierarchy_id = monarchy_hierarchy_hierarchies.descendant_id').select(:id)
|
50
|
+
|
51
|
+
parent_access ? roots_with_children(accessible_roots) : accessible_roots
|
52
|
+
end
|
53
|
+
|
54
|
+
def roots_with_children(accessible_roots)
|
55
|
+
accessible_children = unscoped.where(parent_id: accessible_roots).select(:id)
|
56
|
+
accessible_roots.union_all(accessible_children)
|
45
57
|
end
|
46
58
|
|
47
59
|
def accessible_leaves_ids(user_id, inherited_roles = [])
|
@@ -76,6 +88,10 @@ module Monarchy
|
|
76
88
|
Monarchy.role_class.select(:id, :inherited).where(inherited: inherited).to_sql
|
77
89
|
end
|
78
90
|
end
|
91
|
+
|
92
|
+
def accessible_for_options(options = {})
|
93
|
+
Monarchy.configuration.accessible_for_options.to_h.merge(options)
|
94
|
+
end
|
79
95
|
end
|
80
96
|
end
|
81
97
|
end
|
@@ -51,9 +51,9 @@ module Monarchy
|
|
51
51
|
joins(:hierarchy).where(monarchy_hierarchies: { id: hierarchies })
|
52
52
|
end)
|
53
53
|
|
54
|
-
scope :accessible_for, (lambda do |user,
|
54
|
+
scope :accessible_for, (lambda do |user, options = {}|
|
55
55
|
joins(:hierarchy).where(monarchy_hierarchies: { id: Monarchy.hierarchy_class
|
56
|
-
.accessible_for(user,
|
56
|
+
.accessible_for(user, options) })
|
57
57
|
end)
|
58
58
|
end
|
59
59
|
|
data/lib/monarchy/version.rb
CHANGED
data/lib/monarchy.rb
CHANGED
@@ -29,6 +29,8 @@ module Monarchy
|
|
29
29
|
config.hierarchy_class_name = 'Monarchy::Hierarchy'
|
30
30
|
config.members_access_revoke = false
|
31
31
|
config.revoke_strategy = :revoke_member
|
32
|
+
config.accessible_for_options.parent_access = false
|
33
|
+
config.accessible_for_options.inherited_roles = []
|
32
34
|
end
|
33
35
|
|
34
36
|
not_configured do |property|
|
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: 2.0.
|
4
|
+
version: 2.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Exelord
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|