PasswordResetsController   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 75
rs 10
c 1
b 0
f 0
wmc 16

4 Methods

Rating   Name   Duplication   Size   Complexity  
A new() 0 2 1
A edit() 0 2 1
A create() 0 13 2
A update() 0 14 4
1
# frozen_string_literal: true
2
3
# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
4
#
5
# Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below).
6
#
7
# This program is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU Lesser General Public License as published by the Free Software
9
# Foundation; either version 3.0 of the License, or (at your option) any later
10
# version.
11
#
12
# BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14
# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
15
#
16
# You should have received a copy of the GNU Lesser General Public License along
17
# with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
18
19
class PasswordResetsController < ApplicationController
20
  include Emailer
21
22
  before_action :disable_password_reset, unless: -> { Rails.configuration.enable_email_verification }
23
  before_action :find_user, only: [:edit, :update]
24
  before_action :valid_user, only: [:edit, :update]
25
  before_action :check_expiration, only: [:edit, :update]
26
27
  # POST /password_resets/new
28
  def new
29
  end
30
31
  # POST /password_resets
32
  def create
33
    begin
34
      # Check if user exists and throw an error if he doesn't
35
      @user = User.find_by!(email: params[:password_reset][:email].downcase)
36
37
      @user.create_reset_digest
38
      send_password_reset_email(@user)
39
      redirect_to root_path
40
    rescue
41
      # User doesn't exist
42
      redirect_to root_path, flash: { success: I18n.t("email_sent", email_type: t("reset_password.subtitle")) }
43
    end
44
  end
45
46
  # GET /password_resets/:id/edit
47
  def edit
48
  end
49
50
  # PATCH /password_resets/:id
51
  def update
52
    # Check if password is valid
53
    if params[:user][:password].empty?
54
      flash.now[:alert] = I18n.t("password_empty_notice")
55
    elsif params[:user][:password] != params[:user][:password_confirmation]
56
      # Password does not match password confirmation
57
      flash.now[:alert] = I18n.t("password_different_notice")
58
    elsif @user.update_attributes(user_params)
59
      # Successfully reset password
60
      return redirect_to root_path, flash: { success: I18n.t("password_reset_success") }
61
    end
62
63
    render 'edit'
64
  end
65
66
  private
67
68
  def find_user
69
    @user = User.find_by(email: params[:email])
70
  end
71
72
  def user_params
73
    params.require(:user).permit(:password, :password_confirmation)
74
  end
75
76
  # Checks expiration of reset token.
77
  def check_expiration
78
    redirect_to new_password_reset_url, alert: I18n.t("expired_reset_token") if @user.password_reset_expired?
79
  end
80
81
  # Confirms a valid user.
82
  def valid_user
83
    unless @user.authenticated?(:reset, params[:id])
84
      @user&.activate unless @user&.activated?
85
      redirect_to root_url
86
    end
87
  end
88
89
  # Redirects to 404 if emails are not enabled
90
  def disable_password_reset
91
    redirect_to '/404'
92
  end
93
end
94