2013-04-16 22 views
0

親愛的所有人(特別是瑞恩,感謝你的這個偉大的工作), 我只是完全堅持一些可能只是非常愚蠢的東西,但對於我的生活,我可以把我的大腦包起來解決。可以has_many通過解釋幫助需要嚴重

我正在嘗試使用Can Can進行授權,除了(這是我的問題,在一個特定的事情上,顯然工作正常,但我不確定我是否是我正確或不正確 - 實際上我認爲我無法證明它不能正常工作)。

讓我們來看看,如果我能解釋清楚我在做什麼:

我有兩個模型(用戶和專輯),即通過第三個模型共享連接:

(A)用戶模型

has_many :albums, :dependent => :destroy # ownership 
has_many :shares, :foreign_key => "shared_user_id" #, :dependent => :destroy 
has_many :shared_albums, :through => :shares 

(B)相冊樣板

belongs_to :user 
has_many :shares, :foreign_key => "shared_album_id", :dependent => :destroy 
has_many :shared_users, :through => :shares 

(C)股型

belongs_to :shared_user, :foreign_key => "shared_user_id", :class_name => "User" 
belongs_to :shared_album, :foreign_key => "shared_album_id", :class_name => "Album" 

現在,在我的Ability類中,我想要做的是限制(C)共享模型上特定用戶可以執行的操作。

在我的應用程序中,用戶可以創建一個專輯,之後,他可以將其他用戶添加到該相冊(此用戶是可以訪問特定相冊中只有一個)。另外,作爲專輯一部分的每個用戶都可以添加新用戶。

完成所有這些給予本相冊中的用戶,創建共享的能力(分成模式涉及用戶和相冊樣板)。

現在最大的問題,我如何限制(創建)共享添加到特定的專輯,只有能力是屬於該專輯(通過股權)的一部分用戶?

如果例如我有:

用戶=> ID(1,2,3,4)[總在我的應用程序4組的用戶)

相冊=> ID的(45,32) [在我的應用程序共2個相冊)

股=>(專輯45 => [用戶(1,2,3)]; 32專輯=> [用戶(1,4)])[共在我的應用程序中的5個股份]

我怎麼能說在能力類,在專輯32(例如),只有用戶1和4可以添加(創建)新共享(添加新用戶),而不是用戶2或3 t嘿不能?

我已經限制了用戶2和3無論如何都可以訪問資源專輯32(我在專輯類級別上做了這些),但我想確保無論出於何種原因創建用戶的能力也受到限制。

我直到現在我的能力等級是:

def initialize(user) 

    (A) ALBUM LEVEL 

    # (1) Every User can create Album, without restrictions 
    can :create, Album 

    # (2) Only the user that own the Album can manage it 
    can :manage, Album, :user_id => user.id 

    # (3) The Album can be read by all the users that are part of that specific album 
    can :read, Album, :shares => {:shared_user_id => user.id} 

    # (4) The Album can be read by every user if the privacy attribute is false 
    can :read, Album, :privacy_setting => false 


    (B) SHARE LEVEL 

    # (1) Only the user that own the Album can manage all the shares for the album 
    can :manage, Share, :shared_album => {:user_id => user.id} 

    # (2) The other users (in the album), can just manage themself (their share) 
    can :manage, Share, :shared_user_id => user.id 

    # (3) The Share in the album can be read by every user if privacy is false (just read) 
    can [:read], Share, :shared_album => {:privacy_setting => false} 
    cannot [:create,:destroy,:delete], Share, :shared_album => {:privacy_setting => false} 


#### (X) CRUCIAL POINT CREATE NEW SHARE 
    can :create, Share, :shared_album => {:shared_users => {:id => user.id}} 

end 

是在(X)要害權條件的條件,讓這一點已經是專輯的一部分用戶,添加新用戶專輯?

這完全令我瘋狂。

謝謝大家,特別是誰能讓我更瞭解這一切。

最佳 Dinuz

回答

0

你ability.rb似乎沒什麼問題。

你的要害執行以下操作:如果有一個共享共享相冊和

用戶可以創建一個共享,如果當前用戶被包含在該共享相冊的shared_users。不過,有一件事要記住。由於您在授權時檢查共享的shared_album,因此您必須在嘗試授權之前先對其進行設置。

例如:

​​

上面的示例將設置一個共享的shared_album_id,這樣就可以批准它。

@share = Share.new 
authorize :create, @share 

這不起作用,因爲共享還沒有相冊。

希望這會有所幫助。

+0

謝謝Arjan!你在暗示它已經到位(我的錯,因爲我沒有發佈我的控制器)!其實在新的行動中,我有以下幾點:@album = Album.find(params [:album_id])和@ share = @ album.shares.build。我只是想確定我的模式是正確的!我很感謝你的回答,但這不是我想要的。謝謝 – Dinuz