4

我正在使用 Devise 和 CanCan 进行用户身份验证和管理角色,这些角色限制了某些用户对我的 Rails 4 应用程序部分的访问。

我在更新用户时遇到了一些问题。更新工作正常,数据库中的用户对象得到更新,但我的用户会话在redirect_to我的用户显示操作之后丢失。current_user变为nil这意味着 CanCan 限制了对用户显示操作的访问。

为什么更新后会current_user变成nil,而其他操作(例如创建、销毁等)不会发生这种情况?

这些是我的用户模型中的设计设置:

devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, :authentication_keys => [:login]

这是我的 users_controller.rb 的更新方法:

class UsersController < ApplicationController

  load_and_authorize_resource
  before_filter :authenticate_user!

  def update
    @user = User.find(params[:id])
    if params[:user][:password].blank?
        params[:user].delete(:password)
    end

    respond_to do |format|
      if @user.update_attributes(user_params)
        format.html { redirect_to user_path, :notice => 'User was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render :action => "edit" }
        format.json { render :json => @user.errors, :status => :unprocessable_entity }
      end
    end
  end
end

这是我的能力.rb 文件:

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new # guest user (not logged in)
    if defined?(user.role_id)
      if user.role? :admin, user.role_id
        can :manage, :all
      elsif user.role? :hauler, user.role_id
        can :manage, [User,Trip,Invoice], user_id: user.id.to_s
      else
        can :create, :Trip
      end
    end
  end
end
4

2 回答 2

5

这取决于正在执行的更新。会话使用某些用户数据位进行序列化。

例如,更新密码将导致会话无效,因为加密密码是序列化哈希的一部分,如果更改,会话将无法再引用原始加密密码。

于 2013-08-28T14:10:37.737 回答
1

为我工作

def update

    respond_to do |format|

      if @user.update(user_params)

        sign_in(@user, :bypass=>true)

        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { render :show, status: :ok, location: @user }

      else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end

    end

end

魔术发生在 :bypass=>true

于 2017-07-21T01:49:10.470 回答