Skip to content

Commit

Permalink
Allow creating and updating (user|org) names with dots (#4925)
Browse files Browse the repository at this point in the history
In #4690, we made it possible to create usernames from SSO signups that include dots. But that would have failed, as I found out working on this, due to a database constraint that needed to be updated.

Additionally, it was still not possible to change or manually create an account with a username that
includes a dot in the UI. This PR fixes that too.

For consistency, we allow dots in org names as well.

Fixes #4909, #4920
  • Loading branch information
tsenart committed Jul 17, 2019
1 parent a5e8889 commit 992aa56
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ All notable changes to Sourcegraph are documented in this file.

### Changed

- Usernames can now contain the `.` character (#4690).
- Usernames and org names can now contain the `.` character (#4674).

### Added

Expand Down
4 changes: 2 additions & 2 deletions cmd/frontend/db/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ Indexes:
Check constraints:
"orgs_display_name_max_length" CHECK (char_length(display_name) <= 255)
"orgs_name_max_length" CHECK (char_length(name::text) <= 255)
"orgs_name_valid_chars" CHECK (name ~ '^[a-zA-Z0-9](?:[a-zA-Z0-9]|-(?=[a-zA-Z0-9]))*$'::citext)
"orgs_name_valid_chars" CHECK (name ~ '^[a-zA-Z0-9](?:[a-zA-Z0-9]|[-.](?=[a-zA-Z0-9]))*$'::citext)
Referenced by:
TABLE "names" CONSTRAINT "names_org_id_fkey" FOREIGN KEY (org_id) REFERENCES orgs(id) ON UPDATE CASCADE ON DELETE CASCADE
TABLE "org_invitations" CONSTRAINT "org_invitations_org_id_fkey" FOREIGN KEY (org_id) REFERENCES orgs(id)
Expand Down Expand Up @@ -612,7 +612,7 @@ Indexes:
Check constraints:
"users_display_name_max_length" CHECK (char_length(display_name) <= 255)
"users_username_max_length" CHECK (char_length(username::text) <= 255)
"users_username_valid_chars" CHECK (username ~ '^[a-zA-Z0-9](?:[a-zA-Z0-9]|-(?=[a-zA-Z0-9]))*$'::citext)
"users_username_valid_chars" CHECK (username ~ '^[a-zA-Z0-9](?:[a-zA-Z0-9]|[-.](?=[a-zA-Z0-9]))*$'::citext)
Referenced by:
TABLE "access_tokens" CONSTRAINT "access_tokens_creator_user_id_fkey" FOREIGN KEY (creator_user_id) REFERENCES users(id)
TABLE "access_tokens" CONSTRAINT "access_tokens_subject_user_id_fkey" FOREIGN KEY (subject_user_id) REFERENCES users(id)
Expand Down
9 changes: 8 additions & 1 deletion cmd/frontend/db/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,21 @@ var usernamesForTests = []struct {
{"deadmau5", true},
{"deadmau-5", true},
{"3blindmice", true},
{"nick.com", true},
{"nick.com.uk", true},
{"nick.com-put-er", true},
{"777", true},
{"7-7", true},
{"long-butnotquitelongenoughtoreachlimit", true},

{".nick", false},
{"-nick", false},
{"nick-", false},
{"nick.", false},
{"nick--s", false},
{"nick--sny", false},
{"nick.com", false},
{"nick..sny", false},
{"nick.-sny", false},
{"nick_s", false},
{"_", false},
{"_nick", false},
Expand Down
13 changes: 13 additions & 0 deletions migrations/1528395581_allows_dots_in_usernames.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
BEGIN;

ALTER TABLE orgs
DROP CONSTRAINT orgs_name_valid_chars,
ADD CONSTRAINT orgs_name_valid_chars
CHECK (name ~ '^[a-zA-Z0-9](?:[a-zA-Z0-9]|-(?=[a-zA-Z0-9]))*$');

ALTER TABLE users
DROP CONSTRAINT users_username_valid_chars,
ADD CONSTRAINT users_username_valid_chars
CHECK (username ~ '^[a-zA-Z0-9](?:[a-zA-Z0-9]|-(?=[a-zA-Z0-9]))*$');

COMMIT;
13 changes: 13 additions & 0 deletions migrations/1528395581_allows_dots_in_usernames.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
BEGIN;

ALTER TABLE orgs
DROP CONSTRAINT orgs_name_valid_chars,
ADD CONSTRAINT orgs_name_valid_chars
CHECK (name ~ '^[a-zA-Z0-9](?:[a-zA-Z0-9]|[-.](?=[a-zA-Z0-9]))*$');

ALTER TABLE users
DROP CONSTRAINT users_username_valid_chars,
ADD CONSTRAINT users_username_valid_chars
CHECK (username ~ '^[a-zA-Z0-9](?:[a-zA-Z0-9]|[-.](?=[a-zA-Z0-9]))*$');

COMMIT;
48 changes: 48 additions & 0 deletions migrations/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions web/src/org/new/NewOrganizationPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ export class NewOrganizationPage extends React.Component<Props, State> {
aria-describedby="new-org-page__form-name-help"
/>
<small id="new-org-page__form-name-help" className="form-text text-muted">
An organization name consists of letters, numbers, hyphens (-) and may not begin or end with
a hyphen
An organization name consists of letters, numbers, hyphens (-), dots (.) and may not begin
or end with a hyphen nor a dot.
</small>
</div>

Expand Down
4 changes: 4 additions & 0 deletions web/src/site-admin/SiteAdminCreateUserPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ export class SiteAdminCreateUserPage extends React.Component<Props, State> {
disabled={this.state.loading}
autoFocus={true}
/>
<small className="form-text text-muted">
A username consists of letters, numbers, hyphens (-), dots (.) and may not begin or end
with a hyphen nor a dot.
</small>
</div>
<div className="form-group site-admin-create-user-page__form-group">
<label htmlFor="site-admin-create-user-page__form-email">Email</label>
Expand Down
2 changes: 1 addition & 1 deletion web/src/user/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Regular expression to identify valid username.
*/
export const VALID_USERNAME_REGEXP = /^[a-zA-Z0-9](?:[a-zA-Z0-9]|-(?=[a-zA-Z0-9]))*$/.source
export const VALID_USERNAME_REGEXP = /^[a-zA-Z0-9](?:[a-zA-Z0-9]|[-.](?=[a-zA-Z0-9]))*$/.source

/** Maximum allowed length for a username. */
export const USERNAME_MAX_LENGTH = 255
Expand Down
6 changes: 3 additions & 3 deletions web/src/user/settings/profile/UserSettingsProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ export class UserSettingsProfilePage extends React.Component<Props, State> {
}
aria-describedby="user-settings-profile-page__form-username-help"
/>
<small id="user-settings-profile-page__form-username-help" className="form-text text-muted">
A username consists of letters, numbers, hyphens (-) and may not begin or end with a
hyphen
<small className="form-text text-muted">
A username consists of letters, numbers, hyphens (-), dots (.) and may not begin or end
with a hyphen nor a dot.
</small>
</div>
<div className="form-group">
Expand Down

0 comments on commit 992aa56

Please sign in to comment.