我想让它 devise_ldap_authenticatable。 只为每个用户创建一条记录, 登录时给定一个用户名. 我遇到的问题是,devise_ldap_authenticatable保存的是同一个用户名,但是是大写字母。对于我们的用户来说,拥有重复的用户配置文件变得很麻烦。当两条记录都用大写字母和小写字母创建时,我只收到一个 "已完成302发现"。尽管活动目录用户名大写,但我如何才能为每个用户只创建一条记录?
如果一个用户用dwilliams和Dwilliams登录,就不应该在数据库中创建和存储一个新用户。
class User < ApplicationRecord
devise :ldap_authenticatable...
before_save :ldap_before_save
validates :username, presence: true, uniqueness: {case_sensitive: true}
extend FriendlyId
friendly_id :username, use: :slugged
def ldap_before_save
self.email = Devise::LDAP::Adapter.get_ldap_param(self.username, "mail").try(:first)
self.name = Devise::LDAP::Adapter.get_ldap_param(self.username, "name").try(:first)
end
end
Rails日志
LDAP: LDAP search for login: sAMAccount=dwilliams
LDAP: search yielded 1 matches
LDAP: Requested param mail has value ["[email protected]"]
LDAP: LDAP search login: sAMAccountName=dwilliams
LDAP: LDAP search yielded 1 matches
LDAP: Requested param has value ["David Williams"]
(0.4ms) COMMIT
Redirected to
Completed 302 Found
.....
LDAP: LDAP search for login: sAMAccount=Dwilliams
LDAP: search yielded 1 matches
LDAP: Authorizing user CN=David Williams, OU....
(0.4ms) BEGIN
User Update...
(1.5ms) COMMIT
你应该验证 uniqueness
不区分大小写
validates :username, presence: true, uniqueness: { case_sensitive: false }
如果都不行,那么你也可以在后台数据库中添加验证规则,避免用户名列的值重复。只是我的两点意见