Class: ActiveAcl::Acts::AccessObject::ObjectHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/active_acl/handler/object_handler.rb

Overview

handels grouped objects the group is a nested_set

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, options = {}) ⇒ ObjectHandler



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/active_acl/handler/object_handler.rb', line 10

def initialize(klass,options={})
  @klass = klass
  if options[:grouped_by]
    @group_class_name = options[:grouped_by].to_s.classify
    @group_table_name=@group_class_name.constantize.table_name
    @join_table = options[:join_table] || [klass.name.pluralize.underscore.gsub(/\//,'_'), group_class_name.pluralize.underscore.gsub(/\//,'_')].sort.join('_')  
    @foreign_key = options[:foreign_key] || "#{klass.name.demodulize.underscore}_id" 
    @association_foreign_key = options[:association_foreign_key] || "#{group_class_name.demodulize.underscore}_id"
    @habtm = options[:habtm] || (options[:grouped_by].to_s.demodulize.singularize != options[:grouped_by].to_s.demodulize)
  end
  
  #set the SQL fragments
  requester_query
  target_query
end

Instance Attribute Details

#association_foreign_keyObject (readonly)

:nodoc:



8
9
10
# File 'lib/active_acl/handler/object_handler.rb', line 8

def association_foreign_key
  @association_foreign_key
end

#foreign_keyObject (readonly)

:nodoc:



8
9
10
# File 'lib/active_acl/handler/object_handler.rb', line 8

def foreign_key
  @foreign_key
end

#group_class_nameObject (readonly)

:nodoc:



8
9
10
# File 'lib/active_acl/handler/object_handler.rb', line 8

def group_class_name
  @group_class_name
end

#group_table_nameObject (readonly)

:nodoc:



8
9
10
# File 'lib/active_acl/handler/object_handler.rb', line 8

def group_table_name
  @group_table_name
end

#join_tableObject (readonly)

:nodoc:



8
9
10
# File 'lib/active_acl/handler/object_handler.rb', line 8

def join_table
  @join_table
end

#klassObject (readonly)

:nodoc:



8
9
10
# File 'lib/active_acl/handler/object_handler.rb', line 8

def klass
  @klass
end

#query_t_selectObject (readonly)

Returns the value of attribute query_t_select.



98
99
100
# File 'lib/active_acl/handler/object_handler.rb', line 98

def query_t_select
  @query_t_select
end

#query_t_whereObject (readonly)

Returns the value of attribute query_t_where.



98
99
100
# File 'lib/active_acl/handler/object_handler.rb', line 98

def query_t_where
  @query_t_where
end

Instance Method Details

#delete_cached(requester) ⇒ Object

destroy the 2nd level cache



94
95
96
# File 'lib/active_acl/handler/object_handler.rb', line 94

def delete_cached(requester)
  cache.delete(requester_cache_id(requester))
end

#get_instance_cache(requester) ⇒ Object

gets the instance cache from the background store or a hash



90
91
92
# File 'lib/active_acl/handler/object_handler.rb', line 90

def get_instance_cache(requester)
  cache.get(requester_cache_id(requester)) || {}
end

#group_handlerObject



35
36
37
# File 'lib/active_acl/handler/object_handler.rb', line 35

def group_handler
  ActiveAcl::GROUP_CLASSES[@group_class_name]
end

#grouped?Boolean



28
29
30
# File 'lib/active_acl/handler/object_handler.rb', line 28

def grouped?
  !!@group_class_name
end

#habtm?Boolean



25
26
27
# File 'lib/active_acl/handler/object_handler.rb', line 25

def habtm?
  @habtm
end

#has_privilege?(requester, privilege, target = nil) ⇒ Boolean

checks the privilege of a requester on a target (optional)



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/active_acl/handler/object_handler.rb', line 40

def has_privilege?(requester,privilege,target=nil)
  value = get_cached(requester,privilege,target)
  
  return value unless value.nil? #got the cached and return
  #todo check cash l2
  
  vars={'requester_id' => requester.id}
  vars['requester_group_id'] = requester.send(association_foreign_key) if !self.habtm? && self.grouped?
  sql = ''
  sql << query_r_select
  if target
    t_handler=target.active_acl_handler
    
    sql << t_handler.query_t_select
    sql << "\n WHERE "
    sql << query_r_where_3d
    sql << t_handler.query_t_where
    sql << "\n ORDER BY "
    
    #TODO: ordering is a mess (use an array?)
    order = (grouped? ? order_by_3d.dup : [])
    if t_handler.grouped? 
      order << "(CASE WHEN t_g_links.acl_id IS NULL THEN 0 ELSE 1 END) ASC"
      order << t_handler.group_handler.order_by(target,true)
      vars['target_group_id'] = target.send(t_handler.association_foreign_key) unless t_handler.habtm?
    end
    order << 'acls.updated_at DESC'
    sql << order.join(',')
    
    sql << " LIMIT 1"
    vars['privilege_id'] = privilege.id
    vars['target_id'] = target.id
    vars['target_type'] = target.class.base_class.name
  else
    sql << " WHERE "
    sql << query_r_where_2d
    sql << "\n ORDER BY "
    sql << order_by_2d
  end
  
  #replacing the vars in the SQL
  sql=sql.gsub(/%{[^}]+}/) do |var|
    vars[var[2..-2]] || var
  end
  
  results = ActiveAcl::OPTIONS[:db].query(sql) #get the query from the db
  value=set_cached(requester,privilege,target,results)
  return value
end

#klass_nameObject



32
33
34
# File 'lib/active_acl/handler/object_handler.rb', line 32

def klass_name 
  klass.base_class.name
end