使用Devise进行管理员用户管理 [英] Admin user administration with Devise

查看:105
本文介绍了使用Devise进行管理员用户管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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



提前感谢您的帮助。

解决方案

非常感谢你的帮助。这本质上正是我在做什么我发现一个线索帮助我解决了在这个维基编辑他们自己的记录时清除用户会话的问题:



https://github.com/plataformatec/devise/wiki/如何: - 允许用户编辑 - 他们的帐户 - 不提供密码



这是我需要的行:

  sign_in resource_name,resource,:bypass => true 

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

  class PeopleController< ApplicationController 
包括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 => 成功更新用户。
else
render:action => 'edit'
end
end

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



再次感谢您的回复。


I am trying out Devise for the first time. One of the things that I wanted to do is provide an interface for Admin users to create, find and edit users. Here's where I may have gone wrong.

I created a PeopleController class which inherits from ApplicationController that lists people and provides methods and views for creating and updating users. Everything works fine with one exception. When the admin user updates their own record, the session is cleared and they have to login again after saving it.

In this application I'm not using the registerable module. Only an admin user can create new users. What is the right way in devise to provide user management tools. Creating my own controller seems to have been the wrong path to take.

Thanks in advance for your help.

解决方案

Thank you very much for the help. This is essentially exactly what I am doing. I discovered a clue that helped me solve the problem of the user's session being cleared when they edit their own record in this wiki:

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

This is the line I needed:

sign_in resource_name, resource, :bypass => true

This method is located in Devise::Controllers::Helpers so I did this in my controller.

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

Then in my update method I call it only if the current_user.id equals the id that is being edited:

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

Now if the current user edits their own record, the session is restored after it is saved.

Thanks again for your responses.

这篇关于使用Devise进行管理员用户管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆