| Total Complexity | 50 |
| Total Lines | 335 |
| Duplicated Lines | 2.39 % |
| 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 | |||
| 25 | manage_users = [:edit_user, :promote, :demote, :ban_user, :unban_user, :approve] |
||
| 26 | site_settings = [:branding, :coloring, :coloring_lighten, :coloring_darken, |
||
| 27 | :registration_method, :room_authentication, :room_limit, :default_recording_visibility] |
||
| 28 | |||
| 29 | authorize_resource class: false |
||
| 30 | before_action :find_user, only: manage_users |
||
| 31 | before_action :verify_admin_of_user, only: manage_users |
||
| 32 | before_action :find_setting, only: site_settings |
||
| 33 | |||
| 34 | # GET /admins |
||
| 35 | def index |
||
| 36 | @search = params[:search] || "" |
||
| 37 | @order_column = params[:column] && params[:direction] != "none" ? params[:column] : "created_at" |
||
| 38 | @order_direction = params[:direction] && params[:direction] != "none" ? params[:direction] : "DESC" |
||
| 39 | @role = nil |
||
| 40 | |||
| 41 | @role = Role.find_by(name: params[:role], provider: @user_domain) if params[:role] |
||
| 42 | |||
| 43 | @pagy, @users = pagy(user_list) |
||
| 44 | end |
||
| 45 | |||
| 46 | # GET /admins/site_settings |
||
| 47 | def site_settings |
||
| 48 | end |
||
| 49 | |||
| 50 | # GET /admins/server_recordings |
||
| 51 | def server_recordings |
||
| 52 | server_rooms = if Rails.configuration.loadbalanced_configuration |
||
| 53 | Room.includes(:owner).where(users: { provider: user_settings_provider }).pluck(:bbb_id) |
||
| 54 | else |
||
| 55 | Room.pluck(:bbb_id) |
||
| 56 | end |
||
| 57 | |||
| 58 | @search, @order_column, @order_direction, recs = |
||
| 59 | all_recordings(server_rooms, @user_domain, params.permit(:search, :column, :direction), true, true) |
||
| 60 | @pagy, @recordings = pagy_array(recs) |
||
| 61 | end |
||
| 62 | |||
| 63 | # MANAGE USERS |
||
| 64 | |||
| 65 | # GET /admins/edit/:user_uid |
||
| 66 | def edit_user |
||
| 67 | end |
||
| 68 | |||
| 69 | # POST /admins/ban/:user_uid |
||
| 70 | def ban_user |
||
| 71 | @user.roles = [] |
||
| 72 | @user.add_role :denied |
||
| 73 | redirect_to admins_path, flash: { success: I18n.t("administrator.flash.banned") } |
||
| 74 | end |
||
| 75 | |||
| 76 | # POST /admins/unban/:user_uid |
||
| 77 | def unban_user |
||
| 78 | @user.remove_role :denied |
||
| 79 | @user.add_role :user |
||
| 80 | redirect_to admins_path, flash: { success: I18n.t("administrator.flash.unbanned") } |
||
| 81 | end |
||
| 82 | |||
| 83 | # POST /admins/approve/:user_uid |
||
| 84 | def approve |
||
| 85 | @user.remove_role :pending |
||
| 86 | @user.add_role :user |
||
| 87 | |||
| 88 | send_user_approved_email(@user) |
||
| 89 | |||
| 90 | redirect_to admins_path, flash: { success: I18n.t("administrator.flash.approved") } |
||
| 91 | end |
||
| 92 | |||
| 93 | # POST /admins/invite |
||
| 94 | def invite |
||
| 95 | email = params[:invite_user][:email] |
||
| 96 | |||
| 97 | begin |
||
| 98 | invitation = create_or_update_invite(email) |
||
| 99 | |||
| 100 | send_invitation_email(current_user.name, email, invitation.invite_token) |
||
| 101 | rescue => e |
||
| 102 | logger.error "Error in email delivery: #{e}" |
||
| 103 | flash[:alert] = I18n.t(params[:message], default: I18n.t("delivery_error")) |
||
| 104 | else |
||
| 105 | flash[:success] = I18n.t("administrator.flash.invite", email: email) |
||
| 106 | end |
||
| 107 | |||
| 108 | redirect_to admins_path |
||
| 109 | end |
||
| 110 | |||
| 111 | # SITE SETTINGS |
||
| 112 | |||
| 113 | # POST /admins/branding |
||
| 114 | def branding |
||
| 115 | @settings.update_value("Branding Image", params[:url]) |
||
| 116 | redirect_to admin_site_settings_path, flash: { success: I18n.t("administrator.flash.settings") } |
||
| 117 | end |
||
| 118 | |||
| 119 | # POST /admins/color |
||
| 120 | def coloring |
||
| 121 | @settings.update_value("Primary Color", params[:color]) |
||
| 122 | @settings.update_value("Primary Color Lighten", color_lighten(params[:color])) |
||
| 123 | @settings.update_value("Primary Color Darken", color_darken(params[:color])) |
||
| 124 | redirect_to admin_site_settings_path, flash: { success: I18n.t("administrator.flash.settings") } |
||
| 125 | end |
||
| 126 | |||
| 127 | View Code Duplication | def coloring_lighten |
|
|
|
|||
| 128 | @settings.update_value("Primary Color Lighten", params[:color]) |
||
| 129 | redirect_to admin_site_settings_path, flash: { success: I18n.t("administrator.flash.settings") } |
||
| 130 | end |
||
| 131 | |||
| 132 | View Code Duplication | def coloring_darken |
|
| 133 | @settings.update_value("Primary Color Darken", params[:color]) |
||
| 134 | redirect_to admin_site_settings_path, flash: { success: I18n.t("administrator.flash.settings") } |
||
| 135 | end |
||
| 136 | |||
| 137 | # POST /admins/room_authentication |
||
| 138 | def room_authentication |
||
| 139 | @settings.update_value("Room Authentication", params[:value]) |
||
| 140 | redirect_to admin_site_settings_path, flash: { success: I18n.t("administrator.flash.settings") } |
||
| 141 | end |
||
| 142 | |||
| 143 | # POST /admins/registration_method/:method |
||
| 144 | def registration_method |
||
| 145 | new_method = Rails.configuration.registration_methods[params[:method].to_sym] |
||
| 146 | |||
| 147 | # Only allow change to Join by Invitation if user has emails enabled |
||
| 148 | if !Rails.configuration.enable_email_verification && new_method == Rails.configuration.registration_methods[:invite] |
||
| 149 | redirect_to admin_site_settings_path, |
||
| 150 | flash: { alert: I18n.t("administrator.flash.invite_email_verification") } |
||
| 151 | else |
||
| 152 | @settings.update_value("Registration Method", new_method) |
||
| 153 | redirect_to admin_site_settings_path, |
||
| 154 | flash: { success: I18n.t("administrator.flash.registration_method_updated") } |
||
| 155 | end |
||
| 156 | end |
||
| 157 | |||
| 158 | # POST /admins/room_limit |
||
| 159 | def room_limit |
||
| 160 | @settings.update_value("Room Limit", params[:limit]) |
||
| 161 | redirect_to admin_site_settings_path, flash: { success: I18n.t("administrator.flash.settings") } |
||
| 162 | end |
||
| 163 | |||
| 164 | # POST /admins/default_recording_visibility |
||
| 165 | def default_recording_visibility |
||
| 166 | @settings.update_value("Default Recording Visibility", params[:visibility]) |
||
| 167 | redirect_to admins_path, flash: { success: I18n.t("administrator.flash.settings") + ". " + |
||
| 168 | I18n.t("administrator.site_settings.recording_visibility.warning") } |
||
| 169 | end |
||
| 170 | |||
| 171 | # ROLES |
||
| 172 | |||
| 173 | # GET /admins/roles |
||
| 174 | def roles |
||
| 175 | @roles = Role.editable_roles(@user_domain) |
||
| 176 | |||
| 177 | if @roles.count.zero? |
||
| 178 | Role.create_default_roles(@user_domain) |
||
| 179 | @roles = Role.editable_roles(@user_domain) |
||
| 180 | end |
||
| 181 | |||
| 182 | @selected_role = if params[:selected_role].nil? |
||
| 183 | @roles.find_by(name: 'user') |
||
| 184 | else |
||
| 185 | @roles.find(params[:selected_role]) |
||
| 186 | end |
||
| 187 | end |
||
| 188 | |||
| 189 | # POST /admin/role |
||
| 190 | def new_role |
||
| 191 | new_role_name = params[:role][:name] |
||
| 192 | |||
| 193 | if Role.duplicate_name(new_role_name, @user_domain) |
||
| 194 | flash[:alert] = I18n.t("administrator.roles.duplicate_name") |
||
| 195 | |||
| 196 | return redirect_to admin_roles_path |
||
| 197 | end |
||
| 198 | |||
| 199 | if new_role_name.strip.empty? |
||
| 200 | flash[:alert] = I18n.t("administrator.roles.empty_name") |
||
| 201 | |||
| 202 | return redirect_to admin_roles_path |
||
| 203 | end |
||
| 204 | |||
| 205 | new_role = Role.create(name: new_role_name, provider: @user_domain) |
||
| 206 | user_role = Role.find_by(name: 'user', provider: @user_domain) |
||
| 207 | |||
| 208 | new_role.priority = user_role.priority |
||
| 209 | user_role.priority += 1 |
||
| 210 | |||
| 211 | new_role.save! |
||
| 212 | user_role.save! |
||
| 213 | |||
| 214 | redirect_to admin_roles_path(selected_role: new_role.id) |
||
| 215 | end |
||
| 216 | |||
| 217 | # PATCH /admin/roles/order |
||
| 218 | def change_role_order |
||
| 219 | user_role = Role.find_by(name: "user", provider: @user_domain) |
||
| 220 | admin_role = Role.find_by(name: "admin", provider: @user_domain) |
||
| 221 | |||
| 222 | current_user_role = current_user.highest_priority_role |
||
| 223 | |||
| 224 | if params[:role].include?(user_role.id.to_s) || params[:role].include?(admin_role.id.to_s) |
||
| 225 | flash[:alert] = I18n.t("administrator.roles.invalid_order") |
||
| 226 | |||
| 227 | return redirect_to admin_roles_path |
||
| 228 | end |
||
| 229 | |||
| 230 | params[:role].each do |id| |
||
| 231 | role = Role.find(id) |
||
| 232 | if role.priority <= current_user_role.priority || role.provider != @user_domain |
||
| 233 | flash[:alert] = I18n.t("administrator.roles.invalid_update") |
||
| 234 | return redirect_to admin_roles_path |
||
| 235 | end |
||
| 236 | end |
||
| 237 | |||
| 238 | top_priority = 0 |
||
| 239 | |||
| 240 | params[:role].each_with_index do |id, index| |
||
| 241 | new_priority = index + [current_user_role.priority, 0].max + 1 |
||
| 242 | top_priority = new_priority |
||
| 243 | Role.where(id: id).update_all(priority: new_priority) |
||
| 244 | end |
||
| 245 | |||
| 246 | user_role.priority = top_priority + 1 |
||
| 247 | user_role.save! |
||
| 248 | end |
||
| 249 | |||
| 250 | # POST /admin/role/:role_id |
||
| 251 | def update_role |
||
| 252 | role = Role.find(params[:role_id]) |
||
| 253 | current_user_role = current_user.highest_priority_role |
||
| 254 | |||
| 255 | if role.priority <= current_user_role.priority || role.provider != @user_domain |
||
| 256 | flash[:alert] = I18n.t("administrator.roles.invalid_update") |
||
| 257 | return redirect_to admin_roles_path(selected_role: role.id) |
||
| 258 | end |
||
| 259 | |||
| 260 | role_params = params.require(:role).permit(:name) |
||
| 261 | permission_params = params.require(:role) |
||
| 262 | .permit( |
||
| 263 | :can_create_rooms, |
||
| 264 | :send_promoted_email, |
||
| 265 | :send_demoted_email, |
||
| 266 | :can_edit_site_settings, |
||
| 267 | :can_edit_roles, |
||
| 268 | :can_manage_users, |
||
| 269 | :colour |
||
| 270 | ) |
||
| 271 | |||
| 272 | if role.name != role_params[:name] && !Role.duplicate_name(role_params[:name], @user_domain) && |
||
| 273 | !role_params[:name].strip.empty? |
||
| 274 | role.name = role_params[:name] |
||
| 275 | elsif role.name != role_params[:name] |
||
| 276 | flash[:alert] = I18n.t("administrator.roles.duplicate_name") |
||
| 277 | |||
| 278 | return redirect_to admin_roles_path(selected_role: role.id) |
||
| 279 | end |
||
| 280 | |||
| 281 | role.update(permission_params) |
||
| 282 | |||
| 283 | role.save! |
||
| 284 | |||
| 285 | redirect_to admin_roles_path(selected_role: role.id) |
||
| 286 | end |
||
| 287 | |||
| 288 | # DELETE admins/role/:role_id |
||
| 289 | def delete_role |
||
| 290 | role = Role.find(params[:role_id]) |
||
| 291 | |||
| 292 | if role.users.count.positive? |
||
| 293 | flash[:alert] = I18n.t("administrator.roles.role_has_users", user_count: role.users.count) |
||
| 294 | return redirect_to admin_roles_path(selected_role: role.id) |
||
| 295 | elsif Role::RESERVED_ROLE_NAMES.include?(role) || role.provider != @user_domain || |
||
| 296 | role.priority <= current_user.highest_priority_role.priority |
||
| 297 | return redirect_to admin_roles_path(selected_role: role.id) |
||
| 298 | else |
||
| 299 | role.delete |
||
| 300 | end |
||
| 301 | |||
| 302 | redirect_to admin_roles_path |
||
| 303 | end |
||
| 304 | |||
| 305 | private |
||
| 306 | |||
| 307 | def find_user |
||
| 308 | @user = User.where(uid: params[:user_uid]).includes(:roles).first |
||
| 309 | end |
||
| 310 | |||
| 311 | def find_setting |
||
| 312 | @settings = Setting.find_or_create_by!(provider: user_settings_provider) |
||
| 313 | end |
||
| 314 | |||
| 315 | def verify_admin_of_user |
||
| 316 | redirect_to admins_path, |
||
| 317 | flash: { alert: I18n.t("administrator.flash.unauthorized") } unless current_user.admin_of?(@user) |
||
| 318 | end |
||
| 319 | |||
| 320 | # Gets the list of users based on your configuration |
||
| 321 | def user_list |
||
| 322 | initial_list = if current_user.has_role? :super_admin |
||
| 323 | User.where.not(id: current_user.id) |
||
| 324 | else |
||
| 325 | User.without_role(:super_admin).where.not(id: current_user.id) |
||
| 326 | end |
||
| 327 | |||
| 328 | if Rails.configuration.loadbalanced_configuration |
||
| 329 | initial_list.where(provider: user_settings_provider) |
||
| 330 | .admins_search(@search, @role) |
||
| 331 | .admins_order(@order_column, @order_direction) |
||
| 332 | else |
||
| 333 | initial_list.admins_search(@search, @role) |
||
| 334 | .admins_order(@order_column, @order_direction) |
||
| 335 | end |
||
| 336 | end |
||
| 337 | |||
| 338 | # Creates the invite if it doesn't exist, or updates the updated_at time if it does |
||
| 339 | def create_or_update_invite(email) |
||
| 340 | invite = Invitation.find_by(email: email, provider: @user_domain) |
||
| 341 | |||
| 342 | # Invite already exists |
||
| 343 | if invite.present? |
||
| 344 | # Updates updated_at to now |
||
| 345 | invite.touch |
||
| 346 | else |
||
| 347 | # Creates invite |
||
| 348 | invite = Invitation.create(email: email, provider: @user_domain) |
||
| 349 | end |
||
| 350 | |||
| 351 | invite |
||
| 352 | end |
||
| 353 | end |
||
| 354 |