| Total Complexity | 52 |
| Total Lines | 360 |
| Duplicated Lines | 1.67 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like AdminsController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # frozen_string_literal: true |
||
| 19 | class AdminsController < ApplicationController |
||
| 20 | include Pagy::Backend |
||
| 21 | include Themer |
||
| 22 | include Emailer |
||
| 23 | include Recorder |
||
| 24 | include Rolify |
||
| 25 | include Populator |
||
| 26 | |||
| 27 | manage_users = [:edit_user, :promote, :demote, :ban_user, :unban_user, :approve, :reset, :merge_user] |
||
| 28 | manage_deleted_users = [:undelete] |
||
| 29 | authorize_resource class: false |
||
| 30 | before_action :find_user, only: manage_users |
||
| 31 | before_action :find_deleted_user, only: manage_deleted_users |
||
| 32 | before_action :verify_admin_of_user, only: [manage_users, manage_deleted_users] |
||
| 33 | |||
| 34 | # GET /admins |
||
| 35 | def index |
||
| 36 | # Initializa the data manipulation variables |
||
| 37 | @search = params[:search] || "" |
||
| 38 | @order_column = params[:column] && params[:direction] != "none" ? params[:column] : "created_at" |
||
| 39 | @order_direction = params[:direction] && params[:direction] != "none" ? params[:direction] : "DESC" |
||
| 40 | @tab = params[:tab] || "active" |
||
| 41 | @role = params[:role] ? Role.find_by(name: params[:role], provider: @user_domain) : nil |
||
| 42 | |||
| 43 | users = if @tab == "invited" |
||
| 44 | invited_users_list |
||
| 45 | else |
||
| 46 | manage_users_list |
||
| 47 | end |
||
| 48 | |||
| 49 | @pagy, @users = pagy(users) |
||
| 50 | end |
||
| 51 | |||
| 52 | # GET /admins/site_settings |
||
| 53 | def site_settings |
||
| 54 | @tab = params[:tab] || "appearance" |
||
| 55 | end |
||
| 56 | |||
| 57 | # GET /admins/server_recordings |
||
| 58 | def server_recordings |
||
| 59 | @search = params[:search] || "" |
||
| 60 | |||
| 61 | if @search.present? |
||
| 62 | if @search.include? "@" |
||
| 63 | user_email = @search |
||
| 64 | else |
||
| 65 | room_uid = @search |
||
| 66 | end |
||
| 67 | else |
||
| 68 | @latest = true |
||
| 69 | end |
||
| 70 | |||
| 71 | @pagy, @recordings = pagy_array(recordings_to_show(user_email, room_uid)) |
||
| 72 | end |
||
| 73 | |||
| 74 | # GET /admins/rooms |
||
| 75 | def server_rooms |
||
| 76 | @search = params[:search] || "" |
||
| 77 | @order_column = params[:column] && params[:direction] != "none" ? params[:column] : "status" |
||
| 78 | @order_direction = params[:direction] && params[:direction] != "none" ? params[:direction] : "DESC" |
||
| 79 | |||
| 80 | begin |
||
| 81 | meetings = all_running_meetings[:meetings] |
||
| 82 | rescue BigBlueButton::BigBlueButtonException |
||
| 83 | flash[:alert] = I18n.t("administrator.rooms.timeout", server: I18n.t("bigbluebutton")) |
||
| 84 | meetings = [] |
||
| 85 | end |
||
| 86 | |||
| 87 | @order_column = "created_at" if meetings.empty? |
||
| 88 | @running_room_bbb_ids = meetings.pluck(:meetingID) |
||
| 89 | |||
| 90 | @participants_count = {} |
||
| 91 | meetings.each do |meet| |
||
| 92 | @participants_count[meet[:meetingID]] = meet[:participantCount] |
||
| 93 | end |
||
| 94 | |||
| 95 | @pagy, @rooms = pagy_array(server_rooms_list) |
||
| 96 | end |
||
| 97 | |||
| 98 | # GET /admins/room_configuration |
||
| 99 | def room_configuration |
||
| 100 | end |
||
| 101 | |||
| 102 | # MANAGE USERS |
||
| 103 | |||
| 104 | # GET /admins/edit/:user_uid |
||
| 105 | def edit_user |
||
| 106 | session[:prev_url] = request.referer if request.referer.present? |
||
| 107 | end |
||
| 108 | |||
| 109 | # POST /admins/ban/:user_uid |
||
| 110 | def ban_user |
||
| 111 | @user.set_role :denied |
||
| 112 | |||
| 113 | redirect_back fallback_location: admins_path, flash: { success: I18n.t("administrator.flash.banned") } |
||
| 114 | end |
||
| 115 | |||
| 116 | # POST /admins/unban/:user_uid |
||
| 117 | def unban_user |
||
| 118 | @user.set_role :user |
||
| 119 | |||
| 120 | redirect_back fallback_location: admins_path, flash: { success: I18n.t("administrator.flash.unbanned") } |
||
| 121 | end |
||
| 122 | |||
| 123 | # POST /admins/approve/:user_uid |
||
| 124 | def approve |
||
| 125 | @user.set_role :user |
||
| 126 | |||
| 127 | send_user_approved_email(@user) |
||
| 128 | |||
| 129 | redirect_back fallback_location: admins_path, flash: { success: I18n.t("administrator.flash.approved") } |
||
| 130 | end |
||
| 131 | |||
| 132 | # POST /admins/approve/:user_uid |
||
| 133 | def undelete |
||
| 134 | # Undelete the user and all of his rooms |
||
| 135 | @user.undelete! |
||
| 136 | @user.rooms.deleted.each(&:undelete!) |
||
| 137 | |||
| 138 | redirect_back fallback_location: admins_path, flash: { success: I18n.t("administrator.flash.restored") } |
||
| 139 | end |
||
| 140 | |||
| 141 | # POST /admins/invite |
||
| 142 | def invite |
||
| 143 | emails = params[:invite_user][:email].split(",") |
||
| 144 | |||
| 145 | emails.each do |email| |
||
| 146 | invitation = create_or_update_invite(email) |
||
| 147 | |||
| 148 | send_invitation_email(current_user.name, email, invitation.invite_token) |
||
| 149 | end |
||
| 150 | |||
| 151 | redirect_back fallback_location: admins_path |
||
| 152 | end |
||
| 153 | |||
| 154 | # GET /admins/reset |
||
| 155 | def reset |
||
| 156 | send_password_reset_email(@user, @user.create_reset_digest) |
||
| 157 | |||
| 158 | View Code Duplication | if session[:prev_url].present? |
|
|
|
|||
| 159 | redirect_path = session[:prev_url] |
||
| 160 | session.delete(:prev_url) |
||
| 161 | else |
||
| 162 | redirect_path = admins_path |
||
| 163 | end |
||
| 164 | |||
| 165 | redirect_to redirect_path, flash: { success: I18n.t("administrator.flash.reset_password") } |
||
| 166 | end |
||
| 167 | |||
| 168 | # POST /admins/merge/:user_uid |
||
| 169 | def merge_user |
||
| 170 | begin |
||
| 171 | # Get uid of user that will be merged into the other account |
||
| 172 | uid_to_merge = params[:merge] |
||
| 173 | logger.info "#{current_user.uid} is attempting to merge #{uid_to_merge} into #{@user.uid}" |
||
| 174 | |||
| 175 | # Check to make sure the 2 users are unique |
||
| 176 | raise "Can not merge the user into themself" if uid_to_merge == @user.uid |
||
| 177 | |||
| 178 | # Find user to merge |
||
| 179 | user_to_merge = User.find_by(uid: uid_to_merge) |
||
| 180 | |||
| 181 | # Move over user's rooms |
||
| 182 | user_to_merge.rooms.each do |room| |
||
| 183 | room.owner = @user |
||
| 184 | |||
| 185 | room.name = "(#{I18n.t('merged')}) #{room.name}" |
||
| 186 | |||
| 187 | room.save! |
||
| 188 | end |
||
| 189 | |||
| 190 | # Reload user to update merge rooms |
||
| 191 | user_to_merge.reload |
||
| 192 | |||
| 193 | # Delete merged user |
||
| 194 | user_to_merge.destroy(true) |
||
| 195 | rescue => e |
||
| 196 | logger.info "Failed to merge #{uid_to_merge} into #{@user.uid}: #{e}" |
||
| 197 | flash[:alert] = I18n.t("administrator.flash.merge_fail") |
||
| 198 | else |
||
| 199 | logger.info "#{current_user.uid} successfully merged #{uid_to_merge} into #{@user.uid}" |
||
| 200 | flash[:success] = I18n.t("administrator.flash.merge_success") |
||
| 201 | end |
||
| 202 | |||
| 203 | redirect_back fallback_location: admins_path |
||
| 204 | end |
||
| 205 | |||
| 206 | # GET /admins/merge_list |
||
| 207 | def merge_list |
||
| 208 | # Returns a list of users that can merged into another user |
||
| 209 | initial_list = User.without_role(:super_admin) |
||
| 210 | .where.not(uid: current_user.uid) |
||
| 211 | .merge_list_search(params[:search]) |
||
| 212 | |||
| 213 | initial_list = initial_list.where(provider: @user_domain) if Rails.configuration.loadbalanced_configuration |
||
| 214 | |||
| 215 | # Respond with JSON object of users |
||
| 216 | respond_to do |format| |
||
| 217 | format.json { render body: initial_list.pluck_to_hash(:uid, :name, :email).to_json } |
||
| 218 | end |
||
| 219 | end |
||
| 220 | |||
| 221 | # SITE SETTINGS |
||
| 222 | |||
| 223 | # POST /admins/update_settings |
||
| 224 | def update_settings |
||
| 225 | tab = params[:tab] || "settings" |
||
| 226 | @settings.update_value(params[:setting], params[:value]) |
||
| 227 | |||
| 228 | flash_message = I18n.t("administrator.flash.settings") |
||
| 229 | |||
| 230 | if params[:value] == "Default Recording Visibility" |
||
| 231 | flash_message += ". " + I18n.t("administrator.site_settings.recording_visibility.warning") |
||
| 232 | end |
||
| 233 | |||
| 234 | redirect_to admin_site_settings_path(tab: tab), flash: { success: flash_message } |
||
| 235 | end |
||
| 236 | |||
| 237 | # POST /admins/color |
||
| 238 | def coloring |
||
| 239 | @settings.update_value("Primary Color", params[:value]) |
||
| 240 | @settings.update_value("Primary Color Lighten", color_lighten(params[:value])) |
||
| 241 | @settings.update_value("Primary Color Darken", color_darken(params[:value])) |
||
| 242 | redirect_to admin_site_settings_path(tab: "appearance"), flash: { success: I18n.t("administrator.flash.settings") } |
||
| 243 | end |
||
| 244 | |||
| 245 | # POST /admins/registration_method/:method |
||
| 246 | def registration_method |
||
| 247 | new_method = Rails.configuration.registration_methods[params[:value].to_sym] |
||
| 248 | |||
| 249 | # Only allow change to Join by Invitation if user has emails enabled |
||
| 250 | if !Rails.configuration.enable_email_verification && new_method == Rails.configuration.registration_methods[:invite] |
||
| 251 | redirect_to admin_site_settings_path(tab: "settings"), |
||
| 252 | flash: { alert: I18n.t("administrator.flash.invite_email_verification") } |
||
| 253 | else |
||
| 254 | @settings.update_value("Registration Method", new_method) |
||
| 255 | redirect_to admin_site_settings_path(tab: "settings"), |
||
| 256 | flash: { success: I18n.t("administrator.flash.registration_method_updated") } |
||
| 257 | end |
||
| 258 | end |
||
| 259 | |||
| 260 | # POST /admins/clear_auth |
||
| 261 | def clear_auth |
||
| 262 | User.include_deleted.where(provider: @user_domain).update_all(social_uid: nil) |
||
| 263 | |||
| 264 | redirect_to admin_site_settings_path(tab: "settings"), flash: { success: I18n.t("administrator.flash.settings") } |
||
| 265 | end |
||
| 266 | |||
| 267 | # POST /admins/clear_cache |
||
| 268 | def clear_cache |
||
| 269 | Rails.cache.delete("#{@user_domain}/getUser") |
||
| 270 | Rails.cache.delete("#{@user_domain}/getUserGreenlightCredentials") |
||
| 271 | |||
| 272 | redirect_to admin_site_settings_path(tab: "settings"), flash: { success: I18n.t("administrator.flash.settings") } |
||
| 273 | end |
||
| 274 | |||
| 275 | # POST /admins/log_level |
||
| 276 | def log_level |
||
| 277 | Rails.logger.level = params[:value].to_i |
||
| 278 | |||
| 279 | redirect_to admin_site_settings_path(tab: "administration"), flash: { success: I18n.t("administrator.flash.settings") } |
||
| 280 | end |
||
| 281 | |||
| 282 | # ROOM CONFIGURATION |
||
| 283 | # POST /admins/update_room_configuration |
||
| 284 | def update_room_configuration |
||
| 285 | @settings.update_value(params[:setting], params[:value]) |
||
| 286 | |||
| 287 | flash_message = I18n.t("administrator.flash.room_configuration") |
||
| 288 | |||
| 289 | redirect_to admin_room_configuration_path, flash: { success: flash_message } |
||
| 290 | end |
||
| 291 | |||
| 292 | # ROLES |
||
| 293 | |||
| 294 | # GET /admins/roles |
||
| 295 | def roles |
||
| 296 | @roles = all_roles(params[:selected_role]) |
||
| 297 | end |
||
| 298 | |||
| 299 | # POST /admins/role |
||
| 300 | # This method creates a new role scoped to the users provider |
||
| 301 | def new_role |
||
| 302 | new_role = create_role(params[:role][:name]) |
||
| 303 | |||
| 304 | return redirect_to admin_roles_path, flash: { alert: I18n.t("administrator.roles.invalid_create") } if new_role.nil? |
||
| 305 | |||
| 306 | redirect_to admin_roles_path(selected_role: new_role.id) |
||
| 307 | end |
||
| 308 | |||
| 309 | # PATCH /admin/roles/order |
||
| 310 | # This updates the priority of a site's roles |
||
| 311 | # Note: A lower priority role will always get used before a higher priority one |
||
| 312 | def change_role_order |
||
| 313 | unless update_priority(params[:role]) |
||
| 314 | redirect_to admin_roles_path, flash: { alert: I18n.t("administrator.roles.invalid_order") } |
||
| 315 | end |
||
| 316 | end |
||
| 317 | |||
| 318 | # POST /admin/role/:role_id |
||
| 319 | # This method updates the permissions assigned to a role |
||
| 320 | def update_role |
||
| 321 | role = Role.find(params[:role_id]) |
||
| 322 | flash[:alert] = I18n.t("administrator.roles.invalid_update") unless update_permissions(role) |
||
| 323 | redirect_to admin_roles_path(selected_role: role.id) |
||
| 324 | end |
||
| 325 | |||
| 326 | # DELETE admins/role/:role_id |
||
| 327 | # This deletes a role |
||
| 328 | def delete_role |
||
| 329 | role = Role.find(params[:role_id]) |
||
| 330 | |||
| 331 | # Make sure no users are assigned to the role and the role isn't a reserved role |
||
| 332 | # before deleting |
||
| 333 | if role.users.count.positive? |
||
| 334 | flash[:alert] = I18n.t("administrator.roles.role_has_users", user_count: role.users.count) |
||
| 335 | return redirect_to admin_roles_path(selected_role: role.id) |
||
| 336 | elsif Role::RESERVED_ROLE_NAMES.include?(role) || role.provider != @user_domain || |
||
| 337 | role.priority <= current_user.role.priority |
||
| 338 | return redirect_to admin_roles_path(selected_role: role.id) |
||
| 339 | else |
||
| 340 | role.role_permissions.delete_all |
||
| 341 | role.delete |
||
| 342 | end |
||
| 343 | |||
| 344 | redirect_to admin_roles_path |
||
| 345 | end |
||
| 346 | |||
| 347 | private |
||
| 348 | |||
| 349 | def find_user |
||
| 350 | @user = User.find_by(uid: params[:user_uid]) |
||
| 351 | end |
||
| 352 | |||
| 353 | def find_deleted_user |
||
| 354 | @user = User.deleted.find_by(uid: params[:user_uid]) |
||
| 355 | end |
||
| 356 | |||
| 357 | # Verifies that admin is an administrator of the user in the action |
||
| 358 | def verify_admin_of_user |
||
| 359 | redirect_to admins_path, |
||
| 360 | flash: { alert: I18n.t("administrator.flash.unauthorized") } unless current_user.admin_of?(@user, "can_manage_users") |
||
| 361 | end |
||
| 362 | |||
| 363 | # Creates the invite if it doesn't exist, or updates the updated_at time if it does |
||
| 364 | def create_or_update_invite(email) |
||
| 365 | invite = Invitation.find_by(email: email, provider: @user_domain) |
||
| 366 | |||
| 367 | # Invite already exists |
||
| 368 | if invite.present? |
||
| 369 | # Updates updated_at to now |
||
| 370 | invite.touch |
||
| 371 | else |
||
| 372 | # Creates invite |
||
| 373 | invite = Invitation.create(email: email, provider: @user_domain) |
||
| 374 | end |
||
| 375 | |||
| 376 | invite |
||
| 377 | end |
||
| 378 | end |
||
| 379 |