From e324f97877d1c71a9d0b267ab6b3b0646c3f0a93 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Wed, 8 Jan 2020 10:38:21 +0100
Subject: [PATCH] FEAT #12091 TIME 0:20 Onlyoffice when create user

---
 apps/maarch_entreprise/ozwilloConnect.php                | 7 +++++--
 .../controllers/DocumentEditorController.php             | 9 ++++++++-
 src/app/user/controllers/UserController.php              | 7 +++++++
 src/app/user/models/UserModelAbstract.php                | 3 ++-
 4 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/apps/maarch_entreprise/ozwilloConnect.php b/apps/maarch_entreprise/ozwilloConnect.php
index 0a07da61e5f..40e87de79e3 100755
--- a/apps/maarch_entreprise/ozwilloConnect.php
+++ b/apps/maarch_entreprise/ozwilloConnect.php
@@ -32,11 +32,14 @@ if (empty($user)) {
     if (empty($ozwilloConfig['entityId'])) {
         $ozwilloConfig['entityId'] = 'VILLE';
     }
+    $group = \Group\models\GroupModel::getByGroupId(['groupId' => $ozwilloConfig['groupId'], 'select' => ['id']]);
     $firstname = empty($profile->given_name) ? 'utilisateur' : $profile->given_name;
     $lastname = empty($profile->family_name) ? 'utilisateur' : $profile->family_name;
-    \User\models\UserModel::create(['user' => ['userId' => $idToken->sub, 'firstname' => $firstname, 'lastname' => $lastname]]);
+    $preferences = ['documentEdition' => 'java'];
+
+    \User\models\UserModel::create(['user' => ['userId' => $idToken->sub, 'firstname' => $firstname, 'lastname' => $lastname, 'preferences' => json_encode($preferences)]]);
     $user = \User\models\UserModel::getByLogin(['login' => $idToken->sub]);
-    \User\models\UserModel::addGroup(['id' => $user['id'], 'groupId' => $ozwilloConfig['groupId']]);
+    \User\models\UserGroupModel::create(['user_id' => $user['id'], 'group_id' => $group['id']]);
     \User\models\UserEntityModel::addUserEntity(['id' => $user['id'], 'entityId' => $ozwilloConfig['entityId'], 'primaryEntity' => 'Y']);
 }
 
diff --git a/src/app/contentManagement/controllers/DocumentEditorController.php b/src/app/contentManagement/controllers/DocumentEditorController.php
index 42325e1f63e..89b93cfc0fb 100644
--- a/src/app/contentManagement/controllers/DocumentEditorController.php
+++ b/src/app/contentManagement/controllers/DocumentEditorController.php
@@ -23,6 +23,13 @@ class DocumentEditorController
     const DOCUMENT_EDITION_METHODS = ['java', 'onlyoffice'];
 
     public static function get(Request $request, Response $response)
+    {
+        $allowedMethods = DocumentEditorController::getAllowedMethods();
+
+        return $response->withJson($allowedMethods);
+    }
+
+    public static function getAllowedMethods()
     {
         $loadedXml = CoreConfigModel::getXmlLoaded(['path' => 'apps/maarch_entreprise/xml/documentEditorsConfig.xml']);
 
@@ -33,6 +40,6 @@ class DocumentEditorController
             }
         }
 
-        return $response->withJson($allowedMethods);
+        return $allowedMethods;
     }
 }
diff --git a/src/app/user/controllers/UserController.php b/src/app/user/controllers/UserController.php
index 5cb111acffa..5aa5ac2e585 100755
--- a/src/app/user/controllers/UserController.php
+++ b/src/app/user/controllers/UserController.php
@@ -205,6 +205,13 @@ class UserController
             $data['loginmode'] = 'standard';
         }
 
+        $preferences = ['documentEdition' => 'java'];
+        $allowedMethods = DocumentEditorController::getAllowedMethods();
+        if (in_array('onlyoffice', $allowedMethods)) {
+            $preferences = ['documentEdition' => 'onlyoffice'];
+        }
+        $data['preferences'] = json_encode($preferences);
+
         $id = UserModel::create(['user' => $data]);
 
         $userQuota = ParameterModel::getById(['id' => 'user_quota', 'select' => ['param_value_int']]);
diff --git a/src/app/user/models/UserModelAbstract.php b/src/app/user/models/UserModelAbstract.php
index 49bda96f94b..af8ffb45963 100755
--- a/src/app/user/models/UserModelAbstract.php
+++ b/src/app/user/models/UserModelAbstract.php
@@ -84,7 +84,7 @@ abstract class UserModelAbstract
     {
         ValidatorModel::notEmpty($args, ['user']);
         ValidatorModel::notEmpty($args['user'], ['userId', 'firstname', 'lastname']);
-        ValidatorModel::stringType($args['user'], ['userId', 'firstname', 'lastname', 'mail', 'initials', 'phone', 'loginmode']);
+        ValidatorModel::stringType($args['user'], ['userId', 'firstname', 'lastname', 'mail', 'initials', 'phone', 'loginmode', 'preferences']);
 
         $password = AuthenticationModel::generatePassword();
         $nextSequenceId = DatabaseModel::getNextSequenceValue(['sequenceId' => 'users_id_seq']);
@@ -100,6 +100,7 @@ abstract class UserModelAbstract
                 'phone'                         => $args['user']['phone'],
                 'initials'                      => $args['user']['initials'],
                 'status'                        => 'OK',
+                'preferences'                   => $args['user']['preferences'],
                 'loginmode'                     => empty($args['user']['loginmode']) ? 'standard' : $args['user']['loginmode'],
                 'password'                      => AuthenticationModel::getPasswordHash($password),
                 'password_modification_date'    => 'CURRENT_TIMESTAMP'
-- 
GitLab