action_sentinel 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fa3b8401bf44885aec2145a79689ff7f38b02dac73c69922fbc22c0fdbd7b307
4
- data.tar.gz: 6e4b635a86277548e76d18b6f4f73e5aa73619f5941923e2cfd5433acf7e526f
3
+ metadata.gz: 204f3d0ef817c0d6cc2d1a1b35885452e3b7d5db74e9568ddd95ef610da7bebd
4
+ data.tar.gz: 383b26ad27efd68c7a1e463f2238f83ce47af9d229061d2840fbf66ca5aa1664
5
5
  SHA512:
6
- metadata.gz: b19c9599910bce3915839ee7342090bfc66b5dbe46b8cbb11f6e96da2c55d0efed6a64e2bdae8b20d99b7e256f2bae3a2bf27aeb4b55b4315294a7a98f95d974
7
- data.tar.gz: 22dd485c1ba4af40cdd4cd6076a98e267515510c6eb63f3c13e68c4567fce329d59dc940679170d644ef71bc4d3574a54925390c396aee1e258efbf1fc6051c6
6
+ metadata.gz: 505df6f90e87e78dbbdc7f0aa21ec54302a17fe8938fcbe59f2e899c02dc27ca13f873ddc72e540bf9a59e0ba6b29ac1c588bdde73af271264227a4ccddfe6ec
7
+ data.tar.gz: c39df3332e792c823e04dbd81e0667e84a877d3c54c076cc698a5f3484a66b21de6b3f28fd781ee7afbb54982dad2af82333d56d72d45345ff17874573aba909
data/CHANGELOG.md CHANGED
@@ -3,3 +3,8 @@
3
3
  ## [0.1.0] - 2023-12-04
4
4
 
5
5
  - Initial release
6
+
7
+ ## [0.2.0] - 2023-12-27
8
+
9
+ - Changed `controller_name` attribute to `controller_path` in `AccessPermission` model
10
+ - Allow to create actions permissions to scoped controllers
data/README.md CHANGED
@@ -40,7 +40,7 @@ The generator will create the AccessPermission model and a migration, and insert
40
40
  class AccessPermission < ApplicationRecord
41
41
  belongs_to :user
42
42
 
43
- validates :controller_name, uniqueness: { scope: :group_id }
43
+ validates :controller_path, uniqueness: { scope: :user_id }
44
44
  end
45
45
 
46
46
  # User model with permissions added
@@ -79,9 +79,9 @@ user.add_permissions_to 'show', 'users'
79
79
  # Adding permissions to access create and update actions in UsersController
80
80
  user.add_permissions_to 'create', 'update', 'users'
81
81
  ```
82
- _The arguments must be related to the actions of a controller, and the last argument is the name of the controller. The actions arguments must be in downcase format and must be equal to the actions methods of the controller. The controller argument, must be in downcase and plural format, ignoring the "Controller" prefix._
82
+ The arguments must be related to the actions of a controller, and the last argument is the name of the controller. The actions arguments must be in downcase format and must be equal to the actions methods of the controller. The controller argument, must be in downcase and plural format, ignoring the "Controller" suffix.
83
83
 
84
- _For example: a controller called `UsersController` must be passed just as `users`._
84
+ For example, a controller called `UsersController` must be passed just as `users`.
85
85
 
86
86
  Also is possible to pass the arguments as symbols:
87
87
  ```ruby
@@ -108,6 +108,21 @@ To check if the user has permission to access an action from a controller, you j
108
108
  user.has_permission_to? 'create', 'users'
109
109
  ```
110
110
 
111
+ ### Scoped Controllers
112
+
113
+ For controllers that are scoped in a module, its argument also must be informed in the same downcase and plural format, but with the prefix of the module separated by a slash. For example, a controller called `Api::UsersController` must be passed as `api/users`:
114
+
115
+ ```ruby
116
+ # Adding permissions
117
+ user.add_permissions_to 'create', 'update', 'api/users'
118
+
119
+ # Removing permissions
120
+ user.remove_permissions_to 'create', 'update', 'api/users'
121
+
122
+ # Checking permission
123
+ user.has_permission_to? 'create', 'api/users'
124
+ ```
125
+
111
126
  ## Authorization
112
127
 
113
128
  To authorize the actions in a controller, you must call `authorize_action!`. Action Sentinel will authorize the access if the current user has permission to access the action.
@@ -143,7 +158,6 @@ end
143
158
  ```
144
159
 
145
160
  ### Rescuing an UnauthorizedAction in ApplicationController
146
- ---
147
161
 
148
162
  Action Sentinel raises an `ActionSentinel::UnauthorizedAction` if the user does not have the permission to access an action. You can rescue this error and respond in your customized format using `rescue_from` in your `ApplicationController`:
149
163
 
@@ -163,7 +177,7 @@ end
163
177
 
164
178
  ## Contributing
165
179
 
166
- Bug reports and pull requests are welcome on GitHub at https://github.com/Null-Bug-Company/action_sentinel.
180
+ Bug reports and pull requests are welcome on GitHub at https://github.com/denisstael/action_sentinel.
167
181
 
168
182
  ## License
169
183
 
@@ -10,9 +10,9 @@ module ActionSentinel
10
10
  # @raise [UnauthorizedAction] if the user is not authorized.
11
11
  # @return [void]
12
12
  def authorize_action!
13
- return if action_user.has_permission_to?(action_name, controller_name)
13
+ return if action_user.has_permission_to?(action_name, controller_path)
14
14
 
15
- raise UnauthorizedAction, "Not allowed to access '#{action_name}' action in #{controller_name}_controller"
15
+ raise UnauthorizedAction, "Not allowed to access '#{action_name}' action in #{controller_path.camelize}Controller"
16
16
  end
17
17
 
18
18
  # Retrieve the user associated with the current action.
@@ -28,10 +28,10 @@ module ActionSentinel
28
28
  # Add permissions to the access_permissions association for a specific controller.
29
29
  #
30
30
  # @param actions [Array<Symbol, String>] The actions to add permissions for.
31
- # @param controller_name [String] The name of the controller.
31
+ # @param controller_path [String] The name of the controller.
32
32
  # @return [Boolean] true if the permission was saved, false otherwise.
33
- def add_permissions_to(*actions, controller_name)
34
- permission = access_permissions.find_or_initialize_by(controller_name: controller_name)
33
+ def add_permissions_to(*actions, controller_path)
34
+ permission = access_permissions.find_or_initialize_by(controller_path: controller_path)
35
35
  permission.assign_attributes(actions: (permission.actions + sanitize_actions_array(actions)).uniq)
36
36
  permission.save
37
37
  end
@@ -39,11 +39,11 @@ module ActionSentinel
39
39
  # Remove permissions from the access_permissions association for a specific controller.
40
40
  #
41
41
  # @param actions [Array<Symbol, String>] The actions to remove permissions for.
42
- # @param controller_name [String] The name of the controller.
42
+ # @param controller_path [String] The name of the controller.
43
43
  # @return [Boolean, nil] true if the permission was saved, false if it was not or nil
44
44
  # if the permission was not found.
45
- def remove_permissions_to(*actions, controller_name)
46
- permission = access_permissions.find_by(controller_name: controller_name)
45
+ def remove_permissions_to(*actions, controller_path)
46
+ permission = access_permissions.find_by(controller_path: controller_path)
47
47
  permission&.update(actions: (permission.actions - sanitize_actions_array(actions)))
48
48
  end
49
49
 
@@ -52,10 +52,10 @@ module ActionSentinel
52
52
  # Check if the model has permission to perform a specific action in a controller.
53
53
  #
54
54
  # @param action [Symbol, String] The action to check permission for.
55
- # @param controller_name [String] The name of the controller.
55
+ # @param controller_path [String] The name of the controller.
56
56
  # @return [Boolean] true if the model has permission, false otherwise.
57
- def has_permission_to?(action, controller_name)
58
- query = access_permissions.where(controller_name: controller_name)
57
+ def has_permission_to?(action, controller_path)
58
+ query = access_permissions.where(controller_path: controller_path)
59
59
 
60
60
  query = if %w[sqlite sqlite3].include? self.class.connection.adapter_name.downcase
61
61
  query.where("actions LIKE ?", "%#{action}%")
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActionSentinel
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -29,10 +29,14 @@ module ActionSentinel
29
29
  template "access_permission.rb", File.join("app", "models", "access_permission.rb")
30
30
  end
31
31
 
32
- def table_id_type
32
+ def primary_key_type
33
33
  options.uuid? ? ", id: :uuid" : ""
34
34
  end
35
35
 
36
+ def foreign_key_type
37
+ options.uuid? ? ", type: :uuid" : ""
38
+ end
39
+
36
40
  def generate_migration
37
41
  migration_template "migration.rb", "db/migrate/create_access_permissions.rb"
38
42
  end
@@ -52,7 +56,7 @@ module ActionSentinel
52
56
  def inject_action_permissible_into_model
53
57
  model_file = File.join("app", "models", "#{singular_model_name}.rb")
54
58
  inject_into_class(model_file, model_class) do
55
- "\taction_permissible\n\n"
59
+ "\taction_permissible\n"
56
60
  end
57
61
  end
58
62
  end
@@ -3,5 +3,5 @@
3
3
  class AccessPermission < ApplicationRecord
4
4
  belongs_to :<%= singular_model_name %>
5
5
 
6
- validates :controller_name, uniqueness: { scope: :<%= singular_model_name %>_id }
6
+ validates :controller_path, uniqueness: { scope: :<%= singular_model_name %>_id }
7
7
  end
@@ -1,13 +1,13 @@
1
1
  class CreateAccessPermissions < ActiveRecord::Migration<%= migration_version %>
2
2
  def change
3
- create_table :access_permissions<%= table_id_type %> do |t|
4
- t.string :controller_name, null: false
3
+ create_table :access_permissions<%= primary_key_type %> do |t|
4
+ t.string :controller_path, null: false
5
5
  t.string :actions, null: false, array: true, default: []
6
- t.references :<%= singular_model_name %>, null: false<%= table_id_type %>
6
+ t.references :<%= singular_model_name %>, null: false<%= foreign_key_type %>
7
7
 
8
8
  t.timestamps
9
9
  end
10
10
 
11
- add_index :access_permissions, [:controller_name, :<%= singular_model_name %>_id], unique: true
11
+ add_index :access_permissions, [:controller_path, :<%= singular_model_name %>_id], unique: true
12
12
  end
13
13
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_sentinel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Stael
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-06 00:00:00.000000000 Z
11
+ date: 2023-12-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -55,7 +55,7 @@ dependencies:
55
55
  description: " This gem enables access authorization control, based on the access
56
56
  permission settings for \n each controller and its actions, at the model level.\n"
57
57
  email:
58
- - denis@nullbug.dev
58
+ - denissantistael@gmail.com
59
59
  executables: []
60
60
  extensions: []
61
61
  extra_rdoc_files: []
@@ -76,12 +76,13 @@ files:
76
76
  - lib/generators/action_sentinel/access_permission_generator.rb
77
77
  - lib/generators/action_sentinel/templates/access_permission.rb
78
78
  - lib/generators/action_sentinel/templates/migration.rb
79
- homepage: https://github.com/Null-Bug-Company/action_sentinel
79
+ homepage: https://github.com/denisstael/action_sentinel
80
80
  licenses:
81
81
  - MIT
82
82
  metadata:
83
- homepage_uri: https://github.com/Null-Bug-Company/action_sentinel
84
- source_code_uri: https://github.com/Null-Bug-Company/action_sentinel
83
+ homepage_uri: https://github.com/denisstael/action_sentinel
84
+ source_code_uri: https://github.com/denisstael/action_sentinel
85
+ changelog_uri: https://github.com/denisstael/action_sentinel/blob/main/CHANGELOG.md
85
86
  post_install_message:
86
87
  rdoc_options: []
87
88
  require_paths: