Class: NewsmastMastodon::Api::V1::CustomPasswordsController
- Inherits:
-
Api::BaseController
- Object
- Api::BaseController
- NewsmastMastodon::Api::V1::CustomPasswordsController
- Includes:
- AccountableConcern, Concerns::ApiResponseHelper, NonChannelHelper
- Defined in:
- app/controllers/newsmast_mastodon/api/v1/custom_passwords_controller.rb
Constant Summary collapse
- ACCESS_TOKEN_SCOPES =
'read write follow push profile'
Instance Method Summary collapse
- #bristol_cable_sign_in ⇒ Object
- #change_email ⇒ Object
- #change_password ⇒ Object
- #create ⇒ Object
- #request_otp ⇒ Object
- #update ⇒ Object
- #verify_otp ⇒ Object
Methods included from NonChannelHelper
Instance Method Details
#bristol_cable_sign_in ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'app/controllers/newsmast_mastodon/api/v1/custom_passwords_controller.rb', line 139 def bristol_cable_sign_in account = Account.where(username: params[:username]) if account.exists? return render_result({}, 'api.account.errors.username_taken', :unprocessable_entity) end account = account.first_or_initialize(username: params[:username]) account.save(validate: false) @user = User.where(email: params[:email]) if @user.exists? return render_result({}, 'api.account.errors.email_taken', :unprocessable_entity) end @user = @user.first_or_initialize(email: params[:email], password: params[:password], password_confirmation: params[:password], confirmed_at: Time.now.utc, role: UserRole.find_by(name: ''), account: account, agreement: true, approved: true) @user.save! @user.approve! render json: generate_access_token end |
#change_email ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'app/controllers/newsmast_mastodon/api/v1/custom_passwords_controller.rb', line 100 def change_email @user = current_user unless @user && verify_otp_params[:email].present? && password_params[:current_password].present? return render_result({}, 'api.account.errors.missing_field', :unprocessable_entity) end unless @user.valid_password?(password_params[:current_password]) return render_result({}, 'api.account.errors.password_incorrect', :unprocessable_entity) end new_email = verify_otp_params[:email] return render_result({}, 'api.account.errors.email_taken', :unprocessable_entity) if User.exists?(email: new_email) email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i return render_result({}, 'api.account.errors.email_invalid', :unprocessable_entity) unless new_email.match?(email_regex) @user.skip_confirmation! if new_email != @user.email @user.update!( unconfirmed_email: new_email, confirmation_sent_at: Time.current, otp_secret: generate_otp_token, confirmed_at: nil ) update_bot_email(new_email: new_email) unless is_non_channel? log_action :change_email, @user # Revoke all access tokens and destroy sessions @user.revoke_access! Devise.sign_out_all_scopes ? sign_out : sign_out(@user) CustomPasswordsMailer.with(user: @user).reset_password_confirmation.deliver_later end render_response(key: :message, data: generate_access_token, status: :ok) rescue ActiveSupport::MessageVerifier::InvalidSignature render_result({}, 'api.account.errors.email_update_fail', :unprocessable_entity) end |
#change_password ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'app/controllers/newsmast_mastodon/api/v1/custom_passwords_controller.rb', line 78 def change_password @user = current_user unless @user && password_params[:password].present? && password_params[:password_confirmation].present? && password_params[:current_password].present? && @user&.otp_secret.nil? return render_result({}, 'api.account.errors.missing_field', :unprocessable_entity) end unless @user.valid_password?(password_params[:current_password]) return render_result({}, 'api.account.errors.password_incorrect', :unprocessable_entity) end @user.password = password_params[:password] @user.skip_password_change_notification = true @user.save(validate: false) render_updated({}, 'api.account.messages.password_updated') rescue ActiveSupport::MessageVerifier::InvalidSignature render_result({}, 'api.account.errors.password_update_fail', :unprocessable_entity) end |
#create ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'app/controllers/newsmast_mastodon/api/v1/custom_passwords_controller.rb', line 16 def create user = User.find_by(email: verify_otp_params[:email]) if user raw, enc = Devise.token_generator.generate(User, :reset_password_token) user.reset_password_token = enc user.reset_password_sent_at = Time.now.utc user.otp_secret = generate_otp_token user.save! CustomPasswordsMailer.with(user: user).reset_password_confirmation.deliver_later render_response(key: :reset_password_token, data: user.reload.reset_password_token, status: :ok) else render_not_found end end |
#request_otp ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'app/controllers/newsmast_mastodon/api/v1/custom_passwords_controller.rb', line 48 def request_otp if @user @user.otp_secret = generate_otp_token @user.save! CustomPasswordsMailer.with(user: @user).reset_password_confirmation.deliver_later render_response(key: :access_token, data: verify_otp_params[:id], status: :ok) else render_not_found('api.account.errors.email_not_found') end end |
#update ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'app/controllers/newsmast_mastodon/api/v1/custom_passwords_controller.rb', line 31 def update unless @user && password_params[:password].present? && password_params[:password_confirmation].present? && @user&.otp_secret.nil? return render_result({}, 'api.account.errors.missing_field', :unprocessable_entity) end unless password_params[:password].eql?(password_params[:password_confirmation]) return render_result({}, 'api.account.errors.password_unmatch', :unprocessable_entity) end @user.password = password_params[:password] @user.skip_password_change_notification = true @user.save(validate: false) render_updated({}, 'api.account.messages.password_updated') rescue ActiveSupport::MessageVerifier::InvalidSignature render_result({}, 'api.account.errors.password_update_fail', :unprocessable_entity) end |
#verify_otp ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'app/controllers/newsmast_mastodon/api/v1/custom_passwords_controller.rb', line 59 def verify_otp unless @user && verify_otp?(verify_otp_params[:otp_secret], reset_password: reset_password?) return render_result({}, 'api.account.errors.otp_invalid', :unprocessable_entity) end waitlist_entry = is_non_channel? ? nil : find_waitlist_entry @can_register = registration_allowed?(waitlist_entry) return render_result({}, 'api.account.errors.register_not_allow', :unprocessable_entity) unless @can_register ActiveRecord::Base.transaction do handle_user_confirmation(waitlist_entry) handle_email_change if change_email? end render_response(key: :message, data: generate_access_token, status: :ok) rescue ActiveRecord::RecordInvalid => e render_result({}, e., :unprocessable_entity) end |