GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 05614b...ee0a33 )
by Jesus
05:48
created

User   C

Complexity

Total Complexity 53

Size/Duplication

Total Lines 263
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 263
rs 6.96
wmc 53

10 Methods

Rating   Name   Duplication   Size   Complexity  
A new_token() 0 3 1
A check_if_email_can_be_blank() 0 9 5
A admins_order() 0 4 1
A admin_of? 0 15 3
A activate() 0 5 1
A digest() 0 4 2
A assign_default_role() 0 3 2
A authenticated? 0 5 2
A admins_search() 0 28 3
B from_omniauth() 0 29 8

How to fix   Complexity   

Complex Class

Complex classes like User 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
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
require 'bbb_api'
20
21
class User < ApplicationRecord
22
  rolify
23
  include ::BbbApi
24
25
  attr_accessor :reset_token
26
  after_create :assign_default_role
27
  after_create :initialize_main_room
28
29
  before_save { email.try(:downcase!) }
30
31
  before_destroy :destroy_rooms
32
33
  has_many :rooms
34
  belongs_to :main_room, class_name: 'Room', foreign_key: :room_id, required: false
35
36
  validates :name, length: { maximum: 256 }, presence: true
37
  validates :provider, presence: true
38
  validate :check_if_email_can_be_blank
39
  validates :email, length: { maximum: 256 }, allow_blank: true,
40
                    uniqueness: { case_sensitive: false, scope: :provider },
41
                    format: { with: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i }
42
43
  validates :password, length: { minimum: 6 }, confirmation: true, if: :greenlight_account?, on: :create
44
45
  # Bypass validation if omniauth
46
  validates :accepted_terms, acceptance: true,
47
                             unless: -> { !greenlight_account? || !Rails.configuration.terms }
48
49
  # We don't want to require password validations on all accounts.
50
  has_secure_password(validations: false)
51
52
  class << self
53
    # Generates a user from omniauth.
54
    def from_omniauth(auth)
55
      # Provider is the customer name if in loadbalanced config mode
56
      provider = auth['provider'] == "bn_launcher" ? auth['info']['customer'] : auth['provider']
57
      u = find_by(social_uid: auth['uid'], provider: provider)
58
59
      if ENV["MAINTENANCE_MODE"] == "readonly"
60
        raise ActiveRecord::ReadOnlyRecord if u.nil?
61
62
        return u
63
      end
64
65
      return User.create(
66
        name: auth_name(auth),
67
        username: auth_username(auth),
68
        email: auth_email(auth),
69
        social_uid: auth['uid'],
70
        provider: provider,
71
        image: auth_image(auth),
72
        email_verified: true
73
      ) if u.nil?
74
75
      u.name = auth_name(auth) unless u.name
76
      u.username = auth_username(auth) unless u.username
77
      u.email = auth_email(auth)
78
      u.image = auth_image(auth) unless u.image
79
      u.email_verified = true
80
      u.save!
81
      u
82
    end
83
84
    private
85
86
    # Provider attributes.
87
    def auth_name(auth)
88
      case auth['provider']
89
      when :office365
90
        auth['info']['display_name']
91
      else
92
        auth['info']['name']
93
      end
94
    end
95
96
    def auth_username(auth)
97
      case auth['provider']
98
      when :google
99
        auth['info']['email'].split('@').first
100
      when :bn_launcher
101
        auth['info']['username']
102
      else
103
        auth['info']['nickname']
104
      end
105
    end
106
107
    def auth_email(auth)
108
      auth['info']['email']
109
    end
110
111
    def auth_image(auth)
112
      case auth['provider']
113
      when :twitter
114
        auth['info']['image'].gsub("http", "https").gsub("_normal", "")
115
      else
116
        auth['info']['image']
117
      end
118
    end
119
  end
120
121
  def self.admins_search(string, role)
122
    active_database = Rails.configuration.database_configuration[Rails.env]["adapter"]
123
    # Postgres requires created_at to be cast to a string
124
    created_at_query = if active_database == "postgresql"
125
      "created_at::text"
126
    else
127
      "created_at"
128
    end
129
130
    search_query = ""
131
    role_search_param = ""
132
    if role.present?
133
      search_query = "(users.name LIKE :search OR email LIKE :search OR username LIKE :search" \
134
                    " OR users.#{created_at_query} LIKE :search OR provider LIKE :search)" \
135
                    " AND roles.name = :roles_search"
136
      role_search_param = role
137
    else
138
      search_query = "users.name LIKE :search OR email LIKE :search OR username LIKE :search" \
139
                    " OR users.#{created_at_query} LIKE :search OR provider LIKE :search" \
140
                    " OR roles.name LIKE :roles_search"
141
      role_search_param = "%#{string}%".downcase
142
    end
143
144
    search_param = "%#{string}%"
145
    joins("LEFT OUTER JOIN users_roles ON users_roles.user_id = users.id LEFT OUTER JOIN roles " \
146
      "ON roles.id = users_roles.role_id").distinct
147
      .where(search_query, search: search_param, roles_search: role_search_param)
148
  end
149
150
  def self.admins_order(column, direction)
151
    # Arel.sql to avoid sql injection
152
    order(Arel.sql("#{column} #{direction}"))
153
  end
154
155
  # Activates an account and initialize a users main room
156
  def activate
157
    update_attribute(:email_verified, true)
158
    update_attribute(:activated_at, Time.zone.now)
159
    save
160
  end
161
162
  def activated?
163
    return true unless Rails.configuration.enable_email_verification
164
    email_verified
165
  end
166
167
  # Sets the password reset attributes.
168
  def create_reset_digest
169
    self.reset_token = User.new_token
170
    update_attribute(:reset_digest,  User.digest(reset_token))
171
    update_attribute(:reset_sent_at, Time.zone.now)
172
  end
173
174
  # Returns true if the given token matches the digest.
175
  def authenticated?(attribute, token)
176
    digest = send("#{attribute}_digest")
177
    return false if digest.nil?
178
    BCrypt::Password.new(digest).is_password?(token)
179
  end
180
181
  # Return true if password reset link expires
182
  def password_reset_expired?
183
    reset_sent_at < 2.hours.ago
184
  end
185
186
  # Retrives a list of all a users rooms that are not the main room, sorted by last session date.
187
  def secondary_rooms
188
    secondary = (rooms - [main_room])
189
    no_session, session = secondary.partition { |r| r.last_session.nil? }
190
    sorted = session.sort_by(&:last_session)
191
    sorted + no_session
192
  end
193
194
  def name_chunk
195
    charset = ("a".."z").to_a - %w(b i l o s) + ("2".."9").to_a - %w(5 8)
196
    chunk = name.parameterize[0...3]
197
    if chunk.empty?
198
      chunk + (0...3).map { charset.to_a[rand(charset.size)] }.join
199
    elsif chunk.length == 1
200
      chunk + (0...2).map { charset.to_a[rand(charset.size)] }.join
201
    elsif chunk.length == 2
202
      chunk + (0...1).map { charset.to_a[rand(charset.size)] }.join
203
    else
204
      chunk
205
    end
206
  end
207
208
  def greenlight_account?
209
    return true unless provider # For testing cases when provider is set to null
210
    return true if provider == "greenlight"
211
    return false unless Rails.configuration.loadbalanced_configuration
212
    # Proceed with fetching the provider info
213
    provider_info = retrieve_provider_info(provider, 'api2', 'getUserGreenlightCredentials')
214
    provider_info['provider'] == 'greenlight'
215
  end
216
217
  def activation_token
218
    # Create the token.
219
    create_reset_activation_digest(User.new_token)
220
  end
221
222
  def admin_of?(user)
223
    if Rails.configuration.loadbalanced_configuration
224
      # Pulls in the user roles if they weren't request in the original request
225
      # So the has_cached_role? doesn't always return false
226
      user.roles
227
      if has_cached_role? :super_admin
228
        id != user.id
229
      else
230
        (has_cached_role? :admin) && (id != user.id) && (provider == user.provider) &&
231
          (!user.has_cached_role? :super_admin)
232
      end
233
    else
234
      ((has_cached_role? :admin) || (has_cached_role? :super_admin)) && (id != user.id)
235
    end
236
  end
237
238
  def self.digest(string)
239
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost
240
    BCrypt::Password.create(string, cost: cost)
241
  end
242
243
  # Returns a random token.
244
  def self.new_token
245
    SecureRandom.urlsafe_base64
246
  end
247
248
  private
249
250
  def create_reset_activation_digest(token)
251
    # Create the digest and persist it.
252
    self.activation_digest = User.digest(token)
253
    save
254
    token
255
  end
256
257
  # Destory a users rooms when they are removed.
258
  def destroy_rooms
259
    rooms.destroy_all
260
  end
261
262
  # Initializes a room for the user and assign a BigBlueButton user id.
263
  def initialize_main_room
264
    self.uid = "gl-#{(0...12).map { rand(65..90).chr }.join.downcase}"
265
    self.main_room = Room.create!(owner: self, name: I18n.t("home_room"))
266
    save
267
  end
268
269
  # Initialize the user to use the default user role
270
  def assign_default_role
271
    add_role(:user) if roles.blank?
272
  end
273
274
  def check_if_email_can_be_blank
275
    if email.blank?
276
      if Rails.configuration.loadbalanced_configuration && greenlight_account?
277
        errors.add(:email, I18n.t("errors.messages.blank"))
278
      elsif provider == "greenlight"
279
        errors.add(:email, I18n.t("errors.messages.blank"))
280
      end
281
    end
282
  end
283
end
284