Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option for default presentations on tenants #967

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/controllers/api/tenants_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/bigbluebutton_api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions app/controllers/concerns/api_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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('<modules></modules>')
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
10 changes: 9 additions & 1 deletion app/models/tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions docs/rake-README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]`

Expand Down
23 changes: 23 additions & 0 deletions lib/tasks/tenants.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/tasks/tenants_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down