Module: Pageflow::AbilityMixin

Included in:
Ability
Defined in:
lib/pageflow/ability_mixin.rb

Overview

Defines the default abilities for the Pageflow models.

Instance Method Summary collapse

Instance Method Details

#pageflow_default_abilities(user) ⇒ Object

Call this in the ability initializer.


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pageflow/ability_mixin.rb', line 5

def pageflow_default_abilities(user)
  return if user.nil?

  can :read, Folder, :id => user.entries.map(&:folder_id)

  can [:read, :use_files], Entry, :id => user.entry_ids

  can [:edit, :update, :edit_outline, :publish, :restore, :snapshot, :confirm_encoding], Entry do |entry|
    can_edit_entry?(user, entry)
  end

  can :manage, Chapter do |record|
    can_edit_entry?(user, record.entry)
  end

  can :manage, Pageflow.config.file_types.map(&:model) do |record|
    can_edit_any_entry_using_file?(user, record)
  end

  can :manage, Page do |page|
    can_edit_entry?(user, page.chapter.entry)
  end

  can :manage, Revision do |revision|
    can_edit_entry?(user, revision.entry)
  end

  if user.admin?
    can [:read, :create, :update], 
    can :destroy,  do ||
      .users.empty? && .entries.empty?
    end
    can :manage, Theming

    can :manage, ::User

    can :destroy, Membership
    can :create, Membership do |membership|
      membership.entry.nil? ||
        membership.user.nil? ||
        membership.entry. == membership.user.
    end

    can :manage, Folder
    can :manage, [Entry, Revision]
    can :manage, [Chapter, Page]
    can :manage, Pageflow.config.file_types.map(&:model)

    can :manage, Resque
  elsif user.
    can :manage, Theming, :account_id => user.
    can :manage, Folder, :account_id => user..id
    can :manage, Entry, :account_id => user..id
    can :manage, ::User, :account_id => user..id
    can :manage, Revision, :entry => {:account_id => user..id}

    can :destroy, Membership, :entry => {:account_id => user..id}
    can :destroy, Membership, :user => {:account_id => user..id}

    can :create, Membership do |membership|
      (membership.entry.nil? || membership.entry. == user.) &&
        (membership.user.nil? || membership.user. == user.)
    end
  end
end