i_am_i_can 4.3.3 → 4.4.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
  SHA1:
3
- metadata.gz: bc8efa706ad5886dd1188273bfed33bf80c80fbf
4
- data.tar.gz: f1449022f144128db5ed05e85c5d973b08ded6d5
3
+ metadata.gz: ab915220af5d95373eaa8b8a57563d311a7f2903
4
+ data.tar.gz: ea007c8d5b61f511dcbfd799432d5221dd666804
5
5
  SHA512:
6
- metadata.gz: 0f809fa65dc6c707f40bae254e048103dc0ad3614831447228046f79586b188e46f4257221849243f2b6bed41d997f3f0d048b3b5a52df1aa263f007fe2512ed
7
- data.tar.gz: 9ff64d80f81f29e1e549bc8af7016c8ae64b43d41225aa63c39ba9fc5952678d7978f388fa1ab81173b93d97eb68826c05738483f0c121e005e64732b2976a72
6
+ metadata.gz: dd645226ade084917ddc64405009d1c7c6f2b8ead5c69544be10e296264a963ab38ab2f851ac46633cbb36c45d052330a1899e3381b1517bc1804c83fb7dbf26
7
+ data.tar.gz: 0e7ecdb8a56621234a9f52dc9c3f8a6131b55ea0674fe7c78a5c1f84c66305f0ce80a7137d2d3e0a109cae2260cd9cf4f23e8de257906be08aa9f9f843180462
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  IAmICan::Configs.set_for(subject: '<%= name_c %>',
2
4
  role: '<%= @ii_opts[:role_class] %>',
3
5
  permission: '<%= @ii_opts[:permission_class] %>'<% unless @ii_opts[:without_group] %>,
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class <%= permission_c %> < ActiveRecord::Base
2
4
  has_and_belongs_to_many :related_roles,
3
5
  join_table: '<%= role_pms_tb %>', foreign_key: :<%= permission_u %>_id,
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class <%= role_c %> < ActiveRecord::Base
2
4
  has_and_belongs_to_many :related_users,
3
5
  join_table: '<%= subj_role_tb %>', foreign_key: :<%= role_u %>_id,
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class RoleGroup < ActiveRecord::Base
2
4
  has_and_belongs_to_many :permissions,
3
5
  join_table: '<%= group_pms_tb %>', foreign_key: :<%= group_u %>_id,
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module IAmICan
2
4
  module Configs
3
5
  class Config
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'i_am_i_can/configs/config'
2
4
 
3
5
  module IAmICan
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module IAmICan
2
4
  module Dynamic
3
5
  extend self
@@ -8,9 +10,20 @@ module IAmICan
8
10
  #
9
11
  # scope :with_stored_roles, -> { includes(:stored_roles) }
10
12
  #
13
+ # Usage:
14
+ # User.with_stored_roles(role_id = 1)
15
+ # User.with_stored_roles?(role_id = 1)
11
16
  proc do |keys|
12
17
  keys.each do |k|
13
- scope :"with_#{_reflect_of(k)}", -> { includes(_reflect_of(k)) }
18
+ scope :"with_#{_reflect_of(k)}", -> (ids = nil) do
19
+ break includes(_reflect_of(k)) unless ids
20
+ includes(_reflect_of(k)).where(
21
+ i_am_i_can.send("#{k}_class").underscore.pluralize => { id: ids })
22
+ end
23
+
24
+ define_singleton_method :"with_#{_reflect_of(k)}?" do |ids|
25
+ send(:"with_#{_reflect_of(k)}", ids).present?
26
+ end
14
27
  end
15
28
  end
16
29
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module IAmICan
2
4
  module ResultOf
3
5
  module Role
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module IAmICan
2
4
  module Permission
3
5
  module Assignment
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module IAmICan
2
4
  module Permission
3
5
  module Definition
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'i_am_i_can/permission/definition'
2
4
  require 'i_am_i_can/permission/assignment'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module IAmICan
2
4
  module Resource
3
5
  extend ActiveSupport::Concern
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module IAmICan
2
4
  module Role
3
5
  module Assignment
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module IAmICan
2
4
  module Role
3
5
  module Definition
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module IAmICan
2
4
  module Role
3
5
  module Grouping
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'i_am_i_can/role/definition'
2
4
  require 'i_am_i_can/role/assignment'
3
5
  require 'i_am_i_can/role/grouping'
@@ -15,6 +17,11 @@ module IAmICan
15
17
  def names
16
18
  self.pluck(:name).map(&:to_sym)
17
19
  end
20
+
21
+ def can?(action, obj)
22
+ send("with_#{_reflect_of('permission')}?",
23
+ i_am_i_can.permission_model.matched(action, obj).ids)
24
+ end
18
25
  end
19
26
 
20
27
  included do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module IAmICan
2
4
  module RoleGroup
3
5
  module Definition
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module IAmICan
2
4
  module Subject
3
5
  module PermissionQuerying
@@ -40,12 +42,12 @@ module IAmICan
40
42
  alias can_every! can_each!
41
43
 
42
44
  def temporarily_can? action, obj
43
- return false if temporary_roles.blank?
44
- valid_temporary_roles._permissions.matched?(action, obj)
45
+ return false if try(:temporary_roles).blank?
46
+ valid_temporary_roles.can?(action, obj)
45
47
  end
46
48
 
47
49
  def stored_can? action, obj
48
- _roles._permissions.matched?(action, obj)
50
+ _roles.can?(action, obj)
49
51
  end
50
52
 
51
53
  def group_can? action, obj, without_group = false
@@ -1,48 +1,58 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module IAmICan
2
4
  module Subject
3
5
  module RoleQuerying
4
6
 
5
7
  # === Role Querying ===
6
8
 
7
- def is? role_name
8
- roles.find_by(name: role_name).present?
9
+ def is? role_name, with_tmp: true
10
+ role_id = i_am_i_can.role_model.find_by(name: role_name)&.id
11
+ return false unless role_id
12
+ get_roles(with_tmp: with_tmp).exists?(id: role_id)
9
13
  end
10
14
 
11
15
  alias is_role? is?
12
16
  alias has_role? is?
13
17
 
14
- def isnt? role
15
- !is? role
18
+ def isnt? role_name, with_tmp: true
19
+ !is? role_name, with_tmp: with_tmp
16
20
  end
17
21
 
18
- def is! role
19
- raise VerificationFailed if isnt? role
22
+ def is! role_name, with_tmp: true
23
+ raise VerificationFailed if isnt? role_name, with_tmp: with_tmp
20
24
  true
21
25
  end
22
26
 
23
27
  alias is_role! is!
24
28
  alias has_role! is!
25
29
 
26
- def is_one_of? *role_names
27
- roles.where(name: role_names).present?
30
+ def is_one_of? *role_names, with_tmp: true
31
+ role_ids = i_am_i_can.role_model.where(name: role_names).ids
32
+ return false if role_ids.blank?
33
+ get_roles(with_tmp: with_tmp).exists?(id: role_ids)
28
34
  end
29
35
 
30
36
  alias is_one_of_roles? is_one_of?
31
37
 
32
- def is_one_of! *roles
33
- raise VerificationFailed unless is_one_of? *roles
38
+ def is_one_of! *role_names, with_tmp: true
39
+ raise VerificationFailed unless is_one_of? *role_names, with_tmp: with_tmp
40
+ true
34
41
  end
35
42
 
36
43
  alias is_one_of_roles! is_one_of!
37
44
 
38
- def is_every? *role_names
39
- roles.where(name: role_names).count == role_names.count
45
+ def is_every? *role_names, with_tmp: true
46
+ role_ids = i_am_i_can.role_model.where(name: role_names).ids
47
+ return false if role_ids.size != role_names.size
48
+ get_roles(with_tmp: with_tmp).where(id: role_ids).size == role_names.size
40
49
  end
41
50
 
42
51
  alias is_every_role_in? is_every?
43
52
 
44
- def is_every! *roles
45
- roles.each { |role| is! role } && true
53
+ def is_every! *role_names, with_tmp: true
54
+ raise VerificationFailed unless is_every?(*role_names, with_tmp: true)
55
+ true
46
56
  end
47
57
 
48
58
  alias is_every_role_in! is_every!
@@ -51,7 +61,7 @@ module IAmICan
51
61
 
52
62
  def is_in_role_group? name
53
63
  group_members = i_am_i_can.role_group_model.find_by!(name: name)._roles.names
54
- (roles.names & group_members).present?
64
+ (get_roles.names & group_members).present?
55
65
  end
56
66
 
57
67
  alias in_role_group? is_in_role_group?
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'i_am_i_can/subject/role_querying'
2
4
  require 'i_am_i_can/subject/permission_querying'
3
5
 
@@ -15,6 +17,14 @@ module IAmICan
15
17
  Class.new(ActiveRecord::Base)
16
18
  has_many :"assoc_with_#{__roles}", -> { where('expire_at IS NULL OR expire_at > ?', Time.current) },
17
19
  class_name: role_assoc_class
20
+
21
+ def get_roles(with_tmp: true)
22
+ if with_tmp && (tmp_ids = try(:temporary_roles)&.map(&:id)).present?
23
+ i_am_i_can.role_model.where(id: (tmp_ids + _roles.ids).uniq)
24
+ else
25
+ _roles
26
+ end
27
+ end
18
28
  end
19
29
  end
20
30
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module IAmICan
2
4
  module Association_ClassMethods
3
5
  def has_many_temporary_roles(name: nil)
@@ -11,12 +13,6 @@ module IAmICan
11
13
  i_am_i_can.role_model.where(id: temporary_roles.map(&:id))
12
14
  end
13
15
 
14
- raise "Do not set the role association name to `roles` in #{self.class.name} model" if respond_to?(:roles)
15
- define_method :roles do
16
- ids = (temporary_roles.map(&:id) + _roles.ids).uniq
17
- i_am_i_can.role_model.where(id: ids)
18
- end
19
-
20
16
  define_method :temporary_role_names do
21
17
  temporary_roles.map(&:name).map(&:to_sym)
22
18
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'i_am_i_can/configs/configs'
2
4
 
3
5
  module IAmICan
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module IAmICan
2
4
  module Reflection
3
5
  extend ActiveSupport::Concern
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module IAmICan
2
- VERSION = '4.3.3'
4
+ VERSION = '4.4.0'
3
5
  end
data/lib/i_am_i_can.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_record'
2
4
  require 'active_support/core_ext/object/inclusion'
3
5
  require 'active_support/core_ext/hash/deep_merge'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i_am_i_can
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.3.3
4
+ version: 4.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - zhandao
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-12 00:00:00.000000000 Z
11
+ date: 2019-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler