Skip to content

Commit

Permalink
Enable users to update password without inputing current password once
Browse files Browse the repository at this point in the history
  • Loading branch information
tomoasleep committed May 22, 2017
1 parent f96d0e9 commit 1cd0497
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 4 deletions.
8 changes: 8 additions & 0 deletions app/controllers/auth/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ def check_enabled_registrations
redirect_to root_path if single_user_mode? || !Setting.open_registrations
end

def update_resource(resource, params)
if resource.try(:has_dummy_password?)
resource.update_without_current_password(params)
else
super
end
end

private

def determine_layout
Expand Down
1 change: 1 addition & 0 deletions app/models/form/oauth_registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def build_user
locale: locale,
password: password,
password_confirmation: password,
dummy_password_flag: true,
account_attributes: {
username: username,
avatar: avatar
Expand Down
23 changes: 23 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class User < ApplicationRecord
scope :admins, -> { where(admin: true) }
scope :confirmed, -> { where.not(confirmed_at: nil) }

before_validation :disable_dummy_password_flag, on: :update, if: :encrypted_password_changed?

def confirmed?
confirmed_at.present?
end
Expand All @@ -40,4 +42,25 @@ def setting_boost_modal
def setting_auto_play_gif
settings.auto_play_gif
end

def has_dummy_password?
dummy_password_flag
end

def disable_dummy_password_flag
self.dummy_password_flag = false
true
end

def update_without_current_password(params, *options)
if params[:password].blank?
params.delete(:password)
params.delete(:password_confirmation) if params[:password_confirmation].blank?
end
p params

result = update_attributes(params, *options)
clean_up_passwords
result
end
end
10 changes: 7 additions & 3 deletions app/views/auth/registrations/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
= render 'shared/error_messages', object: resource

= f.input :email, placeholder: t('simple_form.labels.defaults.email'), input_html: { 'aria-label' => t('simple_form.labels.defaults.email') }
= f.input :password, autocomplete: "off", placeholder: t('simple_form.labels.defaults.new_password'), input_html: { 'aria-label' => t('simple_form.labels.defaults.new_password') }
= 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') }
= 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') }
- if current_user.has_dummy_password?
= f.input :password, autocomplete: "off", placeholder: t('simple_form.labels.defaults.password'), input_html: { 'aria-label' => t('simple_form.labels.defaults.password') }
= 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') }
- else
= f.input :password, autocomplete: "off", placeholder: t('simple_form.labels.defaults.new_password'), input_html: { 'aria-label' => t('simple_form.labels.defaults.new_password') }
= 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') }
= 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') }

.actions
= f.button :button, t('generic.save_changes'), type: :submit
5 changes: 5 additions & 0 deletions db/migrate/20170517123337_add_dummy_password_flag_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDummyPasswordFlagToUser < ActiveRecord::Migration[5.0]
def change
add_column :users, :dummy_password_flag, :boolean, default: false, null: false
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170504103736) do
ActiveRecord::Schema.define(version: 20170517123337) do

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

0 comments on commit 1cd0497

Please sign in to comment.