Devise로 사용자를 "소프트 삭제"하는 방법
저는 현재 Rails 프로젝트에서 사용자 등록 / 인증을 위해 Devise를 사용하고 있습니다. 사용자가 자신의 계정을 취소하고 싶을 때 사용자 개체가 파괴되어 내 애플리케이션이 원하지 않는 상태가됩니다.
"소프트 삭제"를 구현하는 가장 쉬운 방법, 즉 개인 데이터 만 제거하고 사용자를 삭제 된 것으로 표시하는 방법은 무엇입니까? 여전히 모든 레코드 연결을 유지하고 싶습니다.
먼저 사용자를 위해 새로운 "삭제 된"열을 도입해야한다고 가정합니다. 하지만 사용자의 프로필보기에이 기본 코드가 붙어 있습니다.
<p>Unhappy? <%= link_to "Cancel my account", registration_path(resource_name), :confirm => "Are you sure?", :method => :delete %>.</p>
:delete
방법 은 어디에서 찾을 수 있습니까 ? 기본 Devise 메서드를 어떻게 덮어 쓰나요?
destroy
사용자 모델에서 메서드를 재정 의하여 update_attribute(:deleted_at, Time.current)
실제로 삭제하는 대신 간단히 수행 하도록 조언 할 수 있지만 표준 API와의이 편차는 향후 부담이 될 수 있으므로 컨트롤러를 수정하는 방법은 다음과 같습니다.
Devise에는 기본 컨트롤러가 많이 있습니다. 이를 사용자 정의하는 가장 좋은 방법은 해당 장치 컨트롤러를 상속하는 고유 한 컨트롤러를 만드는 것입니다. 이 경우 우리는 Devise::RegistrationsController
소스를 보면 쉽게 알아볼 수 있습니다. 따라서 새 컨트롤러를 만듭니다.
class RegistrationsController < Devise::RegistrationsController
end
이제 우리는 고안에서 제공하는 모든 로직을 완전히 상속하는 자체 컨트롤러가 있습니다. 다음 단계는 devise에게 기본 대신 사용하도록 지시하는 것입니다. 당신의 경로에는 devise_for
라인이 있습니다. 등록 컨트롤러를 포함하도록 변경해야합니다.
devise_for :users, :controllers => { :registrations => 'registrations' }
이상하게 보이지만 기본적으로 단순히 '등록'이 아닌 '개발 / 등록'이기 때문에 의미가 있습니다.
다음 단계는 destroy
등록 컨트롤러 의 작업 을 재정의하는 것 입니다. 사용할 때 registration_path(:user), :method => :delete
-그것이 링크되는 곳입니다. 에 destroy
등록 컨트롤러의 액션입니다.
현재 devise는 다음을 수행합니다.
def destroy
resource.destroy
set_flash_message :notice, :destroyed
sign_out_and_redirect(self.resource)
end
대신이 코드를 사용할 수 있습니다. 먼저 User
모델 에 새로운 방법을 추가해 보겠습니다 .
class User < ActiveRecord::Base
def soft_delete
# assuming you have deleted_at column added already
update_attribute(:deleted_at, Time.current)
end
end
# Use this for Devise 2.1.0 and newer versions
class RegistrationsController < Devise::RegistrationsController
def destroy
resource.soft_delete
Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)
set_flash_message :notice, :destroyed if is_navigational_format?
respond_with_navigational(resource){ redirect_to after_sign_out_path_for(resource_name) }
end
end
# Use this for older Devise versions
class RegistrationsController < Devise::RegistrationsController
def destroy
resource.soft_delete
set_flash_message :notice, :destroyed
sign_out_and_redirect(resource)
end
end
이제 모든 설정이 완료되었습니다. 범위를 사용하여 삭제 된 사용자를 필터링합니다.
hakunin의 답변에 추가 :
"소프트 삭제 된"사용자가 로그인하지 못하도록하려면 모델 에서 재정의 active_for_authentication?
합니다 User
.
def active_for_authentication?
super && !deleted_at
end
개체를 삭제하는 대신 deleted_at를 설정하는 사용자 모델에 acts_as_paranoid 를 사용할 수 있습니다 .
A complete tutorial can be found at Soft Delete a Devise User Account on the Devise wiki page.
Summary:
1. Add a "deleted_at" DATETIME column
2. Override users/registrations#destroy in your routes
3. Override users/registrations#destroy in the registrations controller
4. Update user model with a soft_delete & check if user is active on authentication
5. Add a custom delete message
참고URL : https://stackoverflow.com/questions/5140643/how-to-soft-delete-user-with-devise
'Program Tip' 카테고리의 다른 글
클래스없이 특정 요소를 얻기 위해 jQuery : not 및 hasClass ()에서 사용하는 방법 (0) | 2020.11.09 |
---|---|
jQuery Keypress 화살표 키 (0) | 2020.11.09 |
Highcharts 세로 막 대형 차트 내에서 각 범주의 색상을 어떻게 변경합니까? (0) | 2020.11.09 |
PagerAdapter 시작 위치 (0) | 2020.11.09 |
목록에서 숫자의 누적 합계를 찾는 방법은 무엇입니까? (0) | 2020.11.09 |