From e4f50026f1b0292b2b75dcd33214c59defb9be21 Mon Sep 17 00:00:00 2001 From: farhatahmad <35435341+farhatahmad@users.noreply.github.com> Date: Tue, 9 Jul 2019 10:56:06 -0400 Subject: [PATCH] Added setting for admin to limit the number of rooms for the user (#607) --- app/assets/stylesheets/admins.scss | 9 +++ app/controllers/admins_controller.rb | 8 ++- app/controllers/rooms_controller.rb | 12 ++++ app/helpers/admins_helper.rb | 4 ++ app/helpers/rooms_helper.rb | 22 +++++++ app/models/setting.rb | 2 + app/views/rooms/show.html.erb | 64 +++++++++++-------- .../admin_settings/_site_settings.html.erb | 36 ++++++++++- config/application.rb | 3 + config/locales/en.yml | 5 ++ config/routes.rb | 1 + spec/controllers/admins_controller_spec.rb | 18 ++++++ spec/controllers/rooms_controller_spec.rb | 17 ++++- 13 files changed, 171 insertions(+), 30 deletions(-) diff --git a/app/assets/stylesheets/admins.scss b/app/assets/stylesheets/admins.scss index 0a5542ff..ee882a5d 100644 --- a/app/assets/stylesheets/admins.scss +++ b/app/assets/stylesheets/admins.scss @@ -45,4 +45,13 @@ .authentication-required{ padding-top: 2px; +} + +#site_settings { + .colorinput-color { + text-align: center; + padding-top: 4px; + height: 2rem; + width: 2rem; + } } \ No newline at end of file diff --git a/app/controllers/admins_controller.rb b/app/controllers/admins_controller.rb index 8f87171e..00ab91bc 100644 --- a/app/controllers/admins_controller.rb +++ b/app/controllers/admins_controller.rb @@ -23,7 +23,7 @@ class AdminsController < ApplicationController manage_users = [:edit_user, :promote, :demote, :ban_user, :unban_user, :approve] site_settings = [:branding, :coloring, :coloring_lighten, :coloring_darken, - :registration_method, :room_authentication] + :registration_method, :room_authentication, :room_limit] authorize_resource class: false before_action :find_user, only: manage_users @@ -153,6 +153,12 @@ class AdminsController < ApplicationController end end + # POST /admins/room_limit + def room_limit + @settings.update_value("Room Limit", params[:limit]) + redirect_to admins_path, flash: { success: I18n.t("administrator.flash.settings") } + end + private def find_user diff --git a/app/controllers/rooms_controller.rb b/app/controllers/rooms_controller.rb index b96166da..fde621b1 100644 --- a/app/controllers/rooms_controller.rb +++ b/app/controllers/rooms_controller.rb @@ -33,6 +33,8 @@ class RoomsController < ApplicationController def create redirect_to(root_path) && return unless current_user + return redirect_to current_user.main_room, flash: { alert: I18n.t("room.room_limit") } if room_limit_exceeded + @room = Room.new(name: room_params[:name]) @room.owner = current_user @room.room_settings = create_room_settings_string(room_params[:mute_on_join], room_params[:client]) @@ -279,4 +281,14 @@ class RoomsController < ApplicationController Setting.find_or_create_by!(provider: user_settings_provider).get_value("Room Authentication") == "true" && current_user.nil? end + + def room_limit_exceeded + limit = Setting.find_or_create_by!(provider: user_settings_provider).get_value("Room Limit").to_i + + # Does not apply to admin + # 15+ option is used as unlimited + return false if current_user&.has_role?(:admin) || limit == 15 + + current_user.rooms.count >= limit + end end diff --git a/app/helpers/admins_helper.rb b/app/helpers/admins_helper.rb index ab430e71..af23f731 100644 --- a/app/helpers/admins_helper.rb +++ b/app/helpers/admins_helper.rb @@ -53,4 +53,8 @@ module AdminsHelper I18n.t("administrator.site_settings.registration.methods.approval") end end + + def room_limit_number + Setting.find_or_create_by!(provider: user_settings_provider).get_value("Room Limit").to_i + end end diff --git a/app/helpers/rooms_helper.rb b/app/helpers/rooms_helper.rb index e355a8e5..cf0f3dd7 100644 --- a/app/helpers/rooms_helper.rb +++ b/app/helpers/rooms_helper.rb @@ -27,4 +27,26 @@ module RoomsHelper Setting.find_or_create_by!(provider: user_settings_provider).get_value("Room Authentication") == "true" && current_user.nil? end + + def number_of_rooms_allowed + Setting.find_or_create_by!(provider: user_settings_provider).get_value("Room Limit").to_i + end + + def room_limit_exceeded + limit = Setting.find_or_create_by!(provider: user_settings_provider).get_value("Room Limit").to_i + + # Does not apply to admin or users that aren't signed in + # 15+ option is used as unlimited + return false if !current_user || current_user&.has_role?(:admin) || limit == 15 + + current_user.rooms.length >= limit + end + + def current_room_exceeds_limit(room) + # Get how many rooms need to be deleted to reach allowed room number + limit = Setting.find_or_create_by!(provider: user_settings_provider).get_value("Room Limit").to_i + @diff = current_user.rooms.count - limit + + @diff.positive? && current_user.rooms.pluck(:id).index(room.id) + 1 > limit + end end diff --git a/app/models/setting.rb b/app/models/setting.rb index b4f07ff0..82f395f5 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -41,6 +41,8 @@ class Setting < ApplicationRecord Rails.configuration.registration_method_default when "Room Authentication" false + when "Room Limit" + Rails.configuration.number_of_rooms_default end end end diff --git a/app/views/rooms/show.html.erb b/app/views/rooms/show.html.erb index 83aa968d..d527be81 100644 --- a/app/views/rooms/show.html.erb +++ b/app/views/rooms/show.html.erb @@ -13,6 +13,12 @@ # with BigBlueButton; if not, see . %> +<% exceeds_limit = current_room_exceeds_limit(@room)%> +<% if exceeds_limit%> +
+ <%= t("room.room_limit_exceeded", difference: @diff) %> +
+<% end %>
@@ -27,41 +33,45 @@ <% end %>

<%= @room.sessions %> <%= t("room.sessions") %> | <%= @recordings.length %> <%= t("room.recordings") %>

- -
-
- -
-
-
-
- - - <%= t("copy") %> - + <% unless exceeds_limit %> + +
+
+ -
- <% if Rails.configuration.enable_google_calendar_button %> - - - <%= t("add_to_google_calendar") %> +
+
+
+ +
+ <% if Rails.configuration.enable_google_calendar_button %> + + + <%= t("add_to_google_calendar") %> + + <% end %> +
-
+ <% end %>
<% if @is_running %> <%= button_to t("room.join"), room_path(@room), class: "btn btn-primary btn-block px-7 start-button float-right" %> <% else %> - <%= button_to t("room.start"), start_room_path(@room), class: "btn btn-primary btn-block px-7 start-button float-right" %> + <% unless exceeds_limit %> + <%= button_to t("room.start"), start_room_path(@room), class: "btn btn-primary btn-block px-7 start-button float-right" %> + <% end %> <% end %>
@@ -82,7 +92,9 @@ <%= render "shared/modals/delete_room_modal", room: room %> <% end %> <% end %> - <%= render "shared/components/create_room_block"%> + <% unless room_limit_exceeded %> + <%= render "shared/components/create_room_block"%> + <% end %>
diff --git a/app/views/shared/admin_settings/_site_settings.html.erb b/app/views/shared/admin_settings/_site_settings.html.erb index 62d124f1..e9721d84 100644 --- a/app/views/shared/admin_settings/_site_settings.html.erb +++ b/app/views/shared/admin_settings/_site_settings.html.erb @@ -78,7 +78,7 @@
-
+
@@ -99,4 +99,38 @@
+
+
+
+ + +
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+
+
diff --git a/config/application.rb b/config/application.rb index 06cd5765..7e6f8915 100644 --- a/config/application.rb +++ b/config/application.rb @@ -130,6 +130,9 @@ module Greenlight # Default registration method if the user does not specify one config.registration_method_default = config.registration_methods[:open] + # Default limit on number of rooms users can create + config.number_of_rooms_default = 15 + # Default admin password config.admin_password_default = ENV['ADMIN_PASSWORD'] || 'administrator' end diff --git a/config/locales/en.yml b/config/locales/en.yml index f0eb5539..1e444d3c 100755 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -55,6 +55,9 @@ en: approval: Approve/Decline invite: Join by Invitation open: Open Registration + rooms: + info: Limits the number of rooms that a user can have (including Home Room). This setting does not apply to administrators. + title: Number of Rooms per User subtitle: Customize Greenlight title: Site Settings flash: @@ -353,6 +356,8 @@ en: owner: Owner no_sessions: This room has no sessions, yet! recordings: Room Recordings + room_limit: You have reached the maximum number of rooms allowed + room_limit_exceeded: You have exceeded the number of rooms allowed. Please delete %{difference} room(s) to access this room. sessions: Sessions settings: Room Settings start: Start diff --git a/config/routes.rb b/config/routes.rb index f2a39a23..a99d62cb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -51,6 +51,7 @@ Rails.application.routes.draw do post '/invite', to: 'admins#invite', as: :invite_user post '/registration_method/:method', to: 'admins#registration_method', as: :admin_change_registration post '/approve/:user_uid', to: 'admins#approve', as: :admin_approve + post '/room_limit', to: 'admins#room_limit', as: :admin_room_limit end scope '/themes' do diff --git a/spec/controllers/admins_controller_spec.rb b/spec/controllers/admins_controller_spec.rb index 4bdc4aa3..3f55a2c0 100644 --- a/spec/controllers/admins_controller_spec.rb +++ b/spec/controllers/admins_controller_spec.rb @@ -247,7 +247,9 @@ describe AdminsController, type: :controller do expect(response).to redirect_to(admins_path) end end + end + describe "Site Settings" do context "POST #registration_method" do it "changes the registration method for the given context" do allow(Rails.configuration).to receive(:enable_email_verification).and_return(true) @@ -294,5 +296,21 @@ describe AdminsController, type: :controller do expect(response).to redirect_to(admins_path) end end + + context "POST #room_limit" do + it "changes the room limit setting" do + allow(Rails.configuration).to receive(:loadbalanced_configuration).and_return(true) + allow_any_instance_of(User).to receive(:greenlight_account?).and_return(true) + + @request.session[:user_id] = @admin.id + + post :room_limit, params: { limit: 5 } + + feature = Setting.find_by(provider: "provider1").features.find_by(name: "Room Limit") + + expect(feature[:value]).to eq("5") + expect(response).to redirect_to(admins_path) + end + end end end diff --git a/spec/controllers/rooms_controller_spec.rb b/spec/controllers/rooms_controller_spec.rb index 1492e84c..b9899079 100644 --- a/spec/controllers/rooms_controller_spec.rb +++ b/spec/controllers/rooms_controller_spec.rb @@ -129,7 +129,7 @@ describe RoomsController, type: :controller do expect(response).to redirect_to(r) end - it "it should redirect to root if not logged in" do + it "should redirect to root if not logged in" do expect do name = Faker::Games::Pokemon.name post :create, params: { room: { name: name } } @@ -138,7 +138,7 @@ describe RoomsController, type: :controller do expect(response).to redirect_to(root_path) end - it "it should redirect back to main room with error if it fails" do + it "should redirect back to main room with error if it fails" do @request.session[:user_id] = @owner.id room_params = { name: "", "client": "html5", "mute_on_join": "1" } @@ -148,6 +148,19 @@ describe RoomsController, type: :controller do expect(flash[:alert]).to be_present expect(response).to redirect_to(@owner.main_room) end + + it "redirects to main room if room limit is reached" do + allow_any_instance_of(Setting).to receive(:get_value).and_return(1) + + @request.session[:user_id] = @owner.id + + room_params = { name: Faker::Games::Pokemon.name, "client": "html5", "mute_on_join": "1" } + + post :create, params: { room: room_params } + + expect(flash[:alert]).to be_present + expect(response).to redirect_to(@owner.main_room) + end end describe "POST #join" do