Commit 7b11adac authored by Jan Provaznik's avatar Jan Provaznik

Merge branch '341068-create-proper-usernamespace-entity-when-a-user-is-created' into 'master'

Workspace: Create proper UserNamespace entity when a user is created

See merge request gitlab-org/gitlab!71221
parents f4ae58f2 c6a22cae
...@@ -44,6 +44,8 @@ class Namespace < ApplicationRecord ...@@ -44,6 +44,8 @@ class Namespace < ApplicationRecord
# This should _not_ be `inverse_of: :namespace`, because that would also set # This should _not_ be `inverse_of: :namespace`, because that would also set
# `user.namespace` when this user creates a group with themselves as `owner`. # `user.namespace` when this user creates a group with themselves as `owner`.
# TODO: can this be moved into the UserNamespace class?
# evaluate in issue https://gitlab.com/gitlab-org/gitlab/-/issues/341070
belongs_to :owner, class_name: "User" belongs_to :owner, class_name: "User"
belongs_to :parent, class_name: "Namespace" belongs_to :parent, class_name: "Namespace"
......
...@@ -112,9 +112,14 @@ class User < ApplicationRecord ...@@ -112,9 +112,14 @@ class User < ApplicationRecord
# #
# Namespace for personal projects # Namespace for personal projects
# TODO: change to `type: Namespaces::UserNamespace.sti_name` when # TODO: change to `:namespace, -> { where(type: Namespaces::UserNamespace.sti_name}, class_name: 'Namespaces::UserNamespace'...`
# working on issue https://gitlab.com/gitlab-org/gitlab/-/issues/341070 # when working on issue https://gitlab.com/gitlab-org/gitlab/-/issues/341070
has_one :namespace, -> { where(type: [nil, Namespaces::UserNamespace.sti_name]) }, dependent: :destroy, foreign_key: :owner_id, inverse_of: :owner, autosave: true # rubocop:disable Cop/ActiveRecordDependent has_one :namespace,
-> { where(type: [nil, Namespaces::UserNamespace.sti_name]) },
dependent: :destroy, # rubocop:disable Cop/ActiveRecordDependent
foreign_key: :owner_id,
inverse_of: :owner,
autosave: true # rubocop:disable Cop/ActiveRecordDependent
# Profile # Profile
has_many :keys, -> { regular_keys }, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent has_many :keys, -> { regular_keys }, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
...@@ -1437,7 +1442,10 @@ class User < ApplicationRecord ...@@ -1437,7 +1442,10 @@ class User < ApplicationRecord
namespace.path = username if username_changed? namespace.path = username if username_changed?
namespace.name = name if name_changed? namespace.name = name if name_changed?
else else
namespace = build_namespace(path: username, name: name) # TODO: we should no longer need the `type` parameter once we can make the
# the `has_one :namespace` association use the correct class.
# issue https://gitlab.com/gitlab-org/gitlab/-/issues/341070
namespace = build_namespace(path: username, name: name, type: ::Namespaces::UserNamespace.sti_name)
namespace.build_namespace_settings namespace.build_namespace_settings
end end
end end
......
...@@ -5,6 +5,8 @@ FactoryBot.define do ...@@ -5,6 +5,8 @@ FactoryBot.define do
sequence(:name) { |n| "namespace#{n}" } sequence(:name) { |n| "namespace#{n}" }
path { name.downcase.gsub(/\s/, '_') } path { name.downcase.gsub(/\s/, '_') }
# TODO: can this be moved into the :user_namespace factory?
# evaluate in issue https://gitlab.com/gitlab-org/gitlab/-/issues/341070
owner { association(:user, strategy: :build, namespace: instance, username: path) } owner { association(:user, strategy: :build, namespace: instance, username: path) }
trait :with_aggregation_schedule do trait :with_aggregation_schedule do
......
...@@ -378,7 +378,7 @@ RSpec.describe User do ...@@ -378,7 +378,7 @@ RSpec.describe User do
end end
context 'when username is changed' do context 'when username is changed' do
let(:user) { build_stubbed(:user, username: 'old_path', namespace: build_stubbed(:namespace)) } let(:user) { build_stubbed(:user, username: 'old_path', namespace: build_stubbed(:user_namespace)) }
it 'validates move_dir is allowed for the namespace' do it 'validates move_dir is allowed for the namespace' do
expect(user.namespace).to receive(:any_project_has_container_registry_tags?).and_return(true) expect(user.namespace).to receive(:any_project_has_container_registry_tags?).and_return(true)
...@@ -3855,7 +3855,7 @@ RSpec.describe User do ...@@ -3855,7 +3855,7 @@ RSpec.describe User do
end end
context 'with runner in a personal project' do context 'with runner in a personal project' do
let!(:namespace) { create(:namespace, owner: user) } let!(:namespace) { create(:user_namespace, owner: user) }
let!(:project) { create(:project, namespace: namespace) } let!(:project) { create(:project, namespace: namespace) }
let!(:runner) { create(:ci_runner, :project, projects: [project]) } let!(:runner) { create(:ci_runner, :project, projects: [project]) }
...@@ -3923,7 +3923,7 @@ RSpec.describe User do ...@@ -3923,7 +3923,7 @@ RSpec.describe User do
end end
context 'with personal project runner in an owned group in an owned namespace and a group runner in that group' do context 'with personal project runner in an owned group in an owned namespace and a group runner in that group' do
let!(:namespace) { create(:namespace, owner: user) } let!(:namespace) { create(:user_namespace, owner: user) }
let!(:group) { create(:group) } let!(:group) { create(:group) }
let!(:group_runner) { create(:ci_runner, :group, groups: [group]) } let!(:group_runner) { create(:ci_runner, :group, groups: [group]) }
let!(:project) { create(:project, namespace: namespace, group: group) } let!(:project) { create(:project, namespace: namespace, group: group) }
...@@ -3937,7 +3937,7 @@ RSpec.describe User do ...@@ -3937,7 +3937,7 @@ RSpec.describe User do
end end
context 'with personal project runner in an owned namespace, an owned group, a subgroup and a group runner in that subgroup' do context 'with personal project runner in an owned namespace, an owned group, a subgroup and a group runner in that subgroup' do
let!(:namespace) { create(:namespace, owner: user) } let!(:namespace) { create(:user_namespace, owner: user) }
let!(:group) { create(:group) } let!(:group) { create(:group) }
let!(:subgroup) { create(:group, parent: group) } let!(:subgroup) { create(:group, parent: group) }
let!(:group_runner) { create(:ci_runner, :group, groups: [subgroup]) } let!(:group_runner) { create(:ci_runner, :group, groups: [subgroup]) }
...@@ -4619,6 +4619,7 @@ RSpec.describe User do ...@@ -4619,6 +4619,7 @@ RSpec.describe User do
user.save! user.save!
expect(user.namespace).not_to be_nil expect(user.namespace).not_to be_nil
expect(user.namespace).to be_kind_of(Namespaces::UserNamespace)
end end
it 'creates the namespace setting' do it 'creates the namespace setting' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment