Module: LoginSystem::ClassMethods

Defined in:
lib/login_system.rb

Instance Method Summary collapse

Instance Method Details

#controller_permissionsObject



55
56
57
# File 'lib/login_system.rb', line 55

def controller_permissions
  @controller_permissions ||= Hash.new { |h,k| h[k.to_s.intern] = Hash.new }
end

#no_login_requiredObject



40
41
42
43
# File 'lib/login_system.rb', line 40

def 
  # skip_before_filter :authenticate_user
  # Not working in some systems
end

#only_allow_access_to(*args) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/login_system.rb', line 45

def only_allow_access_to(*args)
  options = {}
  options = args.pop.dup if args.last.kind_of?(Hash)
  options.symbolize_keys!
  actions = args.map { |a| a.to_s.intern }
  actions.each do |action|
    controller_permissions[action] = options
  end
end

#user_has_access_to_action?(user, action, instance = new) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/login_system.rb', line 59

def user_has_access_to_action?(user, action, instance=new)
  result = false
  
  permissions = controller_permissions[action.to_s.intern]
  case
  when permissions[:when].present?
    allowed_roles = [permissions[:when]].flatten
    
    # We no longer have an admin role, if it's in there replace it with administrator
    allowed_roles.map! { |role| role == :admin ? :administrator : role }
    
    result = allowed_roles.include? user.class_name.downcase.to_sym
  when permissions[:if].present?
    result = instance.send(permissions[:if])
  else
    result = [:administrator,:designer,:user].include? user.class_name.downcase.to_sym
  end
  
  return result
end