Module: LoginSystem::ClassMethods

Defined in:
lib/login_system.rb

Instance Method Summary collapse

Instance Method Details

#controller_permissionsObject



111
112
113
# File 'lib/login_system.rb', line 111

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

#login_requiredObject



95
96
97
98
99
# File 'lib/login_system.rb', line 95

def 
  unless 
    prepend_before_action :authenticate, :authorize
  end
end

#login_required?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/login_system.rb', line 91

def 
  filter_chain.any? { |f| f.method == :authenticate || f.method == :authorize }
end

#only_allow_access_to(*args) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/login_system.rb', line 101

def only_allow_access_to(*args)
  options = {}
  options = args.pop.dup if args.last.is_a?(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)


115
116
117
118
119
120
121
122
123
124
125
# File 'lib/login_system.rb', line 115

def user_has_access_to_action?(user, action, instance = new)
  permissions = controller_permissions[action.to_s.intern]
  if allowed_roles = permissions[:when]
    allowed_roles = [allowed_roles].flatten
    user.present? ? allowed_roles.any? { |role| user.role?(role) } : false
  elsif condition_method = permissions[:if]
    instance.send(condition_method)
  else
    true
  end
end