簡體   English   中英

如何使管理員用戶使用Rails 3中的Google Gem進行注冊

[英]How to make an admin user using sign up with google gem in rails 3

我正在制作一個我只想使用Google登錄的應用程序(因為我正在使用其他Google api,並且我想同步帳戶。)我也希望管理員也可以使其他用戶成為管理員。

我想讓一些用戶管理員,但是如果他們使用Google登錄,我該如何在控制器中設置此異常? 最好將特定用戶保存在數據庫中? 我好困惑! 請幫忙。

現在這就是我要為管理員准備的

omn​​iauth用於檢查Google登錄

class OmniauthCallbacksController < ApplicationController
    def google_oauth2
        if auth_details.info['email'].split("@")[1] == "company_name.net"
            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 = "Please provide a password"
                redirect_to new_user_registration_url
            end
        else
            render :text => "Sorry this site is for company_name employees only"
        end
    end
end

管理員到用戶的遷移

class AddAdminToUser < ActiveRecord::Migration
  def change
    add_column :users, :admin, :boolean, :default => true
  end
end

用戶角色表

class CreateRoles < ActiveRecord::Migration
  def change
    create_table :roles do |t|
      t.string :name

      t.timestamps
    end
  end
end

HaveAndBelongToMany遷移

class UsersHaveAndBelongToManyRoles < ActiveRecord::Migration
  def self.up
    create_table :roles_users, :id => false do |t|
        t.references :role, :user
    end
  end

  def self.down
    drop_table :roles_users
  end
end

首先,我認為您可能希望將新用戶默認為不是管理員,並且僅當當前管理員稍后將其設為管理員時,才將該boolean設置為true。

為了對人員進行身份驗證,建議您將omn​​iauth gem與一種Google策略結合使用。 它將有助於根據用戶的Google帳戶驗證其憑據。 然后,您可以在自己的數據庫中存儲與該應用相關的有關該用戶的任何信息,包括他們是否是管理員,並使用該信息在您的應用中進行授權。 希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM