2019-04-11 12:17:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-06 16:31:58 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Users::RefreshAuthorizedProjectsService do
|
2018-06-28 18:51:36 +00:00
|
|
|
include ExclusiveLeaseHelpers
|
|
|
|
|
Use CTEs for nested groups and authorizations
This commit introduces the usage of Common Table Expressions (CTEs) to
efficiently retrieve nested group hierarchies, without having to rely on
the "routes" table (which is an _incredibly_ inefficient way of getting
the data). This requires a patch to ActiveRecord (found in the added
initializer) to work properly as ActiveRecord doesn't support WITH
statements properly out of the box.
Unfortunately MySQL provides no efficient way of getting nested groups.
For example, the old routes setup could easily take 5-10 seconds
depending on the amount of "routes" in a database. Providing vastly
different logic for both MySQL and PostgreSQL will negatively impact the
development process. Because of this the various nested groups related
methods return empty relations when used in combination with MySQL.
For project authorizations the logic is split up into two classes:
* Gitlab::ProjectAuthorizations::WithNestedGroups
* Gitlab::ProjectAuthorizations::WithoutNestedGroups
Both classes get the fresh project authorizations (= as they should be
in the "project_authorizations" table), including nested groups if
PostgreSQL is used. The logic of these two classes is quite different
apart from their public interface. This complicates development a bit,
but unfortunately there is no way around this.
This commit also introduces Gitlab::GroupHierarchy. This class can be
used to get the ancestors and descendants of a base relation, or both by
using a UNION. This in turn is used by methods such as:
* Namespace#ancestors
* Namespace#descendants
* User#all_expanded_groups
Again this class relies on CTEs and thus only works on PostgreSQL. The
Namespace methods will return an empty relation when MySQL is used,
while User#all_expanded_groups will return only the groups a user is a
direct member of.
Performance wise the impact is quite large. For example, on GitLab.com
Namespace#descendants used to take around 580 ms to retrieve data for a
particular user. Using CTEs we are able to reduce this down to roughly 1
millisecond, returning the exact same data.
== On The Fly Refreshing
Refreshing of authorizations on the fly (= when
users.authorized_projects_populated was not set) is removed with this
commit. This simplifies the code, and ensures any queries used for
authorizations are not mutated because they are executed in a Rails
scope (e.g. Project.visible_to_user).
This commit includes a migration to schedule refreshing authorizations
for all users, ensuring all of them have their authorizations in place.
Said migration schedules users in batches of 5000, with 5 minutes
between every batch to smear the load around a bit.
== Spec Changes
This commit also introduces some changes to various specs. For example,
some specs for ProjectTeam assumed that creating a personal project
would _not_ lead to the owner having access, which is incorrect. Because
we also no longer refresh authorizations on the fly for new users some
code had to be added to the "empty_project" factory. This chunk of code
ensures that the owner's permissions are refreshed after creating the
project, something that is normally done in Projects::CreateService.
2017-04-24 15:19:22 +00:00
|
|
|
# We're using let! here so that any expectations for the service class are not
|
|
|
|
# triggered twice.
|
2017-08-02 19:55:11 +00:00
|
|
|
let!(:project) { create(:project) }
|
Use CTEs for nested groups and authorizations
This commit introduces the usage of Common Table Expressions (CTEs) to
efficiently retrieve nested group hierarchies, without having to rely on
the "routes" table (which is an _incredibly_ inefficient way of getting
the data). This requires a patch to ActiveRecord (found in the added
initializer) to work properly as ActiveRecord doesn't support WITH
statements properly out of the box.
Unfortunately MySQL provides no efficient way of getting nested groups.
For example, the old routes setup could easily take 5-10 seconds
depending on the amount of "routes" in a database. Providing vastly
different logic for both MySQL and PostgreSQL will negatively impact the
development process. Because of this the various nested groups related
methods return empty relations when used in combination with MySQL.
For project authorizations the logic is split up into two classes:
* Gitlab::ProjectAuthorizations::WithNestedGroups
* Gitlab::ProjectAuthorizations::WithoutNestedGroups
Both classes get the fresh project authorizations (= as they should be
in the "project_authorizations" table), including nested groups if
PostgreSQL is used. The logic of these two classes is quite different
apart from their public interface. This complicates development a bit,
but unfortunately there is no way around this.
This commit also introduces Gitlab::GroupHierarchy. This class can be
used to get the ancestors and descendants of a base relation, or both by
using a UNION. This in turn is used by methods such as:
* Namespace#ancestors
* Namespace#descendants
* User#all_expanded_groups
Again this class relies on CTEs and thus only works on PostgreSQL. The
Namespace methods will return an empty relation when MySQL is used,
while User#all_expanded_groups will return only the groups a user is a
direct member of.
Performance wise the impact is quite large. For example, on GitLab.com
Namespace#descendants used to take around 580 ms to retrieve data for a
particular user. Using CTEs we are able to reduce this down to roughly 1
millisecond, returning the exact same data.
== On The Fly Refreshing
Refreshing of authorizations on the fly (= when
users.authorized_projects_populated was not set) is removed with this
commit. This simplifies the code, and ensures any queries used for
authorizations are not mutated because they are executed in a Rails
scope (e.g. Project.visible_to_user).
This commit includes a migration to schedule refreshing authorizations
for all users, ensuring all of them have their authorizations in place.
Said migration schedules users in batches of 5000, with 5 minutes
between every batch to smear the load around a bit.
== Spec Changes
This commit also introduces some changes to various specs. For example,
some specs for ProjectTeam assumed that creating a personal project
would _not_ lead to the owner having access, which is incorrect. Because
we also no longer refresh authorizations on the fly for new users some
code had to be added to the "empty_project" factory. This chunk of code
ensures that the owner's permissions are refreshed after creating the
project, something that is normally done in Projects::CreateService.
2017-04-24 15:19:22 +00:00
|
|
|
|
2016-12-06 16:31:58 +00:00
|
|
|
let(:user) { project.namespace.owner }
|
|
|
|
let(:service) { described_class.new(user) }
|
|
|
|
|
2017-07-11 03:35:47 +00:00
|
|
|
describe '#execute', :clean_gitlab_redis_shared_state do
|
2017-01-16 21:34:13 +00:00
|
|
|
it 'refreshes the authorizations using a lease' do
|
2018-06-28 18:51:36 +00:00
|
|
|
lease_key = "refresh_authorized_projects:#{user.id}"
|
2017-01-16 21:34:13 +00:00
|
|
|
|
2018-06-28 18:51:36 +00:00
|
|
|
expect_to_obtain_exclusive_lease(lease_key, 'uuid')
|
|
|
|
expect_to_cancel_exclusive_lease(lease_key, 'uuid')
|
2017-01-16 21:34:13 +00:00
|
|
|
expect(service).to receive(:execute_without_lease)
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
end
|
2020-02-13 21:08:59 +00:00
|
|
|
|
|
|
|
context 'callbacks' do
|
|
|
|
let(:callback) { double('callback') }
|
|
|
|
|
|
|
|
context 'incorrect_auth_found_callback callback' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:service) do
|
|
|
|
described_class.new(user,
|
|
|
|
incorrect_auth_found_callback: callback)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is called' do
|
|
|
|
access_level = Gitlab::Access::DEVELOPER
|
|
|
|
create(:project_authorization, user: user, project: project, access_level: access_level)
|
|
|
|
|
|
|
|
expect(callback).to receive(:call).with(project.id, access_level).once
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'missing_auth_found_callback callback' do
|
|
|
|
let(:service) do
|
|
|
|
described_class.new(user,
|
|
|
|
missing_auth_found_callback: callback)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is called' do
|
|
|
|
ProjectAuthorization.delete_all
|
|
|
|
|
|
|
|
expect(callback).to receive(:call).with(project.id, Gitlab::Access::MAINTAINER).once
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-01-16 21:34:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute_without_lease' do
|
2016-12-06 16:31:58 +00:00
|
|
|
before do
|
|
|
|
user.project_authorizations.delete_all
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the authorized projects of the user' do
|
2017-08-02 19:55:11 +00:00
|
|
|
project2 = create(:project)
|
2017-06-21 13:48:12 +00:00
|
|
|
to_remove = user.project_authorizations
|
2018-07-11 14:36:08 +00:00
|
|
|
.create!(project: project2, access_level: Gitlab::Access::MAINTAINER)
|
2016-12-06 16:31:58 +00:00
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(service).to receive(:update_authorizations)
|
2018-07-11 14:36:08 +00:00
|
|
|
.with([to_remove.project_id], [[user.id, project.id, Gitlab::Access::MAINTAINER]])
|
2016-12-06 16:31:58 +00:00
|
|
|
|
2017-01-16 21:34:13 +00:00
|
|
|
service.execute_without_lease
|
2016-12-06 16:31:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the access level of a project to the highest available level' do
|
Use CTEs for nested groups and authorizations
This commit introduces the usage of Common Table Expressions (CTEs) to
efficiently retrieve nested group hierarchies, without having to rely on
the "routes" table (which is an _incredibly_ inefficient way of getting
the data). This requires a patch to ActiveRecord (found in the added
initializer) to work properly as ActiveRecord doesn't support WITH
statements properly out of the box.
Unfortunately MySQL provides no efficient way of getting nested groups.
For example, the old routes setup could easily take 5-10 seconds
depending on the amount of "routes" in a database. Providing vastly
different logic for both MySQL and PostgreSQL will negatively impact the
development process. Because of this the various nested groups related
methods return empty relations when used in combination with MySQL.
For project authorizations the logic is split up into two classes:
* Gitlab::ProjectAuthorizations::WithNestedGroups
* Gitlab::ProjectAuthorizations::WithoutNestedGroups
Both classes get the fresh project authorizations (= as they should be
in the "project_authorizations" table), including nested groups if
PostgreSQL is used. The logic of these two classes is quite different
apart from their public interface. This complicates development a bit,
but unfortunately there is no way around this.
This commit also introduces Gitlab::GroupHierarchy. This class can be
used to get the ancestors and descendants of a base relation, or both by
using a UNION. This in turn is used by methods such as:
* Namespace#ancestors
* Namespace#descendants
* User#all_expanded_groups
Again this class relies on CTEs and thus only works on PostgreSQL. The
Namespace methods will return an empty relation when MySQL is used,
while User#all_expanded_groups will return only the groups a user is a
direct member of.
Performance wise the impact is quite large. For example, on GitLab.com
Namespace#descendants used to take around 580 ms to retrieve data for a
particular user. Using CTEs we are able to reduce this down to roughly 1
millisecond, returning the exact same data.
== On The Fly Refreshing
Refreshing of authorizations on the fly (= when
users.authorized_projects_populated was not set) is removed with this
commit. This simplifies the code, and ensures any queries used for
authorizations are not mutated because they are executed in a Rails
scope (e.g. Project.visible_to_user).
This commit includes a migration to schedule refreshing authorizations
for all users, ensuring all of them have their authorizations in place.
Said migration schedules users in batches of 5000, with 5 minutes
between every batch to smear the load around a bit.
== Spec Changes
This commit also introduces some changes to various specs. For example,
some specs for ProjectTeam assumed that creating a personal project
would _not_ lead to the owner having access, which is incorrect. Because
we also no longer refresh authorizations on the fly for new users some
code had to be added to the "empty_project" factory. This chunk of code
ensures that the owner's permissions are refreshed after creating the
project, something that is normally done in Projects::CreateService.
2017-04-24 15:19:22 +00:00
|
|
|
user.project_authorizations.delete_all
|
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
to_remove = user.project_authorizations
|
|
|
|
.create!(project: project, access_level: Gitlab::Access::DEVELOPER)
|
2016-12-06 16:31:58 +00:00
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(service).to receive(:update_authorizations)
|
2018-07-11 14:36:08 +00:00
|
|
|
.with([to_remove.project_id], [[user.id, project.id, Gitlab::Access::MAINTAINER]])
|
2016-12-06 16:31:58 +00:00
|
|
|
|
2017-01-16 21:34:13 +00:00
|
|
|
service.execute_without_lease
|
2016-12-06 16:31:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a User' do
|
2017-01-16 21:34:13 +00:00
|
|
|
expect(service.execute_without_lease).to be_an_instance_of(User)
|
2016-12-06 16:31:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#update_authorizations' do
|
2016-12-28 13:41:30 +00:00
|
|
|
context 'when there are no rows to add and remove' do
|
|
|
|
it 'does not change authorizations' do
|
|
|
|
expect(user).not_to receive(:remove_project_authorizations)
|
|
|
|
expect(ProjectAuthorization).not_to receive(:insert_authorizations)
|
2016-12-06 16:31:58 +00:00
|
|
|
|
2016-12-28 13:41:30 +00:00
|
|
|
service.update_authorizations([], [])
|
|
|
|
end
|
2016-12-06 16:31:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes authorizations that should be removed' do
|
Use CTEs for nested groups and authorizations
This commit introduces the usage of Common Table Expressions (CTEs) to
efficiently retrieve nested group hierarchies, without having to rely on
the "routes" table (which is an _incredibly_ inefficient way of getting
the data). This requires a patch to ActiveRecord (found in the added
initializer) to work properly as ActiveRecord doesn't support WITH
statements properly out of the box.
Unfortunately MySQL provides no efficient way of getting nested groups.
For example, the old routes setup could easily take 5-10 seconds
depending on the amount of "routes" in a database. Providing vastly
different logic for both MySQL and PostgreSQL will negatively impact the
development process. Because of this the various nested groups related
methods return empty relations when used in combination with MySQL.
For project authorizations the logic is split up into two classes:
* Gitlab::ProjectAuthorizations::WithNestedGroups
* Gitlab::ProjectAuthorizations::WithoutNestedGroups
Both classes get the fresh project authorizations (= as they should be
in the "project_authorizations" table), including nested groups if
PostgreSQL is used. The logic of these two classes is quite different
apart from their public interface. This complicates development a bit,
but unfortunately there is no way around this.
This commit also introduces Gitlab::GroupHierarchy. This class can be
used to get the ancestors and descendants of a base relation, or both by
using a UNION. This in turn is used by methods such as:
* Namespace#ancestors
* Namespace#descendants
* User#all_expanded_groups
Again this class relies on CTEs and thus only works on PostgreSQL. The
Namespace methods will return an empty relation when MySQL is used,
while User#all_expanded_groups will return only the groups a user is a
direct member of.
Performance wise the impact is quite large. For example, on GitLab.com
Namespace#descendants used to take around 580 ms to retrieve data for a
particular user. Using CTEs we are able to reduce this down to roughly 1
millisecond, returning the exact same data.
== On The Fly Refreshing
Refreshing of authorizations on the fly (= when
users.authorized_projects_populated was not set) is removed with this
commit. This simplifies the code, and ensures any queries used for
authorizations are not mutated because they are executed in a Rails
scope (e.g. Project.visible_to_user).
This commit includes a migration to schedule refreshing authorizations
for all users, ensuring all of them have their authorizations in place.
Said migration schedules users in batches of 5000, with 5 minutes
between every batch to smear the load around a bit.
== Spec Changes
This commit also introduces some changes to various specs. For example,
some specs for ProjectTeam assumed that creating a personal project
would _not_ lead to the owner having access, which is incorrect. Because
we also no longer refresh authorizations on the fly for new users some
code had to be added to the "empty_project" factory. This chunk of code
ensures that the owner's permissions are refreshed after creating the
project, something that is normally done in Projects::CreateService.
2017-04-24 15:19:22 +00:00
|
|
|
authorization = user.project_authorizations.find_by(project_id: project.id)
|
2016-12-06 16:31:58 +00:00
|
|
|
|
2017-01-06 17:26:06 +00:00
|
|
|
service.update_authorizations([authorization.project_id])
|
2016-12-06 16:31:58 +00:00
|
|
|
|
|
|
|
expect(user.project_authorizations).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'inserts authorizations that should be added' do
|
Use CTEs for nested groups and authorizations
This commit introduces the usage of Common Table Expressions (CTEs) to
efficiently retrieve nested group hierarchies, without having to rely on
the "routes" table (which is an _incredibly_ inefficient way of getting
the data). This requires a patch to ActiveRecord (found in the added
initializer) to work properly as ActiveRecord doesn't support WITH
statements properly out of the box.
Unfortunately MySQL provides no efficient way of getting nested groups.
For example, the old routes setup could easily take 5-10 seconds
depending on the amount of "routes" in a database. Providing vastly
different logic for both MySQL and PostgreSQL will negatively impact the
development process. Because of this the various nested groups related
methods return empty relations when used in combination with MySQL.
For project authorizations the logic is split up into two classes:
* Gitlab::ProjectAuthorizations::WithNestedGroups
* Gitlab::ProjectAuthorizations::WithoutNestedGroups
Both classes get the fresh project authorizations (= as they should be
in the "project_authorizations" table), including nested groups if
PostgreSQL is used. The logic of these two classes is quite different
apart from their public interface. This complicates development a bit,
but unfortunately there is no way around this.
This commit also introduces Gitlab::GroupHierarchy. This class can be
used to get the ancestors and descendants of a base relation, or both by
using a UNION. This in turn is used by methods such as:
* Namespace#ancestors
* Namespace#descendants
* User#all_expanded_groups
Again this class relies on CTEs and thus only works on PostgreSQL. The
Namespace methods will return an empty relation when MySQL is used,
while User#all_expanded_groups will return only the groups a user is a
direct member of.
Performance wise the impact is quite large. For example, on GitLab.com
Namespace#descendants used to take around 580 ms to retrieve data for a
particular user. Using CTEs we are able to reduce this down to roughly 1
millisecond, returning the exact same data.
== On The Fly Refreshing
Refreshing of authorizations on the fly (= when
users.authorized_projects_populated was not set) is removed with this
commit. This simplifies the code, and ensures any queries used for
authorizations are not mutated because they are executed in a Rails
scope (e.g. Project.visible_to_user).
This commit includes a migration to schedule refreshing authorizations
for all users, ensuring all of them have their authorizations in place.
Said migration schedules users in batches of 5000, with 5 minutes
between every batch to smear the load around a bit.
== Spec Changes
This commit also introduces some changes to various specs. For example,
some specs for ProjectTeam assumed that creating a personal project
would _not_ lead to the owner having access, which is incorrect. Because
we also no longer refresh authorizations on the fly for new users some
code had to be added to the "empty_project" factory. This chunk of code
ensures that the owner's permissions are refreshed after creating the
project, something that is normally done in Projects::CreateService.
2017-04-24 15:19:22 +00:00
|
|
|
user.project_authorizations.delete_all
|
|
|
|
|
2018-07-11 14:36:08 +00:00
|
|
|
service.update_authorizations([], [[user.id, project.id, Gitlab::Access::MAINTAINER]])
|
2016-12-06 16:31:58 +00:00
|
|
|
|
|
|
|
authorizations = user.project_authorizations
|
|
|
|
|
|
|
|
expect(authorizations.length).to eq(1)
|
|
|
|
expect(authorizations[0].user_id).to eq(user.id)
|
|
|
|
expect(authorizations[0].project_id).to eq(project.id)
|
2018-07-11 14:36:08 +00:00
|
|
|
expect(authorizations[0].access_level).to eq(Gitlab::Access::MAINTAINER)
|
2016-12-06 16:31:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#fresh_access_levels_per_project' do
|
|
|
|
let(:hash) { service.fresh_access_levels_per_project }
|
|
|
|
|
|
|
|
it 'returns a Hash' do
|
|
|
|
expect(hash).to be_an_instance_of(Hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the keys to the project IDs' do
|
|
|
|
expect(hash.keys).to eq([project.id])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the values to the access levels' do
|
2018-07-11 14:36:08 +00:00
|
|
|
expect(hash.values).to eq([Gitlab::Access::MAINTAINER])
|
2016-12-06 16:31:58 +00:00
|
|
|
end
|
2017-02-27 22:16:39 +00:00
|
|
|
|
|
|
|
context 'personal projects' do
|
|
|
|
it 'includes the project with the right access level' do
|
2018-07-11 14:36:08 +00:00
|
|
|
expect(hash[project.id]).to eq(Gitlab::Access::MAINTAINER)
|
2017-02-27 22:16:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'projects the user is a member of' do
|
2017-08-02 19:55:11 +00:00
|
|
|
let!(:other_project) { create(:project) }
|
2017-02-27 22:16:39 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
other_project.team.add_reporter(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the project with the right access level' do
|
|
|
|
expect(hash[other_project.id]).to eq(Gitlab::Access::REPORTER)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'projects of groups the user is a member of' do
|
|
|
|
let(:group) { create(:group) }
|
2017-08-02 19:55:11 +00:00
|
|
|
let!(:other_project) { create(:project, group: group) }
|
2017-02-27 22:16:39 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
group.add_owner(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the project with the right access level' do
|
|
|
|
expect(hash[other_project.id]).to eq(Gitlab::Access::OWNER)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-24 09:20:54 +00:00
|
|
|
context 'projects of subgroups of groups the user is a member of' do
|
2017-02-27 22:16:39 +00:00
|
|
|
let(:group) { create(:group) }
|
|
|
|
let(:nested_group) { create(:group, parent: group) }
|
2017-08-02 19:55:11 +00:00
|
|
|
let!(:other_project) { create(:project, group: nested_group) }
|
2017-02-27 22:16:39 +00:00
|
|
|
|
|
|
|
before do
|
2018-07-11 14:36:08 +00:00
|
|
|
group.add_maintainer(user)
|
2017-02-27 22:16:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the project with the right access level' do
|
2018-07-11 14:36:08 +00:00
|
|
|
expect(hash[other_project.id]).to eq(Gitlab::Access::MAINTAINER)
|
2017-02-27 22:16:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'projects shared with groups the user is a member of' do
|
|
|
|
let(:group) { create(:group) }
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:other_project) { create(:project) }
|
2017-02-27 22:16:39 +00:00
|
|
|
let!(:project_group_link) { create(:project_group_link, project: other_project, group: group, group_access: Gitlab::Access::GUEST) }
|
|
|
|
|
|
|
|
before do
|
2018-07-11 14:36:08 +00:00
|
|
|
group.add_maintainer(user)
|
2017-02-27 22:16:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the project with the right access level' do
|
|
|
|
expect(hash[other_project.id]).to eq(Gitlab::Access::GUEST)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-24 09:20:54 +00:00
|
|
|
context 'projects shared with subgroups of groups the user is a member of' do
|
2017-02-27 22:16:39 +00:00
|
|
|
let(:group) { create(:group) }
|
|
|
|
let(:nested_group) { create(:group, parent: group) }
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:other_project) { create(:project) }
|
2017-02-27 22:16:39 +00:00
|
|
|
let!(:project_group_link) { create(:project_group_link, project: other_project, group: nested_group, group_access: Gitlab::Access::DEVELOPER) }
|
|
|
|
|
|
|
|
before do
|
2018-07-11 14:36:08 +00:00
|
|
|
group.add_maintainer(user)
|
2017-02-27 22:16:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the project with the right access level' do
|
|
|
|
expect(hash[other_project.id]).to eq(Gitlab::Access::DEVELOPER)
|
|
|
|
end
|
|
|
|
end
|
2016-12-06 16:31:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#current_authorizations_per_project' do
|
|
|
|
let(:hash) { service.current_authorizations_per_project }
|
|
|
|
|
|
|
|
it 'returns a Hash' do
|
|
|
|
expect(hash).to be_an_instance_of(Hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the keys to the project IDs' do
|
|
|
|
expect(hash.keys).to eq([project.id])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the values to the project authorization rows' do
|
2017-01-06 17:26:06 +00:00
|
|
|
expect(hash.values.length).to eq(1)
|
|
|
|
|
|
|
|
value = hash.values[0]
|
|
|
|
|
|
|
|
expect(value.project_id).to eq(project.id)
|
2018-07-11 14:36:08 +00:00
|
|
|
expect(value.access_level).to eq(Gitlab::Access::MAINTAINER)
|
2016-12-06 16:31:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#current_authorizations' do
|
|
|
|
context 'without authorizations' do
|
|
|
|
it 'returns an empty list' do
|
Use CTEs for nested groups and authorizations
This commit introduces the usage of Common Table Expressions (CTEs) to
efficiently retrieve nested group hierarchies, without having to rely on
the "routes" table (which is an _incredibly_ inefficient way of getting
the data). This requires a patch to ActiveRecord (found in the added
initializer) to work properly as ActiveRecord doesn't support WITH
statements properly out of the box.
Unfortunately MySQL provides no efficient way of getting nested groups.
For example, the old routes setup could easily take 5-10 seconds
depending on the amount of "routes" in a database. Providing vastly
different logic for both MySQL and PostgreSQL will negatively impact the
development process. Because of this the various nested groups related
methods return empty relations when used in combination with MySQL.
For project authorizations the logic is split up into two classes:
* Gitlab::ProjectAuthorizations::WithNestedGroups
* Gitlab::ProjectAuthorizations::WithoutNestedGroups
Both classes get the fresh project authorizations (= as they should be
in the "project_authorizations" table), including nested groups if
PostgreSQL is used. The logic of these two classes is quite different
apart from their public interface. This complicates development a bit,
but unfortunately there is no way around this.
This commit also introduces Gitlab::GroupHierarchy. This class can be
used to get the ancestors and descendants of a base relation, or both by
using a UNION. This in turn is used by methods such as:
* Namespace#ancestors
* Namespace#descendants
* User#all_expanded_groups
Again this class relies on CTEs and thus only works on PostgreSQL. The
Namespace methods will return an empty relation when MySQL is used,
while User#all_expanded_groups will return only the groups a user is a
direct member of.
Performance wise the impact is quite large. For example, on GitLab.com
Namespace#descendants used to take around 580 ms to retrieve data for a
particular user. Using CTEs we are able to reduce this down to roughly 1
millisecond, returning the exact same data.
== On The Fly Refreshing
Refreshing of authorizations on the fly (= when
users.authorized_projects_populated was not set) is removed with this
commit. This simplifies the code, and ensures any queries used for
authorizations are not mutated because they are executed in a Rails
scope (e.g. Project.visible_to_user).
This commit includes a migration to schedule refreshing authorizations
for all users, ensuring all of them have their authorizations in place.
Said migration schedules users in batches of 5000, with 5 minutes
between every batch to smear the load around a bit.
== Spec Changes
This commit also introduces some changes to various specs. For example,
some specs for ProjectTeam assumed that creating a personal project
would _not_ lead to the owner having access, which is incorrect. Because
we also no longer refresh authorizations on the fly for new users some
code had to be added to the "empty_project" factory. This chunk of code
ensures that the owner's permissions are refreshed after creating the
project, something that is normally done in Projects::CreateService.
2017-04-24 15:19:22 +00:00
|
|
|
user.project_authorizations.delete_all
|
|
|
|
|
2016-12-06 16:31:58 +00:00
|
|
|
expect(service.current_authorizations.empty?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an authorization' do
|
|
|
|
let(:row) { service.current_authorizations.take }
|
|
|
|
|
|
|
|
it 'returns the currently authorized projects' do
|
|
|
|
expect(service.current_authorizations.length).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the project ID for every row' do
|
|
|
|
expect(row.project_id).to eq(project.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the access level for every row' do
|
2018-07-11 14:36:08 +00:00
|
|
|
expect(row.access_level).to eq(Gitlab::Access::MAINTAINER)
|
2016-12-06 16:31:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#fresh_authorizations' do
|
|
|
|
it 'returns the new authorized projects' do
|
|
|
|
expect(service.fresh_authorizations.length).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the highest access level' do
|
|
|
|
project.team.add_guest(user)
|
|
|
|
|
|
|
|
rows = service.fresh_authorizations.to_a
|
|
|
|
|
|
|
|
expect(rows.length).to eq(1)
|
2018-07-11 14:36:08 +00:00
|
|
|
expect(rows.first.access_level).to eq(Gitlab::Access::MAINTAINER)
|
2016-12-06 16:31:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'every returned row' do
|
|
|
|
let(:row) { service.fresh_authorizations.take }
|
|
|
|
|
|
|
|
it 'includes the project ID' do
|
|
|
|
expect(row.project_id).to eq(project.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the access level' do
|
2018-07-11 14:36:08 +00:00
|
|
|
expect(row.access_level).to eq(Gitlab::Access::MAINTAINER)
|
2016-12-06 16:31:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|