Class: Zuul::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/zuul/configuration.rb

Defined Under Namespace

Classes: ClassStruct

Constant Summary collapse

PRIMARY_AUTHORIZATION_CLASSES =
{
  :subject_class => :user,
  :role_class => :role,
  :permission_class => :permission
}
AUTHORIZATION_JOIN_CLASSES =
{
  :role_subject_class => :role_user,
  :permission_role_class => :permission_role,
  :permission_subject_class => :permission_user
}
DEFAULT_AUTHORIZATION_CLASSES =
PRIMARY_AUTHORIZATION_CLASSES.merge(AUTHORIZATION_JOIN_CLASSES)
DEFAULT_CONFIGURATION_OPTIONS =
{
  :acl_default => :deny, # :allow, :deny
  :acl_mode => :raise, # :raise, :quiet
  :acl_collect_results => false,
  :subject_method => :current_user,
  :force_context => false,
  :scope => :default,
  :with_permissions => true
}

Instance Method Summary collapse

Instance Method Details

#changedObject



35
36
37
38
39
# File 'lib/zuul/configuration.rb', line 35

def changed
  @changed = {}
  to_hash.each { |key,val| @changed[key] = [@saved_state[key], val] if @saved_state[key] != val }
  @changed
end

#changed?(key) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/zuul/configuration.rb', line 41

def changed?(key)
  changed.has_key?(key)
end

#classesObject



97
98
99
100
101
102
103
# File 'lib/zuul/configuration.rb', line 97

def classes
  cstruct = ClassStruct.new(*DEFAULT_AUTHORIZATION_CLASSES.keys).new
  DEFAULT_AUTHORIZATION_CLASSES.keys.each do |key|
    cstruct.send("#{key.to_s}=", instance_variable_get("@#{key.to_s}"))
  end
  cstruct
end

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



45
46
47
48
49
50
51
# File 'lib/zuul/configuration.rb', line 45

def configure(args={}, &block)
  save_state
  configure_with_args args
  configure_with_block &block
  configure_join_classes if PRIMARY_AUTHORIZATION_CLASSES.keys.any? { |key| changed?(key) }
  self
end

#configure_join_classesObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/zuul/configuration.rb', line 63

def configure_join_classes
  [[:role, :subject], [:permission, :subject], [:permission, :role]].each do |join_types|
    join_key = "#{join_types.sort[0].to_s}_#{join_types.sort[1].to_s}_class".to_sym
    next if changed?(join_key) # don't override join table if it was provided

    namespaces = []
    join_class = join_types.map do |class_type|
      type_class = instance_variable_get "@#{class_type.to_s}_class"
      namespace = (type_class.is_a?(Class) ? type_class.name : type_class.to_s.camelize).split("::")
      class_name = namespace.slice!(namespace.length-1)
      namespaces << namespace.join("::") if namespace.length > 0
      class_name
    end.sort!.join("")
    
    join_class = "#{namespaces[0]}::#{join_class}" if namespaces.length > 0 && namespaces.all? { |ns| ns == namespaces[0] }
    instance_variable_set "@#{join_key.to_s}", join_class
  end
end

#configure_with_args(args) ⇒ Object



53
54
55
56
57
# File 'lib/zuul/configuration.rb', line 53

def configure_with_args(args)
  args.select { |k,v| DEFAULT_AUTHORIZATION_CLASSES.keys.concat(DEFAULT_CONFIGURATION_OPTIONS.keys).include?(k) }.each do |key,val|
    instance_variable_set "@#{key.to_s}", val
  end
end

#configure_with_block(&block) ⇒ Object



59
60
61
# File 'lib/zuul/configuration.rb', line 59

def configure_with_block(&block)
  self.instance_eval(&block) if block_given?
end

#join_classesObject



113
114
115
116
117
118
119
# File 'lib/zuul/configuration.rb', line 113

def join_classes
  cstruct = ClassStruct.new(*AUTHORIZATION_JOIN_CLASSES.keys).new
  AUTHORIZATION_JOIN_CLASSES.keys.each do |key|
    cstruct.send("#{key.to_s}=", instance_variable_get("@#{key.to_s}"))
  end
  cstruct
end

#primary_classesObject



105
106
107
108
109
110
111
# File 'lib/zuul/configuration.rb', line 105

def primary_classes
  cstruct = ClassStruct.new(*PRIMARY_AUTHORIZATION_CLASSES.keys).new
  PRIMARY_AUTHORIZATION_CLASSES.keys.each do |key|
    cstruct.send("#{key.to_s}=", instance_variable_get("@#{key.to_s}"))
  end
  cstruct
end

#save_stateObject



82
83
84
85
# File 'lib/zuul/configuration.rb', line 82

def save_state
  @saved_state = clone.to_hash
  @changed = {}
end

#to_hashObject Also known as: to_h



88
89
90
91
92
93
94
# File 'lib/zuul/configuration.rb', line 88

def to_hash
  h = {}
  DEFAULT_AUTHORIZATION_CLASSES.keys.concat(DEFAULT_CONFIGURATION_OPTIONS.keys).each do |key|
    h[key] = instance_variable_get "@#{key.to_s}"
  end
  h
end