diff --git a/app/controllers/api/tenants_controller.rb b/app/controllers/api/tenants_controller.rb index 567fe77b..f137342a 100644 --- a/app/controllers/api/tenants_controller.rb +++ b/app/controllers/api/tenants_controller.rb @@ -84,12 +84,14 @@ def add_tenant # "id": String # Required # "tenant": { # "name": String, # include the parameter you want updated - # "secrets": String + # "secrets": String, + # "default_presentations": String # } # } def update_tenant @tenant.name = tenant_params[:name] if tenant_params[:name].present? @tenant.secrets = tenant_params[:secrets] if tenant_params[:secrets].present? + @tenant.default_presentations = tenant_params[:default_presentations] if tenant_params[:default_presentations].present? @tenant.save! render json: { tenant: @tenant }, status: :ok rescue ApplicationRedisRecord::RecordNotSaved => e @@ -117,7 +119,7 @@ def set_tenant end def tenant_params - params.require(:tenant).permit(:name, :secrets) + params.require(:tenant).permit(:name, :secrets, :default_presentations) end def check_multitenancy diff --git a/app/controllers/bigbluebutton_api_controller.rb b/app/controllers/bigbluebutton_api_controller.rb index 424021fd..b8a1d6f8 100644 --- a/app/controllers/bigbluebutton_api_controller.rb +++ b/app/controllers/bigbluebutton_api_controller.rb @@ -212,6 +212,9 @@ def create # Read the body if POST body = request.post? ? request.body.read : '' + # Override body with default presentations if empty + body = generate_tenant_presentations unless body != '' + # Send a GET/POST request to the server response = get_post_req(uri, body, **bbb_req_timeout(server)) rescue BBBError diff --git a/app/controllers/concerns/api_helper.rb b/app/controllers/concerns/api_helper.rb index dbce969f..2fb11002 100644 --- a/app/controllers/concerns/api_helper.rb +++ b/app/controllers/concerns/api_helper.rb @@ -217,3 +217,23 @@ def add_additional_params(action, bbb_params) final_params end end + +def generate_tenant_presentations + tenant = fetch_tenant + if tenant.present? + return '' if tenant.default_presentations_array.empty? + doc = Nokogiri::XML('') + module_pres = Nokogiri::XML::Node.new("module", doc) + module_pres['name'] = "presentation" + tenant.default_presentations_array.each { |x| + document = Nokogiri::XML::Node.new("document", doc) + document['url'] = x[0] + document['filename'] = x[1] + module_pres << document + } + doc.root << module_pres + doc.to_xml + else + '' + end +end diff --git a/app/models/tenant.rb b/app/models/tenant.rb index 1fce1188..e939361b 100644 --- a/app/models/tenant.rb +++ b/app/models/tenant.rb @@ -3,7 +3,7 @@ class Tenant < ApplicationRedisRecord SECRETS_SEPARATOR = ':' - define_attribute_methods :id, :name, :secrets + define_attribute_methods :id, :name, :secrets, :default_presentations # Unique ID for this tenant application_redis_attr :id @@ -14,6 +14,9 @@ class Tenant < ApplicationRedisRecord # Shared secrets for making API requests for this tenant (: separated) application_redis_attr :secrets + # Default presentations for this tenant (Sceme: URL->name;URL2->name2) + application_redis_attr :default_presentations + def save! with_connection do |redis| raise RecordNotSaved.new('Cannot update id field', self) if id_changed? && !@new_record @@ -34,6 +37,7 @@ def save! pipeline.del(old_names_key) if !id_changed? && name_changed? # Delete the old name key if it's not a new record and the name was updated pipeline.hset(id_key, 'name', name) if name_changed? pipeline.hset(id_key, 'secrets', secrets) if secrets_changed? + pipeline.hset(id_key, 'default_presentations', default_presentations) if default_presentations_changed? pipeline.sadd?('tenants', id) if id_changed? end end @@ -115,6 +119,10 @@ def destroy! super end + def default_presentations_array + default_presentations.blank? ? [] : default_presentations.split(";").collect { |x| x.split('->') } + end + def secrets_array secrets.split(SECRETS_SEPARATOR) end diff --git a/docs/rake-README.md b/docs/rake-README.md index 344c6b9e..e626c052 100644 --- a/docs/rake-README.md +++ b/docs/rake-README.md @@ -473,10 +473,19 @@ When you run this command, Scalelite will return a list of all tenants, along wi id: 9a870f45-ec23-4d29-828b-4673f3536d7b name: tenant1 secrets: secret1 + default presentations: 0 id: 4f3e4bb8-2a4e-41a6-9af8-0678c651777f name: tenant2 secrets: secret2:secret2a:secret2b + default presentations: 1 + https://test.bigbluebutton.org/default.pdf -> default.pdf ``` + +#### Set default presentations for a tenant +`./bin/rake tenants:presentations[id,"http://url_to_pres.pdf->default.pdf;http://another_pres.pdf->default_2.pdf"]` + +To clear the default presentations, simply omit the pres-param. + #### Associate Old Recordings with a Tenant `./bin/rake recordings:addToTenant[tenant-id]` diff --git a/lib/tasks/tenants.rake b/lib/tasks/tenants.rake index 6a10a277..1a09f064 100644 --- a/lib/tasks/tenants.rake +++ b/lib/tasks/tenants.rake @@ -14,6 +14,10 @@ task tenants: :environment do |_t, _args| puts("id: #{tenant.id}") puts("\tname: #{tenant.name}") puts("\tsecrets: #{tenant.secrets}") + puts("\tdefault presentations: #{tenant.default_presentations_array.length}") + tenant.default_presentations_array.each { |x| + puts("\t\t#{x[0]} -> #{x[1]}") + } end end @@ -59,6 +63,25 @@ namespace :tenants do puts("Updated Tenant id: #{tenant.id}") end + desc 'Set default presentations for an existing Tenant' + task :presentations, [:id, :pres] => :environment do |_t, args| + check_multitenancy + id = args[:id] + pres = args[:pres] + + if id.blank? + puts('Error: id is required to update a Tenant') + exit(1) + end + + tenant = Tenant.find(id) + tenant.default_presentations = (pres.presence || '') + tenant.save! + + puts('OK') + puts("Updated Tenant id: #{tenant.id}") + end + desc 'Remove existing Tenant' task :remove, [:id] => :environment do |_t, args| check_multitenancy diff --git a/spec/tasks/tenants_task_spec.rb b/spec/tasks/tenants_task_spec.rb index b5ad09b3..dca2fc5c 100644 --- a/spec/tasks/tenants_task_spec.rb +++ b/spec/tasks/tenants_task_spec.rb @@ -22,7 +22,7 @@ # rubocop:disable Layout/LineLength expect { task.invoke }.to output( - "id: #{tenant2.id}\n\tname: #{tenant2.name}\n\tsecrets: #{tenant2.secrets}\nid: #{tenant1.id}\n\tname: #{tenant1.name}\n\tsecrets: #{tenant1.secrets}\nTotal number of tenants: 2\n" + "id: #{tenant2.id}\n\tname: #{tenant2.name}\n\tsecrets: #{tenant2.secrets}\n\tdefault presentations: 0\nid: #{tenant1.id}\n\tname: #{tenant1.name}\n\tsecrets: #{tenant1.secrets}\n\tdefault presentations: 0\nTotal number of tenants: 2\n" ).to_stdout # rubocop:enable Layout/LineLength end