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
Pull Request — master (#912)
by Ahmad
04:46
created

User.shared_rooms()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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