diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index ddc06c1838114c32f39a46db851c715662067851..7608624ba504b937f8cb77e00917d6006ed93999 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -69,6 +69,8 @@ class Admin::UsersController < Admin::ApplicationController end def user_params - params.require(:user).permit(:first_name, :last_name, :role, :language_id, :picture, :picture_delete, :picture_infos, :mobile_phone) + params.require(:user) + .permit(:email, :first_name, :last_name, :role, :language_id, :picture, :picture_delete, :picture_infos, :mobile_phone) + .merge(university_id: current_university.id) end end diff --git a/app/models/university/with_users.rb b/app/models/university/with_users.rb index 7f81767ca4c7deefbe2e4041569467ab55328cb4..d66e08afe304f35c253fbd7061a7bdb604d9551e 100644 --- a/app/models/university/with_users.rb +++ b/app/models/university/with_users.rb @@ -3,5 +3,15 @@ module University::WithUsers included do has_many :users, dependent: :destroy + + after_commit :synchronize_server_admin_users, on: :create + + private + + def synchronize_server_admin_users + User.synchronize_server_admin_users(id) + end + handle_asynchronously :synchronize_server_admin_users + end end diff --git a/app/models/user.rb b/app/models/user.rb index 70045a197e7a05e0cc3a86719ad94a67ac1ef216..19c3bd790a7470b740187ac150ce4bf607b22dfa 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -55,6 +55,7 @@ class User < ApplicationRecord include WithAuthentication include WithRoles + include WithSyncBetweenUniversities belongs_to :university belongs_to :language diff --git a/app/models/user/with_sync_between_universities.rb b/app/models/user/with_sync_between_universities.rb new file mode 100644 index 0000000000000000000000000000000000000000..64cfa3e15404238e757dd4f783a5841cdff22434 --- /dev/null +++ b/app/models/user/with_sync_between_universities.rb @@ -0,0 +1,48 @@ +module User::WithSyncBetweenUniversities + extend ActiveSupport::Concern + + included do + after_save :sync_from_current_university, if: Proc.new { |user| user.server_admin? } + after_destroy :remove_from_all_universities, if: Proc.new { |user| user.server_admin? } + + def self.synchronize_server_admin_users(university_id) + university = University.where.not(id: university_id).first + university.users.server_admin.each(&:sync_from_current_university) if university + end + end + + def sync_from_current_university + University.where.not(id: university_id).each do |university| + unless User.where(email: email, university_id: university.id).any? + duplicate_user_for_university(university) + else + User.where(email: email, university_id: university.id).first&.update_columns( + encrypted_password: self.encrypted_password, + first_name: self.first_name, + last_name: self.last_name, + mobile_phone: self.phone, + role: self.role + ) + end + end + end + + private + + def duplicate_user_for_university(university) + # Create user for this university + user = self.dup + user.assign_attributes(university_id: university.id, picture_infos: nil, + password: "MyNewPasswordIs2Strong!", password_confirmation: "MyNewPasswordIs2Strong!", + reset_password_token: nil, unlock_token: nil, encrypted_otp_secret_key: nil, + confirmation_token: Devise.friendly_token, confirmed_at: Time.now) + # as a new user must have a password and we can't access previous user password + user.save + user.update_column(:encrypted_password, self.encrypted_password) if user.valid? + end + + def remove_from_all_universities + User.destroy_by(email: email) + end + +end diff --git a/config/routes.rb b/config/routes.rb index c1aae1fe06e87bd9b0312a5b4fb4316369055d8f..7741968a9db05e355dce1c3aaf9bb4c6582ac0ac 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,7 @@ Rails.application.routes.draw do + + match "/delayed_job" => DelayedJobWeb, :anchor => false, :via => [:get, :post] + devise_for :users, controllers: { confirmations: 'users/confirmations', passwords: 'users/passwords',