11. AuthLogicを使って認証 (1)
user_session.rb: ユーザを探す(ユーザ登録に利用)
class UserSession < AuthLogic::Session::Base
find_by_login_method :find_or_create_from_ldap
verify_password_method : valid_ldap_credentials?
end
認証をLDAP経由に変更
12. AuthLogicを使って認証 (2)
user.rb(抜粋):
class User < ActiveRecord::Base
acts_as_authentic do |c|
c.validate_password_field = false
end
class << self
def find_or_create_from_ldap(loginname)
find_by_login(loginname) ||
create_from_ldap_if_valid(loginname)
end
13. AuthLogicを使って認証 (3)
user.rb(抜粋つづき):
def create_from_ldap_if_valid(loginname)
begin
if u = LdapUser.find(loginname)
User.create(
:login => loginname,
:fullname => u.displayName)
end
rescue ActiveLdap::EntryNotFount
nil
end
end