Passed
Push — master ( 33ca92...23b088 )
by Ahmad
10:28
created

UsersController.update_password()   B

Complexity

Conditions 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
dl 0
loc 22
rs 8.8853
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, :update_password]
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.set_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
    if !Rails.configuration.enable_email_verification || @user.email_verified
60
      @user.set_role :user
61
62
      login(@user) && return
63
    end
64
65
    send_activation_email(@user, @user.create_activation_token)
66
67
    redirect_to root_path
68
  end
69
70
  # GET /u/:user_uid/edit
71
  def edit
72
    redirect_to root_path unless current_user
73
  end
74
75
  # GET /u/:user_uid/change_password
76
  def change_password
77
    redirect_to edit_user_path unless current_user.greenlight_account?
78
  end
79
80
  # GET /u/:user_uid/delete_account
81
  def delete_account
82
  end
83
84
  # POST /u/:user_uid/edit
85
  def update
86 View Code Duplication
    if session[:prev_url].present?
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
87
      path = session[:prev_url]
88
      session.delete(:prev_url)
89
    else
90
      path = admins_path
91
    end
92
93
    redirect_path = current_user.admin_of?(@user, "can_manage_users") ? path : edit_user_path(@user)
94
95
    unless @user.greenlight_account?
96
      params[:user][:name] = @user.name
97
      params[:user][:email] = @user.email
98
    end
99
100
    if @user.update_attributes(user_params)
101
      @user.update_attributes(email_verified: false) if user_params[:email] != @user.email
102
103
      user_locale(@user)
104
105
      if update_roles(params[:user][:role_id])
106
        return redirect_to redirect_path, flash: { success: I18n.t("info_update_success") }
107
      else
108
        flash[:alert] = I18n.t("administrator.roles.invalid_assignment")
109
      end
110
    end
111
112
    render :edit
113
  end
114
115
  # POST /u/:user_uid/change_password
116
  def update_password
117
    # Update the users password.
118
    if @user.authenticate(user_params[:password])
119
      # Verify that the new passwords match.
120
      if user_params[:new_password] == user_params[:password_confirmation]
121
        @user.password = user_params[:new_password]
122
      else
123
        # New passwords don't match.
124
        @user.errors.add(:password_confirmation, "doesn't match")
125
      end
126
    else
127
      # Original password is incorrect, can't update.
128
      @user.errors.add(:password, "is incorrect")
129
    end
130
131
    # Notify the user that their account has been updated.
132
    return redirect_to change_password_path,
133
      flash: { success: I18n.t("info_update_success") } if @user.errors.empty? && @user.save
134
135
    # redirect_to change_password_path
136
    render :change_password
137
  end
138
139
  # DELETE /u/:user_uid
140
  def destroy
141
    # Include deleted users in the check
142
    admin_path = request.referer.present? ? request.referer : admins_path
143
    @user = User.include_deleted.find_by(uid: params[:user_uid])
144
145
    logger.info "Support: #{current_user.email} is deleting #{@user.email}."
146
147
    self_delete = current_user == @user
148
    redirect_url = self_delete ? root_path : admin_path
149
150
    begin
151
      if current_user && (self_delete || current_user.admin_of?(@user, "can_manage_users"))
152
        # Permanently delete if the user is deleting themself
153
        perm_delete = self_delete || (params[:permanent].present? && params[:permanent] == "true")
154
155
        # Permanently delete the rooms under the user if they have not been reassigned
156
        if perm_delete
157
          @user.rooms.include_deleted.each do |room|
158
            room.destroy(true)
159
          end
160
        end
161
162
        @user.destroy(perm_delete)
163
164
        # Log the user out if they are deleting themself
165
        session.delete(:user_id) if self_delete
166
167
        return redirect_to redirect_url, flash: { success: I18n.t("administrator.flash.delete") } unless self_delete
168
      else
169
        flash[:alert] = I18n.t("administrator.flash.delete_fail")
170
      end
171
    rescue => e
172
      logger.error "Support: Error in user deletion: #{e}"
173
      flash[:alert] = I18n.t(params[:message], default: I18n.t("administrator.flash.delete_fail"))
174
    end
175
176
    redirect_to redirect_url
177
  end
178
179
  # GET /u/:user_uid/recordings
180
  def recordings
181
    if current_user && current_user.uid == params[:user_uid]
182
      @search, @order_column, @order_direction, recs =
183
        all_recordings(current_user.rooms.pluck(:bbb_id), params.permit(:search, :column, :direction), true)
184
      @pagy, @recordings = pagy_array(recs)
185
    else
186
      redirect_to root_path
187
    end
188
  end
189
190
  # GET | POST /terms
191
  def terms
192
    redirect_to '/404' unless Rails.configuration.terms
193
194
    if params[:accept] == "true"
195
      current_user.update_attributes(accepted_terms: true)
196
      login(current_user)
197
    end
198
  end
199
200
  private
201
202
  def find_user
203
    @user = User.find_by(uid: params[:user_uid])
204
  end
205
206
  # Verify that GreenLight is configured to allow user signup.
207
  def check_user_signup_allowed
208
    redirect_to root_path unless Rails.configuration.allow_user_signup
209
  end
210
211
  def user_params
212
    params.require(:user).permit(:name, :email, :image, :password, :password_confirmation,
213
      :new_password, :provider, :accepted_terms, :language)
214
  end
215
216
  def send_registration_email
217
    if invite_registration
218
      send_invite_user_signup_email(@user)
219
    elsif approval_registration
220
      send_approval_user_signup_email(@user)
221
    end
222
  end
223
224
  # Checks that the user is allowed to edit this user
225
  def check_admin_of
226
    redirect_to root_path if current_user &&
227
                             @user != current_user &&
228
                             !current_user.admin_of?(@user, "can_manage_users")
229
  end
230
end
231