2019-02-13 12:59:28 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# SSO and authentication relative tasks
|
|
|
|
namespace :fablab do
|
|
|
|
namespace :auth do
|
|
|
|
|
|
|
|
desc 'switch the active authentication provider'
|
|
|
|
task :switch_provider, [:provider] => :environment do |_task, args|
|
2022-04-19 16:52:05 +02:00
|
|
|
unless args.provider
|
|
|
|
puts "\e[0;31mERROR\e[0m: You must pass a provider name to activate"
|
|
|
|
next
|
|
|
|
end
|
2019-02-13 12:59:28 +01:00
|
|
|
|
|
|
|
if AuthProvider.find_by(name: args.provider).nil?
|
|
|
|
providers = AuthProvider.all.inject('') { |str, item| str + item[:name] + ', ' }
|
2022-04-19 16:52:05 +02:00
|
|
|
puts "\e[0;31mERROR\e[0m: the provider '#{args.provider}' does not exists. Available providers are: #{providers[0..-3]}"
|
|
|
|
next
|
2019-02-13 12:59:28 +01:00
|
|
|
end
|
|
|
|
|
2022-04-19 16:52:05 +02:00
|
|
|
if AuthProvider.active.name == args.provider
|
|
|
|
puts "\e[0;31mERROR\e[0m: the provider '#{args.provider}' is already enabled"
|
|
|
|
next
|
|
|
|
end
|
2019-02-13 12:59:28 +01:00
|
|
|
|
|
|
|
# disable previous provider
|
2021-03-23 11:49:05 +01:00
|
|
|
prev_prev = AuthProvider.previous
|
2019-02-13 12:59:28 +01:00
|
|
|
prev_prev&.update_attribute(:status, 'pending')
|
|
|
|
|
2022-04-19 16:52:05 +02:00
|
|
|
AuthProvider.active.update_attribute(:status, 'previous') unless AuthProvider.active.name == 'DatabaseProvider::SimpleAuthProvider'
|
2019-02-13 12:59:28 +01:00
|
|
|
|
|
|
|
# enable given provider
|
|
|
|
AuthProvider.find_by(name: args.provider).update_attribute(:status, 'active')
|
|
|
|
|
|
|
|
# migrate the current users.
|
|
|
|
if AuthProvider.active.providable_type != DatabaseProvider.name
|
|
|
|
# Concerns any providers except local database
|
|
|
|
User.all.each(&:generate_auth_migration_token)
|
|
|
|
else
|
|
|
|
User.all.each do |user|
|
|
|
|
# Concerns local database provider
|
|
|
|
user.update_attribute(:auth_token, nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# ask the user to restart the application
|
2020-06-30 14:25:51 +02:00
|
|
|
next if Rails.env.test?
|
|
|
|
|
2022-04-19 16:52:05 +02:00
|
|
|
puts "\n\e[0;32m#{args.provider} successfully enabled\e[0m"
|
2019-02-13 12:59:28 +01:00
|
|
|
|
2022-04-19 16:52:05 +02:00
|
|
|
puts "\n\e[0;33m⚠ WARNING\e[0m: Please consider the following, otherwise the authentication will be bogus:"
|
|
|
|
puts "\t1) RESTART the application"
|
|
|
|
puts "\t2) NOTIFY the current users with `rails fablab:auth:notify_changed`\n\n"
|
2019-02-13 12:59:28 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
desc 'notify users that the auth provider has changed'
|
|
|
|
task notify_changed: :environment do
|
|
|
|
|
|
|
|
I18n.locale = I18n.default_locale
|
|
|
|
|
|
|
|
# notify every users if the provider is not local database provider
|
|
|
|
if AuthProvider.active.providable_type != DatabaseProvider.name
|
|
|
|
User.all.each do |user|
|
|
|
|
NotificationCenter.call type: 'notify_user_auth_migration',
|
|
|
|
receiver: user,
|
|
|
|
attached_object: user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "\nUsers successfully notified\n\n"
|
|
|
|
end
|
2022-04-19 16:52:05 +02:00
|
|
|
|
|
|
|
desc 'display the current active authentication provider'
|
|
|
|
task current: :environment do
|
|
|
|
puts "Current active authentication provider: #{AuthProvider.active.name}"
|
|
|
|
end
|
2019-02-13 12:59:28 +01:00
|
|
|
end
|
|
|
|
end
|