Skip to content

Commit 1cd0497

Browse files
committed
Enable users to update password without inputing current password once
1 parent f96d0e9 commit 1cd0497

6 files changed

Lines changed: 46 additions & 4 deletions

File tree

app/controllers/auth/registrations_controller.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ def check_enabled_registrations
3232
redirect_to root_path if single_user_mode? || !Setting.open_registrations
3333
end
3434

35+
def update_resource(resource, params)
36+
if resource.try(:has_dummy_password?)
37+
resource.update_without_current_password(params)
38+
else
39+
super
40+
end
41+
end
42+
3543
private
3644

3745
def determine_layout

app/models/form/oauth_registration.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def build_user
6666
locale: locale,
6767
password: password,
6868
password_confirmation: password,
69+
dummy_password_flag: true,
6970
account_attributes: {
7071
username: username,
7172
avatar: avatar

app/models/user.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class User < ApplicationRecord
2121
scope :admins, -> { where(admin: true) }
2222
scope :confirmed, -> { where.not(confirmed_at: nil) }
2323

24+
before_validation :disable_dummy_password_flag, on: :update, if: :encrypted_password_changed?
25+
2426
def confirmed?
2527
confirmed_at.present?
2628
end
@@ -40,4 +42,25 @@ def setting_boost_modal
4042
def setting_auto_play_gif
4143
settings.auto_play_gif
4244
end
45+
46+
def has_dummy_password?
47+
dummy_password_flag
48+
end
49+
50+
def disable_dummy_password_flag
51+
self.dummy_password_flag = false
52+
true
53+
end
54+
55+
def update_without_current_password(params, *options)
56+
if params[:password].blank?
57+
params.delete(:password)
58+
params.delete(:password_confirmation) if params[:password_confirmation].blank?
59+
end
60+
p params
61+
62+
result = update_attributes(params, *options)
63+
clean_up_passwords
64+
result
65+
end
4366
end

app/views/auth/registrations/edit.html.haml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
= render 'shared/error_messages', object: resource
66

77
= f.input :email, placeholder: t('simple_form.labels.defaults.email'), input_html: { 'aria-label' => t('simple_form.labels.defaults.email') }
8-
= f.input :password, autocomplete: "off", placeholder: t('simple_form.labels.defaults.new_password'), input_html: { 'aria-label' => t('simple_form.labels.defaults.new_password') }
9-
= f.input :password_confirmation, autocomplete: "off", placeholder: t('simple_form.labels.defaults.confirm_new_password'), input_html: { 'aria-label' => t('simple_form.labels.defaults.confirm_new_password') }
10-
= f.input :current_password, autocomplete: "off", placeholder: t('simple_form.labels.defaults.current_password'), input_html: { 'aria-label' => t('simple_form.labels.defaults.current_password') }
8+
- if current_user.has_dummy_password?
9+
= f.input :password, autocomplete: "off", placeholder: t('simple_form.labels.defaults.password'), input_html: { 'aria-label' => t('simple_form.labels.defaults.password') }
10+
= f.input :password_confirmation, autocomplete: "off", placeholder: t('simple_form.labels.defaults.confirm_password'), input_html: { 'aria-label' => t('simple_form.labels.defaults.confirm_password') }
11+
- else
12+
= f.input :password, autocomplete: "off", placeholder: t('simple_form.labels.defaults.new_password'), input_html: { 'aria-label' => t('simple_form.labels.defaults.new_password') }
13+
= f.input :password_confirmation, autocomplete: "off", placeholder: t('simple_form.labels.defaults.confirm_new_password'), input_html: { 'aria-label' => t('simple_form.labels.defaults.confirm_new_password') }
14+
= f.input :current_password, autocomplete: "off", placeholder: t('simple_form.labels.defaults.current_password'), input_html: { 'aria-label' => t('simple_form.labels.defaults.current_password') }
1115

1216
.actions
1317
= f.button :button, t('generic.save_changes'), type: :submit
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddDummyPasswordFlagToUser < ActiveRecord::Migration[5.0]
2+
def change
3+
add_column :users, :dummy_password_flag, :boolean, default: false, null: false
4+
end
5+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 20170504103736) do
13+
ActiveRecord::Schema.define(version: 20170517123337) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
@@ -336,6 +336,7 @@
336336
t.boolean "otp_required_for_login"
337337
t.datetime "last_emailed_at"
338338
t.string "otp_backup_codes", array: true
339+
t.boolean "dummy_password_flag", default: false, null: false
339340
t.index ["account_id"], name: "index_users_on_account_id", using: :btree
340341
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree
341342
t.index ["email"], name: "index_users_on_email", unique: true, using: :btree

0 commit comments

Comments
 (0)