2012-06-21 2 views
0

내 ability.rb 파일에서 정의 된 사용자 만 작업을 수행 할 수 있도록 설정하려면 어떻게해야합니까? 그렇지 않으면 정의되지 않은/로그인하지 않은 사용자가 아무 것도 할 수 없습니까?CanCan에서 사용자 작업을 허용하지 않으려면 어떻게해야합니까?

예 :

def initialize(user) 
    user ||= User.new #not logged in user 
    if user.admin_user? 
     can :manage, :all 
    else 
     #can't do anything. Cannot view, edit, or update. 
    end 
end 

감사합니다!

답변

1

저는 CanCan을 사용한 적이 없지만 문서를 살펴 보았습니다. 사용자 이 할 수없는 것을 명시 적으로 정의해야한다고 생각하지 않습니다.. 당신은 그냥 할 수 있어야한다

는 컨트롤러에서 같은 것을 할 : 사용자가 정의 된 능력이있는 역할이 할당되지 않은 경우

if cannot? :destroy, @project 
    # redirect the user or do something else to disallow access 
end 

cannot?

사실 반환해야합니다. 반대로 can?은 false를 반환합니다.

에게 당신은 당신의 초기화 함수에서 간단한 조건 설정을 시도 할 수
0

는 : initializeuser 가능성이 사용자가 로그인하지 않는 nil 일 (사용자 인증 솔루션에 따라) 것이라고

# app/models/ability.rb 
def initialize(user) 
    if user && user.admin_user? 
    # Abilities for registered admin users 
    can :manage, :all 
    elsif user 
    # Abilities for registered users 
    can :read, :all 
    else 
    # Abilities for no user 
    end 
end 

의 트리거 할 마지막 지점.

관련 문제