unidom-authorization 1.0 → 1.1

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
  SHA1:
3
- metadata.gz: c33f00597f81f77f4af640b1cb72e47e736e1c3f
4
- data.tar.gz: b178379cec884fbce6f3a69e2b34e8438c0537a2
3
+ metadata.gz: ea29ec666170f123b6e3ff8a9e408a47283f3f69
4
+ data.tar.gz: 959fb6bbba7b827b9090fe9536ed5f8aff7f38b6
5
5
  SHA512:
6
- metadata.gz: 5306f671d64ab3f36b7358a11b0769c44d058b5615cd8381ee65ada803552ad102fc9357ec95800a6f985dbbb3d2620cf001376799222b8d20ec904811b8a90e
7
- data.tar.gz: 38a89f8d22957deb576c3aee72f99106bb3772c337ab076e7eae70051ab8f29fa02ddb16bf82ae4a0a6fc44d4c3e2400a93fd64d9a7d2dddaf5ed460e6bf9850
6
+ metadata.gz: 42d1405280d706980f7922d05844dde1ee83c755083a58688ad59100a16963ea0b25b24bf9812e1f1290ed575ee227abf53553606fdb820400086f9a072bd480
7
+ data.tar.gz: 428228b087dce180039c7a994a56b6a83dcd2c6a7e1578c640e9a347a62b8a147868f3f913ed6562f4b0f1b4c5c9312b755cbc6d117b21ce5e1010f576c55888
data/README.md CHANGED
@@ -27,5 +27,21 @@ Unidom::Authorization::Permission.valid_at.alive
27
27
  Unidom::Authorization::Authorizing.valid_at.alive
28
28
 
29
29
  permission = Unidom::Authorization::Permission.valid_at.alive.first
30
+
31
+ permission.authorized? user, at: Time.now # false
30
32
  Unidom::Authorization::Authorizing.authorize! permission: permission, authorized: user
33
+ # or
34
+ permission.authorize! user, by: current_user, at: Time.now
35
+ permission.authorized? user, at: Time.now # true
36
+
37
+ ```
38
+
39
+ ## Include the Conerns
40
+ ```ruby
41
+ include Unidom::Authorization::Concerns::AsAuthorized
31
42
  ```
43
+
44
+ ### As Authorized concern
45
+ The As Authorized concern do the following tasks for the includer automatically:
46
+ 1. Define the has_many :authorizings macro as: ``has_many :authorizings, class_name: 'Unidom::Authorization::Authorizing', as: :authorized``
47
+ 2. Define the has_many :permissions macro as: ``has_many :permissions, through: :authorizings, source: :permission``
@@ -4,7 +4,8 @@ module Unidom::Authorization::Concerns::AsAuthorized
4
4
 
5
5
  self.included do |includer|
6
6
 
7
- has_many :authorizings, class_name: 'Unidom::Authorization::Authorizing', as: :authorized
7
+ has_many :authorizings, class_name: 'Unidom::Authorization::Authorizing', as: :authorized
8
+ has_many :permissions, through: :authorizings, source: :permission
8
9
 
9
10
  end
10
11
 
@@ -13,4 +13,19 @@ class Unidom::Authorization::Permission < ActiveRecord::Base
13
13
 
14
14
  scope :path_is, ->(path) { where path: path }
15
15
 
16
+ def authorize!(authorized, by: nil, at: Time.now)
17
+ attributes = { authorized: authorized, opened_at: at }
18
+ if by.present?
19
+ attributes[:authorizer] = by
20
+ else
21
+ attributes[:authorizer_id] = Unidom::Common::NULL_UUID
22
+ attributes[:authorizer_type] = ''
23
+ end
24
+ authorizings.create! attributes
25
+ end
26
+
27
+ def authorized?(authorized, at: Time.now)
28
+ authorizings.authorized_is(authorized).valid_at(now: at).alive.exists?
29
+ end
30
+
16
31
  end
@@ -1,5 +1,5 @@
1
1
  module Unidom
2
2
  module Authorization
3
- VERSION = '1.0'.freeze
3
+ VERSION = '1.1'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unidom-authorization
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.0'
4
+ version: '1.1'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Topbit Du
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-19 00:00:00.000000000 Z
11
+ date: 2016-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: unidom-common