制定和放大器;康康舞 - 康康舞与2.0 API问题 [英] Devise & CanCan — Issues with CanCan 2.0 API

查看:192
本文介绍了制定和放大器;康康舞 - 康康舞与2.0 API问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有额外的属性为我的用户模式,不希望创建一个单独的配置文件的模型。
我试图更新非标准«更新»自定义字段从REST风格的一套动作:

 类UsersController< ApplicationController中
  的before_filter:的authenticate_user!
  #...
  DEF更新
    @user = User.find(PARAMS [:ID])
    授权! :更新,@user
    做的respond_to |格式|
      如果@ user.update_attributes(PARAMS [:用户])
        format.html {redirect_to的@user,公告:用户已成功更新。 }
        format.json {头:OK】
      其他
        format.html {渲染动作:编辑}
        format.json {渲染JSON:@ user.errors,状态:unprocessable_entity}
      结束
    结束
  结束
结束

和除了事实CURRENT_USER能够更新任何用户的个人资料这一切都很好。看来我不能限制任何用户操作。我试过:

 可以:更新,用户:ID =>用户名

 不能:更新,用户在#所有

没有运气。通过制定和1.5.0康康舞2.0.0.alpha

下面是我的ability.rb

 类能力
  包括惨惨::能力  高清初始化(用户)
    用户|| = User.new(:角色= GT;无)#来宾用户(没有登录)
    可以:访问:所有
    如果user.admin?
      可以:管理:所有
    其他
      可以:阅读,回顾
      如果user.customer?
        可以:更新,用户:ID =>用户名
        可以[:创建,:更新:灭],评论,:USER_ID =>用户名
      结束
    结束
  结束
结束


解决方案

code对我来说很好。
如果你尝试了首先要简化第二个条件,并采取了客户的状况?也许采取了能:访问:所有的

是这样的:

 类能力
  包括惨惨::能力  高清初始化(用户)
    用户|| = User.new(:角色= GT;无)#来宾用户(没有登录)
    如果user.admin?
      可以:访问:所有
    其他
      可以:阅读,:全部
      可以:更新:用户:ID =>用户名
      可以[:创建,:更新:灭]:评论,:USER_ID =>用户名
    结束
  结束
结束

对于是否评测(即用户只能编辑自己的意见)的限制的工作?
我有一个类似能力的文件,但我总是一个单独的剖面模型工作..

I'd like to have additional attributes for my User model and don't want to create a separate Profile model. I'm trying to update custom fields with standart «update» from RESTful set of actions:

class UsersController < ApplicationController
  before_filter :authenticate_user!
  # ...
  def update
    @user = User.find(params[:id])
    authorize! :update, @user
    respond_to do |format|
      if @user.update_attributes(params[:user])
        format.html { redirect_to @user, 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

And it all goes fine except the fact that the current_user is able to update any user's profile. It seems I can't restrict any User action. I've tried:

can :update, User, :id => user.id

and

cannot :update, User # at all

with no luck. Using Devise 1.5.0 and CanCan 2.0.0.alpha

Here's my ability.rb

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new(:role => nil) # guest user (not logged in)
    can :access, :all
    if user.admin?
      can :manage, :all
    else
      can :read, Review
      if user.customer?
        can :update, User, :id => user.id
        can [:create, :update, :destroy], Review, :user_id => user.id
      end
    end
  end
end

解决方案

Code looks good to me. What if you try to simplify the second condition first and take out the customer condition? And maybe take out "can :access, :all

Something like:

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new(:role => nil) # guest user (not logged in)
    if user.admin?
      can :access, :all
    else
      can :read, :all
      can :update, :users, :id => user.id
      can [:create, :update, :destroy], :reviews, :user_id => user.id
    end
  end
end

Does your restriction work for Reviews (that user can only edit his own reviews) ? I have a similar ability file but I always work with a seperate profile model..

这篇关于制定和放大器;康康舞 - 康康舞与2.0 API问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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