LoginSignup
0
0

More than 5 years have passed since last update.

ErrbitをLDAP連携してログインできるようにする

Posted at

ErrbitのREADMEを見ると、Configuring LDAP authenticationというのがあり、ログイン認証にLDAPを使うことができるようです。
Errbitにログインするために一々アカウントを作るのが面倒なのと、LDAPサーバがシステム内にあるので連携してみることにしました。

大体はConfiguring LDAP authenticationの通りに設定すれば良いのですが、ところどころ設定に躓いたのでまとめました。

USER_HAS_USERNAMEをtrueに変更します。

# .env
ERRBIT_USER_HAS_USERNAME=true

Devise LDAP Authenticatableというgemをインストールします。

# Gemfile
# 追加
gem "devise_ldap_authenticatable", :git => "git://github.com/cschiewek/devise_ldap_authenticatable.git"
$ bundle install --path=vendor/bundle
$ bundle exec script/rails generate devise_ldap_authenticatable:install
      create  config/ldap.yml
      insert  config/initializers/devise.rb
        gsub  app/models/user.rb
      insert  app/controllers/application_controller.rb

config.ldap_create_userを有効にします。

# config/initializers/devise.rb
Devise.setup do |config|
...(省略)...
  config.ldap_create_user = true
...(省略)...
end

devise_ldap.rbを新規作成します。

# config/initializers/devise_ldap.rb
Errbit::Config.devise_modules << :ldap_authenticatable

LDAP認証後にErrbitでユーザ登録させます。
上記URLの説明だとLDAPに登録されている、givenName(名)、sn(姓)、mail(メールアドレス)からユーザ情報を登録させていますが、管理しているLDAPサーバにそれらの情報は登録していないので下記のように修正します。
また、タイムゾーンとadmin権限も合わせて設定します。

注意点として、ldap_before_saveメソッドはpublicで定義する必要があるので、privateより上で定義します。

# app/models/user.rb
  # 追加
  def ldap_before_save
    name = Devise::LDAP::Adapter.get_ldap_param(self.username, "cn")
    self.name = name.first
    self.email = self.name + "@foo.com"
    self.time_zone = "Tokyo"
    self.admin = true
  end

LDAPサーバへの接続設定を行います。
ErrbitをProduction環境で動かしているので以下のように設定します。

# config/ldap.yml
production:
  host: <LDAPサーバのホスト名>
  port: 389
  attribute: cn
  base: ou=people,dc=foo,dc=com
  admin_user: cn=admin,dc=test,dc=com # 設定する必要なし
  admin_password: admin_password      # 設定する必要なし
  ssl: false
  # <<: *AUTHORIZATIONS

ここまで設定したら、Unicornを再起動します。
私の場合、daemontoolsで起動しているので、プロセスをバッサリ切っても自動的に起動するので問題ありません。

$ sudo kill -QUIT $(cat tmp/unicorn.pid)

起動完了したら、Errbitにアクセスして、ユーザ名(メールアドレスではない)とパスワードを入力すれば、Errbitにユーザ登録されるはずです。
何かエラーがでたら、Railsのログを見て対応します。

参考URL

元記事はこちら

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0