Explorar el Código

redirect user when password needs to be changed

Olivier Massot hace 2 años
padre
commit
b423b9861d

+ 15 - 0
ot_connect/Classes/Service/OtAuthenticationService.php

@@ -9,6 +9,8 @@ use GuzzleHttp\Exception\RequestException;
 use Opentalent\OtCore\Exception\ApiRequestException;
 use Opentalent\OtCore\Logging\OtLogger;
 use Opentalent\OtCore\Service\OpentalentApiService;
+use Opentalent\OtCore\Service\OpentalentEnvService;
+use Opentalent\OtCore\Utility\UrlBuilder;
 use TYPO3\CMS\Core\Crypto\Random;
 use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Database\Query\QueryHelper;
@@ -235,6 +237,19 @@ class OtAuthenticationService extends AbstractAuthenticationService
                 return false;
             }
 
+            $data = json_decode((string)$response->getBody(), true);
+
+            # Redirect the user if the password needs to be changed
+            if (isset($data['type']) && $data['type'] === 'change_password') {
+                $redirectTo = UrlBuilder::concat(
+                    OpentalentEnvService::get('ADMIN_BASE_URL'),
+                    ["/account", $data['organization'], "secure/password/", $data['token']],
+                    []
+                );
+                echo "<body onload=\"window.location.replace('".$redirectTo."')\">";
+                die;
+            }
+
             // The API accepted the login request
 
             // Set the cookies returned by the Api  (SESSID and BEARER)

+ 86 - 0
ot_core/Classes/Utility/UrlBuilder.php

@@ -0,0 +1,86 @@
+<?php
+declare(strict_types=1);
+
+namespace Opentalent\OtCore\Utility;
+
+/**
+ * Building url utilities
+ */
+class UrlBuilder
+{
+    /**
+     * Concatenate a base url and a path
+     *
+     * @param string $base The base url
+     * @param array<string> $tails La suite de l'URL sous forme de tableau
+     * @return string
+     */
+    public static function concatPath(string $base, array $tails): string
+    {
+        $url = $base;
+        foreach ($tails as $tail){
+            $url = rtrim($url, '/') . '/' . ltrim(strval($tail), '/');
+        }
+        return $url;
+    }
+
+    /**
+     * Concatenate an url and a list of parameters
+     *
+     * @param string $url
+     * @param list<string | int> $parameters
+     * @return string
+     */
+    public static function concatParameters(string $url, array $parameters = []): string
+    {
+        if (!$parameters) { return $url; }
+        $url = rtrim($url, '?&/');
+        $query = implode(
+            '&',
+            array_map(
+                static function ($k, $v = '') {
+                    $q = urlencode((string)$k);
+                    if ((string)$v) {
+                        $q .= '=' . urlencode((string)$v);
+                    }
+                    return $q;
+                },
+                array_keys($parameters),
+                array_values($parameters)
+            )
+        );
+        return $url . (str_contains($url, '?') ?  '&' : '?') . $query;
+    }
+
+    /**
+     * Prepend the 'https://' part if neither 'http://' of 'https://' is present, else: does nothing
+     *
+     * @param string $url
+     * @return string
+     */
+    public static function prependHttps(string $url): string
+    {
+        if (!preg_match('/^https?:\/\/.*/', $url)) {
+            $url = 'https://' . $url;
+        }
+        return $url;
+    }
+
+    /**
+     * Build an url
+     *
+     * @param string $url The base url
+     * @param array<string> $tails la suite de l'url sous forme de tableau
+     * @param list<string> $parameters A list of parameters (can be an empty array)
+     * @param bool $preprendHttps Should the 'https://' be prepended if missing
+     * @return string
+     */
+    public static function concat(string $url, array $tails, array $parameters, bool $preprendHttps = false): string
+    {
+        $url = self::concatParameters(self::concatPath($url, $tails), $parameters);
+        if ($preprendHttps) {
+            $url = self::prependHttps($url);
+        }
+        return $url;
+    }
+}

+ 8 - 7
ot_core/ext_localconf.php

@@ -37,13 +37,14 @@ $GLOBALS['TYPO3_CONF_VARS']['EXT']['news']['Controller/NewsController.php']['cre
 // In this case we merge them so that the pre-defined vars are not overridden.
 // (/!\ do not forget to clear the cache after any update here)
 $GLOBALS['OT'] = array_merge([
-    'API_BASE_URI' => 'https://api.opentalent.fr',
-    'DB_HOST' => 'prod-back',
+    'API_BASE_URI' => 'http://docker.nginx.opentalent.fr',
+    'DB_HOST' => 'db',
     'DB_USER' => 'dbcloner',
     'DB_PASSWORD' => 'wWZ4hYcrmHLW2mUK',
-    'FRAMES_BASE_URI' => 'https://frames.opentalent.fr',
-    'WEBSITE' => 'https://opentalent.fr',
-    'DASHBOARD_URL' => 'https://admin.opentalent.fr/#/dashboard',
-    'LOGIN_PAGE_URL' => 'https://admin.opentalent.fr/#/login',
-    'FILE_STORAGE_URL' => 'https://api.opentalent.fr/app.php/_internal/secure/files/'
+    'FRAMES_BASE_URI' => 'https://local.frames.opentalent.fr',
+    'WEBSITE' => 'https://local.opentalent.fr',
+    'ADMIN_BASE_URL' => 'https://local.admin.opentalent.fr/',
+    'DASHBOARD_URL' => 'https://local.admin.opentalent.fr/#/dashboard',
+    'LOGIN_PAGE_URL' => 'https://local.admin.opentalent.fr/#/login',
+    'FILE_STORAGE_URL' => 'https://local.api.opentalent.fr/app.php/_internal/secure/files/'
 ], ($GLOBALS['OT'] ?? []));