【问题标题】:Admin user administration with Devise使用 Devise 管理用户
【发布时间】:2011-06-13 22:55:55
【问题描述】:

我是第一次尝试设计。我想做的一件事是为管理员用户提供一个界面来创建、查找和编辑用户。这就是我可能出错的地方。

我创建了一个继承自 ApplicationController 的 PeopleController 类,该类列出人员并提供用于创建和更新用户的方法和视图。一切正常,只有一个例外。当管理员用户更新他们自己的记录时,会话被清除,他们必须在保存后重新登录。

在这个应用程序中,我没有使用可注册模块。只有管​​理员用户可以创建新用户。设计提供用户管理工具的正确方法是什么。创建我自己的控制器似乎是一条错误的道路。

提前感谢您的帮助。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 devise


    【解决方案1】:

    非常感谢您的帮助。这基本上正是我正在做的事情。我发现了一条线索,帮助我解决了用户在此 wiki 中编辑自己的记录时会话被清除的问题:

    https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-account-without-providing-a-password

    这是我需要的行:

    sign_in resource_name, resource, :bypass => true
    

    此方法位于 Devise::Controllers::Helpers 中,因此我在控制器中执行此操作。

    class PeopleController < ApplicationController
       include Devise::Controllers::Helpers
    

    然后在我的更新方法中,我只在 current_user.id 等于正在编辑的 id 时才调用它:

    def update
      @person = User.find(params[:id])
      if @person.update_attributes(params[:user])
        sign_in @person, :bypass => true if current_user.id == @person.id
        redirect_to  person_path(@person), :notice  => "Successfully updated user."
      else
        render :action => 'edit'
      end
    end
    

    现在如果当前用户编辑自己的记录,会话在保存后会恢复。

    再次感谢您的回复。

    【讨论】:

      【解决方案2】:

      这就是我在我的一个应用中管理用户的方式。我只有一个用

      生成的User
      rails g devise User
      

      我在此迁移中添加了 role 列:

      class AddRoleToUser < ActiveRecord::Migration
        def change
          add_column :users, :role, :string, :default => "client"
        end
      end
      

      还有我的User 模特:

      class User < ActiveRecord::Base
        # Include default devise modules. Others available are:
        # :token_authenticatable, :confirmable, :lockable and :timeoutable
        devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable
      
        # Setup accessible (or protected) attributes for your model
        attr_accessible :email, :password, :password_confirmation, :remember_me
      
        def admin?
          self.role == "admin"
        end
      end
      

      然后,要创建新用户,您所要做的就是在控制器中提供一个自定义方法(甚至可能是子类 Devise::RegistrationsController),如下所示:

      # some_controller.rb
      def custom_create_user
        if current_user.admin?
          User.create(:email => params[:email], password => params[:password])
          redirect_to(some_path, :notice => 'sucessfully updated user.')
        else
          redirect_to(some_other_path, :notice => 'You are not authorized to do this.')
        end
      end
      

      【讨论】:

      • 我还建议使用单独的“角色”模型,以便用户在必要时可以拥有多个角色。可能也会让一切变得更容易。然后是一个连接模型,可能称为“UserRole”,其中包含 user_id 和 role_id。
      • 确实如此,虽然我通常在需要定义多个角色或复杂权限时使用cancan
      • 我也使用 CanCan,但我仍然使用独立角色模型。查看 Ryan Bates 制作的这个很棒的 wiki 页面:github.com/ryanb/cancan/wiki/Separate-Role-Model
      猜你喜欢
      • 1970-01-01
      • 2011-04-22
      • 1970-01-01
      • 1970-01-01
      • 2012-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多