From 30e1e2aa272a272f317757e51ae0019b7b1755bf Mon Sep 17 00:00:00 2001
From: Benjami <benjami94@gmail.com>
Date: Wed, 12 Jul 2023 06:30:15 +0200
Subject: [PATCH] Crear a method to make internal user and some fixes

---
 .../models/auth_oauth_provider.py             |  2 +-
 energy_communities/models/res_users.py        | 22 ++++++++++++-------
 .../wizards/assign_admin_wizard.py            |  2 +-
 3 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/energy_communities/models/auth_oauth_provider.py b/energy_communities/models/auth_oauth_provider.py
index 7616b863e..c4618e5fa 100644
--- a/energy_communities/models/auth_oauth_provider.py
+++ b/energy_communities/models/auth_oauth_provider.py
@@ -22,7 +22,7 @@ class OAuthProvider(models.Model):
                             placeholder='admin', required=False)
     superuser_pwd = fields.Char(string='Superuser password', help='"Superuser" user password',
                                 placeholder='I hope is not "admin"', required=False)
-    admin_user_endpoint = fields.Char(string='User admin URL', required=True)
+    admin_user_endpoint = fields.Char(string='User admin URL')
     root_endpoint = fields.Char(string='Root URL', required=True, default='http://keycloak-ccee.local:8080/auth/')
     realm_name = fields.Char(string='Realm name', required=True, default='0')
     reset_password_endpoint = fields.Char(string='Reset password URL')
diff --git a/energy_communities/models/res_users.py b/energy_communities/models/res_users.py
index c6d1dd686..aeed77993 100644
--- a/energy_communities/models/res_users.py
+++ b/energy_communities/models/res_users.py
@@ -227,11 +227,17 @@ class ResUsers(models.Model):
             )
 
     def make_internal_user(self):
-        # TODO: Require rebase new roles branch
-        pass
-        # self.env["res.users.role.line"].sudo().create({
-        #     "user_id": self.id,
-        #     "active": True,
-        #     "role_id": role.id,
-        #     "company_id": self.id,
-        # })
\ No newline at end of file
+        already_user = self.env["res.users.role.line"].sudo().search([
+            ("user_id.id", "=", self.id),
+            ("active", "=", True),
+            ("role_id.code", "=", "role_internal_user")
+        ])
+        if not already_user:
+            role = self.env["res.users.role"].sudo().search([(
+                "code", "=", "role_internal_user"
+            )])
+            self.env["res.users.role.line"].sudo().create({
+                "user_id": self.id,
+                "active": True,
+                "role_id": role.id,
+            })
diff --git a/energy_communities/wizards/assign_admin_wizard.py b/energy_communities/wizards/assign_admin_wizard.py
index fad683081..e4241fe15 100644
--- a/energy_communities/wizards/assign_admin_wizard.py
+++ b/energy_communities/wizards/assign_admin_wizard.py
@@ -46,7 +46,7 @@ class AssignAdminWizard(models.TransientModel):
             "firstname": self.first_name,
             "lastname": self.last_name,
             "company_id": company_id,
-            "company_ids": [company_id],
+            "company_ids": [(6, 0, [company_id])],
             "lang": self.lang.code,
             "email": self.email,
         }
-- 
GitLab