GRN2-253: Added the ability to share rooms across multiple users (#912)

* Added ability to share rooms with other users

* Fixed testcases
This commit is contained in:
Ahmad Farhat
2020-01-23 09:04:41 -05:00
committed by farhatahmad
parent 8cbfc3f730
commit 967130e57c
36 changed files with 748 additions and 55 deletions

View File

@ -42,7 +42,7 @@ class Role < ApplicationRecord
Role.create(name: "super_admin", provider: provider, priority: -2, colour: "#cd201f")
.update_all_role_permissions(can_create_rooms: true,
send_promoted_email: true, send_demoted_email: true, can_edit_site_settings: true,
can_edit_roles: true, can_manage_users: true)
can_edit_roles: true, can_manage_users: true, can_appear_in_share_list: true)
end
def self.create_new_role(role_name, provider)
@ -69,6 +69,7 @@ class Role < ApplicationRecord
update_permission("can_edit_roles", permissions[:can_edit_roles].to_s)
update_permission("can_manage_users", permissions[:can_manage_users].to_s)
update_permission("can_manage_rooms_recordings", permissions[:can_manage_rooms_recordings].to_s)
update_permission("can_appear_in_share_list", permissions[:can_appear_in_share_list].to_s)
end
# Updates the value of the permission and enables it
@ -85,7 +86,12 @@ class Role < ApplicationRecord
value = if permission[:enabled]
permission[:value]
else
"false"
case name
when "can_appear_in_share_list"
Rails.configuration.shared_access_default.to_s
else
"false"
end
end
if return_boolean

View File

@ -26,6 +26,7 @@ class Room < ApplicationRecord
validates :name, presence: true
belongs_to :owner, class_name: 'User', foreign_key: :user_id
has_many :shared_access
def self.admins_search(string)
active_database = Rails.configuration.database_configuration[Rails.env]["adapter"]
@ -59,6 +60,15 @@ class Room < ApplicationRecord
user.rooms.include?(self)
end
def shared_users
User.where(id: shared_access.pluck(:user_id))
end
def shared_with?(user)
return false if user.nil?
shared_users.include?(user)
end
# Determines the invite path for the room.
def invite_path
"#{Rails.configuration.relative_url_root}/#{CGI.escape(uid)}"

View File

@ -43,6 +43,8 @@ class Setting < ApplicationRecord
false
when "Room Limit"
Rails.configuration.number_of_rooms_default
when "Shared Access"
Rails.configuration.shared_access_default
end
end
end

View File

@ -0,0 +1,5 @@
# frozen_string_literal: true
class SharedAccess < ApplicationRecord
belongs_to :room
end

View File

@ -29,6 +29,7 @@ class User < ApplicationRecord
before_destroy :destroy_rooms
has_many :rooms
has_many :shared_access
belongs_to :main_room, class_name: 'Room', foreign_key: :room_id, required: false
has_and_belongs_to_many :roles, -> { includes :role_permissions }, join_table: :users_roles
@ -135,6 +136,11 @@ class User < ApplicationRecord
room_list.where.not(last_session: nil).order("last_session desc") + room_list.where(last_session: nil)
end
# Retrieves a list of rooms that are shared with the user
def shared_rooms
Room.where(id: shared_access.pluck(:room_id))
end
def name_chunk
charset = ("a".."z").to_a - %w(b i l o s) + ("2".."9").to_a - %w(5 8)
chunk = name.parameterize[0...3]
@ -228,11 +234,22 @@ class User < ApplicationRecord
User.where.not(id: with_role(role).pluck(:id))
end
def self.with_highest_priority_role(role)
User.all_users_highest_priority_role.where(roles: { name: role })
end
def self.all_users_with_roles
User.joins("INNER JOIN users_roles ON users_roles.user_id = users.id INNER JOIN roles " \
"ON roles.id = users_roles.role_id INNER JOIN role_permissions ON roles.id = role_permissions.role_id").distinct
end
def self.all_users_highest_priority_role
User.joins("INNER JOIN (SELECT user_id, role_id, min(roles.priority) FROM users_roles " \
"INNER JOIN roles ON users_roles.role_id = roles.id GROUP BY user_id) as a ON " \
"a.user_id = users.id INNER JOIN roles ON roles.id = a.role_id " \
" INNER JOIN role_permissions ON roles.id = role_permissions.role_id").distinct
end
private
def create_reset_activation_digest(token)