zuul 0.2.5 → 0.2.6
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 +4 -4
- data/lib/zuul/active_record.rb +1 -2
- data/lib/zuul/active_record/permission.rb +7 -6
- data/lib/zuul/active_record/permission_role.rb +2 -2
- data/lib/zuul/active_record/permission_subject.rb +2 -2
- data/lib/zuul/active_record/role.rb +6 -6
- data/lib/zuul/active_record/role_subject.rb +2 -2
- data/lib/zuul/active_record/scope.rb +16 -1
- data/lib/zuul/active_record/subject.rb +10 -10
- data/lib/zuul/version.rb +1 -1
- data/spec/zuul/active_record/scope_spec.rb +18 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 58d203aaa4eca804ec402bbd10742127e45b0607
|
4
|
+
data.tar.gz: 53c92c3fd32d310492026b5012603cd57c7fd458
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4400d89b4ce82f09b6b1ac148febb141c1b98801b7784f4d395b0aa9b2fc788860cef0b935a91ea61c503fc6b095d91ca3c188c32fcc2222deb8afc4cabd6322
|
7
|
+
data.tar.gz: d24b6237c95fabf90e608a523b724e07b3c8d1acbd7ffa0d4b4ff37a7fe9752536d4bdb182940c72fa96b7767f08a59625e304becfed027b2f1ea16ba7edc056
|
data/lib/zuul/active_record.rb
CHANGED
@@ -22,10 +22,9 @@ module Zuul
|
|
22
22
|
#
|
23
23
|
# The args parameter is an optional hash of configuration options.
|
24
24
|
def acts_as_authorization_model(args={}, &block)
|
25
|
-
include AuthorizationMethods
|
25
|
+
include AuthorizationMethods unless ancestors.include?(AuthorizationMethods)
|
26
26
|
auth_config = Zuul.configuration.clone.configure(args, &block)
|
27
27
|
@auth_scopes ||= {}
|
28
|
-
raise "Scope already in use: #{auth_config.scope}" if @auth_scopes.has_key?(auth_config.scope)
|
29
28
|
@auth_scopes[auth_config.scope] = Scope.new(auth_config)
|
30
29
|
@auth_scopes[:default] ||= @auth_scopes[auth_config.scope]
|
31
30
|
@auth_scopes[auth_config.scope]
|
@@ -22,10 +22,11 @@ module Zuul
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def self.add_associations(base)
|
25
|
-
base.send :has_many, base.auth_scope.
|
26
|
-
base.send :has_many, base.auth_scope.
|
27
|
-
|
28
|
-
base.send :has_many, base.auth_scope.
|
25
|
+
base.send :has_many, base.auth_scope.permission_role_plural_key, :class_name => base.auth_scope.permission_role_class_name, :dependent => :destroy
|
26
|
+
base.send :has_many, base.auth_scope.role_plural_key, :class_name => base.auth_scope.role_class_name, :through => base.auth_scope.permission_role_plural_key
|
27
|
+
|
28
|
+
base.send :has_many, base.auth_scope.permission_subject_plural_key, :class_name => base.auth_scope.permission_subject_class_name, :dependent => :destroy
|
29
|
+
base.send :has_many, base.auth_scope.subject_plural_key, :class_name => base.auth_scope.subject_class_name, :through => base.auth_scope.permission_subject_plural_key
|
29
30
|
end
|
30
31
|
end
|
31
32
|
|
@@ -38,14 +39,14 @@ module Zuul
|
|
38
39
|
# Returns a list of contexts within which the permission has been assigned to roles
|
39
40
|
def role_contexts
|
40
41
|
auth_scope do
|
41
|
-
send(
|
42
|
+
send(permission_role_plural_key).group(:context_type, :context_id).map(&:context)
|
42
43
|
end
|
43
44
|
end
|
44
45
|
|
45
46
|
# Returns a list of contexts within which the permission has been assigned to subjects
|
46
47
|
def subject_contexts
|
47
48
|
auth_scope do
|
48
|
-
send(
|
49
|
+
send(permission_subject_plural_key).group(:context_type, :context_id).map(&:context)
|
49
50
|
end
|
50
51
|
end
|
51
52
|
end
|
@@ -21,8 +21,8 @@ module Zuul
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def self.add_associations(base)
|
24
|
-
base.send :belongs_to, base.auth_scope.
|
25
|
-
base.send :belongs_to, base.auth_scope.
|
24
|
+
base.send :belongs_to, base.auth_scope.permission_singular_key, :class_name => base.auth_scope.permission_class_name
|
25
|
+
base.send :belongs_to, base.auth_scope.role_singular_key, :class_name => base.auth_scope.role_class_name
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
@@ -21,8 +21,8 @@ module Zuul
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def self.add_associations(base)
|
24
|
-
base.send :belongs_to, base.auth_scope.
|
25
|
-
base.send :belongs_to, base.auth_scope.
|
24
|
+
base.send :belongs_to, base.auth_scope.permission_singular_key, :class_name => base.auth_scope.permission_class_name
|
25
|
+
base.send :belongs_to, base.auth_scope.subject_singular_key, :class_name => base.auth_scope.subject_class_name
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
@@ -25,11 +25,11 @@ module Zuul
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def self.add_associations(base)
|
28
|
-
base.send :has_many, base.auth_scope.
|
29
|
-
base.send :has_many, base.auth_scope.
|
28
|
+
base.send :has_many, base.auth_scope.role_subject_plural_key, :class_name => base.auth_scope.role_subject_class_name, :dependent => :destroy
|
29
|
+
base.send :has_many, base.auth_scope.subject_plural_key, :class_name => base.auth_scope.subject_class_name, :through => base.auth_scope.role_subject_plural_key
|
30
30
|
if base.auth_scope.config.with_permissions
|
31
|
-
base.send :has_many, base.auth_scope.
|
32
|
-
base.send :has_many, base.auth_scope.
|
31
|
+
base.send :has_many, base.auth_scope.permission_role_plural_key, :class_name => base.auth_scope.permission_role_class_name, :dependent => :destroy
|
32
|
+
base.send :has_many, base.auth_scope.permission_plural_key, :class_name => base.auth_scope.permission_class_name, :through => base.auth_scope.permission_role_plural_key
|
33
33
|
end
|
34
34
|
end
|
35
35
|
end
|
@@ -38,7 +38,7 @@ module Zuul
|
|
38
38
|
# Returns a list of contexts within which the role has been assigned to subjects
|
39
39
|
def assigned_contexts
|
40
40
|
auth_scope do
|
41
|
-
send(
|
41
|
+
send(role_subject_plural_key).group(:context_type, :context_id).map(&:context)
|
42
42
|
end
|
43
43
|
end
|
44
44
|
end
|
@@ -111,7 +111,7 @@ module Zuul
|
|
111
111
|
force_context ||= config.force_context
|
112
112
|
context = Zuul::Context.parse(context)
|
113
113
|
if force_context
|
114
|
-
return permission_class.joins(
|
114
|
+
return permission_class.joins(permission_role_plural_key).where(permission_role_plural_key => {role_foreign_key.to_sym => id, :context_type => context.class_name, :context_id => context.id})
|
115
115
|
else
|
116
116
|
return permission_class.joins("LEFT JOIN #{permission_roles_table_name} ON #{permission_roles_table_name}.#{permission_foreign_key} = #{permissions_table_name}.id").where("#{permission_roles_table_name}.#{role_foreign_key} = ? AND (#{permission_roles_table_name}.context_type #{sql_is_or_equal(context.class_name)} ? OR #{permission_roles_table_name}.context_type IS NULL) AND (#{permission_roles_table_name}.context_id #{sql_is_or_equal(context.id)} ? OR #{permission_roles_table_name}.context_id IS NULL)", id, context.class_name, context.id)
|
117
117
|
end
|
@@ -21,8 +21,8 @@ module Zuul
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def self.add_associations(base)
|
24
|
-
base.send :belongs_to, base.auth_scope.
|
25
|
-
base.send :belongs_to, base.auth_scope.
|
24
|
+
base.send :belongs_to, base.auth_scope.role_singular_key, :class_name => base.auth_scope.role_class_name
|
25
|
+
base.send :belongs_to, base.auth_scope.subject_singular_key, :class_name => base.auth_scope.subject_class_name
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
@@ -14,10 +14,12 @@ module Zuul
|
|
14
14
|
|
15
15
|
# Define dynamic reflection methods that reference the config to be used for subjects, roles, permissions and their associations.
|
16
16
|
def define_reflection_methods
|
17
|
+
|
17
18
|
# *_class_name, *_class, *_table_name methods for all classes
|
18
19
|
@config.classes.to_h.each do |class_type,class_name|
|
19
20
|
class_type_name = class_type.to_s.gsub(/_class$/,'').singularize
|
20
21
|
class_eval do
|
22
|
+
|
21
23
|
# def CLASS_TYPE_class_name
|
22
24
|
define_method "#{class_type_name}_class_name" do
|
23
25
|
if @config.send(class_type).is_a?(Class)
|
@@ -39,13 +41,26 @@ module Zuul
|
|
39
41
|
send("#{class_type_name}_class").table_name
|
40
42
|
end
|
41
43
|
alias_method "#{class_type_name.pluralize}_table_name", "#{class_type_name}_table_name"
|
44
|
+
|
45
|
+
# def CLASS_TYPE_singular_key (used primarily for associations)
|
46
|
+
define_method "#{class_type_name}_singular_key" do
|
47
|
+
send("#{class_type_name}_class_name").underscore.split("/").last.singularize.to_sym
|
48
|
+
end
|
49
|
+
alias_method "#{class_type_name.pluralize}_singular_key", "#{class_type_name}_singular_key"
|
50
|
+
|
51
|
+
# def CLASS_TYPE_plural_key (use primarily for associations)
|
52
|
+
define_method "#{class_type_name}_plural_key" do
|
53
|
+
send("#{class_type_name}_class_name").underscore.split("/").last.pluralize.to_sym
|
54
|
+
end
|
55
|
+
alias_method "#{class_type_name.pluralize}_plural_key", "#{class_type_name}_plural_key"
|
42
56
|
|
43
57
|
unless class_type.to_s.underscore == "#{class_name.to_s.underscore}_class"
|
44
|
-
|
58
|
+
%w(_class_name _class _table_name _singular_key _plural_key).each do |suffix|
|
45
59
|
alias_method "#{class_name.to_s.underscore.singularize}#{suffix}", "#{class_type_name}#{suffix}"
|
46
60
|
alias_method "#{class_name.to_s.underscore.pluralize}#{suffix}", "#{class_name.to_s.underscore.singularize}#{suffix}"
|
47
61
|
end
|
48
62
|
end
|
63
|
+
|
49
64
|
end
|
50
65
|
end
|
51
66
|
|
@@ -14,8 +14,8 @@ module Zuul
|
|
14
14
|
|
15
15
|
module ClassMethods
|
16
16
|
def self.extended(base)
|
17
|
-
base.send :has_many, base.auth_scope.
|
18
|
-
base.send :has_many, base.auth_scope.
|
17
|
+
base.send :has_many, base.auth_scope.role_subject_plural_key, :class_name => base.auth_scope.role_subjects_class_name, :dependent => :destroy
|
18
|
+
base.send :has_many, base.auth_scope.role_plural_key, :class_name => base.auth_scope.role_class_name, :through => base.auth_scope.role_subject_plural_key
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
@@ -65,7 +65,7 @@ module Zuul
|
|
65
65
|
target = target_role(role, context, force_context)
|
66
66
|
return false if target.nil?
|
67
67
|
|
68
|
-
return true unless (context.id.nil? && !force_context) || role_subject_class.joins(
|
68
|
+
return true unless (context.id.nil? && !force_context) || role_subject_class.joins(role_table_name.singularize.to_sym).where(subject_foreign_key.to_sym => id, role_foreign_key.to_sym => target.id, :context_type => context.class_name, :context_id => context.id).first.nil?
|
69
69
|
return false if force_context
|
70
70
|
return true unless context.class_name.nil? || role_subject_class.where(subject_foreign_key.to_sym => id, role_foreign_key.to_sym => target.id, :context_type => context.class_name, :context_id => nil).first.nil?
|
71
71
|
return !role_subject_class.where(subject_foreign_key.to_sym => id, role_foreign_key.to_sym => target.id, :context_type => nil, :context_id => nil).first.nil?
|
@@ -89,9 +89,9 @@ module Zuul
|
|
89
89
|
|
90
90
|
return true if has_role?(target, context, force_context)
|
91
91
|
|
92
|
-
return true unless context.id.nil? || role_subject_class.joins(
|
93
|
-
return true unless context.class_name.nil? || role_subject_class.joins(
|
94
|
-
return !role_subject_class.joins(
|
92
|
+
return true unless context.id.nil? || role_subject_class.joins(role_table_name.singularize.to_sym).where(subject_foreign_key.to_sym => id, :context_type => context.class_name, :context_id => context.id).where("#{roles_table_name}.level >= ? AND #{roles_table_name}.context_type #{sql_is_or_equal(target.context_type)} ? AND #{roles_table_name}.context_id #{sql_is_or_equal(target.context_id)} ?", target.level, target.context_type, target.context_id).first.nil?
|
93
|
+
return true unless context.class_name.nil? || role_subject_class.joins(role_table_name.singularize.to_sym).where(subject_foreign_key.to_sym => id, :context_type => context.class_name, :context_id => nil).where("#{roles_table_name}.level >= ? AND #{roles_table_name}.context_type #{sql_is_or_equal(target.context_type)} ? AND #{roles_table_name}.context_id #{sql_is_or_equal(target.context_id)} ?", target.level, target.context_type, target.context_id).first.nil?
|
94
|
+
return !role_subject_class.joins(role_table_name.singularize.to_sym).where(subject_foreign_key.to_sym => id, :context_type => nil, :context_id => nil).where("#{roles_table_name}.level >= ? AND #{roles_table_name}.context_type #{sql_is_or_equal(target.context_type)} ? AND #{roles_table_name}.context_id #{sql_is_or_equal(target.context_id)} ?", target.level, target.context_type, target.context_id).first.nil?
|
95
95
|
end
|
96
96
|
end
|
97
97
|
alias_method :role_or_higher?, :has_role_or_higher?
|
@@ -113,9 +113,9 @@ module Zuul
|
|
113
113
|
force_context ||= config.force_context
|
114
114
|
context = Zuul::Context.parse(context)
|
115
115
|
if force_context
|
116
|
-
return role_class.joins(
|
116
|
+
return role_class.joins(role_subject_plural_key).where("#{role_subjects_table_name}.#{subject_foreign_key} = ? AND #{role_subjects_table_name}.context_type #{sql_is_or_equal(context.class_name)} ? AND #{role_subjects_table_name}.context_id #{sql_is_or_equal(context.id)} ?", id, context.class_name, context.id)
|
117
117
|
else
|
118
|
-
return role_class.joins(
|
118
|
+
return role_class.joins(role_subject_plural_key).where("#{role_subjects_table_name}.#{subject_foreign_key} = ? AND ((#{role_subjects_table_name}.context_type #{sql_is_or_equal(context.class_name)} ? OR #{role_subjects_table_name}.context_type IS NULL) AND (#{role_subjects_table_name}.context_id #{sql_is_or_equal(context.id)} ? OR #{role_subjects_table_name}.context_id IS NULL))", id, context.class_name, context.id)
|
119
119
|
end
|
120
120
|
end
|
121
121
|
end
|
@@ -137,8 +137,8 @@ module Zuul
|
|
137
137
|
|
138
138
|
module ClassMethods
|
139
139
|
def self.extended(base)
|
140
|
-
base.send :has_many, base.auth_scope.
|
141
|
-
base.send :has_many, base.auth_scope.
|
140
|
+
base.send :has_many, base.auth_scope.permission_subject_plural_key, :class_name => base.auth_scope.permission_subject_class_name, :dependent => :destroy
|
141
|
+
base.send :has_many, base.auth_scope.permission_plural_key, :class_name => base.auth_scope.permission_class_name, :through => base.auth_scope.permission_subject_plural_key
|
142
142
|
end
|
143
143
|
end
|
144
144
|
|
data/lib/zuul/version.rb
CHANGED
@@ -30,6 +30,18 @@ describe "Zuul::ActiveRecord::Scope" do
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
+
it "should define *_singular_key methods for each of the class types" do
|
34
|
+
Zuul::Configuration::DEFAULT_AUTHORIZATION_CLASSES.keys.each do |class_type|
|
35
|
+
@scope.should respond_to("#{class_type.to_s.gsub(/_class$/,'').singularize}_singular_key")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should define *_plural_key methods for each of the class types" do
|
40
|
+
Zuul::Configuration::DEFAULT_AUTHORIZATION_CLASSES.keys.each do |class_type|
|
41
|
+
@scope.should respond_to("#{class_type.to_s.gsub(/_class$/,'').singularize}_plural_key")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
33
45
|
it "should define *_foreign_key methods for each of the primary class types" do
|
34
46
|
Zuul::Configuration::PRIMARY_AUTHORIZATION_CLASSES.keys.each do |class_type|
|
35
47
|
@scope.should respond_to("#{class_type.to_s.gsub(/_class$/,'').singularize}_foreign_key")
|
@@ -41,6 +53,8 @@ describe "Zuul::ActiveRecord::Scope" do
|
|
41
53
|
@scope.should respond_to("#{class_type.to_s.gsub(/_class$/,'').pluralize}_class_name")
|
42
54
|
@scope.should respond_to("#{class_type.to_s.gsub(/_class$/,'').pluralize}_class")
|
43
55
|
@scope.should respond_to("#{class_type.to_s.gsub(/_class$/,'').pluralize}_table_name")
|
56
|
+
@scope.should respond_to("#{class_type.to_s.gsub(/_class$/,'').pluralize}_singular_key")
|
57
|
+
@scope.should respond_to("#{class_type.to_s.gsub(/_class$/,'').pluralize}_plural_key")
|
44
58
|
end
|
45
59
|
Zuul::Configuration::PRIMARY_AUTHORIZATION_CLASSES.keys.each do |class_type|
|
46
60
|
@scope.should respond_to("#{class_type.to_s.gsub(/_class$/,'').pluralize}_foreign_key")
|
@@ -54,6 +68,8 @@ describe "Zuul::ActiveRecord::Scope" do
|
|
54
68
|
@scope.should respond_to("#{@scope.config.send(class_type).to_s.singularize}_class_name")
|
55
69
|
@scope.should respond_to("#{@scope.config.send(class_type).to_s.singularize}_class")
|
56
70
|
@scope.should respond_to("#{@scope.config.send(class_type).to_s.singularize}_table_name")
|
71
|
+
@scope.should respond_to("#{@scope.config.send(class_type).to_s.singularize}_singular_key")
|
72
|
+
@scope.should respond_to("#{@scope.config.send(class_type).to_s.singularize}_plural_key")
|
57
73
|
end
|
58
74
|
Zuul::Configuration::PRIMARY_AUTHORIZATION_CLASSES.keys.each do |class_type|
|
59
75
|
@scope.should respond_to("#{@scope.config.send(class_type).to_s.singularize}_foreign_key")
|
@@ -65,6 +81,8 @@ describe "Zuul::ActiveRecord::Scope" do
|
|
65
81
|
@scope.should respond_to("#{@scope.config.send(class_type).to_s.underscore.pluralize}_class_name")
|
66
82
|
@scope.should respond_to("#{@scope.config.send(class_type).to_s.underscore.pluralize}_class")
|
67
83
|
@scope.should respond_to("#{@scope.config.send(class_type).to_s.underscore.pluralize}_table_name")
|
84
|
+
@scope.should respond_to("#{@scope.config.send(class_type).to_s.underscore.pluralize}_singular_key")
|
85
|
+
@scope.should respond_to("#{@scope.config.send(class_type).to_s.underscore.pluralize}_plural_key")
|
68
86
|
end
|
69
87
|
Zuul::Configuration::PRIMARY_AUTHORIZATION_CLASSES.keys.each do |class_type|
|
70
88
|
@scope.should respond_to("#{@scope.config.send(class_type).to_s.underscore.pluralize}_foreign_key")
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zuul
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark Rebec
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-06-
|
11
|
+
date: 2013-06-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -184,3 +184,4 @@ test_files:
|
|
184
184
|
- spec/zuul/active_record/context_spec.rb
|
185
185
|
- spec/zuul/active_record/scope_spec.rb
|
186
186
|
- spec/spec_helper.rb
|
187
|
+
has_rdoc:
|