dynamican 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cc1cfc7f02f93e35dc7103dc3aeda6f542b87409cc83ea9a4c860a577b0bdc96
4
- data.tar.gz: dadb49882f3d180376751cce2b1fecac2fd93589152ed89759e5a7a0fe5c2223
3
+ metadata.gz: ee2ebd9f3470a7ad1002ee3344a357fc1549e8065dffc09986f9c466577de8a9
4
+ data.tar.gz: e1cac4d8aa2b6f209dc6885cebe31d9ec5548af8578514aa9bf44fe1836fb697
5
5
  SHA512:
6
- metadata.gz: 256b081ed7c6620a65327d9c3473610128a4ebd9e67d8dd2f5d34e1fcc99899697c932e382ddea70249acaab61372a0ab795022cee491341b0d57f77d4967055
7
- data.tar.gz: '09d697b8c66f6c3257ea7b25451f71d45fd4c8730214bf579b0f4ff996b57ad8049e25b44c60d708258e4cbdbcf4c69d1cf840067f817ea9a66dd70ddd049cf3'
6
+ metadata.gz: 63e6761a9fed8385830c6f10697a0448ec08a8e2a781612ac54544ed810635bbfec6006c26e5054b653131a495815c6da5ed1842e11262736f9b1a274c9b0882
7
+ data.tar.gz: be88310e037fb8cb2e71af8f8616f1441a2963b4b65a6599762e92b6800e30cec6ede2216c5558d2ff8c8ffc7407c94e866c5894a3f8e15c31517dcb213ac5a6
data/README.md CHANGED
@@ -120,3 +120,7 @@ You can apply the scope `for_action(action_name)` to Permission to find permissi
120
120
  There is a `for_item(item_name)` scope, which turns to string and then classifies automatically the argument to match it with the classified item name. The scope filters all Permission records that have an item with the specified name in its items list.
121
121
  There is also a `without_item` scope to filter records that are not linked to any item.
122
122
  As mentioned before, you can also use `conditional` and `unconditional` scopes to find objects with or without any condition attached.
123
+
124
+ ### Controller usage
125
+
126
+ Your controllers now all have the `authorize!` method, which accepts one or two arguments: the first is the action, and the second (optional) is the item (or list of items) you want to check permissions for. As you can see, the usage is similar to the `can?` method. The reason is that is actually calls that method on the instance of `@current_user` and, whether permissions are evaluated as false, it raises an exception which is rescued by an `unauthorized` response rendered.
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'dynamican'
3
- s.version = '1.0.0'
3
+ s.version = '1.0.1'
4
4
  s.date = '2020-09-10'
5
5
  s.summary = "Dynamic permissions"
6
6
  s.description = "Dynamic and flexible database configurable permissions for your application"
@@ -1,3 +1,4 @@
1
+ require 'dynamican/authorization'
1
2
  require 'dynamican/evaluator'
2
3
  require 'dynamican/model'
3
4
  require 'models/dynamican/permission'
@@ -0,0 +1,25 @@
1
+ module Dynamican
2
+ module Authorization
3
+ extend ActiveSupport::Concern
4
+
5
+ class UnauthorizedResource < StandardError; end
6
+
7
+ included do
8
+ rescue_from UnauthorizedResource, with: :unauthorized
9
+ end
10
+
11
+ def authorize!(action, item = nil)
12
+ raise UnauthorizedResource unless @current_user.can? action, item
13
+ end
14
+
15
+ def unauthorized
16
+ render status: :unauthorized
17
+ end
18
+ end
19
+ end
20
+
21
+ if defined? ActiveSupport
22
+ ActiveSupport.on_load(:action_controller) do
23
+ include Dynamican::Authorization
24
+ end
25
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamican
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Valerio Bellaveglia
@@ -20,6 +20,7 @@ files:
20
20
  - README.md
21
21
  - dynamican.gemspec
22
22
  - lib/dynamican.rb
23
+ - lib/dynamican/authorization.rb
23
24
  - lib/dynamican/evaluator.rb
24
25
  - lib/dynamican/model.rb
25
26
  - lib/generators/dynamican_migration_generator.rb