active_admin_role 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.rubocop.yml +17 -0
- data/.ruby-style.yml +242 -0
- data/.travis.yml +5 -0
- data/Appraisals +12 -0
- data/Gemfile +24 -0
- data/LICENSE.txt +21 -0
- data/README.md +79 -0
- data/Rakefile +7 -0
- data/active_admin_role.gemspec +21 -0
- data/app/models/active_admin/managed_resource.rb +64 -0
- data/app/models/active_admin/permission.rb +59 -0
- data/config/locales/en.yml +40 -0
- data/config/locales/ja.yml +37 -0
- data/gemfiles/.bundle/config +2 -0
- data/gemfiles/rails42.gemfile +26 -0
- data/gemfiles/rails50.gemfile +27 -0
- data/lib/active_admin_role/active_admin/dsl.rb +24 -0
- data/lib/active_admin_role/active_admin/resource_controller.rb +21 -0
- data/lib/active_admin_role/can_can/ability.rb +15 -0
- data/lib/active_admin_role/config.rb +17 -0
- data/lib/active_admin_role/engine.rb +15 -0
- data/lib/active_admin_role/manageable_resource.rb +44 -0
- data/lib/active_admin_role/model.rb +7 -0
- data/lib/active_admin_role/role_based_authorizable.rb +45 -0
- data/lib/active_admin_role/version.rb +3 -0
- data/lib/active_admin_role.rb +21 -0
- data/lib/generators/active_admin_role/USAGE +9 -0
- data/lib/generators/active_admin_role/helper.rb +64 -0
- data/lib/generators/active_admin_role/install_generator.rb +54 -0
- data/lib/generators/active_admin_role/templates/admin/permission.rb +71 -0
- data/lib/generators/active_admin_role/templates/initializer.rb +18 -0
- data/lib/generators/active_admin_role/templates/migration/add_role_to_admin_users.rb +5 -0
- data/lib/generators/active_admin_role/templates/migration/create_active_admin_managed_resources.rb +13 -0
- data/lib/generators/active_admin_role/templates/migration/create_active_admin_permissions.rb +13 -0
- data/lib/generators/active_admin_role/templates/model/ability.rb +17 -0
- data/tasks/test.rake +10 -0
- metadata +110 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 19b1f254b92b8095871aeed483aa900a0d946976
|
4
|
+
data.tar.gz: c316f53a4ae5e9bdd97713b251c56e2c323ad3f9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3f620d97e1248ccd49aa487376652bd8f85ad186068529a939f942ce4971f38fd47467c5080f6a83d755131eb21cc02b3d822353c3a933de9c55f9b9919c644a
|
7
|
+
data.tar.gz: 181eea908a807136570b767cde786d0df6805148e1861f45d8466455a528d5bc3139a59a011832861c92238aea2af11dff928c7bf90ca1571c610ceaf0b17a17
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
inherit_from:
|
2
|
+
- .ruby-style.yml
|
3
|
+
|
4
|
+
AllCops:
|
5
|
+
Exclude:
|
6
|
+
- "bin/*"
|
7
|
+
- "vendor/**/*"
|
8
|
+
- "gemfiles/**/*"
|
9
|
+
- "spec/rails/**/*"
|
10
|
+
- "lib/generators/active_admin_role/templates/**/*"
|
11
|
+
- Gemfile
|
12
|
+
- Rakefile
|
13
|
+
DisplayCopNames: true
|
14
|
+
TargetRubyVersion: 2.1
|
15
|
+
|
16
|
+
Metrics/LineLength:
|
17
|
+
Enabled: false
|
data/.ruby-style.yml
ADDED
@@ -0,0 +1,242 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- "vendor/**/*"
|
4
|
+
UseCache: false
|
5
|
+
Style/CollectionMethods:
|
6
|
+
Description: Preferred collection methods.
|
7
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#map-find-select-reduce-size
|
8
|
+
Enabled: true
|
9
|
+
PreferredMethods:
|
10
|
+
collect: map
|
11
|
+
collect!: map!
|
12
|
+
find: detect
|
13
|
+
find_all: select
|
14
|
+
reduce: inject
|
15
|
+
Style/DotPosition:
|
16
|
+
Description: Checks the position of the dot in multi-line method calls.
|
17
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#consistent-multi-line-chains
|
18
|
+
Enabled: true
|
19
|
+
EnforcedStyle: trailing
|
20
|
+
SupportedStyles:
|
21
|
+
- leading
|
22
|
+
- trailing
|
23
|
+
Style/FileName:
|
24
|
+
Description: Use snake_case for source file names.
|
25
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#snake-case-files
|
26
|
+
Enabled: false
|
27
|
+
Exclude: []
|
28
|
+
Style/GuardClause:
|
29
|
+
Description: Check for conditionals that can be replaced with guard clauses
|
30
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-nested-conditionals
|
31
|
+
Enabled: false
|
32
|
+
MinBodyLength: 1
|
33
|
+
Style/IfUnlessModifier:
|
34
|
+
Description: Favor modifier if/unless usage when you have a single-line body.
|
35
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#if-as-a-modifier
|
36
|
+
Enabled: false
|
37
|
+
MaxLineLength: 80
|
38
|
+
Style/OptionHash:
|
39
|
+
Description: Don't use option hashes when you can use keyword arguments.
|
40
|
+
Enabled: false
|
41
|
+
Style/PercentLiteralDelimiters:
|
42
|
+
Description: Use `%`-literal delimiters consistently
|
43
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#percent-literal-braces
|
44
|
+
Enabled: false
|
45
|
+
PreferredDelimiters:
|
46
|
+
"%": "()"
|
47
|
+
"%i": "()"
|
48
|
+
"%q": "()"
|
49
|
+
"%Q": "()"
|
50
|
+
"%r": "{}"
|
51
|
+
"%s": "()"
|
52
|
+
"%w": "()"
|
53
|
+
"%W": "()"
|
54
|
+
"%x": "()"
|
55
|
+
Style/PredicateName:
|
56
|
+
Description: Check the names of predicate methods.
|
57
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#bool-methods-qmark
|
58
|
+
Enabled: true
|
59
|
+
NamePrefix:
|
60
|
+
- is_
|
61
|
+
- has_
|
62
|
+
- have_
|
63
|
+
NamePrefixBlacklist:
|
64
|
+
- is_
|
65
|
+
Exclude:
|
66
|
+
- spec/**/*
|
67
|
+
Style/RaiseArgs:
|
68
|
+
Description: Checks the arguments passed to raise/fail.
|
69
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#exception-class-messages
|
70
|
+
Enabled: false
|
71
|
+
EnforcedStyle: exploded
|
72
|
+
SupportedStyles:
|
73
|
+
- compact
|
74
|
+
- exploded
|
75
|
+
Style/SignalException:
|
76
|
+
Description: Checks for proper usage of fail and raise.
|
77
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#fail-method
|
78
|
+
Enabled: false
|
79
|
+
EnforcedStyle: semantic
|
80
|
+
SupportedStyles:
|
81
|
+
- only_raise
|
82
|
+
- only_fail
|
83
|
+
- semantic
|
84
|
+
Style/SingleLineBlockParams:
|
85
|
+
Description: Enforces the names of some block params.
|
86
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#reduce-blocks
|
87
|
+
Enabled: false
|
88
|
+
Methods:
|
89
|
+
- reduce:
|
90
|
+
- a
|
91
|
+
- e
|
92
|
+
- inject:
|
93
|
+
- a
|
94
|
+
- e
|
95
|
+
Style/SingleLineMethods:
|
96
|
+
Description: Avoid single-line methods.
|
97
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-single-line-methods
|
98
|
+
Enabled: false
|
99
|
+
AllowIfMethodIsEmpty: true
|
100
|
+
Style/StringLiterals:
|
101
|
+
Description: Checks if uses of quotes match the configured preference.
|
102
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#consistent-string-literals
|
103
|
+
Enabled: true
|
104
|
+
EnforcedStyle: double_quotes
|
105
|
+
SupportedStyles:
|
106
|
+
- single_quotes
|
107
|
+
- double_quotes
|
108
|
+
Style/StringLiteralsInInterpolation:
|
109
|
+
Description: Checks if uses of quotes inside expressions in interpolated strings
|
110
|
+
match the configured preference.
|
111
|
+
Enabled: true
|
112
|
+
EnforcedStyle: single_quotes
|
113
|
+
SupportedStyles:
|
114
|
+
- single_quotes
|
115
|
+
- double_quotes
|
116
|
+
Style/TrailingCommaInArguments:
|
117
|
+
Description: 'Checks for trailing comma in argument lists.'
|
118
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas'
|
119
|
+
Enabled: false
|
120
|
+
EnforcedStyleForMultiline: no_comma
|
121
|
+
SupportedStyles:
|
122
|
+
- comma
|
123
|
+
- consistent_comma
|
124
|
+
- no_comma
|
125
|
+
Style/TrailingCommaInLiteral:
|
126
|
+
Description: 'Checks for trailing comma in array and hash literals.'
|
127
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas'
|
128
|
+
Enabled: false
|
129
|
+
EnforcedStyleForMultiline: no_comma
|
130
|
+
SupportedStyles:
|
131
|
+
- comma
|
132
|
+
- consistent_comma
|
133
|
+
- no_comma
|
134
|
+
Metrics/AbcSize:
|
135
|
+
Description: A calculated magnitude based on number of assignments, branches, and
|
136
|
+
conditions.
|
137
|
+
Enabled: false
|
138
|
+
Max: 15
|
139
|
+
Metrics/ClassLength:
|
140
|
+
Description: Avoid classes longer than 100 lines of code.
|
141
|
+
Enabled: false
|
142
|
+
CountComments: false
|
143
|
+
Max: 100
|
144
|
+
Metrics/ModuleLength:
|
145
|
+
CountComments: false
|
146
|
+
Max: 100
|
147
|
+
Description: Avoid modules longer than 100 lines of code.
|
148
|
+
Enabled: false
|
149
|
+
Metrics/CyclomaticComplexity:
|
150
|
+
Description: A complexity metric that is strongly correlated to the number of test
|
151
|
+
cases needed to validate a method.
|
152
|
+
Enabled: false
|
153
|
+
Max: 6
|
154
|
+
Metrics/MethodLength:
|
155
|
+
Description: Avoid methods longer than 10 lines of code.
|
156
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#short-methods
|
157
|
+
Enabled: false
|
158
|
+
CountComments: false
|
159
|
+
Max: 10
|
160
|
+
Metrics/ParameterLists:
|
161
|
+
Description: Avoid parameter lists longer than three or four parameters.
|
162
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#too-many-params
|
163
|
+
Enabled: false
|
164
|
+
Max: 5
|
165
|
+
CountKeywordArgs: true
|
166
|
+
Metrics/PerceivedComplexity:
|
167
|
+
Description: A complexity metric geared towards measuring complexity for a human
|
168
|
+
reader.
|
169
|
+
Enabled: false
|
170
|
+
Max: 7
|
171
|
+
Lint/AssignmentInCondition:
|
172
|
+
Description: Don't use assignment in conditions.
|
173
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#safe-assignment-in-condition
|
174
|
+
Enabled: false
|
175
|
+
AllowSafeAssignment: true
|
176
|
+
Style/InlineComment:
|
177
|
+
Description: Avoid inline comments.
|
178
|
+
Enabled: false
|
179
|
+
Style/AccessorMethodName:
|
180
|
+
Description: Check the naming of accessor methods for get_/set_.
|
181
|
+
Enabled: false
|
182
|
+
Style/Alias:
|
183
|
+
Description: Use alias_method instead of alias.
|
184
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#alias-method
|
185
|
+
Enabled: false
|
186
|
+
Style/Documentation:
|
187
|
+
Description: Document classes and non-namespace modules.
|
188
|
+
Enabled: false
|
189
|
+
Style/DoubleNegation:
|
190
|
+
Description: Checks for uses of double negation (!!).
|
191
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-bang-bang
|
192
|
+
Enabled: false
|
193
|
+
Style/EachWithObject:
|
194
|
+
Description: Prefer `each_with_object` over `inject` or `reduce`.
|
195
|
+
Enabled: false
|
196
|
+
Style/EmptyLiteral:
|
197
|
+
Description: Prefer literals to Array.new/Hash.new/String.new.
|
198
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#literal-array-hash
|
199
|
+
Enabled: false
|
200
|
+
Style/ModuleFunction:
|
201
|
+
Description: Checks for usage of `extend self` in modules.
|
202
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#module-function
|
203
|
+
Enabled: false
|
204
|
+
Style/OneLineConditional:
|
205
|
+
Description: Favor the ternary operator(?:) over if/then/else/end constructs.
|
206
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#ternary-operator
|
207
|
+
Enabled: false
|
208
|
+
Style/PerlBackrefs:
|
209
|
+
Description: Avoid Perl-style regex back references.
|
210
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-perl-regexp-last-matchers
|
211
|
+
Enabled: false
|
212
|
+
Style/Send:
|
213
|
+
Description: Prefer `Object#__send__` or `Object#public_send` to `send`, as `send`
|
214
|
+
may overlap with existing methods.
|
215
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#prefer-public-send
|
216
|
+
Enabled: false
|
217
|
+
Style/SpecialGlobalVars:
|
218
|
+
Description: Avoid Perl-style global variables.
|
219
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-cryptic-perlisms
|
220
|
+
Enabled: false
|
221
|
+
Style/VariableInterpolation:
|
222
|
+
Description: Don't interpolate global, instance and class variables directly in
|
223
|
+
strings.
|
224
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#curlies-interpolate
|
225
|
+
Enabled: false
|
226
|
+
Style/WhenThen:
|
227
|
+
Description: Use when x then ... for one-line cases.
|
228
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#one-line-cases
|
229
|
+
Enabled: false
|
230
|
+
Lint/EachWithObjectArgument:
|
231
|
+
Description: Check for immutable argument given to each_with_object.
|
232
|
+
Enabled: true
|
233
|
+
Lint/HandleExceptions:
|
234
|
+
Description: Don't suppress exception.
|
235
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#dont-hide-exceptions
|
236
|
+
Enabled: false
|
237
|
+
Lint/LiteralInCondition:
|
238
|
+
Description: Checks of literals used in conditions.
|
239
|
+
Enabled: false
|
240
|
+
Lint/LiteralInInterpolation:
|
241
|
+
Description: Checks for literals used in interpolation.
|
242
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/Appraisals
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
appraise 'rails42' do
|
2
|
+
gem 'rails', '~> 4.2.0'
|
3
|
+
gem 'devise', '~> 4.2.0'
|
4
|
+
gem 'activeadmin', '1.0.0.pre4'
|
5
|
+
end
|
6
|
+
|
7
|
+
appraise 'rails50' do
|
8
|
+
gem 'rails', '~> 5.0.0'
|
9
|
+
gem 'devise', '~> 4.2.0'
|
10
|
+
gem 'inherited_resources', github: 'activeadmin/inherited_resources'
|
11
|
+
gem 'activeadmin', github: 'activeadmin/activeadmin'
|
12
|
+
end
|
data/Gemfile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
gem "rails", "~> 4.2.0"
|
6
|
+
gem "activeadmin", "1.0.0.pre4"
|
7
|
+
gem "devise", "~> 4.2.0"
|
8
|
+
gem "pry"
|
9
|
+
gem "appraisal"
|
10
|
+
|
11
|
+
group :development do
|
12
|
+
gem "bundler", "~> 1.13.0"
|
13
|
+
gem "rake", "~> 10.0"
|
14
|
+
gem "rubocop", "~> 0.40.0"
|
15
|
+
end
|
16
|
+
|
17
|
+
group :test do
|
18
|
+
gem "capybara"
|
19
|
+
gem "rspec-rails"
|
20
|
+
gem "database_cleaner"
|
21
|
+
gem "shoulda-matchers"
|
22
|
+
gem "sqlite3", platforms: :mri
|
23
|
+
gem "poltergeist"
|
24
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Yoshiyuki Hirano
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# ActiveAdminRole
|
2
|
+
|
3
|
+
Role based authorization with CanCanCan for Active Admin
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'active_admin_role'
|
11
|
+
```
|
12
|
+
|
13
|
+
And run `bundle`
|
14
|
+
|
15
|
+
## Dependencies
|
16
|
+
|
17
|
+
- rails (>= 4.2)
|
18
|
+
- activeadmin (>= 1.0.0.pre4)
|
19
|
+
- cancancan (>= 1.15.0)
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
1. Run this command after `rails generate active_admin:install`:
|
24
|
+
|
25
|
+
```sh
|
26
|
+
$ bin/rails generate active_admin_role:install
|
27
|
+
create config/initializers/active_admin_role.rb
|
28
|
+
insert app/models/admin_user.rb
|
29
|
+
create db/migrate/20161128090641_add_role_to_admin_users.rb
|
30
|
+
create db/migrate/20161128090642_create_active_admin_managed_resources.rb
|
31
|
+
create db/migrate/20161128090643_create_active_admin_permissions.rb
|
32
|
+
create app/models/ability.rb
|
33
|
+
gsub config/initializers/active_admin.rb
|
34
|
+
create app/admin/permission.rb
|
35
|
+
insert app/admin/admin_user.rb
|
36
|
+
|
37
|
+
$ bin/rails db:migrate
|
38
|
+
```
|
39
|
+
|
40
|
+
2. You have to login as **admin** after migration.
|
41
|
+
|
42
|
+
3. You have to **Reload** permissions.
|
43
|
+
|
44
|
+
![](https://cloud.githubusercontent.com/assets/15371677/20662507/015c877c-b597-11e6-82dc-bf80dac8c6e9.png)
|
45
|
+
|
46
|
+
4. Edit permissions however you like.
|
47
|
+
|
48
|
+
![](https://cloud.githubusercontent.com/assets/15371677/20662765/2a8be9c0-b598-11e6-88c5-b9b7c018c876.png)
|
49
|
+
|
50
|
+
5. Of course, you can edit AdminUser's roles.
|
51
|
+
|
52
|
+
![](https://cloud.githubusercontent.com/assets/15371677/20662882/ba2f9f18-b598-11e6-8a4b-ed7c6d5b1246.png)
|
53
|
+
|
54
|
+
## Configuration
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
ActiveAdminRole.configure do |config|
|
58
|
+
# [Required:Hash]
|
59
|
+
# == Role | default: { guest: 0, support: 1, staff: 2, manager: 3, admin: 99 }
|
60
|
+
config.roles = { guest: 0, support: 1, staff: 2, manager: 3, admin: 99 }
|
61
|
+
|
62
|
+
# [Optional:Array]
|
63
|
+
# == Special roles which don't need to manage on database
|
64
|
+
config.super_user_roles = [:admin]
|
65
|
+
config.guest_user_roles = [:guest]
|
66
|
+
|
67
|
+
# [Optional:String]
|
68
|
+
# == User class name | default: 'AdminUser'
|
69
|
+
config.user_class_name = "AdminUser"
|
70
|
+
|
71
|
+
# [Optional:Symbol]
|
72
|
+
# == Default permission | default: :cannot
|
73
|
+
config.default_state = :cannot
|
74
|
+
end
|
75
|
+
```
|
76
|
+
|
77
|
+
## License
|
78
|
+
|
79
|
+
[MIT License](http://opensource.org/licenses/MIT)
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "lib", "active_admin_role", "version")
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
gem.name = "active_admin_role"
|
5
|
+
gem.version = ActiveAdminRole::VERSION
|
6
|
+
gem.authors = ["Yoshiyuki Hirano"]
|
7
|
+
gem.email = ["yhirano@me.com"]
|
8
|
+
|
9
|
+
gem.summary = "Role based authorization with CanCanCan for Active Admin"
|
10
|
+
gem.description = gem.summary
|
11
|
+
gem.homepage = "https://github.com/yhirano55/active_admin_role"
|
12
|
+
gem.license = "MIT"
|
13
|
+
|
14
|
+
gem.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
|
17
|
+
gem.required_ruby_version = ">= 2.1.0"
|
18
|
+
|
19
|
+
gem.add_dependency "activeadmin", ">= 1.0.0.pre4"
|
20
|
+
gem.add_dependency "cancancan", ">= 1.15.0"
|
21
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module ActiveAdmin
|
2
|
+
class ManagedResource < ActiveRecord::Base
|
3
|
+
self.table_name = :active_admin_managed_resources
|
4
|
+
|
5
|
+
has_many :permissions, dependent: :destroy
|
6
|
+
|
7
|
+
with_options presence: true do
|
8
|
+
validates :class_name
|
9
|
+
validates :action
|
10
|
+
end
|
11
|
+
|
12
|
+
def const
|
13
|
+
@_const ||= class_name.try(:safe_constantize)
|
14
|
+
end
|
15
|
+
|
16
|
+
def active?
|
17
|
+
!const.nil?
|
18
|
+
end
|
19
|
+
|
20
|
+
def for_active_admin_page?
|
21
|
+
class_name == "ActiveAdmin::Page"
|
22
|
+
end
|
23
|
+
|
24
|
+
class << self
|
25
|
+
def reload
|
26
|
+
ActiveRecord::Base.transaction do
|
27
|
+
clear_cache
|
28
|
+
update_managed_resources
|
29
|
+
cleanup_managed_resources
|
30
|
+
update_permissions
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def update_managed_resources
|
37
|
+
manageable_resources.each(&method(:find_or_create_by!))
|
38
|
+
end
|
39
|
+
|
40
|
+
def cleanup_managed_resources
|
41
|
+
(persisted_resources - manageable_resources).each do |condition|
|
42
|
+
where(condition).destroy_all
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def update_permissions
|
47
|
+
::ActiveAdmin::Permission.clear_cache
|
48
|
+
::ActiveAdmin::Permission.update_all_from_managed_resources(all)
|
49
|
+
end
|
50
|
+
|
51
|
+
def persisted_resources
|
52
|
+
all.map(&:attributes).map { |attribute| attribute.slice(*%w(class_name action name)).symbolize_keys }
|
53
|
+
end
|
54
|
+
|
55
|
+
def manageable_resources
|
56
|
+
@_manageable_resources ||= ::ActiveAdminRole::ManageableResource.new.call
|
57
|
+
end
|
58
|
+
|
59
|
+
def clear_cache
|
60
|
+
@_manageable_resources = nil
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module ActiveAdmin
|
2
|
+
class Permission < ActiveRecord::Base
|
3
|
+
self.table_name = :active_admin_permissions
|
4
|
+
|
5
|
+
role_based_authorizable
|
6
|
+
|
7
|
+
enum state: { cannot: 0, can: 1 }
|
8
|
+
|
9
|
+
belongs_to :managed_resource
|
10
|
+
|
11
|
+
delegate :class_name, :action, :name, :const, :active?, :for_active_admin_page?, to: :managed_resource
|
12
|
+
delegate :clear_cache, to: :class
|
13
|
+
|
14
|
+
after_update :clear_cache
|
15
|
+
|
16
|
+
with_options presence: true do
|
17
|
+
validates :managed_resource_id
|
18
|
+
validates :role
|
19
|
+
validates :state
|
20
|
+
end
|
21
|
+
|
22
|
+
validates :managed_resource_id, uniqueness: { scope: [:role] }
|
23
|
+
|
24
|
+
def to_condition
|
25
|
+
[].tap do |cond|
|
26
|
+
cond << state
|
27
|
+
cond << action.to_sym
|
28
|
+
cond << const
|
29
|
+
cond << { name: name } if for_active_admin_page?
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class << self
|
34
|
+
def update_all_from_managed_resources(managed_resources)
|
35
|
+
managed_resources.each do |managed_resource|
|
36
|
+
manageable_roles.values.each do |value_of_role|
|
37
|
+
find_or_create_by!(managed_resource_id: managed_resource.id, role: value_of_role) do |permission|
|
38
|
+
permission.state = default_state
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def indexed_cache
|
45
|
+
@_indexed_cache ||= eager_load(:managed_resource).all.group_by(&:role)
|
46
|
+
end
|
47
|
+
|
48
|
+
def clear_cache
|
49
|
+
@_indexed_cache = nil
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def default_state
|
55
|
+
@_default_state ||= ::ActiveAdminRole.config.default_state
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
en:
|
2
|
+
activerecord:
|
3
|
+
models:
|
4
|
+
active_admin/managed_resource: Managed Resource
|
5
|
+
active_admin/permission: Permission
|
6
|
+
attributes:
|
7
|
+
base: &base
|
8
|
+
id: ID
|
9
|
+
created_at: Created at
|
10
|
+
updated_at: Updated at
|
11
|
+
managed_resource: &managed_resource
|
12
|
+
class_name: Class Name
|
13
|
+
action: Action
|
14
|
+
name: Name
|
15
|
+
active_admin/managed_resource:
|
16
|
+
<<: *base
|
17
|
+
<<: *managed_resource
|
18
|
+
active_admin/permission:
|
19
|
+
<<: *base
|
20
|
+
<<: *managed_resource
|
21
|
+
managed_resource_id: Managed Resource
|
22
|
+
role: Role
|
23
|
+
state: State
|
24
|
+
|
25
|
+
views:
|
26
|
+
admin_user:
|
27
|
+
notice:
|
28
|
+
assigned: "Selected records have assigned as %{role}"
|
29
|
+
permission:
|
30
|
+
notice:
|
31
|
+
state_changed: "Selected records have changed to %{state}"
|
32
|
+
reloaded: "Reloaded"
|
33
|
+
action_item:
|
34
|
+
reload: "Reload"
|
35
|
+
|
36
|
+
active_admin:
|
37
|
+
batch_actions:
|
38
|
+
labels:
|
39
|
+
can: "Enable"
|
40
|
+
cannot: "Disable"
|
@@ -0,0 +1,37 @@
|
|
1
|
+
ja:
|
2
|
+
activerecord:
|
3
|
+
models:
|
4
|
+
active_admin/managed_resource: リソース
|
5
|
+
active_admin/permission: 権限
|
6
|
+
attributes:
|
7
|
+
base: &base
|
8
|
+
id: ID
|
9
|
+
created_at: 作成日時
|
10
|
+
updated_at: 更新日時
|
11
|
+
managed_resource: &managed_resource
|
12
|
+
class_name: クラス
|
13
|
+
action: 操作
|
14
|
+
name: ページ名称
|
15
|
+
active_admin/managed_resource:
|
16
|
+
<<: *base
|
17
|
+
<<: *managed_resource
|
18
|
+
active_admin/permission:
|
19
|
+
<<: *base
|
20
|
+
<<: *managed_resource
|
21
|
+
managed_resource_id: リソース
|
22
|
+
role: 役割
|
23
|
+
state: 状態
|
24
|
+
|
25
|
+
views:
|
26
|
+
permission:
|
27
|
+
notice:
|
28
|
+
state_changed: "選択した行を%{state}に変更しました"
|
29
|
+
reloaded: "再読込しました"
|
30
|
+
action_item:
|
31
|
+
reload: "再読込"
|
32
|
+
|
33
|
+
active_admin:
|
34
|
+
batch_actions:
|
35
|
+
labels:
|
36
|
+
can: "有効にする"
|
37
|
+
cannot: "無効にする"
|