Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Don't allow usernames to clash with teams
Browse files Browse the repository at this point in the history
Up until now we allowed users to have the same username as some teams. This is
troublesome because then the private team assigned to this user would clash
with the existing one.

In the future we should figure out a way to better handle these hidden teams
(e.g. by creating one with a random name and the reference it on the user row).
For now, in order to not be too disruptive we will disallow creating users with
the same name as an existing team (note that the other way around is also
disallowed).

Fixes #836

Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
  • Loading branch information
mssola committed Apr 25, 2016
1 parent 0cc831b commit b5b0896
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
21 changes: 13 additions & 8 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class User < ActiveRecord::Base
}

# Actions performed before/after create.
validate :private_namespace_available, on: :create
validate :private_namespace_and_team_available, on: :create
after_create :create_personal_namespace!

has_many :team_users
Expand All @@ -64,9 +64,11 @@ def email_required?
!(Portus::LDAP.enabled? && !ldap_name.nil?)
end

def private_namespace_available
return unless Namespace.exists?(name: username)
errors.add(:username, "cannot be used as name for private namespace")
# It adds an error if the username clashes with either a namespace or a team.
def private_namespace_and_team_available
return unless Namespace.exists?(name: username) || Team.exists?(name: username)
errors.add(:username, "'#{username}' cannot be used: there's either a "\
"namespace or a team named like this.")
end

# Returns true if the current user is the Portus user.
Expand All @@ -81,10 +83,13 @@ def create_personal_namespace!
# the registry is not configured yet, we cannot create the namespace
return unless Registry.any?

team = Team.find_by(name: username)
if team.nil?
team = Team.create!(name: username, owners: [self], hidden: true)
end
# Leave early if the namespace already exists.
ns = Namespace.find_by(name: username)
return ns if ns

# Note that this shouldn't be a problem since the User controller will make
# sure that we don't create a user that clashes with this team.
team = Team.create!(name: username, owners: [self], hidden: true)

default_description = "This personal namespace belongs to #{username}."
Namespace.find_or_create_by!(
Expand Down
3 changes: 2 additions & 1 deletion spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
expect(user.save).to be false
expect(user.errors.size).to eq(1)
expect(user.errors.first)
.to match_array([:username, "cannot be used as name for private namespace"])
.to match_array([:username, "'coolname' cannot be used: there's either a namespace or " \
"a team named like this."])
end

it "#email_required?" do
Expand Down

0 comments on commit b5b0896

Please sign in to comment.