-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into stages/rc-2018-08-16
- Loading branch information
Showing
44 changed files
with
993 additions
and
181 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 |
---|---|---|
@@ -1,30 +1,20 @@ | ||
module AccountReset | ||
class CancelController < ApplicationController | ||
def cancel | ||
account_reset = AccountResetService.cancel_request(params[:token]) | ||
if account_reset | ||
handle_success(account_reset.user) | ||
else | ||
handle_failure | ||
end | ||
def create | ||
result = AccountReset::Cancel.new(params[:token]).call | ||
|
||
analytics.track_event(Analytics::ACCOUNT_RESET, result.to_h) | ||
|
||
handle_success if result.success? | ||
|
||
redirect_to root_url | ||
end | ||
|
||
private | ||
|
||
def handle_success(user) | ||
analytics.track_event(Analytics::ACCOUNT_RESET, | ||
event: :cancel, token_valid: true, user_id: user.uuid) | ||
def handle_success | ||
sign_out if current_user | ||
UserMailer.account_reset_cancel(user.email).deliver_later | ||
phone = user.phone | ||
SmsAccountResetCancellationNotifierJob.perform_now(phone: phone) if phone.present? | ||
flash[:success] = t('devise.two_factor_authentication.account_reset.successful_cancel') | ||
end | ||
|
||
def handle_failure | ||
return if params[:token].blank? | ||
analytics.track_event(Analytics::ACCOUNT_RESET, event: :cancel, token_valid: false) | ||
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
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,67 @@ | ||
module AccountReset | ||
class Cancel | ||
include ActiveModel::Model | ||
|
||
validates :token, presence: { message: I18n.t('errors.account_reset.cancel_token_missing') } | ||
validate :valid_token | ||
|
||
def initialize(token) | ||
@token = token | ||
end | ||
|
||
def call | ||
@success = valid? | ||
|
||
if success | ||
notify_user_via_email_of_account_reset_cancellation | ||
notify_user_via_phone_of_account_reset_cancellation if phone.present? | ||
update_account_reset_request | ||
end | ||
|
||
FormResponse.new(success: success, errors: errors.messages, extra: extra_analytics_attributes) | ||
end | ||
|
||
private | ||
|
||
attr_reader :success, :token | ||
|
||
def valid_token | ||
return if account_reset_request | ||
|
||
errors.add(:token, I18n.t('errors.account_reset.cancel_token_invalid')) if token | ||
end | ||
|
||
def notify_user_via_email_of_account_reset_cancellation | ||
UserMailer.account_reset_cancel(user.email).deliver_later | ||
end | ||
|
||
def notify_user_via_phone_of_account_reset_cancellation | ||
SmsAccountResetCancellationNotifierJob.perform_now(phone: phone) | ||
end | ||
|
||
def update_account_reset_request | ||
account_reset_request.update!(cancelled_at: Time.zone.now, | ||
request_token: nil, | ||
granted_token: nil) | ||
end | ||
|
||
def account_reset_request | ||
@account_reset_request ||= AccountResetRequest.find_by(request_token: token) | ||
end | ||
|
||
def user | ||
account_reset_request&.user || AnonymousUser.new | ||
end | ||
|
||
def phone | ||
user.phone | ||
end | ||
|
||
def extra_analytics_attributes | ||
{ | ||
event: 'cancel', | ||
user_id: user.uuid, | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module AccountResetHealthChecker | ||
module_function | ||
|
||
Summary = Struct.new(:healthy, :result) do | ||
def as_json(*args) | ||
to_h.as_json(*args) | ||
end | ||
|
||
alias_method :healthy?, :healthy | ||
end | ||
|
||
# @return [Summary] | ||
def check | ||
rec = find_request_not_serviced_within_26_hours | ||
Summary.new(rec.nil?, rec) | ||
end | ||
|
||
# @api private | ||
def find_request_not_serviced_within_26_hours | ||
records = AccountResetRequest.where( | ||
sql, tvalue: Time.zone.now - Figaro.env.account_reset_wait_period_days.to_i.days - 2.hours | ||
).order('requested_at ASC').limit(1) | ||
records.first | ||
end | ||
|
||
def sql | ||
<<~SQL | ||
cancelled_at IS NULL AND | ||
granted_at IS NULL AND | ||
requested_at < :tvalue AND | ||
request_token IS NOT NULL AND | ||
granted_token IS NULL | ||
SQL | ||
end | ||
end |
Oops, something went wrong.