Last active
August 29, 2015 14:08
-
-
Save katryo/008db48fb21b5dc6ddf2 to your computer and use it in GitHub Desktop.
deviseで、パスワードを登録していないユーザーはパスワード追加、登録しているユーザーはパスワードを更新する ref: http://qiita.com/katryo/items/b3ee01758bbe72fb764d
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
# Update record attributes when :current_password matches, otherwise returns | |
# error on :current_password. It also automatically rejects :password and | |
# :password_confirmation if they are blank. | |
def update_with_password(params, *options) | |
current_password = params.delete(:current_password) | |
if params[:password].blank? | |
params.delete(:password) | |
params.delete(:password_confirmation) if params[:password_confirmation].blank? | |
end | |
result = if valid_password?(current_password) | |
update_attributes(params, *options) | |
else | |
self.assign_attributes(params, *options) | |
self.valid? | |
self.errors.add(:current_password, current_password.blank? ? :blank : :invalid) | |
false | |
end | |
clean_up_passwords | |
result | |
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
link_to('パスワードを変える', edit_user_registration_path(@user)) |
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
# PUT /resource | |
# We need to use a copy of the resource because we don't want to change | |
# the current user in place. | |
def update | |
self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key) | |
prev_unconfirmed_email = resource.unconfirmed_email if resource.respond_to?(:unconfirmed_email) | |
if update_resource(resource, account_update_params) | |
yield resource if block_given? | |
if is_flashing_format? | |
flash_key = update_needs_confirmation?(resource, prev_unconfirmed_email) ? | |
:update_needs_confirmation : :updated | |
set_flash_message :notice, flash_key | |
end | |
sign_in resource_name, resource, bypass: true | |
respond_with resource, location: after_update_path_for(resource) | |
else | |
clean_up_passwords resource | |
respond_with resource | |
end | |
end | |
# ... | |
protected | |
# ... | |
# By default we want to require a password checks on update. | |
# You can overwrite this method in your own RegistrationsController. | |
def update_resource(resource, params) | |
resource.update_with_password(params) | |
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
.main | |
.container | |
.row | |
.span4.offset4 | |
h2 | |
| パスワード変更 | |
= resource_name.to_s.humanize | |
= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| | |
= devise_error_messages! | |
- if current_user.encrypted_password.present? | |
div | |
= f.label :current_password, '現在のパスワード' | |
br | |
= f.password_field :current_password | |
div | |
= f.label :password, '新しいパスワード' | |
br | |
= f.password_field :password, :autocomplete => 'on' | |
div | |
= f.label :password_confirmation, '新しいパスワードをもう一度' | |
br | |
= f.password_field :password_confirmation | |
div | |
= f.submit '更新する' |
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
class RegistrationsController < Devise::RegistrationsController | |
before_filter :configure_permitted_parameters, if: :devise_controller? | |
#override | |
def update_resource(resource, params) | |
resource.update_with_password_if_password_present(params) | |
end | |
def configure_permitted_parameters | |
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :pen_name, :password, :password_confirmation, :current_password) } | |
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
class User < ActiveRecord::Base | |
# 省略 | |
devise :database_authenticatable, :registerable, | |
:recoverable, :rememberable, :trackable, :validatable | |
# 省略 | |
def update_with_password_if_password_present(params) | |
if encrypted_password.present? | |
return update_with_password(params) | |
else | |
result = update_attributes(params) | |
clean_up_passwords | |
return result | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment