Completed
Branch master (ce2c9c)
by Jesus
04:32
created

UsersController.send_registration_email()   A

Complexity

Conditions 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
dl 0
loc 12
rs 9.8
c 0
b 0
f 0
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 UsersController < ApplicationController
20
  include RecordingsHelper
21
  include Pagy::Backend
22
  include Emailer
23
  include Registrar
24
25
  before_action :find_user, only: [:edit, :update, :destroy]
26
  before_action :ensure_unauthenticated, only: [:new, :create]
27
28
  # POST /u
29
  def create
30
    # Verify that GreenLight is configured to allow user signup.
31
    return unless Rails.configuration.allow_user_signup
32
33
    @user = User.new(user_params)
34
    @user.provider = @user_domain
35
36
    # User or recpatcha is not valid
37
    render(:new) && return unless valid_user_or_captcha
38
39
    # Redirect to root if user token is either invalid or expired
40 View Code Duplication
    return redirect_to root_path, flash: { alert: I18n.t("registration.invite.fail") } unless passes_invite_reqs
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
41
42
    # User has passed all validations required
43
    @user.save
44
45
    # Set user to pending and redirect if Approval Registration is set
46 View Code Duplication
    if approval_registration
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
47
      @user.add_role :pending
48
49
      return redirect_to root_path,
50
        flash: { success: I18n.t("registration.approval.signup") } unless Rails.configuration.enable_email_verification
51
    end
52
53
    send_registration_email if Rails.configuration.enable_email_verification
54
55
    # Sign in automatically if email verification is disabled or if user is already verified.
56
    login(@user) && return if !Rails.configuration.enable_email_verification || @user.email_verified
57
58
    send_verification
59
60
    redirect_to root_path
61
  end
62
63
  # GET /signin
64
  def signin
65
  end
66
67
  # GET /signup
68
  def new
69
    return redirect_to root_path unless Rails.configuration.allow_user_signup
70
71
    # Check if the user needs to be invited
72
    if invite_registration
73
      redirect_to root_path, flash: { alert: I18n.t("registration.invite.no_invite") } unless params[:invite_token]
74
75
      session[:invite_token] = params[:invite_token]
76
    end
77
78
    @user = User.new
79
  end
80
81
  # GET /u/:user_uid/edit
82
  def edit
83
    if current_user
84
      redirect_to current_user.main_room if @user != current_user && !current_user.admin_of?(@user)
85
    else
86
      redirect_to root_path
87
    end
88
  end
89
90
  # PATCH /u/:user_uid/edit
91
  def update
92
    if params[:setting] == "password"
93
      # Update the users password.
94
      errors = {}
95
96
      if @user.authenticate(user_params[:password])
97
        # Verify that the new passwords match.
98
        if user_params[:new_password] == user_params[:password_confirmation]
99
          @user.password = user_params[:new_password]
100
        else
101
          # New passwords don't match.
102
          errors[:password_confirmation] = "doesn't match"
103
        end
104
      else
105
        # Original password is incorrect, can't update.
106
        errors[:password] = "is incorrect"
107
      end
108
109
      if errors.empty? && @user.save
110
        # Notify the user that their account has been updated.
111
        flash[:success] = I18n.t("info_update_success")
112
        redirect_to edit_user_path(@user)
113
      else
114
        # Append custom errors.
115
        errors.each { |k, v| @user.errors.add(k, v) }
116
        render :edit, params: { settings: params[:settings] }
117
      end
118
    elsif user_params[:email] != @user.email && @user.update_attributes(user_params)
119
      @user.update_attributes(email_verified: false)
120
      flash[:success] = I18n.t("info_update_success")
121
      redirect_to edit_user_path(@user)
122
    elsif @user.update_attributes(user_params)
123
      update_locale(@user)
124
      flash[:success] = I18n.t("info_update_success")
125
      redirect_to edit_user_path(@user)
126
    else
127
      render :edit, params: { settings: params[:settings] }
128
    end
129
  end
130
131
  # DELETE /u/:user_uid
132
  def destroy
133
    if current_user && current_user == @user
134
      @user.destroy
135
      session.delete(:user_id)
136
    elsif current_user.admin_of?(@user)
137
      begin
138
        @user.destroy
139
      rescue => e
140
        logger.error "Error in user deletion: #{e}"
141
        flash[:alert] = I18n.t(params[:message], default: I18n.t("administrator.flash.delete_fail"))
142
      else
143
        flash[:success] = I18n.t("administrator.flash.delete")
144
      end
145
      redirect_to(admins_path) && return
146
    end
147
    redirect_to root_path
148
  end
149
150
  # GET /u/:user_uid/recordings
151
  def recordings
152
    if current_user && current_user.uid == params[:user_uid]
153
      @search, @order_column, @order_direction, recs =
154
        current_user.all_recordings(params.permit(:search, :column, :direction), true)
155
      @pagy, @recordings = pagy_array(recs)
156
    else
157
      redirect_to root_path
158
    end
159
  end
160
161
  # GET | POST /terms
162
  def terms
163
    redirect_to '/404' unless Rails.configuration.terms
164
165
    if params[:accept] == "true"
166
      current_user.update_attributes(accepted_terms: true)
167
      login(current_user)
168
    end
169
  end
170
171
  private
172
173
  def find_user
174
    @user = User.find_by!(uid: params[:user_uid])
175
  end
176
177
  def ensure_unauthenticated
178
    redirect_to current_user.main_room if current_user
179
  end
180
181
  def user_params
182
    params.require(:user).permit(:name, :email, :image, :password, :password_confirmation,
183
      :new_password, :provider, :accepted_terms, :language)
184
  end
185
186
  def send_verification
187
    # Start email verification and redirect to root.
188
    begin
189
      send_activation_email(@user)
190
    rescue => e
191
      logger.error "Error in email delivery: #{e}"
192
      flash[:alert] = I18n.t(params[:message], default: I18n.t("delivery_error"))
193
    else
194
      flash[:success] = I18n.t("email_sent", email_type: t("verify.verification"))
195
    end
196
  end
197
198
  def send_registration_email
199
    begin
200
      if invite_registration
201
        send_invite_user_signup_email(@user)
202
      elsif approval_registration
203
        send_approval_user_signup_email(@user)
204
      end
205
    rescue => e
206
      logger.error "Error in email delivery: #{e}"
207
      flash[:alert] = I18n.t(params[:message], default: I18n.t("delivery_error"))
208
    end
209
  end
210
211
  # Add validation errors to model if they exist
212
  def valid_user_or_captcha
213
    valid_user = @user.valid?
214
    valid_captcha = Rails.configuration.recaptcha_enabled ? verify_recaptcha(model: @user) : true
215
216
    valid_user && valid_captcha
217
  end
218
219
  # Checks if the user passes the requirements to be invited
220
  def passes_invite_reqs
221
    # check if user needs to be invited and IS invited
222
    invitation = check_user_invited(@user.email, session[:invite_token], @user_domain)
223
224
    @user.email_verified = true if invitation[:verified]
225
226
    invitation[:present]
227
  end
228
end
229