Class: RestfulAcl::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/restful_acl/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Base

Returns a new instance of Base.



7
8
9
10
# File 'lib/restful_acl/base.rb', line 7

def initialize(options)
  options.each{|(k,v)| instance_variable_set "@#{k}", v}
  (@object_id.present?) ? load_actors_from_id : load_actors_from_uri
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



4
5
6
# File 'lib/restful_acl/base.rb', line 4

def action
  @action
end

#controller_nameObject

Returns the value of attribute controller_name.



4
5
6
# File 'lib/restful_acl/base.rb', line 4

def controller_name
  @controller_name
end

#objectObject

Returns the value of attribute object.



4
5
6
# File 'lib/restful_acl/base.rb', line 4

def object
  @object
end

#object_idObject

Returns the value of attribute object_id.



4
5
6
# File 'lib/restful_acl/base.rb', line 4

def object_id
  @object_id
end

#parentObject

Returns the value of attribute parent.



4
5
6
# File 'lib/restful_acl/base.rb', line 4

def parent
  @parent
end

#uriObject

Returns the value of attribute uri.



4
5
6
# File 'lib/restful_acl/base.rb', line 4

def uri
  @uri
end

#userObject

Returns the value of attribute user.



4
5
6
# File 'lib/restful_acl/base.rb', line 4

def user
  @user
end

Instance Method Details

#admin?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/restful_acl/base.rb', line 38

def admin?
  @user.respond_to?("is_admin?") && @user.is_admin?
end

#allowed?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/restful_acl/base.rb', line 42

def allowed?
  return true if admin?

  case @action
    when "index"          then object_class.is_indexable_by(@user, @parent)
    when "new", "create"  then object_class.is_creatable_by(@user, @parent)
    when "show"           then @object.is_readable_by(@user, @parent)
    when "edit", "update" then @object.is_updatable_by(@user, @parent)
    when "destroy"        then @object.is_deletable_by(@user, @parent)
    else check_non_restful_route
  end
end

#check_non_restful_routeObject



55
56
57
58
59
60
61
62
63
# File 'lib/restful_acl/base.rb', line 55

def check_non_restful_route
  if @object.present?
    @object.is_readable_by(@user, @parent)
  elsif object_class.present?
    object_class.is_indexable_by(@user, @parent)
  else
    false # If all else fails, deny access
  end
end

#load_actors_from_idObject



12
13
14
15
# File 'lib/restful_acl/base.rb', line 12

def load_actors_from_id
  @object = object_class.find(@object_id.to_i)
  @parent = @object.get_mom if object_class.has_parent?
end

#load_actors_from_uriObject



17
18
19
20
# File 'lib/restful_acl/base.rb', line 17

def load_actors_from_uri
  @parent = load_parent_from_uri if object_class.has_parent?
  @object = (object_class.is_singleton?) ? load_singleton_object : nil
end

#load_parent_from_uriObject



26
27
28
29
30
31
32
# File 'lib/restful_acl/base.rb', line 26

def load_parent_from_uri
  parent_klass = object_class.mom.to_s
  bits         = @uri.split('/')
  parent_id    = bits.at(bits.index(parent_klass.pluralize) + 1)

  parent_klass.classify.constantize.find(parent_id.to_i)
end

#load_singleton_objectObject



22
23
24
# File 'lib/restful_acl/base.rb', line 22

def load_singleton_object
  @parent.send(object_class.to_s.tableize.singularize.to_sym)
end

#object_classObject



34
35
36
# File 'lib/restful_acl/base.rb', line 34

def object_class
  @object_class ||= @controller_name.classify.demodulize.constantize
end