-
Couldn't load subscription status.
- Fork 0
FEATURE: Add anonymous username generator plugin #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9a313b0
FEATURE: Add anonymous username generator plugin
Grubba27 2ee30e6
DEV: update
Grubba27 4e769d5
DEV: reduce random word list
Grubba27 b7d2d47
DEV: make random word list optional
Grubba27 f3e62a7
DEV: update name to invite-username-input
Grubba27 de2272a
DEV: use username_validator modifier
Grubba27 ee000d4
DEV: rename modifier to username_validations
Grubba27 f04ac9a
DEV: update based on core changes
Grubba27 d00b127
DEV: update modifier naming + context.user
Grubba27 24f1883
DEV: lint
Grubba27 1f9a61c
DEV: revert name again!
Grubba27 e7e09a4
dev: update spec to use model directly
Grubba27 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
assets/javascripts/discourse/components/randomizer-button.gjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import Component from "@glimmer/component"; | ||
| import { action } from "@ember/object"; | ||
| import { service } from "@ember/service"; | ||
| import DButton from "discourse/components/d-button"; | ||
|
|
||
| const getFirstName = (fullName) => fullName.split(" ")[0]; | ||
|
|
||
| export default class RandomizerButton extends Component { | ||
| @service siteSettings; | ||
|
|
||
| get randomWordsList() { | ||
| return this.siteSettings.random_words_list.split("|") || []; | ||
| } | ||
|
|
||
| fetchRandomWord() { | ||
| if (this.randomWordsList.length === 0) { | ||
| return ""; | ||
| } | ||
| return this.randomWordsList[ | ||
| Math.floor(Math.random() * this.randomWordsList.length) | ||
| ]; | ||
| } | ||
|
|
||
| @action | ||
| async generate() { | ||
| const randomizeFrom = getFirstName(this.args.randomizeFrom); | ||
| const randomWord = this.fetchRandomWord(); | ||
|
|
||
| this.args.onGenerate({ | ||
| target: { | ||
| value: randomizeFrom + randomWord + Math.floor(Math.random() * 1000), | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| get canGenerate() { | ||
| return ( | ||
| this.args.randomizeFrom && | ||
| this.args.randomizeFrom.length !== 0 && | ||
| getFirstName(this.args.randomizeFrom).trim() !== | ||
| this.args.randomizeFrom.trim() | ||
| ); | ||
| } | ||
|
|
||
| <template> | ||
| <DButton | ||
| @icon="rotate" | ||
| class="btn-primary randomizer-btn" | ||
| @action={{this.generate}} | ||
| disabled={{if this.canGenerate false true}} | ||
| /> | ||
| </template> | ||
| } |
45 changes: 45 additions & 0 deletions
45
assets/javascripts/discourse/initializers/sign-up-username-override.gjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { on } from "@ember/modifier"; | ||
| import InputTip from "discourse/components/input-tip"; | ||
| import valueEntered from "discourse/helpers/value-entered"; | ||
| import { apiInitializer } from "discourse/lib/api"; | ||
| import { i18n } from "discourse-i18n"; | ||
| import RandomizerButton from "../components/randomizer-button"; | ||
|
|
||
| export default apiInitializer((api) => { | ||
| const siteSettings = api.container.lookup("service:site-settings"); | ||
|
|
||
| api.renderInOutlet( | ||
| "username-input-invite", | ||
| <template> | ||
| <div class="input-with-randomizer-btn"> | ||
| <input | ||
| {{on "focusin" @controller.scrollInputIntoView}} | ||
| {{on "input" @controller.setAccountUsername}} | ||
| type="text" | ||
| value={{@controller.accountUsername}} | ||
| class={{valueEntered @controller.accountUsername}} | ||
| id="new-account-username" | ||
| name="username" | ||
| disabled={{siteSettings.only_generated_usernames}} | ||
| maxlength={{@controller.maxUsernameLength}} | ||
| autocomplete="off" | ||
| /> | ||
|
|
||
| <RandomizerButton | ||
| @randomizeFrom={{@controller.accountName}} | ||
| @onGenerate={{@controller.setAccountUsername}} | ||
| /> | ||
|
|
||
| </div> | ||
| {{#unless @controller.accountUsername}} | ||
| <label class="alt-placeholder" for="new-account-username"> | ||
| {{i18n "user.username.title"}} | ||
| </label> | ||
| {{/unless}} | ||
| <InputTip | ||
| @validation={{@controller.usernameValidation}} | ||
| id="username-validation" | ||
| /> | ||
| </template> | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| @use "lib/viewport"; | ||
|
|
||
| // change ordering of username input in invite sign up | ||
| .invites-show { | ||
| .invite-form form { | ||
| .input-group { | ||
| &.email-input { | ||
| order: 1; | ||
| } | ||
|
|
||
| &.name-input.name-required { | ||
| order: 2; | ||
| } | ||
|
|
||
| &.username-input { | ||
| order: 3; | ||
| } | ||
|
|
||
| &.password-input { | ||
| order: 4; | ||
| } | ||
| } | ||
|
|
||
| .invitation-cta { | ||
| order: 5; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| .input-with-randomizer-btn { | ||
| display: flex; | ||
|
|
||
| .btn-primary { | ||
| margin-left: 0.5rem; | ||
| height: 2.5rem; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,4 @@ | ||
| en: | ||
| discourse_anon_usernames: | ||
| errors: | ||
| username_invalid: "The chosen username appears to contain your last name. Please choose a different username." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # frozen_string_literal: true | ||
| RSpec.describe "Users Creation" do | ||
| before { SiteSetting.discourse_anon_usernames_enabled = true } | ||
|
|
||
| describe "InvitesController#perform_accept_invitation" do | ||
| context "with an email invite" do | ||
| let(:topic) { Fabricate(:topic) } | ||
| let(:invite) { Invite.generate(topic.user, email: "[email protected]", topic: topic) } | ||
|
|
||
| it "does not allow leaking last names in usernames" do | ||
| put "/invites/show/#{invite.invite_key}.json", | ||
| params: { | ||
| username: "John_smith", | ||
| name: "John Smith", | ||
| password: "someverystringpassword", | ||
| } | ||
|
|
||
| expect(response.status).to eq(412) | ||
| expect(response.body).to include(I18n.t("discourse_anon_usernames.errors.username_invalid")) | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| RSpec.describe "User invite flow" do | ||
| before do | ||
| SiteSetting.discourse_anon_usernames_enabled = true | ||
|
|
||
| SiteSetting.full_name_requirement = "required_at_signup" | ||
| SiteSetting.random_words_list = "Apple|Grape|Orange" | ||
| end | ||
|
|
||
| let(:topic) { Fabricate(:topic) } | ||
| let(:invite) { Invite.generate(topic.user, email: "[email protected]", topic: topic) } | ||
| let(:invite_form) { PageObjects::Pages::DiscourseAnonUsernames::InviteFormPage.new } | ||
| describe "invite acceptance" do | ||
| it "does not let user leak last names in usernames" do | ||
| invite_form.open(invite.invite_key) | ||
|
|
||
| invite_form.fill_name("John Smith") | ||
| invite_form.fill_username("John_smith") | ||
| invite_form.fill_password("someverystringpassword") | ||
|
|
||
| invite_form.accept! | ||
|
|
||
| expect(page).to have_content(I18n.t("discourse_anon_usernames.errors.username_invalid")) | ||
| end | ||
|
|
||
| describe "with `only_generated_usernames` setting enabled" do | ||
| before { SiteSetting.only_generated_usernames = true } | ||
|
|
||
| it "forces users to use generated usernames" do | ||
| invite_form.open(invite.invite_key) | ||
|
|
||
| expect(invite_form).to have_username_disabled | ||
| expect(invite_form.username_field.value).to eq("") | ||
|
|
||
| # generated usernames follow the pattern: | ||
| # <first_name><random_word><number> | ||
|
|
||
| invite_form.fill_name("John Smith") | ||
| invite_form.randomize_username! | ||
|
|
||
| generated_username = invite_form.username_field.value | ||
| expect(generated_username).to include("John") | ||
|
|
||
| random_words = SiteSetting.random_words_list.split("|") | ||
| contains_random_word = random_words.any? { |word| generated_username.include?(word) } | ||
| expect(contains_random_word).to eq(true) | ||
|
|
||
| invite_form.fill_password("someverystringpassword") | ||
| invite_form.accept! | ||
|
|
||
| expect(page).to have_css(".login-welcome-header") | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module PageObjects | ||
| module Pages | ||
| module DiscourseAnonUsernames | ||
| class InviteFormPage < PageObjects::Pages::InviteForm | ||
| def fill_name(full_name) | ||
| find("#new-account-name").fill_in(with: full_name) | ||
| end | ||
|
|
||
| def accept! | ||
| find(".invitation-cta__accept").click | ||
| end | ||
|
|
||
| def username_field | ||
| find("#new-account-username") | ||
| end | ||
|
|
||
| def has_username_disabled? | ||
| username_field[:disabled] == true | ||
| end | ||
|
|
||
| def randomize_username! | ||
| find(".randomizer-btn").click | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is after_commit on create.. isn't the data already in the DB by now?
https://github.com/discourse/discourse/blob/f51960287f6a7297243fd904966811c90b7ce5b1/app/models/user.rb#L207