Class: Gitlab::ProjectAuthorizations

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/project_authorizations.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ ProjectAuthorizations

user - The User object for which to calculate the authorizations.


10
11
12
# File 'lib/gitlab/project_authorizations.rb', line 10

def initialize(user)
  @user = user
end

Instance Attribute Details

#userObject (readonly)

Returns the value of attribute user.


7
8
9
# File 'lib/gitlab/project_authorizations.rb', line 7

def user
  @user
end

Instance Method Details

#calculateObject


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
# File 'lib/gitlab/project_authorizations.rb', line 14

def calculate
  cte = recursive_cte
  cte_alias = cte.table.alias(Group.table_name)
  projects = Project.arel_table
  links = ProjectGroupLink.arel_table

  relations = [
    # The project a user has direct access to.
    user.projects_with_active_memberships.select_for_project_authorization,

    # The personal projects of the user.
    user.personal_projects.select_project_owner_for_project_authorization,

    # Projects that belong directly to any of the groups the user has
    # access to.
    Namespace
      .unscoped
      .select([alias_as_column(projects[:id], 'project_id'),
               cte_alias[:access_level]])
      .from(cte_alias)
      .joins(:projects),

    # Projects shared with any of the namespaces the user has access to.
    Namespace
      .unscoped
      .select([
        links[:project_id],
        least(cte_alias[:access_level], links[:group_access], 'access_level')
      ])
      .from(cte_alias)
      .joins('INNER JOIN project_group_links ON project_group_links.group_id = namespaces.id')
      .joins('INNER JOIN projects ON projects.id = project_group_links.project_id')
      .joins('INNER JOIN namespaces p_ns ON p_ns.id = projects.namespace_id')
      .where('p_ns.share_with_group_lock IS FALSE')
  ]

  ProjectAuthorization
    .unscoped
    .with
    .recursive(cte.to_arel)
    .select_from_union(relations)
end