auth result data

This commit is contained in:
Yuri Kuznetsov
2021-08-13 11:32:23 +03:00
parent 2879f426e0
commit 1ebedaf791
4 changed files with 95 additions and 9 deletions
+6 -2
View File
@@ -197,6 +197,7 @@ class Auth
'message' => $result->getMessage(),
'view' => $result->getView(),
'token' => $result->getToken(),
'data' => $result->getData(),
];
$response->writeBody(json_encode($bodyData));
@@ -254,6 +255,7 @@ class Auth
if ($request->hasHeader('X-Hmac-Authorization')) {
return 'Hmac';
}
if ($request->hasHeader('X-Api-Key')) {
return 'ApiKey';
}
@@ -276,7 +278,8 @@ class Auth
}
if (
$request->getServerParam('PHP_AUTH_USER') && $request->getServerParam('PHP_AUTH_PW')
$request->getServerParam('PHP_AUTH_USER') &&
$request->getServerParam('PHP_AUTH_PW')
) {
$username = $request->getServerParam('PHP_AUTH_USER');
$password = $request->getServerParam('PHP_AUTH_PW');
@@ -285,7 +288,8 @@ class Auth
}
if (
$request->getCookieParam('auth-username') && $request->getCookieParam('auth-token')
$request->getCookieParam('auth-username') &&
$request->getCookieParam('auth-token')
) {
$username = $request->getCookieParam('auth-username');
@@ -31,6 +31,8 @@ namespace Espo\Core\Authentication;
use Espo\Entities\User;
use stdClass;
/**
* An authentication result.
*/
@@ -56,11 +58,15 @@ class Result
private $failReason = null;
private $data = null;
private function __construct(string $status, ?User $user = null, ?ResultData $data = null)
{
$this->user = $user;
$this->status = $status;
$this->data = $data;
if ($data) {
$this->message = $data->getMessage();
$this->token = $data->getToken();
@@ -171,6 +177,14 @@ class Result
return $this->token;
}
/**
* Additional data that can be needed to a second step.
*/
public function getData(): ?stdClass
{
return $this->data ? $this->data->getData() : null;
}
/**
* A fail reason.
*/
@@ -31,6 +31,8 @@ namespace Espo\Core\Authentication;
use Espo\Entities\User;
use stdClass;
class ResultData
{
private $message = null;
@@ -43,6 +45,8 @@ class ResultData
private $failReason = null;
private $data = [];
private function __construct(
?string $message = null,
?string $failReason = null,
@@ -72,7 +76,7 @@ class ResultData
return new self($message);
}
public static function fromArray(array $data): self
/*public static function fromArray(array $data): self
{
return new self(
$data['message'] ?? null,
@@ -81,7 +85,7 @@ class ResultData
$data['view'] ?? null,
$data['loggedUser'] ?? null
);
}
}*/
public function getLoggedUser(): ?User
{
@@ -113,10 +117,14 @@ class ResultData
return $this->failReason;
}
public function getData(): stdClass
{
return (object) $this->data;
}
public function withMessage(?string $message): self
{
$obj = clone $this;
$obj->message = $message;
return $obj;
@@ -125,7 +133,6 @@ class ResultData
public function withFailReason(?string $failReason): self
{
$obj = clone $this;
$obj->failReason = $failReason;
return $obj;
@@ -134,7 +141,6 @@ class ResultData
public function withToken(?string $token): self
{
$obj = clone $this;
$obj->token = $token;
return $obj;
@@ -143,7 +149,6 @@ class ResultData
public function withView(?string $view): self
{
$obj = clone $this;
$obj->view = $view;
return $obj;
@@ -152,9 +157,19 @@ class ResultData
public function withLoggedUser(?User $loggedUser): self
{
$obj = clone $this;
$obj->loggedUser = $loggedUser;
return $obj;
}
/**
* @param mixed $value
*/
public function withDataItem(string $name, $value): self
{
$obj = clone $this;
$obj->data[$name] = $value;
return $obj;
}
}
@@ -0,0 +1,53 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM 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, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM 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 EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace tests\unit\Espo\Core\Authentication;
use Espo\Core\Authentication\ResultData;
class ResultDataTest extends \PHPUnit\Framework\TestCase
{
public function testCreate1(): void
{
$data = ResultData::create()
->withMessage('hello')
->withDataItem('test1', '1')
->withDataItem('test2', '2');
$this->assertEquals(
(object) [
'test1' => '1',
'test2' => '2',
],
$data->getData()
);
$this->assertEquals('hello', $data->getMessage());
}
}