Class: Ability

Inherits:
Object
  • Object
show all
Defined in:
app/models/ability.rb

Class Method Summary collapse

Class Method Details

.abilitiesObject


470
471
472
473
474
475
476
# File 'app/models/ability.rb', line 470

def abilities
  @abilities ||= begin
    abilities = Six.new
    abilities << self
    abilities
  end
end

.allowed(user, subject) ⇒ Object


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/ability.rb', line 3

def allowed(user, subject)
  return anonymous_abilities(user, subject) if user.nil?
  return [] unless user.is_a?(User)
  return [] if user.blocked?

  case subject
  when CommitStatus then commit_status_abilities(user, subject)
  when Project then project_abilities(user, subject)
  when Issue then issue_abilities(user, subject)
  when ExternalIssue then external_issue_abilities(user, subject)
  when Note then note_abilities(user, subject)
  when ProjectSnippet then project_snippet_abilities(user, subject)
  when PersonalSnippet then personal_snippet_abilities(user, subject)
  when MergeRequest then merge_request_abilities(user, subject)
  when Group then group_abilities(user, subject)
  when Namespace then namespace_abilities(user, subject)
  when GroupMember then group_member_abilities(user, subject)
  when ProjectMember then project_member_abilities(user, subject)
  when User then user_abilities
  else []
  end.concat(global_abilities(user))
end

.anonymous_abilities(user, subject) ⇒ Object

List of possible abilities for anonymous user


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/ability.rb', line 27

def anonymous_abilities(user, subject)
  case true
  when subject.is_a?(PersonalSnippet)
    anonymous_personal_snippet_abilities(subject)
  when subject.is_a?(ProjectSnippet)
    anonymous_project_snippet_abilities(subject)
  when subject.is_a?(CommitStatus)
    anonymous_commit_status_abilities(subject)
  when subject.is_a?(Project) || subject.respond_to?(:project)
    anonymous_project_abilities(subject)
  when subject.is_a?(Group) || subject.respond_to?(:group)
    anonymous_group_abilities(subject)
  when subject.is_a?(User)
    anonymous_user_abilities
  else
    []
  end
end

.anonymous_commit_status_abilities(subject) ⇒ Object


79
80
81
82
83
84
# File 'app/models/ability.rb', line 79

def anonymous_commit_status_abilities(subject)
  rules = anonymous_project_abilities(subject.project)
  # If subject is Ci::Build which inherits from CommitStatus filter the abilities
  rules = filter_build_abilities(rules) if subject.is_a?(Ci::Build)
  rules
end

.anonymous_group_abilities(subject) ⇒ Object


86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/ability.rb', line 86

def anonymous_group_abilities(subject)
  rules = []

  group = if subject.is_a?(Group)
            subject
          else
            subject.group
          end

  rules << :read_group if group.public?

  rules
end

.anonymous_personal_snippet_abilities(snippet) ⇒ Object


100
101
102
103
104
105
106
# File 'app/models/ability.rb', line 100

def anonymous_personal_snippet_abilities(snippet)
  if snippet.public?
    [:read_personal_snippet]
  else
    []
  end
end

.anonymous_project_abilities(subject) ⇒ Object


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
# File 'app/models/ability.rb', line 46

def anonymous_project_abilities(subject)
  project = if subject.is_a?(Project)
              subject
            else
              subject.project
            end

  if project && project.public?
    rules = [
      :read_project,
      :read_wiki,
      :read_label,
      :read_milestone,
      :read_project_snippet,
      :read_project_member,
      :read_merge_request,
      :read_note,
      :read_commit_status,
      :download_code
    ]

    # Allow to read builds by anonymous user if guests are allowed
    rules << :read_build if project.public_builds?

    # Allow to read issues by anonymous user if issue is not confidential
    rules << :read_issue unless subject.is_a?(Issue) && subject.confidential?

    rules - project_disabled_features_rules(project)
  else
    []
  end
end

.anonymous_project_snippet_abilities(snippet) ⇒ Object


108
109
110
111
112
113
114
# File 'app/models/ability.rb', line 108

def anonymous_project_snippet_abilities(snippet)
  if snippet.public?
    [:read_project_snippet]
  else
    []
  end
end

.anonymous_user_abilitiesObject


116
117
118
# File 'app/models/ability.rb', line 116

def anonymous_user_abilities
  [:read_user] unless restricted_public_level?
end

.can_read_group?(user, group) ⇒ Boolean

Returns:

  • (Boolean)

318
319
320
321
322
323
324
325
# File 'app/models/ability.rb', line 318

def can_read_group?(user, group)
  return true if user.admin?
  return true if group.public?
  return true if group.internal? && !user.external?
  return true if group.users.include?(user)

  GroupProjectsFinder.new(group).execute(user).any?
end

.commit_status_abilities(user, subject) ⇒ Object


450
451
452
453
454
455
# File 'app/models/ability.rb', line 450

def commit_status_abilities(user, subject)
  rules = project_abilities(user, subject.project)
  # If subject is Ci::Build which inherits from CommitStatus filter the abilities
  rules = filter_build_abilities(rules) if subject.is_a?(Ci::Build)
  rules
end

.external_issue_abilities(user, subject) ⇒ Object


478
479
480
# File 'app/models/ability.rb', line 478

def external_issue_abilities(user, subject)
  project_abilities(user, subject.project)
end

.filter_build_abilities(rules) ⇒ Object


457
458
459
460
461
462
463
464
# File 'app/models/ability.rb', line 457

def filter_build_abilities(rules)
  # If we can't read build we should also not have that
  # ability when looking at this in context of commit_status
  %w(read create update admin).each do |rule|
    rules.delete(:"#{rule}_commit_status") unless rules.include?(:"#{rule}_build")
  end
  rules
end

.global_abilities(user) ⇒ Object


120
121
122
123
124
125
# File 'app/models/ability.rb', line 120

def global_abilities(user)
  rules = []
  rules << :create_group if user.can_create_group
  rules << :read_users_list
  rules
end

.group_abilities(user, group) ⇒ Object


293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'app/models/ability.rb', line 293

def group_abilities(user, group)
  rules = []
  rules << :read_group if can_read_group?(user, group)

  # Only group masters and group owners can create new projects
  if group.has_master?(user) || group.has_owner?(user) || user.admin?
    rules += [
      :create_projects,
      :admin_milestones
    ]
  end

  # Only group owner and administrators can admin group
  if group.has_owner?(user) || user.admin?
    rules += [
      :admin_group,
      :admin_namespace,
      :admin_group_member,
      :change_visibility_level
    ]
  end

  rules.flatten
end

.group_member_abilities(user, subject) ⇒ Object


412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'app/models/ability.rb', line 412

def group_member_abilities(user, subject)
  rules = []
  target_user = subject.user
  group = subject.group

  unless group.last_owner?(target_user)
    can_manage = group_abilities(user, group).include?(:admin_group_member)

    if can_manage
      rules << :update_group_member
      rules << :destroy_group_member
    elsif user == target_user
      rules << :destroy_group_member
    end
  end

  rules
end

.namespace_abilities(user, namespace) ⇒ Object


327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'app/models/ability.rb', line 327

def namespace_abilities(user, namespace)
  rules = []

  # Only namespace owner and administrators can admin it
  if namespace.owner == user || user.admin?
    rules += [
      :create_projects,
      :admin_namespace
    ]
  end

  rules.flatten
end

.note_abilities(user, note) ⇒ Object


358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'app/models/ability.rb', line 358

def note_abilities(user, note)
  rules = []

  if note.author == user
    rules += [
      :read_note,
      :update_note,
      :admin_note
    ]
  end

  if note.respond_to?(:project) && note.project
    rules += project_abilities(user, note.project)
  end

  rules
end

.personal_snippet_abilities(user, snippet) ⇒ Object


376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'app/models/ability.rb', line 376

def personal_snippet_abilities(user, snippet)
  rules = []

  if snippet.author == user
    rules += [
      :read_personal_snippet,
      :update_personal_snippet,
      :admin_personal_snippet
    ]
  end

  if snippet.public? || (snippet.internal? && !user.external?)
    rules << :read_personal_snippet
  end

  rules
end

.project_abilities(user, project) ⇒ Object


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
# File 'app/models/ability.rb', line 127

def project_abilities(user, project)
  rules = []
  key = "/user/#{user.id}/project/#{project.id}"

  RequestStore.store[key] ||= begin
    # Push abilities on the users team role
    rules.push(*project_team_rules(project.team, user))

    if project.owner == user ||
      (project.group && project.group.has_owner?(user)) ||
      user.admin?

      rules.push(*project_owner_rules)
    end

    if project.public? || (project.internal? && !user.external?)
      rules.push(*public_project_rules)

      # Allow to read builds for internal projects
      rules << :read_build if project.public_builds?
    end

    if project.archived?
      rules -= project_archived_rules
    end

    rules - project_disabled_features_rules(project)
  end
end

.project_archived_rulesObject


223
224
225
226
227
228
229
230
231
# File 'app/models/ability.rb', line 223

def project_archived_rules
  @project_archived_rules ||= [
    :create_merge_request,
    :push_code,
    :push_code_to_protected_branches,
    :update_merge_request,
    :admin_merge_request
  ]
end

.project_dev_rulesObject


209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'app/models/ability.rb', line 209

def project_dev_rules
  @project_dev_rules ||= project_report_rules + [
    :admin_merge_request,
    :update_merge_request,
    :create_commit_status,
    :update_commit_status,
    :create_build,
    :update_build,
    :create_merge_request,
    :create_wiki,
    :push_code
  ]
end

.project_disabled_features_rules(project) ⇒ Object


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
291
# File 'app/models/ability.rb', line 262

def project_disabled_features_rules(project)
  rules = []

  unless project.issues_enabled
    rules += named_abilities('issue')
  end

  unless project.merge_requests_enabled
    rules += named_abilities('merge_request')
  end

  unless project.issues_enabled or project.merge_requests_enabled
    rules += named_abilities('label')
    rules += named_abilities('milestone')
  end

  unless project.snippets_enabled
    rules += named_abilities('project_snippet')
  end

  unless project.wiki_enabled
    rules += named_abilities('wiki')
  end

  unless project.builds_enabled
    rules += named_abilities('build')
  end

  rules
end

.project_guest_rulesObject


178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/models/ability.rb', line 178

def project_guest_rules
  @project_guest_rules ||= [
    :read_project,
    :read_wiki,
    :read_issue,
    :read_label,
    :read_milestone,
    :read_project_snippet,
    :read_project_member,
    :read_merge_request,
    :read_note,
    :create_project,
    :create_issue,
    :create_note,
    :upload_file
  ]
end

.project_master_rulesObject


233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'app/models/ability.rb', line 233

def project_master_rules
  @project_master_rules ||= project_dev_rules + [
    :push_code_to_protected_branches,
    :update_project_snippet,
    :admin_milestone,
    :admin_project_snippet,
    :admin_project_member,
    :admin_merge_request,
    :admin_note,
    :admin_wiki,
    :admin_project,
    :admin_commit_status,
    :admin_build
  ]
end

.project_member_abilities(user, subject) ⇒ Object


431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# File 'app/models/ability.rb', line 431

def project_member_abilities(user, subject)
  rules = []
  target_user = subject.user
  project = subject.project

  unless target_user == project.owner
    can_manage = project_abilities(user, project).include?(:admin_project_member)

    if can_manage
      rules << :update_project_member
      rules << :destroy_project_member
    elsif user == target_user
      rules << :destroy_project_member
    end
  end

  rules
end

.project_owner_rulesObject


249
250
251
252
253
254
255
256
257
258
259
260
# File 'app/models/ability.rb', line 249

def project_owner_rules
  @project_owner_rules ||= project_master_rules + [
    :change_namespace,
    :change_visibility_level,
    :rename_project,
    :remove_project,
    :archive_project,
    :remove_fork_project,
    :destroy_merge_request,
    :destroy_issue
  ]
end

.project_report_rulesObject


196
197
198
199
200
201
202
203
204
205
206
207
# File 'app/models/ability.rb', line 196

def project_report_rules
  @project_report_rules ||= project_guest_rules + [
    :download_code,
    :fork_project,
    :create_project_snippet,
    :update_issue,
    :admin_issue,
    :admin_label,
    :read_commit_status,
    :read_build,
  ]
end

.project_snippet_abilities(user, snippet) ⇒ Object


394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'app/models/ability.rb', line 394

def project_snippet_abilities(user, snippet)
  rules = []

  if snippet.author == user || user.admin?
    rules += [
      :read_project_snippet,
      :update_project_snippet,
      :admin_project_snippet
    ]
  end

  if snippet.public? || (snippet.internal? && !user.external?) || (snippet.private? && snippet.project.team.member?(user))
    rules << :read_project_snippet
  end

  rules
end

.project_team_rules(team, user) ⇒ Object


157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/models/ability.rb', line 157

def project_team_rules(team, user)
  # Rules based on role in project
  if team.master?(user)
    project_master_rules
  elsif team.developer?(user)
    project_dev_rules
  elsif team.reporter?(user)
    project_report_rules
  elsif team.guest?(user)
    project_guest_rules
  end
end

.public_project_rulesObject


170
171
172
173
174
175
176
# File 'app/models/ability.rb', line 170

def public_project_rules
  @public_project_rules ||= project_guest_rules + [
    :download_code,
    :fork_project,
    :read_commit_status
  ]
end

.user_abilitiesObject


466
467
468
# File 'app/models/ability.rb', line 466

def user_abilities
  [:read_user]
end