trabajo-afectivo/lib/auth.rb

116 lines
2 KiB
Ruby
Raw Normal View History

2016-10-19 03:11:36 +00:00
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
2013-08-17 21:48:01 +00:00
class Auth
include ApplicationLib
2013-08-17 21:48:01 +00:00
=begin
checks if a given user can login. Checks for
- valid user
- active state
- max failed logins
2013-08-17 21:48:01 +00:00
result = Auth.can_login?(user)
2013-08-17 21:48:01 +00:00
returns
result = true | false
2013-08-17 21:48:01 +00:00
=end
def self.can_login?(user)
return false if !user.is_a?(User)
return false if !user.active?
return true if !user.max_login_failed?
Rails.logger.info "Max login failed reached for user #{user.login}."
false
end
=begin
checks if a given user and password match against multiple auth backends
- valid user
- active state
- max failed logins
result = Auth.valid?(user, password)
returns
result = true | false
=end
def self.valid?(user, password)
# try to login against configure auth backends
backends.any? do |config|
next if !backend_validates?(
config: config,
user: user,
password: password,
)
Rails.logger.info "Authentication against #{config[:adapter]} for user #{user.login} ok."
# remember last login date
user.update_last_login
true
end
end
=begin
returns a list of all Auth backend configurations
result = Auth.backends
returns
result = [
{
adapter: 'Auth::Internal',
},
{
adapter: 'Auth::Developer',
},
...
]
=end
def self.backends
2013-08-17 21:48:01 +00:00
# use std. auth backends
config = [
{
adapter: 'Auth::Internal',
2013-08-17 21:48:01 +00:00
},
{
adapter: 'Auth::Developer',
2013-08-17 21:48:01 +00:00
},
]
# added configured backends
Setting.where(area: 'Security::Authentication').each do |setting|
next if setting.state_current[:value].blank?
config.push setting.state_current[:value]
end
2013-08-17 21:48:01 +00:00
config
end
2013-08-17 21:48:01 +00:00
def self.backend_validates?(config:, user:, password:)
return false if !config[:adapter]
2013-08-17 21:48:01 +00:00
instance = config[:adapter].constantize.new(config)
2013-08-17 21:48:01 +00:00
instance.valid?(user, password)
2015-05-01 07:48:10 +00:00
end
private_class_method :backend_validates?
2013-08-17 21:48:01 +00:00
end