3

特定の Google アカウントのみにログオンを許可することはできますか? たとえばmyname@mycompany.com、Google 経由のホストです (実際には Google アカウントです)。を持つユーザーだけ@mycompanyがログオンできるようにしたいのですが、これは可能ですか? あなたはdeviseまたはgoogle apiでこれを行いますか?

ありがとうございました :)

4

1 に答える 1

4

omn​​iauth-google-oauth2を使用している場合はhd、初期化中にオプションに値を指定することで、ドメイン制限を実行できます。

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET'], {
    scope: 'email, profile',
    hd: 'mycompany.com'
  }
end

コールバックを処理しているコントローラーでこれを処理することもできます。で提供される値に応じて、ユーザーを拒否できますrequest.env["omniauth.auth"]

class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def google_oauth2
    auth_details = request.env["omniauth.auth"]
    if auth_details.info['email'].split("@")[1] == "yourdomain.com"
      # do all the bits that come naturally in the callback controller
      user = User.from_omniauth(request.env["omniauth.auth"])
      if user.persisted?
        flash.notice = "Signed in Through Google!"
        sign_in_and_redirect user
      else
        session["devise.user_attributes"] = user.attributes
        flash.notice = "You are almost Done! Please provide a password to finish setting up your account"
        redirect_to new_user_registration_url
      end
    else
      # This is where you turn away the poor souls who do not match your domain
      render :text => "We're sorry, at this time we do not allow access to our app."
    end
  end
end
于 2013-09-17T22:25:37.823 に答える