3

我用ruby​​cas-server搭建了一个 sso 系统,用 devise 做用户的系统,但现在我的问题是,rubycas-server只有一个字段,比如 'email' ,我想用 'email' / 'tel' / 'nickname ' 用于用户登录。wiki中没有更多信息

authenticator:
 class: CASServer::Authenticators::SQLBcrypt
 database:
   adapter: mysql2
   database: xxxx
   username: root
   password: xxxx
   host: localhost
 user_table: users
 username_column: email # tel/nickname
 password_column: encrypted_password

对不起我的英语不好!帮助我,非常感谢!

4

2 回答 2

4

恐怕这不可能。从官方存储库中可以看出,此身份验证器仅将给定的列名与用户名匹配:

  def matching_users
    results = user_model.find(:all, :conditions => ["#{username_column} = ?", @username])
    results.select { |user| BCrypt::Password.new(user.send(password_column.to_sym)) == @password }
  end

对于您的情况,最好的办法是编写匹配的自定义身份验证器email/tel/nickname。然而,这是一个非常艰难的登录名,考虑一些更用户友好的。

于 2014-11-12T10:08:04.930 回答
1

修改rubycas-server/lib/casserver/authenticators/sql_bcrypt.rb中的以下代码

  def matching_users
    results = user_model.find(:all, :conditions => ["#{username_column} = ?", @username])
    results.select { |user| BCrypt::Password.new(user.send(password_column.to_sym)) == @password }
  end

  def matching_users
    if username_column.include?(',')
      columns = username_column.split(',')
      sql = ''
      conditions = []
      columns.each do |field|
        if sql.length != 0
          sql += ' or '
        end
        sql += "#{field} = ?"
        conditions << @username
      end
      conditions.unshift(sql)
    else
      conditions = ["#{username_column} = ?", @username]
    end
    results = user_model.find(:all, :conditions => conditions)
    results.select { |user| BCrypt::Password.new(user.send(password_column.to_sym)) == @password }
  end

然后你可以像这样在 config.yml 中使用 mutli-fieds:

authenticator:
 class: CASServer::Authenticators::SQLBcrypt
 database:
   adapter: mysql2
   database: test
   username: root
   password: map
   host: localhost
 user_table: users
 username_column: email,nickname,tel
 password_column: encrypted_password
于 2015-01-26T07:00:53.170 回答