forked from External/greenlight
GRN2-196: Fixed issues that scrutinizer is complaining about (#765)
* Refactored code to improve scrutinizer score * Bug fixes
This commit is contained in:
67
app/models/concerns/auth_values.rb
Normal file
67
app/models/concerns/auth_values.rb
Normal file
@ -0,0 +1,67 @@
|
||||
# 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 AuthValues
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
# Provider attributes.
|
||||
def auth_name(auth)
|
||||
case auth['provider']
|
||||
when :office365
|
||||
auth['info']['display_name']
|
||||
else
|
||||
auth['info']['name']
|
||||
end
|
||||
end
|
||||
|
||||
def auth_username(auth)
|
||||
case auth['provider']
|
||||
when :google
|
||||
auth['info']['email'].split('@').first
|
||||
when :bn_launcher
|
||||
auth['info']['username']
|
||||
else
|
||||
auth['info']['nickname']
|
||||
end
|
||||
end
|
||||
|
||||
def auth_email(auth)
|
||||
auth['info']['email']
|
||||
end
|
||||
|
||||
def auth_image(auth)
|
||||
case auth['provider']
|
||||
when :twitter
|
||||
auth['info']['image'].gsub("http", "https").gsub("_normal", "")
|
||||
else
|
||||
auth['info']['image']
|
||||
end
|
||||
end
|
||||
|
||||
def auth_roles(user, auth)
|
||||
unless auth['info']['roles'].nil?
|
||||
roles = auth['info']['roles'].split(',')
|
||||
|
||||
role_provider = auth['provider'] == "bn_launcher" ? auth['info']['customer'] : "greenlight"
|
||||
roles.each do |role_name|
|
||||
role = Role.where(provider: role_provider, name: role_name).first
|
||||
user.roles << role unless role.nil?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -20,8 +20,7 @@ require 'bbb_api'
|
||||
|
||||
class User < ApplicationRecord
|
||||
attr_accessor :reset_token
|
||||
after_create :assign_default_role
|
||||
after_create :initialize_main_room
|
||||
after_create :setup_user
|
||||
|
||||
before_save { email.try(:downcase!) }
|
||||
|
||||
@ -49,6 +48,8 @@ class User < ApplicationRecord
|
||||
has_secure_password(validations: false)
|
||||
|
||||
class << self
|
||||
include AuthValues
|
||||
|
||||
# Generates a user from omniauth.
|
||||
def from_omniauth(auth)
|
||||
# Provider is the customer name if in loadbalanced config mode
|
||||
@ -63,54 +64,6 @@ class User < ApplicationRecord
|
||||
u.save!
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Provider attributes.
|
||||
def auth_name(auth)
|
||||
case auth['provider']
|
||||
when :office365
|
||||
auth['info']['display_name']
|
||||
else
|
||||
auth['info']['name']
|
||||
end
|
||||
end
|
||||
|
||||
def auth_username(auth)
|
||||
case auth['provider']
|
||||
when :google
|
||||
auth['info']['email'].split('@').first
|
||||
when :bn_launcher
|
||||
auth['info']['username']
|
||||
else
|
||||
auth['info']['nickname']
|
||||
end
|
||||
end
|
||||
|
||||
def auth_email(auth)
|
||||
auth['info']['email']
|
||||
end
|
||||
|
||||
def auth_image(auth)
|
||||
case auth['provider']
|
||||
when :twitter
|
||||
auth['info']['image'].gsub("http", "https").gsub("_normal", "")
|
||||
else
|
||||
auth['info']['image']
|
||||
end
|
||||
end
|
||||
|
||||
def auth_roles(user, auth)
|
||||
unless auth['info']['roles'].nil?
|
||||
roles = auth['info']['roles'].split(',')
|
||||
|
||||
role_provider = auth['provider'] == "bn_launcher" ? auth['info']['customer'] : "greenlight"
|
||||
roles.each do |role_name|
|
||||
role = Role.where(provider: role_provider, name: role_name).first
|
||||
user.roles << role unless role.nil?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.admins_search(string, role)
|
||||
@ -149,21 +102,17 @@ class User < ApplicationRecord
|
||||
|
||||
# Activates an account and initialize a users main room
|
||||
def activate
|
||||
update_attribute(:email_verified, true)
|
||||
update_attribute(:activated_at, Time.zone.now)
|
||||
save
|
||||
update_attributes(email_verified: true, activated_at: Time.zone.now)
|
||||
end
|
||||
|
||||
def activated?
|
||||
return true unless Rails.configuration.enable_email_verification
|
||||
email_verified
|
||||
Rails.configuration.enable_email_verification ? email_verified : true
|
||||
end
|
||||
|
||||
# Sets the password reset attributes.
|
||||
def create_reset_digest
|
||||
self.reset_token = User.new_token
|
||||
update_attribute(:reset_digest, User.digest(reset_token))
|
||||
update_attribute(:reset_sent_at, Time.zone.now)
|
||||
update_attributes(reset_digest: User.digest(reset_token), reset_sent_at: Time.zone.now)
|
||||
end
|
||||
|
||||
# Returns true if the given token matches the digest.
|
||||
@ -288,8 +237,7 @@ class User < ApplicationRecord
|
||||
|
||||
def create_reset_activation_digest(token)
|
||||
# Create the digest and persist it.
|
||||
self.activation_digest = User.digest(token)
|
||||
save
|
||||
update_attribute(:activation_digest, User.digest(token))
|
||||
token
|
||||
end
|
||||
|
||||
@ -298,19 +246,17 @@ class User < ApplicationRecord
|
||||
rooms.destroy_all
|
||||
end
|
||||
|
||||
# Initializes a room for the user and assign a BigBlueButton user id.
|
||||
def initialize_main_room
|
||||
self.uid = "gl-#{(0...12).map { rand(65..90).chr }.join.downcase}"
|
||||
self.main_room = Room.create!(owner: self, name: I18n.t("home_room"))
|
||||
save
|
||||
end
|
||||
def setup_user
|
||||
# Initializes a room for the user and assign a BigBlueButton user id.
|
||||
id = "gl-#{(0...12).map { rand(65..90).chr }.join.downcase}"
|
||||
room = Room.create!(owner: self, name: I18n.t("home_room"))
|
||||
|
||||
# Initialize the user to use the default user role
|
||||
def assign_default_role
|
||||
update_attributes(uid: id, main_room: room)
|
||||
|
||||
# Initialize the user to use the default user role
|
||||
role_provider = Rails.configuration.loadbalanced_configuration ? provider : "greenlight"
|
||||
|
||||
Role.create_default_roles(role_provider) if Role.where(provider: role_provider).count.zero?
|
||||
|
||||
add_role(:user) if roles.blank?
|
||||
end
|
||||
|
||||
|
Reference in New Issue
Block a user