Module: Pageflow::AbilityMixin

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

Overview

Defines the default abilities for the Pageflow models.

Instance Method Summary collapse

Methods included from ActiveAdminCanCanFix

#can?

Instance Method Details

#pageflow_default_abilities(user) ⇒ Object

Call this in the ability initializer.



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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/pageflow/ability_mixin.rb', line 7

def pageflow_default_abilities(user)
  return if user.nil?

  can :read, Account, AccountPolicy::Scope.new(user, Account).resolve do ||
    AccountPolicy.new(user, ).read?
  end

  can :update, Account do ||
    AccountPolicy.new(user, ).update?
  end

  can :update_feature_configuration_on, Account do ||
    AccountPolicy.new(user, ).update_feature_configuration_on?
  end

  can :add_member_to, Account do ||
    AccountPolicy.new(user, ).add_member_to?
  end

  can :see_user_quota, Account do ||
    AccountPolicy.new(user, ).see_user_quota?
  end

  can :see_badge_belonging_to, Account do ||
    AccountPolicy.new(user, ).see_badge_belonging_to?
  end

  can :create, Membership, Membership.all do |membership|
    membership.entity.nil? ||
      membership.user.nil? ||
      (!(membership.user.entries.include?(membership.entity) ||
         membership.user.accounts.include?(membership.entity))) &&
        MembershipPolicy.new(user, membership).create?
  end

  can :index, Membership, MembershipPolicy::Scope.new(user, Membership).indexable

  can :update, Membership do |membership|
    MembershipPolicy.new(user, membership).edit_role?
  end

  can :destroy, Membership do |membership|
    MembershipPolicy.new(user, membership).destroy?
  end

  can :index, :users do
    UserPolicy.new(user, User.new).index?
  end

  can :create_any, :users do
    UserPolicy.new(user, User.new).create_any?
  end

  can :add_account_to, :users do
    UserPolicy.new(user, User.new).
  end

  can :set_admin, ::User do |managed_user|
    UserPolicy.new(user, managed_user).set_admin?
  end

  can :see, :accounts do
    user.admin? || user.memberships.on_accounts.length > 1
  end

  can :see_own_role_on, :accounts do
    !user.admin?
  end

  can :index, :accounts do
    AccountPolicy.new(user, Account.new).index?
  end

  can :index, :entries

  can :create_any, :entries do
    EntryPolicy.new(user, Entry.new).create_any?
  end

  can :filter_by_type, :entries do
    EntryPolicy.new(user, Entry).filter_by_type?
  end

  can :see_own_role_on, :entries do
    !user.admin?
  end

  can :see_entry_admin_tab, Admin::Tab do |tab|
    Admin::EntryTabPolicy.new(user, tab).see?
  end

  can :see_site_admin_tab, Admin::Tab do |tab|
    Admin::AdminOnlyTabPolicy.new(user, tab).see?
  end

  can :see_account_admin_tab, Admin::Tab do |tab|
    Admin::AdminOnlyTabPolicy.new(user, tab).see?
  end

  can :see_user_admin_tab, Admin::Tab do |tab|
    Admin::AdminOnlyTabPolicy.new(user, tab).see?
  end

  can :see_entry_types, Account do ||
    AccountPolicy.new(user, ).see_entry_types?
  end

  unless user.admin?
    can :configure_folder_on, Account do ||
      AccountPolicy.new(user, ).configure_folder_on?
    end

    can :update_site_on_entry_of, Account do ||
      AccountPolicy.new(user, ).update_site_on_entry_of?
    end

    can :create, Entry, Entry.all do |entry|
      EntryPolicy.new(user, entry).create?
    end

    can :manage, Chapter do |record|
      EntryPolicy.new(user, record.entry).edit?
    end

    can :read, Entry, EntryPolicy::Scope.new(user, Entry).resolve do |entry|
      EntryPolicy.new(user, entry).read?
    end

    can :update, Entry do |entry|
      EntryPolicy.new(user, entry).edit?
    end

    can :destroy, Entry do |entry|
      EntryPolicy.new(user, entry).destroy?
    end

    can :add_member_to, Entry do |entry|
      EntryPolicy.new(user, entry).add_member_to?
    end

    can :configure_folder_for, Entry do |entry|
      AccountPolicy.new(user, entry.).configure_folder_on?
    end

    can :confirm_encoding, Entry do |entry|
      EntryPolicy.new(user, entry).confirm_encoding?
    end

    can :duplicate, Entry do |entry|
      EntryPolicy.new(user, entry).duplicate?
    end

    can :edit_outline, Entry do |entry|
      EntryPolicy.new(user, entry).edit_outline?
    end

    can :index_widgets_for, Entry do |entry|
      EntryPolicy.new(user, entry).index_widgets_for?
    end

    can :publish, Entry do |entry|
      EntryPolicy.new(user, entry).publish?
    end

    can :restore, Entry do |entry|
      EntryPolicy.new(user, entry).restore?
    end

    can :snapshot, Entry do |entry|
      EntryPolicy.new(user, entry).snapshot?
    end

    can :update_account_on, Entry do |entry|
      EntryPolicy.new(user, entry).
    end

    can :update_feature_configuration_on, Entry do |entry|
      EntryPolicy.new(user, entry).update_feature_configuration_on?
    end

    can :update_site_on, Entry do |entry|
      EntryPolicy.new(user, entry).update_site_on?
    end

    can :use_files, Entry, EntryPolicy::Scope.new(user, Entry).resolve do |entry|
      EntryPolicy.new(user, entry).use_files?
    end

    can [:retry, :update], Pageflow.config.file_types.map(&:model) do |record|
      FilePolicy.new(user, record).manage?
    end

    can :destroy, Pageflow.config.file_types.map(&:model) do |record|
      FilePolicy.new(user, record).destroy?
    end

    can :use, Pageflow.config.file_types.map(&:model) do |record|
      FilePolicy.new(user, record).use?
    end

    can [:create, :update, :destroy], Folder, Folder.all do |folder|
      FolderPolicy.new(user, folder).manage?
    end

    can :read, Folder, FolderPolicy::Scope.new(user, Folder).resolve

    can :show_account_selection_on, Folder do |folder|
      FolderPolicy.new(user, folder).
    end

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

    can :read, Revision do |revision|
      EntryPolicy.new(user, revision.entry).preview?
    end

    can :manage, Storyline do |storyline|
      EntryPolicy.new(user, storyline.revision.entry).edit?
    end

    can :read, Site do |site|
      SitePolicy.new(user, site).read?
    end

    can :update, Site do |site|
      SitePolicy.new(user, site).update?
    end

    can :create, EntryTemplate, EntryTemplate.all do |entry_template|
      EntryTemplatePolicy.new(user, entry_template).create?
    end

    can :update, EntryTemplate do |entry_template|
      EntryTemplatePolicy.new(user, entry_template).update?
    end

    can :create, ::User, ::User.all do |managed_user|
      UserPolicy.new(user, managed_user).create?
    end

    can :read, ::User, UserPolicy::Scope.new(user, ::User).resolve do |managed_user|
      UserPolicy.new(user, managed_user).read?
    end

    can :see_admin_status, ::User do |managed_user|
      UserPolicy.new(user, managed_user).see_admin_status?
    end

    can :redirect_to_user,
        ::User,
        UserPolicy::Scope.new(user, ::User).resolve do |managed_user|
      UserPolicy.new(user, managed_user).redirect_to_user?
    end

    can :suspend, ::User do |managed_user|
      UserPolicy.new(user, managed_user).suspend?
    end

    can :destroy, ::User do |managed_user|
      UserPolicy.new(user, managed_user).destroy?
    end
  end

  can :delete_own_user, ::User do |user_to_delete|
    UserPolicy.new(user, user_to_delete).delete_own_user?
  end

  if user.admin?
    can [:create, :configure_folder_on], Account
    can :create_any, :accounts
    can :destroy, Account do ||
      .users.empty? && .entries.empty?
    end
    can :manage, [Storyline, Chapter, Page]
    can :manage, [Entry, Revision]
    can :manage, Pageflow.config.file_types.map(&:model)
    can :manage, Folder
    can :manage, Site
    can :manage, EntryTemplate
    can :manage, ::User
  end
end