i_am_i_can 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +13 -0
  5. data/CHANGELOG.md +17 -0
  6. data/CODE_OF_CONDUCT.md +74 -0
  7. data/Gemfile +6 -0
  8. data/Gemfile.lock +116 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +442 -0
  11. data/Rakefile +6 -0
  12. data/bin/console +14 -0
  13. data/bin/setup +8 -0
  14. data/i_am_i_can.gemspec +36 -0
  15. data/lib/generators/i_am_i_can/setup_generator.rb +56 -0
  16. data/lib/generators/i_am_i_can/templates/migrations/add_to_subject.erb +5 -0
  17. data/lib/generators/i_am_i_can/templates/migrations/permission.erb +15 -0
  18. data/lib/generators/i_am_i_can/templates/migrations/role.erb +13 -0
  19. data/lib/generators/i_am_i_can/templates/migrations/role_group.erb +14 -0
  20. data/lib/generators/i_am_i_can/templates/models/permission.erb +11 -0
  21. data/lib/generators/i_am_i_can/templates/models/role.erb +10 -0
  22. data/lib/generators/i_am_i_can/templates/models/role_group.erb +11 -0
  23. data/lib/i_am_i_can/config.rb +14 -0
  24. data/lib/i_am_i_can/has_an_array_of.rb +75 -0
  25. data/lib/i_am_i_can/permission/assignment.rb +90 -0
  26. data/lib/i_am_i_can/permission/definition.rb +61 -0
  27. data/lib/i_am_i_can/permission/helpers.rb +46 -0
  28. data/lib/i_am_i_can/permission/p_array.rb +22 -0
  29. data/lib/i_am_i_can/permission.rb +68 -0
  30. data/lib/i_am_i_can/role/assignment.rb +76 -0
  31. data/lib/i_am_i_can/role/definition.rb +100 -0
  32. data/lib/i_am_i_can/role/helpers.rb +28 -0
  33. data/lib/i_am_i_can/subject/permission_querying.rb +69 -0
  34. data/lib/i_am_i_can/subject/role_querying.rb +66 -0
  35. data/lib/i_am_i_can/version.rb +3 -0
  36. data/lib/i_am_i_can.rb +61 -0
  37. metadata +220 -0
@@ -0,0 +1,66 @@
1
+ module IAmICan
2
+ module Subject
3
+ module RoleQuerying
4
+
5
+ # === Role Querying ===
6
+
7
+ def is? role
8
+ role.to_sym.in?(local_role_names) || role.to_sym.in?(stored_role_names)
9
+ end
10
+
11
+ alias is_role? is?
12
+ alias has_role? is?
13
+
14
+ def isnt? role
15
+ !is? role
16
+ end
17
+
18
+ def is! role
19
+ raise VerificationFailed if isnt? role
20
+ true
21
+ end
22
+
23
+ alias is_role! is!
24
+ alias has_role! is!
25
+
26
+ def is_one_of? *roles
27
+ roles.each { |role| return true if is? role } && false
28
+ end
29
+
30
+ alias is_one_of_roles? is_one_of?
31
+
32
+ def is_one_of! *roles
33
+ raise VerificationFailed unless is_one_of? *roles
34
+ end
35
+
36
+ alias is_one_of_roles! is_one_of!
37
+
38
+ def is_every? *roles
39
+ roles.each { |role| return false if isnt? role } && true
40
+ end
41
+
42
+ alias is_every_role_in? is_every?
43
+
44
+ def is_every! *roles
45
+ roles.each { |role| is! role } && true
46
+ end
47
+
48
+ alias is_every_role_in! is_every!
49
+
50
+ # === Group Querying ===
51
+
52
+ def is_in_role_group? name
53
+ group_members = self.class.members_of_role_group(name)
54
+ (roles & group_members).present?
55
+ end
56
+
57
+ alias in_role_group? is_in_role_group?
58
+
59
+ def is_in_one_of? *group_names
60
+ group_names.each { |name| return true if is_in_role_group? name } && false
61
+ end
62
+
63
+ alias in_one_of? is_in_one_of?
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ module IAmICan
2
+ VERSION = '2.1.0'
3
+ end
data/lib/i_am_i_can.rb ADDED
@@ -0,0 +1,61 @@
1
+ require 'active_record'
2
+ require 'active_support/core_ext/object/inclusion'
3
+ require 'active_support/core_ext/hash/deep_merge'
4
+
5
+ require 'i_am_i_can/version'
6
+ require 'i_am_i_can/has_an_array_of'
7
+ require 'i_am_i_can/config'
8
+ require 'i_am_i_can/role/definition'
9
+ require 'i_am_i_can/role/assignment'
10
+ require 'i_am_i_can/permission'
11
+ require 'i_am_i_can/permission/definition'
12
+ require 'i_am_i_can/permission/assignment'
13
+ require 'i_am_i_can/subject/role_querying'
14
+ require 'i_am_i_can/subject/permission_querying'
15
+
16
+ module IAmICan
17
+ include HasAnArrayOf
18
+
19
+ def act_as_i_am_i_can role_model: "#{name}Role".constantize,
20
+ role_group_model: ("#{name}RoleGroup".constantize rescue nil),
21
+ permission_model: "#{name}Permission".constantize, **options
22
+ cattr_accessor :ii_config do
23
+ IAmICan::Config.new(
24
+ role_model: role_model,
25
+ role_group_model: role_group_model,
26
+ permission_model: permission_model,
27
+ subject_model: self, **options
28
+ )
29
+ end
30
+ role_model.cattr_accessor(:config) { ii_config }
31
+ role_group_model&.cattr_accessor(:config) { ii_config }
32
+
33
+ extend IAmICan::Role::Definition
34
+ include IAmICan::Role::Assignment
35
+ include IAmICan::Subject::RoleQuerying
36
+
37
+ permission_model.extend IAmICan::Permission
38
+ role_model.extend IAmICan::Permission::Definition
39
+ role_model.include IAmICan::Permission::Assignment
40
+ role_group_model&.extend IAmICan::Permission::Definition
41
+ role_group_model&.include IAmICan::Permission::Assignment
42
+ self.include IAmICan::Subject::PermissionQuerying
43
+
44
+ opts = {
45
+ attrs: { name: :to_sym },
46
+ located_by: :name,
47
+ prefix: :stored,
48
+ cache_expires_in: options[:cache_expires_in] || 15.minutes
49
+ }
50
+ self.has_an_array_of :roles, model: role_model.name, for_related_name: name.underscore, **opts
51
+ role_group_model&.has_an_array_of :members, model: role_model.name, for_related_name: 'role_group', **opts.except(:prefix)
52
+ role_model.has_an_array_of :permissions, model: permission_model.name, for_related_name: 'role', **opts.except(:attrs, :located_by)
53
+ role_group_model&.has_an_array_of :permissions, model: permission_model.name, for_related_name: 'role_group', **opts.except(:attrs, :located_by)
54
+ end
55
+
56
+ class Error < StandardError; end
57
+ class VerificationFailed < Error; end
58
+ class InsufficientPermission < Error; end
59
+ end
60
+
61
+ ActiveRecord::Base.extend IAmICan
metadata ADDED
@@ -0,0 +1,220 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i_am_i_can
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.1.0
5
+ platform: ruby
6
+ authors:
7
+ - zhandao
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-10-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: database_cleaner
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pg
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: activerecord
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: activesupport
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: railties
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: Concise and Natural DSL for `Subject - Role(Role Group) - Permission`
154
+ Management.
155
+ email:
156
+ - x@skippingcat.com
157
+ executables: []
158
+ extensions: []
159
+ extra_rdoc_files: []
160
+ files:
161
+ - ".gitignore"
162
+ - ".rspec"
163
+ - ".travis.yml"
164
+ - CHANGELOG.md
165
+ - CODE_OF_CONDUCT.md
166
+ - Gemfile
167
+ - Gemfile.lock
168
+ - LICENSE.txt
169
+ - README.md
170
+ - Rakefile
171
+ - bin/console
172
+ - bin/setup
173
+ - i_am_i_can.gemspec
174
+ - lib/generators/i_am_i_can/setup_generator.rb
175
+ - lib/generators/i_am_i_can/templates/migrations/add_to_subject.erb
176
+ - lib/generators/i_am_i_can/templates/migrations/permission.erb
177
+ - lib/generators/i_am_i_can/templates/migrations/role.erb
178
+ - lib/generators/i_am_i_can/templates/migrations/role_group.erb
179
+ - lib/generators/i_am_i_can/templates/models/permission.erb
180
+ - lib/generators/i_am_i_can/templates/models/role.erb
181
+ - lib/generators/i_am_i_can/templates/models/role_group.erb
182
+ - lib/i_am_i_can.rb
183
+ - lib/i_am_i_can/config.rb
184
+ - lib/i_am_i_can/has_an_array_of.rb
185
+ - lib/i_am_i_can/permission.rb
186
+ - lib/i_am_i_can/permission/assignment.rb
187
+ - lib/i_am_i_can/permission/definition.rb
188
+ - lib/i_am_i_can/permission/helpers.rb
189
+ - lib/i_am_i_can/permission/p_array.rb
190
+ - lib/i_am_i_can/role/assignment.rb
191
+ - lib/i_am_i_can/role/definition.rb
192
+ - lib/i_am_i_can/role/helpers.rb
193
+ - lib/i_am_i_can/subject/permission_querying.rb
194
+ - lib/i_am_i_can/subject/role_querying.rb
195
+ - lib/i_am_i_can/version.rb
196
+ homepage: https://github.com/zhandao/i_am_i_can
197
+ licenses:
198
+ - MIT
199
+ metadata: {}
200
+ post_install_message:
201
+ rdoc_options: []
202
+ require_paths:
203
+ - lib
204
+ required_ruby_version: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ required_rubygems_version: !ruby/object:Gem::Requirement
210
+ requirements:
211
+ - - ">="
212
+ - !ruby/object:Gem::Version
213
+ version: '0'
214
+ requirements: []
215
+ rubyforge_project:
216
+ rubygems_version: 2.7.3
217
+ signing_key:
218
+ specification_version: 4
219
+ summary: Concise and Natural DSL for `Subject - Role(Role Group) - Permission` Management.
220
+ test_files: []