UsersController.send_registration_email()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
dl 0
loc 7
rs 10
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 Pagy::Backend
21
  include Authenticator
22
  include Emailer
23
  include Registrar
24
  include Recorder
25
  include Rolify
26
27
  before_action :find_user, only: [:edit, :change_password, :delete_account, :update]
28
  before_action :ensure_unauthenticated_except_twitter, only: [:create]
29
  before_action :check_user_signup_allowed, only: [:create]
30
  before_action :check_admin_of, only: [:edit, :change_password, :delete_account]
31
32
  # POST /u
33
  def create
34
    @user = User.new(user_params)
35
    @user.provider = @user_domain
36
37
    # User or recpatcha is not valid
38
    render("sessions/new") && return unless valid_user_or_captcha
39
40
    # Redirect to root if user token is either invalid or expired
41 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...
42
43
    # User has passed all validations required
44
    @user.save
45
46
    logger.info "Support: #{@user.email} user has been created."
47
48
    # Set user to pending and redirect if Approval Registration is set
49
    if approval_registration
50
      @user.add_role :pending
51
52
      return redirect_to root_path,
53
        flash: { success: I18n.t("registration.approval.signup") } unless Rails.configuration.enable_email_verification
54
    end
55
56
    send_registration_email
57
58
    # Sign in automatically if email verification is disabled or if user is already verified.
59
    login(@user) && return if !Rails.configuration.enable_email_verification || @user.email_verified
60
61
    send_activation_email(@user)
62
63
    redirect_to root_path
64
  end
65
66
  # GET /u/:user_uid/edit
67
  def edit
68
    redirect_to root_path unless current_user
69
  end
70
71
  # GET /u/:user_uid/change_password
72
  def change_password
73
    redirect_to edit_user_path unless current_user.greenlight_account?
74
  end
75
76
  # GET /u/:user_uid/delete_account
77
  def delete_account
78
  end
79
80
  # PATCH /u/:user_uid/edit
81
  def update
82
    profile = params[:setting] == "password" ? change_password_path(@user) : edit_user_path(@user)
83
    redirect_path = current_user.admin_of?(@user) ? admins_path : profile
84
85
    if params[:setting] == "password"
86
      # Update the users password.
87
88
      if @user.authenticate(user_params[:password])
89
        # Verify that the new passwords match.
90
        if user_params[:new_password] == user_params[:password_confirmation]
91
          @user.password = user_params[:new_password]
92
        else
93
          # New passwords don't match.
94
          @user.errors.add(:password_confirmation, "doesn't match")
95
        end
96
      else
97
        # Original password is incorrect, can't update.
98
        @user.errors.add(:password, "is incorrect")
99
      end
100
101
      # Notify the user that their account has been updated.
102
      return redirect_to redirect_path,
103
        flash: { success: I18n.t("info_update_success") } if @user.errors.empty? && @user.save
104
105
      render :change_password
106
    else
107
      if @user.update_attributes(user_params)
108
        @user.update_attributes(email_verified: false) if user_params[:email] != @user.email
109
110
        user_locale(@user)
111
112
        if update_roles(params[:user][:role_ids])
113
          return redirect_to redirect_path, flash: { success: I18n.t("info_update_success") }
114
        else
115
          flash[:alert] = I18n.t("administrator.roles.invalid_assignment")
116
        end
117
      end
118
119
      render :edit
120
    end
121
  end
122
123
  # DELETE /u/:user_uid
124
  def destroy
125
    # Include deleted users in the check
126
    @user = User.include_deleted.find_by(uid: params[:user_uid])
127
128
    logger.info "Support: #{current_user.email} is deleting #{@user.email}."
129
130
    self_delete = current_user == @user
131
    redirect_url = self_delete ? root_path : admins_path
132
133
    begin
134
      if current_user && (self_delete || current_user.admin_of?(@user))
135
        # Permanently delete if the user is deleting themself
136
        perm_delete = self_delete || (params[:permanent].present? && params[:permanent] == "true")
137
138
        # Permanently delete the rooms under the user if they have not been reassigned
139
        if perm_delete
140
          @user.rooms.include_deleted.each do |room|
141
            room.destroy(true)
142
          end
143
        end
144
145
        @user.destroy(perm_delete)
146
147
        # Log the user out if they are deleting themself
148
        session.delete(:user_id) if self_delete
149
150
        return redirect_to redirect_url, flash: { success: I18n.t("administrator.flash.delete") } unless self_delete
151
      else
152
        flash[:alert] = I18n.t("administrator.flash.delete_fail")
153
      end
154
    rescue => e
155
      logger.error "Support: Error in user deletion: #{e}"
156
      flash[:alert] = I18n.t(params[:message], default: I18n.t("administrator.flash.delete_fail"))
157
    end
158
159
    redirect_to redirect_url
160
  end
161
162
  # GET /u/:user_uid/recordings
163
  def recordings
164
    if current_user && current_user.uid == params[:user_uid]
165
      @search, @order_column, @order_direction, recs =
166
        all_recordings(current_user.rooms.pluck(:bbb_id), params.permit(:search, :column, :direction), true)
167
      @pagy, @recordings = pagy_array(recs)
168
    else
169
      redirect_to root_path
170
    end
171
  end
172
173
  # GET | POST /terms
174
  def terms
175
    redirect_to '/404' unless Rails.configuration.terms
176
177
    if params[:accept] == "true"
178
      current_user.update_attributes(accepted_terms: true)
179
      login(current_user)
180
    end
181
  end
182
183
  private
184
185
  def find_user
186
    @user = User.where(uid: params[:user_uid]).includes(:roles).first
187
  end
188
189
  # Verify that GreenLight is configured to allow user signup.
190
  def check_user_signup_allowed
191
    redirect_to root_path unless Rails.configuration.allow_user_signup
192
  end
193
194
  def user_params
195
    params.require(:user).permit(:name, :email, :image, :password, :password_confirmation,
196
      :new_password, :provider, :accepted_terms, :language)
197
  end
198
199
  def send_registration_email
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
  end
206
207
  # Checks that the user is allowed to edit this user
208
  def check_admin_of
209
    redirect_to current_user.main_room if current_user && @user != current_user && !current_user.admin_of?(@user)
210
  end
211
end
212