2013-08-17 21:48:01 +00:00
|
|
|
# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2014-05-03 12:34:36 +00:00
|
|
|
class Auth
|
|
|
|
include ApplicationLib
|
2013-08-17 21:48:01 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
authenticate user via username and password
|
|
|
|
|
2016-02-27 16:00:04 +00:00
|
|
|
result = Auth.check(username, password, user)
|
2013-08-17 21:48:01 +00:00
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = user_model # if authentication was successfully
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.check(username, password, user)
|
|
|
|
|
|
|
|
# use std. auth backends
|
|
|
|
config = [
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
adapter: 'Auth::Internal',
|
2013-08-17 21:48:01 +00:00
|
|
|
},
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
adapter: 'Auth::Developer',
|
2013-08-17 21:48:01 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
# added configured backends
|
2016-02-27 16:00:04 +00:00
|
|
|
Setting.where(area: 'Security::Authentication').each {|setting|
|
2015-09-25 14:37:55 +00:00
|
|
|
if setting.state_current[:value]
|
|
|
|
config.push setting.state_current[:value]
|
2013-08-17 21:48:01 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# try to login against configure auth backends
|
|
|
|
user_auth = nil
|
|
|
|
config.each {|config_item|
|
|
|
|
next if !config_item[:adapter]
|
|
|
|
|
|
|
|
# load backend
|
2016-02-27 16:00:04 +00:00
|
|
|
backend = load_adapter(config_item[:adapter])
|
2015-05-05 14:36:05 +00:00
|
|
|
next if !backend
|
2013-08-17 21:48:01 +00:00
|
|
|
|
2016-02-27 16:00:04 +00:00
|
|
|
user_auth = backend.check(username, password, config_item, user)
|
2013-08-17 21:48:01 +00:00
|
|
|
|
2015-05-07 09:04:40 +00:00
|
|
|
# auth not ok
|
|
|
|
next if !user_auth
|
2013-08-17 21:48:01 +00:00
|
|
|
|
2015-05-07 09:04:40 +00:00
|
|
|
Rails.logger.info "Authentication against #{config_item[:adapter]} for user #{user_auth.login} ok."
|
2015-05-05 05:55:06 +00:00
|
|
|
|
2015-05-07 09:04:40 +00:00
|
|
|
# remember last login date
|
|
|
|
user_auth.update_last_login
|
2013-08-17 21:48:01 +00:00
|
|
|
|
2015-05-07 09:04:40 +00:00
|
|
|
return user_auth
|
2013-08-17 21:48:01 +00:00
|
|
|
}
|
2015-05-01 07:48:10 +00:00
|
|
|
nil
|
|
|
|
end
|
2013-08-17 21:48:01 +00:00
|
|
|
end
|