GRN2-196: Fixed issues that scrutinizer is complaining about (#765)

* Refactored code to improve scrutinizer score

* Bug fixes
This commit is contained in:
farhatahmad
2019-08-27 11:08:58 -04:00
committed by farhatahmad
parent fd6077696d
commit 01b8dbbd0e
33 changed files with 462 additions and 434 deletions

View File

@ -18,16 +18,9 @@
class ApplicationController < ActionController::Base
include BbbServer
include ThemingHelper
before_action :redirect_to_https
before_action :set_user_domain
before_action :set_user_settings
before_action :maintenance_mode?
before_action :migration_error?
before_action :user_locale
before_action :check_admin_password
before_action :check_user_role
before_action :redirect_to_https, :set_user_domain, :set_user_settings, :maintenance_mode?, :migration_error?,
:user_locale, :check_admin_password, :check_user_role
# Manually handle BigBlueButton errors
rescue_from BigBlueButton::BigBlueButtonException, with: :handle_bigbluebutton_error
@ -77,6 +70,23 @@ class ApplicationController < ActionController::Base
@settings = Setting.find_or_create_by(provider: @user_domain)
end
# Redirects the user to a Maintenance page if turned on
def maintenance_mode?
if ENV["MAINTENANCE_MODE"] == "true"
render "errors/greenlight_error", status: 503, formats: :html,
locals: {
status_code: 503,
message: I18n.t("errors.maintenance.message"),
help: I18n.t("errors.maintenance.help"),
}
end
if Rails.configuration.maintenance_window.present?
unless cookies[:maintenance_window] == Rails.configuration.maintenance_window
flash.now[:maintenance] = I18n.t("maintenance.window_alert", date: Rails.configuration.maintenance_window)
end
end
end
# Show an information page when migration fails and there is a version error.
def migration_error?
render :migration_error, status: 500 unless ENV["DB_MIGRATE_FAILED"].blank?
@ -113,23 +123,6 @@ class ApplicationController < ActionController::Base
end
end
# Redirects the user to a Maintenance page if turned on
def maintenance_mode?
if ENV["MAINTENANCE_MODE"] == "true"
render "errors/greenlight_error", status: 503, formats: :html,
locals: {
status_code: 503,
message: I18n.t("errors.maintenance.message"),
help: I18n.t("errors.maintenance.help"),
}
end
if Rails.configuration.maintenance_window.present?
unless cookies[:maintenance_window] == Rails.configuration.maintenance_window
flash.now[:maintenance] = I18n.t("maintenance.window_alert", date: Rails.configuration.maintenance_window)
end
end
end
# Relative root helper (when deploying to subdirectory).
def relative_root
Rails.configuration.relative_url_root || ""

View File

@ -56,6 +56,10 @@ module Authenticator
end
end
def ensure_unauthenticated_except_twitter
redirect_to current_user.main_room if current_user && params[:old_twitter_user_id].nil?
end
# Logs current user out of GreenLight.
def logout
session.delete(:user_id) if current_user

View File

@ -0,0 +1,94 @@
# frozen_string_literal: true
# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
#
# Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below).
#
# This program is free software; you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free Software
# Foundation; either version 3.0 of the License, or (at your option) any later
# version.
#
# BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
module Joiner
extend ActiveSupport::Concern
# Displays the join room page to the user
def show_user_join
# Get users name
@name = if current_user
current_user.name
elsif cookies.encrypted[:greenlight_name]
cookies.encrypted[:greenlight_name]
else
""
end
@search, @order_column, @order_direction, pub_recs =
public_recordings(@room.bbb_id, params.permit(:search, :column, :direction), true)
@pagy, @public_recordings = pagy_array(pub_recs)
render :join
end
# create or update cookie to track the three most recent rooms a user joined
def save_recent_rooms
if current_user
recently_joined_rooms = cookies.encrypted["#{current_user.uid}_recently_joined_rooms"].to_a
cookies.encrypted["#{current_user.uid}_recently_joined_rooms"] =
recently_joined_rooms.prepend(@room.id).uniq[0..2]
end
end
def join_room(opts)
room_settings = JSON.parse(@room[:room_settings])
if room_running?(@room.bbb_id) || @room.owned_by?(current_user) || room_settings["anyoneCanStart"]
# Determine if the user needs to join as a moderator.
opts[:user_is_moderator] = @room.owned_by?(current_user) || room_settings["joinModerator"]
opts[:require_moderator_approval] = room_settings["requireModeratorApproval"]
if current_user
redirect_to join_path(@room, current_user.name, opts, current_user.uid)
else
join_name = params[:join_name] || params[@room.invite_path][:join_name]
redirect_to join_path(@room, join_name, opts)
end
else
search_params = params[@room.invite_path] || params
@search, @order_column, @order_direction, pub_recs =
public_recordings(@room.bbb_id, search_params.permit(:search, :column, :direction), true)
@pagy, @public_recordings = pagy_array(pub_recs)
# They need to wait until the meeting begins.
render :wait
end
end
def incorrect_user_domain
Rails.configuration.loadbalanced_configuration && @room.owner.provider != @user_domain
end
# Default, unconfigured meeting options.
def default_meeting_options
invite_msg = I18n.t("invite_message")
{
user_is_moderator: false,
meeting_logout_url: request.base_url + logout_room_path(@room),
meeting_recorded: true,
moderator_message: "#{invite_msg}\n\n#{request.base_url + room_path(@room)}",
host: request.host,
recording_default_visibility: @settings.get_value("Default Recording Visibility") == "public"
}
end
end

View File

@ -43,4 +43,33 @@ module Registrar
{ present: false, verified: false }
end
end
# Checks if the user passes the requirements to be invited
def passes_invite_reqs
# check if user needs to be invited and IS invited
invitation = check_user_invited(@user.email, session[:invite_token], @user_domain)
@user.email_verified = true if invitation[:verified]
invitation[:present]
end
# Add validation errors to model if they exist
def valid_user_or_captcha
valid_user = @user.valid?
valid_captcha = Rails.configuration.recaptcha_enabled ? verify_recaptcha(model: @user) : true
logger.error("Support: #{@user.email} creation failed: User params are not valid.") unless valid_user
valid_user && valid_captcha
end
# Checks if the user trying to sign in with twitter account
def check_if_twitter_account(log_out = false)
unless params[:old_twitter_user_id].nil? && session[:old_twitter_user_id].nil?
logout if log_out
flash.now[:alert] = I18n.t("registration.deprecated.new_signin")
session[:old_twitter_user_id] = params[:old_twitter_user_id] unless params[:old_twitter_user_id].nil?
end
end
end

View File

@ -22,22 +22,20 @@ module Themer
# Lightens a color by 40%
def color_lighten(color)
# Uses the built in Sass Engine to lighten the color
dummy_scss = "h1 { color: $lighten; }"
compiled = SassC::Engine.new("$lighten:lighten(#{color}, 40%);" + dummy_scss, syntax: :scss).render
string_locater = 'color: '
color_start = compiled.index(string_locater) + string_locater.length
compiled[color_start..color_start + 6]
generate_sass("lighten", color, "40%")
end
# Darkens a color by 10%
def color_darken(color)
# Uses the built in Sass Engine to darken the color
generate_sass("darken", color, "10%")
end
dummy_scss = "h1 { color: $darken; }"
compiled = SassC::Engine.new("$darken:darken(#{color}, 10%);" + dummy_scss, syntax: :scss).render
private
def generate_sass(action, color, percentage)
dummy_scss = "h1 { color: $#{action}; }"
compiled = SassC::Engine.new("$#{action}:#{action}(#{color}, #{percentage});" + dummy_scss, syntax: :scss).render
string_locater = 'color: '
color_start = compiled.index(string_locater) + string_locater.length

View File

@ -39,7 +39,7 @@ class PasswordResetsController < ApplicationController
redirect_to root_path
rescue
# User doesn't exist
redirect_to new_password_reset_path, flash: { alert: I18n.t("no_user_email_exists") }
redirect_to root_path, flash: { success: I18n.t("email_sent", email_type: t("reset_password.subtitle")) }
end
end

View File

@ -34,6 +34,13 @@ class RecordingsController < ApplicationController
redirect_back fallback_location: root_path if res[:updated]
end
# PATCH /:meetingID/:record_id
def rename
update_recording(params[:record_id], "meta_name" => params[:record_name])
redirect_back fallback_location: room_path(@room)
end
# DELETE /:meetingID/:record_id
def delete
delete_recording(params[:record_id])

View File

@ -19,12 +19,13 @@
class RoomsController < ApplicationController
include Pagy::Backend
include Recorder
include Joiner
before_action :validate_accepted_terms, unless: -> { !Rails.configuration.terms }
before_action :validate_verified_email, except: [:show, :join],
unless: -> { !Rails.configuration.enable_email_verification }
before_action :find_room, except: [:create, :join_specific_room]
before_action :verify_room_ownership, except: [:create, :show, :join, :logout, :login, :join_specific_room]
before_action :verify_room_ownership, only: [:destroy, :start, :update_settings]
before_action :verify_room_owner_verified, only: [:show, :join],
unless: -> { !Rails.configuration.enable_email_verification }
before_action :verify_user_not_admin, only: [:show]
@ -42,19 +43,17 @@ class RoomsController < ApplicationController
@room.owner = current_user
@room.room_settings = create_room_settings_string(room_params)
# Save the room
if @room.save
logger.info "Support: #{current_user.email} has created a new room #{@room.uid}."
# Save the room and redirect if it fails
return redirect_to current_user.main_room, flash: { alert: I18n.t("room.create_room_error") } unless @room.save
# Start the room if auto join was turned on
if room_params[:auto_join] == "1"
start
else
redirect_to @room, flash: { success: I18n.t("room.create_room_success") }
end
else
redirect_to current_user.main_room, flash: { alert: I18n.t("room.create_room_error") }
end
logger.info "Support: #{current_user.email} has created a new room #{@room.uid}."
# Redirect to room is auto join was not turned on
return redirect_to @room,
flash: { success: I18n.t("room.create_room_success") } unless room_params[:auto_join] == "1"
# Start the room if auto join was turned on
start
end
# GET /:room_uid
@ -76,6 +75,7 @@ class RoomsController < ApplicationController
render :cant_create_rooms
end
else
<<<<<<< HEAD
return redirect_to root_path, flash: { alert: I18n.t("room.invalid_provider") } if incorrect_user_domain
# Get users name
@ -102,9 +102,10 @@ class RoomsController < ApplicationController
update_room_attributes("name")
elsif params[:setting] == "rename_recording"
update_recording(params[:record_id], "meta_name" => params[:record_name])
=======
show_user_join
>>>>>>> GRN2-196: Fixed issues that scrutinizer is complaining about (#765)
end
redirect_back fallback_location: room_path
end
# POST /:room_uid
@ -123,19 +124,14 @@ class RoomsController < ApplicationController
@join_name = params[@room.invite_path][:join_name]
elsif !params[:join_name]
# Join name not passed.
return
return redirect_to root_path
end
end
# create or update cookie with join name
cookies.encrypted[:greenlight_name] = @join_name unless cookies.encrypted[:greenlight_name] == @join_name
if current_user
# create or update cookie to track the three most recent rooms a user joined
recently_joined_rooms = cookies.encrypted["#{current_user.uid}_recently_joined_rooms"].to_a
cookies.encrypted["#{current_user.uid}_recently_joined_rooms"] =
recently_joined_rooms.prepend(@room.id).uniq[0..2]
end
save_recent_rooms
logger.info "Support: #{current_user.present? ? current_user.email : @join_name} is joining room #{@room.uid}"
join_room(default_meeting_options)
@ -195,20 +191,25 @@ class RoomsController < ApplicationController
def update_settings
begin
raise "Room name can't be blank" if room_params[:name].empty?
raise "Unauthorized Request" if !@room.owned_by?(current_user) || @room == current_user.main_room
@room = Room.find_by!(uid: params[:room_uid])
# Update the rooms settings
update_room_attributes("settings")
if room_params
room_settings_string = create_room_settings_string(room_params)
@room.update_attributes(room_settings: room_settings_string)
end
# Update the rooms name if it has been changed
update_room_attributes("name") if @room.name != room_params[:name]
@room.update_attributes(name: params[:room_name] || room_params[:name]) if @room.name != room_params[:name]
# Update the room's access code if it has changed
update_room_attributes("access_code") if @room.access_code != room_params[:access_code]
rescue => e
logger.error "Error in updating room settings: #{e}"
flash[:alert] = I18n.t("room.update_settings_error")
else
@room.update_attributes(access_code: room_params[:access_code]) if @room.access_code != room_params[:access_code]
flash[:success] = I18n.t("room.update_settings_success")
rescue => e
logger.error "Support: Error in updating room settings: #{e}"
flash[:alert] = I18n.t("room.update_settings_error")
end
redirect_to room_path
end
@ -231,19 +232,6 @@ class RoomsController < ApplicationController
private
def update_room_attributes(update_type)
if @room.owned_by?(current_user) && @room != current_user.main_room
if update_type.eql? "name"
@room.update_attributes(name: params[:room_name] || room_params[:name])
elsif update_type.eql? "settings"
room_settings_string = create_room_settings_string(room_params)
@room.update_attributes(room_settings: room_settings_string)
elsif update_type.eql? "access_code"
@room.update_attributes(access_code: room_params[:access_code])
end
end
end
def create_room_settings_string(options)
room_settings = {}
room_settings["muteOnStart"] = options[:mute_on_join] == "1"
@ -269,18 +257,7 @@ class RoomsController < ApplicationController
# Ensure the user is logged into the room they are accessing.
def verify_room_ownership
bring_to_room unless @room.owned_by?(current_user)
end
# Redirects a user to their room.
def bring_to_room
if current_user
# Redirect authenticated users to their room.
redirect_to room_path(current_user.main_room)
else
# Redirect unauthenticated users to root.
redirect_to root_path
end
return redirect_to root_path unless @room.owned_by?(current_user)
end
def validate_accepted_terms
@ -294,12 +271,7 @@ class RoomsController < ApplicationController
def verify_room_owner_verified
unless @room.owner.activated?
flash[:alert] = t("room.unavailable")
if current_user && !@room.owned_by?(current_user)
redirect_to current_user.main_room
else
redirect_to root_path
end
redirect_to root_path
end
end
@ -321,49 +293,4 @@ class RoomsController < ApplicationController
current_user.rooms.length >= limit
end
helper_method :room_limit_exceeded
def join_room(opts)
room_settings = JSON.parse(@room[:room_settings])
if room_running?(@room.bbb_id) || @room.owned_by?(current_user) || room_settings["anyoneCanStart"]
# Determine if the user needs to join as a moderator.
opts[:user_is_moderator] = @room.owned_by?(current_user) || room_settings["joinModerator"]
opts[:require_moderator_approval] = room_settings["requireModeratorApproval"]
if current_user
redirect_to join_path(@room, current_user.name, opts, current_user.uid)
else
join_name = params[:join_name] || params[@room.invite_path][:join_name]
redirect_to join_path(@room, join_name, opts)
end
else
search_params = params[@room.invite_path] || params
@search, @order_column, @order_direction, pub_recs =
public_recordings(@room.bbb_id, search_params.permit(:search, :column, :direction), true)
@pagy, @public_recordings = pagy_array(pub_recs)
# They need to wait until the meeting begins.
render :wait
end
end
def incorrect_user_domain
Rails.configuration.loadbalanced_configuration && @room.owner.provider != @user_domain
end
# Default, unconfigured meeting options.
def default_meeting_options
invite_msg = I18n.t("invite_message")
{
user_is_moderator: false,
meeting_logout_url: request.base_url + logout_room_path(@room),
meeting_recorded: true,
moderator_message: "#{invite_msg}\n\n#{request.base_url + room_path(@room)}",
host: request.host,
recording_default_visibility: @settings.get_value("Default Recording Visibility") == "public"
}
end
end

View File

@ -23,6 +23,42 @@ class SessionsController < ApplicationController
include LdapAuthenticator
skip_before_action :verify_authenticity_token, only: [:omniauth, :fail]
before_action :check_user_signup_allowed, only: [:new]
before_action :ensure_unauthenticated_except_twitter, only: [:new, :signin]
# GET /signin
def signin
check_if_twitter_account
providers = configured_providers
if one_provider
provider_path = if Rails.configuration.omniauth_ldap
ldap_signin_path
else
"#{Rails.configuration.relative_url_root}/auth/#{providers.first}"
end
return redirect_to provider_path
end
end
# GET /ldap_signin
def ldap_signin
end
# GET /signup
def new
# Check if the user needs to be invited
if invite_registration
redirect_to root_path, flash: { alert: I18n.t("registration.invite.no_invite") } unless params[:invite_token]
session[:invite_token] = params[:invite_token]
end
check_if_twitter_account(true)
@user = User.new
end
# POST /users/login
def create
@ -101,10 +137,20 @@ class SessionsController < ApplicationController
private
# Verify that GreenLight is configured to allow user signup.
def check_user_signup_allowed
redirect_to root_path unless Rails.configuration.allow_user_signup
end
def session_params
params.require(:session).permit(:email, :password)
end
def one_provider
(!allow_user_signup? || !allow_greenlight_accounts?) && providers.count == 1 &&
!Rails.configuration.loadbalanced_configuration
end
def check_user_exists
provider = @auth['provider'] == "bn_launcher" ? @auth['info']['customer'] : @auth['provider']
User.exists?(social_uid: @auth['uid'], provider: provider)

View File

@ -17,7 +17,8 @@
# with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
class ThemesController < ApplicationController
skip_before_action :maintenance_mode?
skip_before_action :redirect_to_https, :maintenance_mode?, :migration_error?, :user_locale,
:check_admin_password, :check_user_role
# GET /primary
def index

View File

@ -25,19 +25,17 @@ class UsersController < ApplicationController
include Rolify
before_action :find_user, only: [:edit, :change_password, :delete_account, :update, :destroy]
before_action :ensure_unauthenticated, only: [:new, :create, :signin]
before_action :ensure_unauthenticated_except_twitter, only: [:create]
before_action :check_user_signup_allowed, only: [:create]
before_action :check_admin_of, only: [:edit, :change_password, :delete_account]
# POST /u
def create
# Verify that GreenLight is configured to allow user signup.
return unless Rails.configuration.allow_user_signup
@user = User.new(user_params)
@user.provider = @user_domain
# User or recpatcha is not valid
render(:new) && return unless valid_user_or_captcha
render("sessions/new") && return unless valid_user_or_captcha
# Redirect to root if user token is either invalid or expired
return redirect_to root_path, flash: { alert: I18n.t("registration.invite.fail") } unless passes_invite_reqs
@ -65,50 +63,6 @@ class UsersController < ApplicationController
redirect_to root_path
end
# GET /signin
def signin
unless params[:old_twitter_user_id].nil? && session[:old_twitter_user_id].nil?
flash[:alert] = I18n.t("registration.deprecated.new_signin")
session[:old_twitter_user_id] = params[:old_twitter_user_id] unless params[:old_twitter_user_id].nil?
end
providers = configured_providers
if (!allow_user_signup? || !allow_greenlight_accounts?) && providers.count == 1 &&
!Rails.configuration.loadbalanced_configuration
provider_path = if Rails.configuration.omniauth_ldap
ldap_signin_path
else
"#{Rails.configuration.relative_url_root}/auth/#{providers.first}"
end
return redirect_to provider_path
end
end
# GET /ldap_signin
def ldap_signin
end
# GET /signup
def new
return redirect_to root_path unless Rails.configuration.allow_user_signup
# Check if the user needs to be invited
if invite_registration
redirect_to root_path, flash: { alert: I18n.t("registration.invite.no_invite") } unless params[:invite_token]
session[:invite_token] = params[:invite_token]
end
unless params[:old_twitter_user_id].nil? && session[:old_twitter_user_id].nil?
logout
flash.now[:alert] = I18n.t("registration.deprecated.new_signin")
session[:old_twitter_user_id] = params[:old_twitter_user_id] unless params[:old_twitter_user_id].nil?
end
@user = User.new
end
# GET /u/:user_uid/edit
def edit
redirect_to root_path unless current_user
@ -116,6 +70,7 @@ class UsersController < ApplicationController
# GET /u/:user_uid/change_password
def change_password
redirect_to edit_user_path unless current_user.greenlight_account?
end
# GET /u/:user_uid/delete_account
@ -124,11 +79,11 @@ class UsersController < ApplicationController
# PATCH /u/:user_uid/edit
def update
redirect_path = current_user.admin_of?(@user) ? admins_path : edit_user_path(@user)
profile = params[:setting] == "password" ? change_password_path(@user) : edit_user_path(@user)
redirect_path = current_user.admin_of?(@user) ? admins_path : profile
if params[:setting] == "password"
# Update the users password.
errors = {}
if @user.authenticate(user_params[:password])
# Verify that the new passwords match.
@ -136,21 +91,18 @@ class UsersController < ApplicationController
@user.password = user_params[:new_password]
else
# New passwords don't match.
errors[:password_confirmation] = "doesn't match"
@user.errors.add(:password_confirmation, "doesn't match")
end
else
# Original password is incorrect, can't update.
errors[:password] = "is incorrect"
@user.errors.add(:password, "is incorrect")
end
if errors.empty? && @user.save
# Notify the user that their account has been updated.
redirect_to redirect_path, flash: { success: I18n.t("info_update_success") }
else
# Append custom errors.
errors.each { |k, v| @user.errors.add(k, v) }
render :edit, params: { settings: params[:settings] }
end
# Notify the user that their account has been updated.
return redirect_to redirect_path,
flash: { success: I18n.t("info_update_success") } if @user.errors.empty? && @user.save
render :change_password
else
if @user.update_attributes(user_params)
@user.update_attributes(email_verified: false) if user_params[:email] != @user.email
@ -164,7 +116,7 @@ class UsersController < ApplicationController
end
end
render :edit, params: { settings: params[:settings] }
render :edit
end
end
@ -172,20 +124,19 @@ class UsersController < ApplicationController
def destroy
logger.info "Support: #{current_user.email} is deleting #{@user.email}."
if current_user && current_user == @user
@user.destroy
session.delete(:user_id)
elsif current_user.admin_of?(@user)
begin
self_delete = current_user == @user
begin
if current_user && (self_delete || current_user.admin_of?(@user))
@user.destroy
rescue => e
logger.error "Support: Error in user deletion: #{e}"
flash[:alert] = I18n.t(params[:message], default: I18n.t("administrator.flash.delete_fail"))
else
flash[:success] = I18n.t("administrator.flash.delete")
session.delete(:user_id) if self_delete
return redirect_to admins_path, flash: { success: I18n.t("administrator.flash.delete") } unless self_delete
end
redirect_to(admins_path) && return
rescue => e
logger.error "Support: Error in user deletion: #{e}"
flash[:alert] = I18n.t(params[:message], default: I18n.t("administrator.flash.delete_fail"))
end
redirect_to root_path
end
@ -216,8 +167,9 @@ class UsersController < ApplicationController
@user = User.where(uid: params[:user_uid]).includes(:roles).first
end
def ensure_unauthenticated
redirect_to current_user.main_room if current_user && params[:old_twitter_user_id].nil?
# Verify that GreenLight is configured to allow user signup.
def check_user_signup_allowed
redirect_to root_path unless Rails.configuration.allow_user_signup
end
def user_params
@ -233,26 +185,6 @@ class UsersController < ApplicationController
end
end
# Add validation errors to model if they exist
def valid_user_or_captcha
valid_user = @user.valid?
valid_captcha = Rails.configuration.recaptcha_enabled ? verify_recaptcha(model: @user) : true
logger.error("Support: #{@user.email} creation failed: User params are not valid.") unless valid_user
valid_user && valid_captcha
end
# Checks if the user passes the requirements to be invited
def passes_invite_reqs
# check if user needs to be invited and IS invited
invitation = check_user_invited(@user.email, session[:invite_token], @user_domain)
@user.email_verified = true if invitation[:verified]
invitation[:present]
end
# Checks that the user is allowed to edit this user
def check_admin_of
redirect_to current_user.main_room if current_user && @user != current_user && !current_user.admin_of?(@user)