From 94988af434b9a007e67401d6be5c15bdca06d5da Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Mon, 18 Oct 2021 17:25:11 +0200
Subject: [PATCH] fix

---
 app/models/user/with_authentication.rb | 11 -----------
 app/models/user/with_roles.rb          |  9 +++++++++
 2 files changed, 9 insertions(+), 11 deletions(-)

diff --git a/app/models/user/with_authentication.rb b/app/models/user/with_authentication.rb
index 6f8b27749..dafe47ac0 100644
--- a/app/models/user/with_authentication.rb
+++ b/app/models/user/with_authentication.rb
@@ -20,7 +20,6 @@ module User::WithAuthentication
     validates :mobile_phone, format: { with: /\A\+[0-9]+\z/ }, allow_blank: true
 
     before_validation :adjust_mobile_phone, :sanitize_fields
-    before_validation :set_default_role, on: :create
 
     def self.find_for_authentication(warden_conditions)
       where(email: warden_conditions[:email].downcase, university_id: warden_conditions[:university_id]).first
@@ -72,16 +71,6 @@ module User::WithAuthentication
       self.mobile_phone = full_sanitizer.sanitize(self.mobile_phone)&.gsub('=', '')
     end
 
-    def set_default_role
-      if User.all.empty?
-        role = :server_admin
-      elsif university.users.empty?
-        role = :admin
-      else
-        role = :visitor
-      end
-    end
-
     def password_required?
       !persisted? || !password.nil? || !password_confirmation.nil?
     end
diff --git a/app/models/user/with_roles.rb b/app/models/user/with_roles.rb
index 59cfd936c..8adc16e8f 100644
--- a/app/models/user/with_roles.rb
+++ b/app/models/user/with_roles.rb
@@ -8,6 +8,7 @@ module User::WithRoles
 
     scope :for_role, -> (role) { where(role: role) }
 
+    before_validation :set_default_role, on: :create
     before_validation :check_modifier_role
 
     def managed_roles
@@ -23,5 +24,13 @@ module User::WithRoles
       errors.add(:role, 'cannot be set to this role') if modified_by && !modified_by.managed_roles.include?(self.role)
     end
 
+    def set_default_role
+      if User.all.empty?
+        self.role = :server_admin
+      elsif university.users.not_server_admin.empty?
+        self.role = :admin
+      end
+    end
+
   end
 end
-- 
GitLab