permission_settings 1.0.2 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -1
- data/CHANGELOG.md +9 -1
- data/README.md +8 -7
- data/lib/permission_settings/configuration.rb +1 -8
- data/lib/permission_settings/patcher.rb +28 -0
- data/lib/permission_settings/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8e3d221f8775059aa809aa4d2f1209859688005a8019da2729b0d9c153ec2cb2
|
4
|
+
data.tar.gz: 5464c88c540fc443cbeb3da665c8d973688274920693f25c555b0bef646e8467
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 82fd496bbd08ff3cd48657cc86cfccade0b187bf51a313265daae5304d1839c7b62b610d9dba676cbc4870ac005c4170d294a85b85ff4105e3bcaad72e0ecca9
|
7
|
+
data.tar.gz: b34b702372daf1b6c2b1cc9386c0e4ac7503833eb316ad8795523b73b6c92814b6047dfb8548a7476549c08f664b3623a62de61ed3085b9bc9ccfb8a86574ee3
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,12 @@
|
|
1
|
-
## [1.0.
|
1
|
+
## [1.0.3] - 2024-02-19
|
2
|
+
|
3
|
+
- Readme update
|
4
|
+
- Better PermissionsDirNotFound error message and handling
|
5
|
+
- Added `permissions` method to get permissions of the calling instance
|
6
|
+
- Added test core coverage
|
7
|
+
|
8
|
+
|
9
|
+
## [1.0.2] - 2024-02-19
|
2
10
|
|
3
11
|
- Commented out `byebug` in the code
|
4
12
|
|
data/README.md
CHANGED
@@ -24,7 +24,7 @@ Generate and run the migration to create the settings table that is used to stor
|
|
24
24
|
|
25
25
|
```shell
|
26
26
|
rails g rails_settings:migration
|
27
|
-
|
27
|
+
rails db:migrate
|
28
28
|
```
|
29
29
|
|
30
30
|
## Usage
|
@@ -36,7 +36,7 @@ You can configure the gem by calling `PermissionSettings.configure` method in an
|
|
36
36
|
```ruby
|
37
37
|
# config/initializers/permission_settings.rb
|
38
38
|
|
39
|
-
PermissionSettings.configure do |
|
39
|
+
PermissionSettings.configure do |config|
|
40
40
|
config.permissions_dir_path = 'config/permissions'
|
41
41
|
config.role_access_method = :role
|
42
42
|
end
|
@@ -98,7 +98,7 @@ end
|
|
98
98
|
|
99
99
|
In order to check permissions of the resource instance you need to pass it as a named argument `resource` to the `#can?` method.
|
100
100
|
|
101
|
-
Take into account that the resource model should have a `role`
|
101
|
+
Take into account that the resource model should have a `role` field or method that returns a role name of the calling instance or you can configure the gem to use another method name by setting `role_access_method` configuration option.
|
102
102
|
|
103
103
|
### Checking permissions
|
104
104
|
|
@@ -111,8 +111,8 @@ user.can?(:read, :notifications, resource: person) # => true
|
|
111
111
|
```
|
112
112
|
|
113
113
|
Method `can?` accepts 2 arguments:
|
114
|
-
* `*permission_keys` - this keys will be used to find permissions in the settings. It can be a string or an array of strings. Required argument
|
115
|
-
* `*resource` - Named argument resource. Instance towards which the permissions will be checked. Required argument
|
114
|
+
* `*permission_keys` - this keys will be used to find permissions in the settings. It can be a string or an array of strings. `Required argument`.
|
115
|
+
* `*resource` - Named argument resource. Instance towards which the permissions will be checked. `Required argument`.
|
116
116
|
|
117
117
|
### Accessing permissions
|
118
118
|
|
@@ -120,7 +120,7 @@ You can also access permissions of the resource or calling instance by calling `
|
|
120
120
|
|
121
121
|
```ruby
|
122
122
|
person = Person.last # => #<Person id: 2, name: "Jane", role: "client">
|
123
|
-
person.
|
123
|
+
person.permissions[:admin][:notifications][:read] # => false
|
124
124
|
```
|
125
125
|
|
126
126
|
More about settings you can read in [Rails Settings](https://github.com/ledermann/rails-settings) gem documentation.
|
@@ -167,7 +167,8 @@ custom_permissions = {
|
|
167
167
|
admin = User.first # => #<User id: 1, name: "John", role: "admin">
|
168
168
|
person = Person.last # => #<User id: 2, name: "Jane", role: "client">
|
169
169
|
|
170
|
-
|
170
|
+
policy_scope = PermissionSettings.configuration.scope_name(User)
|
171
|
+
person.settings(policy_scope).update(custom_permissions)
|
171
172
|
|
172
173
|
admin.can?(:read, :notifications, resource: person) # => false
|
173
174
|
|
@@ -9,20 +9,13 @@ module PermissionSettings
|
|
9
9
|
DEFAULT_PERMISSION_FILE_PATH = 'config/permissions'
|
10
10
|
DEFAULT_ROLE_ACCESS_METHOD = :role
|
11
11
|
|
12
|
-
attr_accessor :role_access_method
|
13
|
-
attr_reader :permissions_dir_path
|
12
|
+
attr_accessor :role_access_method, :permissions_dir_path
|
14
13
|
|
15
14
|
def initialize
|
16
15
|
@permissions_dir_path = DEFAULT_PERMISSION_FILE_PATH
|
17
16
|
@role_access_method = DEFAULT_ROLE_ACCESS_METHOD
|
18
17
|
end
|
19
18
|
|
20
|
-
def permissions_dir_path=(path)
|
21
|
-
raise PermissionsDirNotFound, 'Permissions directory not found' unless Dir.exist?(path)
|
22
|
-
|
23
|
-
@permissions_dir_path = path
|
24
|
-
end
|
25
|
-
|
26
19
|
def scope_name(klass)
|
27
20
|
[klass.name.underscore, 'permissions'].join('_').to_sym
|
28
21
|
end
|
@@ -1,6 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
# require 'byebug'
|
4
|
+
require 'active_record'
|
5
|
+
require 'rails-settings'
|
4
6
|
require_relative 'verify_instance'
|
5
7
|
|
6
8
|
module PermissionSettings
|
@@ -17,12 +19,25 @@ module PermissionSettings
|
|
17
19
|
end
|
18
20
|
|
19
21
|
def call
|
22
|
+
check_permissions_dir
|
20
23
|
setup_settings_interface
|
21
24
|
setup_instance_verification
|
25
|
+
setup_permissions_method
|
22
26
|
end
|
23
27
|
|
24
28
|
private
|
25
29
|
|
30
|
+
def check_permissions_dir
|
31
|
+
path = PermissionSettings.configuration.permissions_dir_path
|
32
|
+
return if Dir.exist?(path)
|
33
|
+
|
34
|
+
raise PermissionSettings::Configuration::PermissionsDirNotFound, dir_missing_message(path)
|
35
|
+
end
|
36
|
+
|
37
|
+
def dir_missing_message(path)
|
38
|
+
"Permissions config directory not found. Please create a directory at #{path} and add permission files there."
|
39
|
+
end
|
40
|
+
|
26
41
|
def setup_settings_interface
|
27
42
|
klass.class_eval do
|
28
43
|
has_settings do |s|
|
@@ -44,5 +59,18 @@ module PermissionSettings
|
|
44
59
|
end
|
45
60
|
end
|
46
61
|
end
|
62
|
+
|
63
|
+
def setup_permissions_method
|
64
|
+
klass.class_eval do
|
65
|
+
define_method(:permissions) do
|
66
|
+
scope = PermissionSettings.configuration.scope_name(self.class)
|
67
|
+
settings(scope).then do |permissions|
|
68
|
+
return ActiveSupport::HashWithIndifferentAccess.new(permissions.value) if permissions.value.present?
|
69
|
+
|
70
|
+
ActiveSupport::HashWithIndifferentAccess.new(default_settings[scope])
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
47
75
|
end
|
48
76
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: permission_settings
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Misha Push
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-02-
|
11
|
+
date: 2024-02-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|