Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backport: MS-2756 backport to v54.6.2 #3986

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion helpers/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

class Layout
{
private static string $templateClass = Template::class;
protected static string $templateClass = Template::class;

public static function setTemplate(string $templateClass): void
{
Expand Down
66 changes: 66 additions & 0 deletions helpers/UserPilotTemplateHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2024 (original work) Open Assessment Technologies SA;
*/

namespace oat\tao\helpers;

use common_exception_Error;
use oat\tao\model\session\Dto\UserPilotDto;

class UserPilotTemplateHelper extends Layout
{
public const USER_PILOT_TEMPLATE = 'blocks/userpilot.tpl';

/**
* @throws common_exception_Error
*/
public static function userPilotCode(UserPilotDto $dto): void
{
$userPilotToken = getenv('USER_PILOT_TOKEN');
if (!$userPilotToken || !method_exists(self::$templateClass, 'inc')) {
return;
}

if (!$dto->getUserId()) {
return;
}

call_user_func(
[self::$templateClass, 'inc'],
self::USER_PILOT_TEMPLATE,
'tao',
[
'userpilot_data' => [
'token' => $userPilotToken,
'user' => [
'id' => $dto->getUserId(),
'name' => $dto->getUserName(),
'login' => $dto->getUserLogin(),
'email' => $dto->getUserEmail(),
'roles' => join(',', $dto->getUserRoles()),
'interface_language' => $dto->getInterfaceLanguage(),
],
'tenant' => [
'id' => $dto->getTenantId(),
]
],
]
);
}
}
38 changes: 38 additions & 0 deletions models/classes/session/Context/TenantDataSessionContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2013-2024 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*/

namespace oat\tao\model\session\Context;

use oat\oatbox\session\SessionContext;

class TenantDataSessionContext implements SessionContext
{
private ?string $tenantId;

public function __construct(string $tenantId = null)
{
$this->tenantId = $tenantId;
}

public function getTenantId(): ?string
{
return $this->tenantId;
}
}
71 changes: 71 additions & 0 deletions models/classes/session/Context/UserDataSessionContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2013-2024 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*/

namespace oat\tao\model\session\Context;

use oat\oatbox\session\SessionContext;

class UserDataSessionContext implements SessionContext
{
private ?string $userId;
private ?string $userLogin;
private ?string $userName;
private ?string $userEmail;
private ?string $locale;

public function __construct(
string $userId = null,
string $userLogin = null,
string $userName = null,
string $userEmail = null,
string $locale = null
) {
$this->userId = $userId;
$this->userLogin = $userLogin;
$this->userName = $userName;
$this->userEmail = $userEmail;
$this->locale = $locale;
}

public function getUserId(): ?string
{
return $this->userId;
}

public function getUserLogin(): ?string
{
return $this->userLogin;
}

public function getUserName(): ?string
{
return $this->userName;
}

public function getUserEmail(): ?string
{
return $this->userEmail;
}

public function getLocale(): ?string
{
return $this->locale;
}
}
97 changes: 97 additions & 0 deletions models/classes/session/Dto/UserPilotDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2024 (original work) Open Assessment Technologies SA;
*/

namespace oat\tao\model\session\Dto;

use common_session_Session;
use oat\tao\model\session\Context\TenantDataSessionContext;
use oat\tao\model\session\Context\UserDataSessionContext;

class UserPilotDto
{
public const NOT_AVAILABLE = 'N/A';
public const DEFAULT_LOCALE = 'en-US';
private ?string $userId = null;
private ?string $userName = null;
private ?string $userLogin = null;
private ?string $userEmail = null;
private ?string $interfaceLanguage = null;
private array $userRoles = [];
private ?string $tenantId = null;

public function __construct($session = null)
{
if ($session instanceof common_session_Session) {
$contexts = $session->getContexts() ?? [];
foreach ($contexts as $context) {
if ($context instanceof UserDataSessionContext) {
$this->userId = $context->getUserId();
$this->userLogin = $context->getUserLogin() ?? self::NOT_AVAILABLE;
$this->userName = $context->getUserName() ?? self::NOT_AVAILABLE;
$this->userEmail = $context->getUserEmail() ?? self::NOT_AVAILABLE;
$this->interfaceLanguage = $context->getLocale() ?? self::DEFAULT_LOCALE;
} elseif ($context instanceof TenantDataSessionContext) {
$this->tenantId = $context->getTenantId();
}
}

if (null !== $this->userId && null !== $this->tenantId) {
$this->userId = $this->tenantId . '|' . $this->userId;
}

$this->userRoles = $session->getUserRoles() ?? [];
}
}

public function getUserId(): ?string
{
return $this->userId;
}

public function getUserLogin(): ?string
{
return $this->userLogin;
}

public function getUserName(): ?string
{
return $this->userName;
}

public function getUserEmail(): ?string
{
return $this->userEmail;
}

public function getInterfaceLanguage(): ?string
{
return $this->interfaceLanguage;
}

public function getUserRoles(): array
{
return $this->userRoles;
}

public function getTenantId(): ?string
{
return $this->tenantId;
}
}
3 changes: 1 addition & 2 deletions test/unit/helpers/LayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace oat\tao\test\unit\helpers;

use oat\tao\helpers\Layout;
use oat\tao\helpers\Template;
use PHPUnit\Framework\TestCase;

class LayoutTest extends TestCase
Expand Down Expand Up @@ -55,7 +54,7 @@ public function testPrintAnalyticsCodeWithoutGaTag(): void
);
}

private function setEnv($key, $value)
protected function setEnv($key, $value): void
{
putenv("$key=$value");
$_ENV[$key] = $value;
Expand Down
Loading
Loading