我的应用程序的用户将很快切换电子邮件域(公司名称更改),我希望他们能够保留他们的数据。但是,我的应用程序正在使用LDAP,因此它不仅仅是我可以让他们在帐户页面中更新的设置。如果该员工编号属于当前用户,我希望它检查他们的员工编号并自动更改他们的登录/电子邮件(数据库的唯一标识符)。这就是我到目前为止所拥有的:
用户模型:
class User < ActiveRecord::Base
devise :ldap_authenticatable, :rememberable, :trackable
validate :check_migrate
def check_migrate
if id.nil? # User is new
uid_number = Devise::LDAP::Adapter.get_ldap_param(self.login, 'uidNumber').first
old_user = User.where(uid_number: uid_number).first
if old_user
old_user.update!(login: self.login) # Gets rolled back after this method
errors.add(:login, "User login migrated to new email! Please sign in again")
else
self.uid_number = uid_number
end
end
end
end
routes.rb中:
Rails.application.routes.draw do
devise_for :users
end
但我希望它返回到登录页面,其中包含来自我添加的错误的精彩flash消息。此外,它没有持久化old_user.update!更改。它似乎在方法内部工作,但在错误之后,它会回到旧值。
这样做有好办法吗? 在此先感谢您的任何帮助
答案 0 :(得分:1)
You will need to add your own session controller that inherits from Devise's, and modify the create method to your liking:
class SessionsController < Devise::SessionsController
def create
# change to whatever
end
end
You also should modify your routes to make sure your server picks this up. This is the relevant section in my routes (you can see I modified things a lot):
devise_for :users, :controllers => {registrations: 'registrations', :sessions => 'sessions', :confirmations => 'confirmations', :passwords => 'passwords'}
Good luck