Module: Zuul::ActiveRecord::ClassMethods

Defined in:
lib/zuul/active_record.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



19
20
# File 'lib/zuul/active_record.rb', line 19

def self.extended(base)
end

Instance Method Details

#acts_as_authorization_context(args = {}, &block) ⇒ Object

Configure the model to act as a zuul authorization context (or resource)

The args parameter is an optional hash of configuration options.



64
65
66
67
68
# File 'lib/zuul/active_record.rb', line 64

def acts_as_authorization_context(args={}, &block)
  scope = acts_as_authorization_model(args, &block)
  prepare_join_classes scope.name
  include Context
end

#acts_as_authorization_context?Boolean

Checks if the model is setup to act as a zuul authorization context/resource

Returns:

  • (Boolean)


136
137
138
# File 'lib/zuul/active_record.rb', line 136

def acts_as_authorization_context?
  acts_as_authorization_model? :context
end

#acts_as_authorization_model(args = {}, &block) ⇒ Object

Includes auth methods into the model and configures auth options and scopes

The args parameter is an optional hash of configuration options.



25
26
27
28
29
30
31
32
# File 'lib/zuul/active_record.rb', line 25

def acts_as_authorization_model(args={}, &block)
  include AuthorizationMethods unless ancestors.include?(AuthorizationMethods)
  auth_config = Zuul.configuration.clone.configure(args, &block)
  @auth_scopes ||= {}
  @auth_scopes[auth_config.scope] = Scope.new(auth_config)
  @auth_scopes[:default] ||= @auth_scopes[auth_config.scope]
  @auth_scopes[auth_config.scope]
end

#acts_as_authorization_model?(type) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/zuul/active_record.rb', line 121

def acts_as_authorization_model?(type)
  ancestors.include?("zuul/active_record/#{type}".camelize.constantize)
end

#acts_as_authorization_permission(args = {}, &block) ⇒ Object

Configure the model to act as a zuul authorization permission

The args parameter is an optional hash of configuration options.



46
47
48
49
50
# File 'lib/zuul/active_record.rb', line 46

def acts_as_authorization_permission(args={}, &block)
  scope = acts_as_authorization_model(args.merge({:permission_class => self.name}), &block)
  prepare_join_classes scope.name
  include Permission
end

#acts_as_authorization_permission?Boolean

Checks if the model is setup to act as a zuul authorization permission

Returns:

  • (Boolean)


131
132
133
# File 'lib/zuul/active_record.rb', line 131

def acts_as_authorization_permission?
  acts_as_authorization_model? :permission
end

#acts_as_authorization_permission_role(args = {}, &block) ⇒ Object

Configure the model to act as a zuul joining model for roles and subjects

The args parameter is an optional hash of configuration options.



73
74
75
76
# File 'lib/zuul/active_record.rb', line 73

def acts_as_authorization_permission_role(args={}, &block)
  scope = acts_as_authorization_model(args.merge({:permission_role_class => self.name}), &block)
  include PermissionRole
end

#acts_as_authorization_permission_role?Boolean

Checks if the model is setup to act as a zuul authorization permission_role

Returns:

  • (Boolean)


156
157
158
# File 'lib/zuul/active_record.rb', line 156

def acts_as_authorization_permission_role?
  acts_as_authorization_model? :permission_role
end

#acts_as_authorization_permission_subject(args = {}, &block) ⇒ Object

Configure the model to act as a zuul joining model for roles and subjects

The args parameter is an optional hash of configuration options.



81
82
83
84
# File 'lib/zuul/active_record.rb', line 81

def acts_as_authorization_permission_subject(args={}, &block)
  scope = acts_as_authorization_model(args.merge({:permission_subject_class => self.name}), &block)
  include PermissionSubject
end

#acts_as_authorization_permission_subject?Boolean

Checks if the model is setup to act as a zuul authorization permission_subject

Returns:

  • (Boolean)


151
152
153
# File 'lib/zuul/active_record.rb', line 151

def acts_as_authorization_permission_subject?
  acts_as_authorization_model? :permission_subject
end

#acts_as_authorization_role(args = {}, &block) ⇒ Object

Configure the model to act as a zuul authorization role

The args parameter is an optional hash of configuration options.



37
38
39
40
41
# File 'lib/zuul/active_record.rb', line 37

def acts_as_authorization_role(args={}, &block)
  scope = acts_as_authorization_model(args.merge({:role_class => self.name}), &block)
  prepare_join_classes scope.name
  include Role 
end

#acts_as_authorization_role?Boolean

Checks if the model is setup to act as a zuul authorization role

Returns:

  • (Boolean)


126
127
128
# File 'lib/zuul/active_record.rb', line 126

def acts_as_authorization_role?
  acts_as_authorization_model? :role
end

#acts_as_authorization_role_subject(args = {}, &block) ⇒ Object

Configure the model to act as a zuul joining model for roles and subjects

The args parameter is an optional hash of configuration options.



89
90
91
92
# File 'lib/zuul/active_record.rb', line 89

def acts_as_authorization_role_subject(args={}, &block)
  scope = acts_as_authorization_model(args.merge({:role_subject_class => self.name}), &block)
  include RoleSubject
end

#acts_as_authorization_role_subject?Boolean

Checks if the model is setup to act as a zuul authorization role_subject

Returns:

  • (Boolean)


146
147
148
# File 'lib/zuul/active_record.rb', line 146

def acts_as_authorization_role_subject?
  acts_as_authorization_model? :role_subject
end

#acts_as_authorization_subject(args = {}, &block) ⇒ Object

Configure the model to act as a zuul authorization subject

The args parameter is an optional hash of configuration options.



55
56
57
58
59
# File 'lib/zuul/active_record.rb', line 55

def acts_as_authorization_subject(args={}, &block)
  scope = acts_as_authorization_model(args.merge({:subject_class => self.name}), &block)
  prepare_join_classes scope.name
  include Subject
end

#acts_as_authorization_subject?Boolean

Checks if the model is setup to act as a zuul authorization subject

Returns:

  • (Boolean)


141
142
143
# File 'lib/zuul/active_record.rb', line 141

def acts_as_authorization_subject?
  acts_as_authorization_model? :subject
end

#prepare_join_classes(scope) ⇒ Object

Sets up the join models for a newly defined scope.

This is similar the the acts_as_authorization_* methods, but it handles all the joining models for a scope.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/zuul/active_record.rb', line 97

def prepare_join_classes(scope)
  scope_config = auth_scope(scope).config

  unless auth_scope(scope).role_subject_class.acts_as_authorization_role_subject?
    auth_scope(scope).role_subject_class.instance_eval do
      acts_as_authorization_role_subject(scope_config.to_h)
    end
  end
  
  if auth_scope(scope).config.with_permissions
    unless auth_scope(scope).permission_subject_class.acts_as_authorization_permission_subject?
      auth_scope(scope).permission_subject_class.instance_eval do
        acts_as_authorization_permission_subject(scope_config.to_h)
      end
    end
    unless auth_scope(scope).permission_role_class.acts_as_authorization_permission_role?
      auth_scope(scope).permission_role_class.instance_eval do
        acts_as_authorization_permission_role(scope_config.to_h)
      end
    end
  end
end