[英]CanCan based access with multiple views
我正在開發一個Rails 4應用程序,我試圖想出一個解決方案。 我想知道如何實現CanCan以允許用戶訪問某些數據。 如客人只能查看內容的某些部分。 所有者可以擁有對內容的完全訪問權限,協作者可以具有部分訪問權限。
我的應用程序包含一個
User
- Developer
- Organization
Developer
- Has many apps on its own
- Has many organizations as founder or collaborator
Organization
- Has many founders and collaborators
- Has many apps
如何將此限制為未登錄的訪客可以查看開發人員/組織/應用程序配置文件的某些方面,創建者可以完全訪問組織,應用程序的所有者具有完全訪問權限,協作者可以訪問。 這有意義嗎?
cancan僅適用於current_user方法。 如果有辦法定制這個,我不會
下面的代碼示例可以指導您僅通過所有者管理內容
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.admin?
can :manage, :all
else
can :read, Content
end
if user
can :create, Content
can :manage, Content do |content|
content.try(:user) == user
end
end
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.