Skip to content
Snippets Groups Projects
Commit 022f8c33 authored by pabois's avatar pabois
Browse files

sync between users

parent abd5a51e
No related branches found
No related tags found
No related merge requests found
......@@ -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
......@@ -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
......@@ -55,6 +55,7 @@
class User < ApplicationRecord
include WithAuthentication
include WithRoles
include WithSyncBetweenUniversities
belongs_to :university
belongs_to :language
......
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
Rails.application.routes.draw do
match "/delayed_job" => DelayedJobWeb, :anchor => false, :via => [:get, :post]
devise_for :users, controllers: {
confirmations: 'users/confirmations',
passwords: 'users/passwords',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment