forked from decidim/decidim
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix nicknames uniqueness with different cases (decidim#8792)
* allowing only lowercase in the nickname * nickname test system * add in locales the "lowercase" precision * changing routing for timeline * changing routing for activities * changing routing for other profile tabs * migration to change commune nicknames * linter * minor changes increase performance for migration * Refactor migration for lisibility * add fix into changelog * linter * Update CHANGELOG.md * add notification when updating nickname * fix number of notification * Add notification when updating nickname (#3) add spec for event add translation for notification normalize locales add logger refactor add random numbers to prevent errors * update message in notification * update test linter * minor changes * Change requests nickname (#8) * back to "any case" nickname * check uniqueness case insensitivity * change profiles url * mention, parse case insensitively * mention, render nicknames case insensitively * mentions spec * update migration * linter * migration to rake tasks spec rake task * linter * add test notification fix test nickname * linter * Update CHANGELOG.md * Update CHANGELOG.md * Update CHANGELOG.md * Update CHANGELOG.md Co-authored-by: Andrés Pereira de Lucena <[email protected]> * Update CHANGELOG.md Co-authored-by: Andrés Pereira de Lucena <[email protected]> * Update CHANGELOG.md * Update account_form.rb * linter * remove the if statement private fonctions * remove test task * relaunch tests * add organization criteria refactors * lint * use nicknamize instead of random numbers * add the not deleted scope * update nicknamizable to check for case Co-authored-by: Andrés Pereira de Lucena <[email protected]>
- Loading branch information
1 parent
9ef90b4
commit 2f6d596
Showing
19 changed files
with
283 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,21 @@ | ||
# frozen-string_literal: true | ||
|
||
module Decidim | ||
class ChangeNicknameEvent < Decidim::Events::SimpleEvent | ||
include Decidim::Events::NotificationEvent | ||
delegate :organization, to: :user, prefix: false | ||
delegate :url_helpers, to: "Decidim::Core::Engine.routes" | ||
|
||
i18n_attributes :link_to_account_settings | ||
|
||
def notification_title | ||
I18n.t("decidim.events.nickname_event.notification_body", i18n_options).to_s.html_safe | ||
end | ||
|
||
def i18n_options | ||
{ | ||
link_to_account_settings: url_helpers.account_path | ||
} | ||
end | ||
end | ||
end |
This file contains 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 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 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 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 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
51 changes: 51 additions & 0 deletions
51
decidim-core/lib/tasks/upgrade/decidim_fix_nickname_uniqueness.rake
This file contains 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,51 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :decidim do | ||
namespace :upgrade do | ||
desc "Modify nicknames with random numbers when exists similar ones case insensitively" | ||
task fix_nickname_uniqueness: :environment do | ||
logger = Logger.new($stdout) | ||
logger.info("Updating conflicting user nicknames...") | ||
|
||
# list of users already changed in the process | ||
has_changed = [] | ||
|
||
Decidim::User.not_deleted.find_each do |user| | ||
next if has_changed.include? user.id | ||
|
||
Decidim::User.where(organization: user.organization) | ||
.where("nickname ILIKE ?", user.nickname.downcase) | ||
.where.not(id: has_changed + [user.id]) | ||
.not_deleted | ||
.order(:created_at) | ||
.each do |similar_user| | ||
# change her nickname to the lowercased one with numbers if needed | ||
begin | ||
update_user_nickname(similar_user, Decidim::UserBaseEntity.nicknamize(similar_user.nickname, organization: similar_user.organization)) | ||
rescue ActiveRecord::RecordInvalid => e | ||
logger.warn("User ID (#{similar_user.id}) : #{e}") | ||
end | ||
has_changed.append(similar_user.id) | ||
end | ||
end | ||
logger.info("Process terminated, #{has_changed.count} users nickname have been updated.") | ||
end | ||
|
||
private | ||
|
||
def send_notification_to(user) | ||
Decidim::EventsManager.publish({ | ||
event: "decidim.events.nickname_event", | ||
event_class: Decidim::ChangeNicknameEvent, | ||
affected_users: [user], | ||
resource: user | ||
}) | ||
end | ||
|
||
def update_user_nickname(user, new_nickname) | ||
user.update!(nickname: new_nickname) | ||
send_notification_to(user) | ||
user | ||
end | ||
end | ||
end |
This file contains 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
25 changes: 25 additions & 0 deletions
25
decidim-core/spec/controllers/decidim/profiles_controller_spec.rb
This file contains 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,25 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
module Decidim | ||
describe ProfilesController, type: :controller do | ||
routes { Decidim::Core::Engine.routes } | ||
|
||
let(:organization) { create(:organization) } | ||
let!(:user) { create(:user, nickname: "Nick", organization: organization) } | ||
|
||
before do | ||
request.env["decidim.current_organization"] = organization | ||
end | ||
|
||
describe "#badges" do | ||
context "with an user with uppercase" do | ||
it "returns the lowercased user" do | ||
get :badges, params: { nickname: "NICK" } | ||
expect(response).to render_template(:show) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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
34 changes: 34 additions & 0 deletions
34
decidim-core/spec/controllers/decidim/user_timeline_controller_spec.rb
This file contains 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,34 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
module Decidim | ||
describe UserTimelineController, type: :controller do | ||
routes { Decidim::Core::Engine.routes } | ||
|
||
let(:organization) { create(:organization) } | ||
let!(:user) { create(:user, :confirmed, nickname: "Nick", organization: organization) } | ||
|
||
before do | ||
request.env["decidim.current_organization"] = organization | ||
sign_in user | ||
end | ||
|
||
describe "#index" do | ||
context "with a different user than me" do | ||
it "raises an ActionController::RoutingError" do | ||
expect do | ||
get :index, params: { nickname: "foobar" } | ||
end.to raise_error(ActionController::RoutingError, "Not Found") | ||
end | ||
end | ||
|
||
context "with my user with uppercase" do | ||
it "returns the lowercased user" do | ||
get :index, params: { nickname: "NICK" } | ||
expect(response).to render_template(:index) | ||
end | ||
end | ||
end | ||
end | ||
end |
17 changes: 17 additions & 0 deletions
17
decidim-core/spec/events/decidim/change_nickname_event_spec.rb
This file contains 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,17 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
describe Decidim::ChangeNicknameEvent do | ||
include_context "when a simple event" | ||
|
||
let(:event_name) { "decidim.events.change_nickname_event" } | ||
let(:resource) { create :user } | ||
let(:author) { resource } | ||
|
||
describe "notification_title" do | ||
it "is generated correctly" do | ||
expect(subject.notification_title).to include("We have corrected the way nicknames are used so that there are no duplicates, and that's why we have removed the case sensitive rule. <br/> Your nickname was created after another one with the same name, so we have automatically renamed it. You can change it from <a href=\"/account\">your account settings</a>.") | ||
end | ||
end | ||
end |
This file contains 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 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 |
---|---|---|
|
@@ -226,6 +226,27 @@ | |
end | ||
end | ||
|
||
context "when nickname is not unique case insensitively" do | ||
let!(:user) { create(:user, nickname: "Nick", organization: organization) } | ||
|
||
it "show an error message" do | ||
find(".sign-up-link").click | ||
|
||
within ".new_user" do | ||
fill_in :registration_user_email, with: "[email protected]" | ||
fill_in :registration_user_name, with: "Responsible Citizen" | ||
fill_in :registration_user_nickname, with: "NiCk" | ||
fill_in :registration_user_password, with: "DfyvHn425mYAy2HL" | ||
fill_in :registration_user_password_confirmation, with: "DfyvHn425mYAy2HL" | ||
check :registration_user_tos_agreement | ||
check :registration_user_newsletter | ||
find("*[type=submit]").click | ||
end | ||
|
||
expect(page).to have_content("has already been taken") | ||
end | ||
end | ||
|
||
context "when sign up is disabled" do | ||
let(:organization) { create(:organization, users_registration_mode: :existing) } | ||
|
||
|
Oops, something went wrong.