diff --git a/app/modules/web/Controllers/Helpers/Account/AccountPasswordHelper.php b/app/modules/web/Controllers/Helpers/Account/AccountPasswordHelper.php index 0e4001b6..b39c6a31 100644 --- a/app/modules/web/Controllers/Helpers/Account/AccountPasswordHelper.php +++ b/app/modules/web/Controllers/Helpers/Account/AccountPasswordHelper.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -29,7 +29,7 @@ use SP\Core\Acl\ActionsInterface; use SP\Core\Application; use SP\Core\Crypt\Crypt; use SP\Core\Crypt\Session as CryptSession; -use SP\DataModel\AccountPassData; +use SP\Domain\Account\Out\AccountPassData; use SP\Domain\Crypt\MasterPassServiceInterface; use SP\Http\RequestInterface; use SP\Modules\Web\Controllers\Helpers\HelperBase; diff --git a/lib/SP/Core/DataCollection.php b/lib/SP/Core/DataCollection.php index de98f161..a0e42df3 100644 --- a/lib/SP/Core/DataCollection.php +++ b/lib/SP/Core/DataCollection.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -50,7 +50,7 @@ abstract class DataCollection implements IteratorAggregate, ArrayAccess, Countab * Traversable * @since 5.0.0 */ - public function getIterator() + public function getIterator(): Traversable { return new ArrayIterator($this->attributes); } @@ -100,7 +100,7 @@ abstract class DataCollection implements IteratorAggregate, ArrayAccess, Countab * @return mixed Can return all value types. * @since 5.0.0 */ - public function offsetGet($offset) + public function offsetGet($offset): mixed { return $this->get($offset); } diff --git a/lib/SP/Core/Events/EventDispatcherBase.php b/lib/SP/Core/Events/EventDispatcherBase.php index 51fdea48..0136d5d5 100644 --- a/lib/SP/Core/Events/EventDispatcherBase.php +++ b/lib/SP/Core/Events/EventDispatcherBase.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -51,7 +51,7 @@ abstract class EventDispatcherBase implements EventDispatcherInterface * * @since 5.1.0 */ - public function attach(SplObserver $observer) + public function attach(SplObserver $observer): void { $observerClass = get_class($observer); @@ -77,7 +77,7 @@ abstract class EventDispatcherBase implements EventDispatcherInterface * @throws InvalidClassException * @since 5.1.0 */ - public function detach(SplObserver $observer) + public function detach(SplObserver $observer): void { $observerClass = get_class($observer); @@ -95,7 +95,7 @@ abstract class EventDispatcherBase implements EventDispatcherInterface * @return void * @since 5.1.0 */ - public function notify() + public function notify(): void { foreach ($this->observers as $observer) { $observer->update($this); diff --git a/lib/SP/DataModel/AccountData.php b/lib/SP/DataModel/AccountData.php deleted file mode 100644 index e1b62890..00000000 --- a/lib/SP/DataModel/AccountData.php +++ /dev/null @@ -1,527 +0,0 @@ -. - */ - -namespace SP\DataModel; - -defined('APP_ROOT') || die(); - -use JsonSerializable; -use SP\Http\Json; - -/** - * Class AccountData - * - * @package SP\Account - */ -class AccountData extends DataModelBase implements JsonSerializable, DataModelInterface -{ - /** - * @var int Id de la cuenta. - */ - public $id = 0; - /** - * @var int Id del usuario principal de la cuenta. - */ - public $userId = 0; - /** - * @var int Id del grupo principal de la cuenta. - */ - public $userGroupId = 0; - /** - * @var int Id del usuario que editó la cuenta. - */ - public $userEditId = 0; - /** - * @var string El nombre de la cuenta. - */ - public $name = ''; - /** - * @var int Id del cliente de la cuenta. - */ - public $clientId = 0; - /** - * @var int Id de la categoría de la cuenta. - */ - public $categoryId = 0; - /** - * @var string El nombre de usuario de la cuenta. - */ - public $login = ''; - /** - * @var string La URL de la cuenta. - */ - public $url = ''; - /** - * @var string La clave de la cuenta. - */ - public $pass = ''; - /** - * @var string La clave de encriptación de la cuenta - */ - public $key = ''; - /** - * @var string Las nosta de la cuenta. - */ - public $notes = ''; - /** - * @var bool Si se permite la edición por los usuarios secundarios. - */ - public $otherUserEdit = false; - /** - * @var bool Si se permita la edición por los grupos secundarios. - */ - public $otherUserGroupEdit = false; - /** - * @var int - */ - public $dateAdd = 0; - /** - * @var int - */ - public $dateEdit = 0; - /** - * @var int - */ - public $countView = 0; - /** - * @var int - */ - public $countDecrypt = 0; - /** - * @var int - */ - public $isPrivate = 0; - /** - * @var int - */ - public $isPrivateGroup = 0; - /** - * @var int - */ - public $passDate = 0; - /** - * @var int - */ - public $passDateChange = 0; - /** - * @var int - */ - public $parentId = 0; - - - /** - * AccountData constructor. - * - * @param int $accountId - */ - public function __construct($accountId = 0) - { - $this->id = (int)$accountId; - } - - /** - * @return int - */ - public function getDateAdd() - { - return $this->dateAdd; - } - - /** - * @param int $dateAdd - */ - public function setDateAdd($dateAdd) - { - $this->dateAdd = $dateAdd; - } - - /** - * @return int - */ - public function getDateEdit() - { - return $this->dateEdit; - } - - /** - * @param int $dateEdit - */ - public function setDateEdit($dateEdit) - { - $this->dateEdit = $dateEdit; - } - - /** - * @return int - */ - public function getUserEditId() - { - return (int)$this->userEditId; - } - - /** - * @param int $userEditId - */ - public function setUserEditId($userEditId) - { - $this->userEditId = (int)$userEditId; - } - - /** - * @return string - */ - public function getPass() - { - return $this->pass; - } - - /** - * @param string $pass - */ - public function setPass($pass) - { - $this->pass = $pass; - } - - /** - * @return string - */ - public function getKey() - { - return $this->key; - } - - /** - * @param string $key - */ - public function setKey($key) - { - $this->key = $key; - } - - /** - * @return int - */ - public function getId() - { - return (int)$this->id; - } - - /** - * @param int $id - */ - public function setId($id) - { - $this->id = (int)$id; - } - - /** - * @return int - */ - public function getUserId() - { - return (int)$this->userId; - } - - /** - * @param int $userId - */ - public function setUserId($userId) - { - $this->userId = (int)$userId; - } - - /** - * @return int - */ - public function getUserGroupId() - { - return (int)$this->userGroupId; - } - - /** - * @param int $userGroupId - */ - public function setUserGroupId($userGroupId) - { - $this->userGroupId = (int)$userGroupId; - } - - /** - * @return int - */ - public function getOtherUserEdit() - { - return (int)$this->otherUserEdit; - } - - /** - * @param bool $otherUserEdit - */ - public function setOtherUserEdit($otherUserEdit) - { - $this->otherUserEdit = (int)$otherUserEdit; - } - - /** - * @return int - */ - public function getOtherUserGroupEdit() - { - return (int)$this->otherUserGroupEdit; - } - - /** - * @param bool $otherUserGroupEdit - */ - public function setOtherUserGroupEdit($otherUserGroupEdit) - { - $this->otherUserGroupEdit = (int)$otherUserGroupEdit; - } - - /** - * @return string - */ - public function getName() - { - return $this->name; - } - - /** - * @param string $name - */ - public function setName($name) - { - $this->name = $name; - } - - /** - * @return int - */ - public function getCategoryId() - { - return (int)$this->categoryId; - } - - /** - * @param int $categoryId - */ - public function setCategoryId($categoryId) - { - $this->categoryId = (int)$categoryId; - } - - /** - * @return int - */ - public function getClientId() - { - return (int)$this->clientId; - } - - /** - * @param int $clientId - */ - public function setClientId($clientId) - { - $this->clientId = (int)$clientId; - } - - /** - * @return string - */ - public function getLogin() - { - return $this->login; - } - - /** - * @param string $login - */ - public function setLogin($login) - { - $this->login = $login; - } - - /** - * @return string - */ - public function getUrl() - { - return $this->url; - } - - /** - * @param string $url - */ - public function setUrl($url) - { - $this->url = $url; - } - - /** - * @return string - */ - public function getNotes() - { - return $this->notes; - } - - /** - * @param string $notes - */ - public function setNotes($notes) - { - $this->notes = $notes; - } - - /** - * Specify data which should be serialized to JSON - * - * @link http://php.net/manual/en/jsonserializable.jsonserialize.php - * @return mixed data which can be serialized by json_encode, - * which is a value of any type other than a resource. - * @since 5.4.0 - */ - public function jsonSerialize() - { - $data = get_object_vars($this); - - unset($data['accountPass'], $data['accountIV']); - - return Json::safeJson($data); - } - - /** - * @return int - */ - public function getCountView() - { - return (int)$this->countView; - } - - /** - * @param int $countView - */ - public function setCountView($countView) - { - $this->countView = (int)$countView; - } - - /** - * @return int - */ - public function getCountDecrypt() - { - return (int)$this->countDecrypt; - } - - /** - * @param int $countDecrypt - */ - public function setCountDecrypt($countDecrypt) - { - $this->countDecrypt = (int)$countDecrypt; - } - - /** - * @return int - */ - public function getIsPrivate() - { - return (int)$this->isPrivate; - } - - /** - * @param int $isPrivate - */ - public function setIsPrivate($isPrivate) - { - $this->isPrivate = (int)$isPrivate; - } - - /** - * @return int - */ - public function getPassDate() - { - return (int)$this->passDate; - } - - /** - * @param int $passDate - */ - public function setPassDate($passDate) - { - $this->passDate = (int)$passDate; - } - - /** - * @return int - */ - public function getPassDateChange() - { - return (int)$this->passDateChange; - } - - /** - * @param int $passDateChange - */ - public function setPassDateChange($passDateChange) - { - $this->passDateChange = (int)$passDateChange; - } - - /** - * @return int - */ - public function getParentId() - { - return (int)$this->parentId; - } - - /** - * @param int $parentId - */ - public function setParentId($parentId) - { - $this->parentId = (int)$parentId; - } - - /** - * @return int - */ - public function getIsPrivateGroup() - { - return (int)$this->isPrivateGroup; - } - - /** - * @param int $isPrivateGroup - */ - public function setIsPrivateGroup($isPrivateGroup) - { - $this->isPrivateGroup = (int)$isPrivateGroup; - } -} \ No newline at end of file diff --git a/lib/SP/DataModel/AccountExtData.php b/lib/SP/DataModel/AccountExtData.php index 8c98090e..876e0d8f 100644 --- a/lib/SP/DataModel/AccountExtData.php +++ b/lib/SP/DataModel/AccountExtData.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -24,6 +24,8 @@ namespace SP\DataModel; +use SP\Domain\Account\Out\AccountData; + /** * Class AccountExtData * diff --git a/lib/SP/DataModel/AccountPassData.php b/lib/SP/DataModel/AccountPassData.php deleted file mode 100644 index 8ea99b1d..00000000 --- a/lib/SP/DataModel/AccountPassData.php +++ /dev/null @@ -1,174 +0,0 @@ -. - */ - -namespace SP\DataModel; - -/** - * Class AccountPassData - * - * @package DataModel - */ -class AccountPassData extends DataModelBase implements DataModelInterface -{ - /** - * @var int Id de la cuenta. - */ - public $id = 0; - /** - * @var string El nombre de la cuenta. - */ - public $name = ''; - /** - * @var string El nombre de usuario de la cuenta. - */ - public $login = ''; - /** - * @var string La clave de la cuenta. - */ - public $pass = ''; - /** - * @var string La clave de encriptación de la cuenta - */ - public $key = ''; - /** - * @var int - */ - public $parentId = 0; - /** - * @var string - */ - public $mPassHash = ''; - - /** - * @return int - */ - public function getId() - { - return (int)$this->id; - } - - /** - * @param int $id - */ - public function setId($id) - { - $this->id = (int)$id; - } - - /** - * @return string - */ - public function getName() - { - return $this->name; - } - - /** - * @param string $name - */ - public function setName($name) - { - $this->name = $name; - } - - /** - * @return string - */ - public function getLogin() - { - return $this->login; - } - - /** - * @param string $login - */ - public function setLogin($login) - { - $this->login = $login; - } - - /** - * @return string - */ - public function getPass() - { - return $this->pass; - } - - /** - * @param string $pass - */ - public function setPass($pass) - { - $this->pass = $pass; - } - - /** - * @return string - */ - public function getKey() - { - return $this->key; - } - - /** - * @param string $key - */ - public function setKey($key) - { - $this->key = $key; - } - - /** - * @return int - */ - public function getParentId() - { - return $this->parentId; - } - - /** - * @param int $parentId - */ - public function setParentId($parentId) - { - $this->parentId = $parentId; - } - - /** - * @return string - */ - public function getMPassHash() - { - return $this->mPassHash; - } - - /** - * @param string $mPassHash - */ - public function setMPassHash($mPassHash) - { - $this->mPassHash = $mPassHash; - } -} \ No newline at end of file diff --git a/lib/SP/DataModel/AccountToUserGroupData.php b/lib/SP/DataModel/AccountToUserGroupData.php index 2bba885d..611534ba 100644 --- a/lib/SP/DataModel/AccountToUserGroupData.php +++ b/lib/SP/DataModel/AccountToUserGroupData.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -24,6 +24,8 @@ namespace SP\DataModel; +use SP\Domain\Common\Out\DataModelBase; + defined('APP_ROOT') || die(); /** diff --git a/lib/SP/DataModel/AccountVData.php b/lib/SP/DataModel/AccountVData.php index 3689fda4..ce6cbb2c 100644 --- a/lib/SP/DataModel/AccountVData.php +++ b/lib/SP/DataModel/AccountVData.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -25,6 +25,8 @@ namespace SP\DataModel; +use SP\Domain\Common\Out\DataModelBase; + /** * Class AccountVData * diff --git a/lib/SP/DataModel/ActionData.php b/lib/SP/DataModel/ActionData.php index 6d06fe15..1b74ea23 100644 --- a/lib/SP/DataModel/ActionData.php +++ b/lib/SP/DataModel/ActionData.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -24,6 +24,8 @@ namespace SP\DataModel; +use SP\Domain\Common\Out\DataModelInterface; + /** * Class ActionData * diff --git a/lib/SP/DataModel/AuthTokenData.php b/lib/SP/DataModel/AuthTokenData.php index 68e418a9..9536b4da 100644 --- a/lib/SP/DataModel/AuthTokenData.php +++ b/lib/SP/DataModel/AuthTokenData.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -25,6 +25,8 @@ namespace SP\DataModel; use SP\Core\Crypt\Vault; +use SP\Domain\Common\Out\DataModelBase; +use SP\Domain\Common\Out\DataModelInterface; /** * Class AuthTokenData diff --git a/lib/SP/DataModel/CategoryData.php b/lib/SP/DataModel/CategoryData.php index a9e9c03c..6b97f989 100644 --- a/lib/SP/DataModel/CategoryData.php +++ b/lib/SP/DataModel/CategoryData.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -24,6 +24,9 @@ namespace SP\DataModel; +use SP\Domain\Common\Out\DataModelBase; +use SP\Domain\Common\Out\DataModelInterface; + defined('APP_ROOT') || die(); /** diff --git a/lib/SP/DataModel/ClientData.php b/lib/SP/DataModel/ClientData.php index 868cfa39..0e9badb6 100644 --- a/lib/SP/DataModel/ClientData.php +++ b/lib/SP/DataModel/ClientData.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -24,6 +24,9 @@ namespace SP\DataModel; +use SP\Domain\Common\Out\DataModelBase; +use SP\Domain\Common\Out\DataModelInterface; + defined('APP_ROOT') || die(); /** diff --git a/lib/SP/DataModel/CustomFieldBaseData.php b/lib/SP/DataModel/CustomFieldBaseData.php index b99d2d06..c31e4e17 100644 --- a/lib/SP/DataModel/CustomFieldBaseData.php +++ b/lib/SP/DataModel/CustomFieldBaseData.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -24,6 +24,8 @@ namespace SP\DataModel; +use SP\Domain\Common\Out\DataModelBase; + defined('APP_ROOT') || die(); /** diff --git a/lib/SP/DataModel/CustomFieldDefDataOld.php b/lib/SP/DataModel/CustomFieldDefDataOld.php index 532a5699..3a90833f 100644 --- a/lib/SP/DataModel/CustomFieldDefDataOld.php +++ b/lib/SP/DataModel/CustomFieldDefDataOld.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -24,6 +24,8 @@ namespace SP\DataModel; +use SP\Domain\Common\Out\DataModelInterface; + /** * Class CustomFieldDefDataOld * diff --git a/lib/SP/DataModel/CustomFieldTypeData.php b/lib/SP/DataModel/CustomFieldTypeData.php index 954ab1e7..f6d10fd6 100644 --- a/lib/SP/DataModel/CustomFieldTypeData.php +++ b/lib/SP/DataModel/CustomFieldTypeData.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -25,6 +25,9 @@ namespace SP\DataModel; +use SP\Domain\Common\Out\DataModelBase; +use SP\Domain\Common\Out\DataModelInterface; + /** * Class CustomFieldTypeData * diff --git a/lib/SP/DataModel/EventlogData.php b/lib/SP/DataModel/EventlogData.php index 53e108f4..793d5edc 100644 --- a/lib/SP/DataModel/EventlogData.php +++ b/lib/SP/DataModel/EventlogData.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -24,6 +24,8 @@ namespace SP\DataModel; +use SP\Domain\Common\Out\DataModelBase; + /** * Class EventlogData * diff --git a/lib/SP/DataModel/FileData.php b/lib/SP/DataModel/FileData.php index 7f7c7dca..c12705df 100644 --- a/lib/SP/DataModel/FileData.php +++ b/lib/SP/DataModel/FileData.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -24,6 +24,9 @@ namespace SP\DataModel; +use SP\Domain\Common\Out\DataModelBase; +use SP\Domain\Common\Out\DataModelInterface; + defined('APP_ROOT') || die(); /** diff --git a/lib/SP/DataModel/ItemData.php b/lib/SP/DataModel/ItemData.php index e8cfb4f5..44cd7f28 100644 --- a/lib/SP/DataModel/ItemData.php +++ b/lib/SP/DataModel/ItemData.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -24,6 +24,8 @@ namespace SP\DataModel; +use SP\Domain\Common\Out\DataModelInterface; + /** * Class ItemData * diff --git a/lib/SP/DataModel/ItemPresetData.php b/lib/SP/DataModel/ItemPresetData.php index 444b81b4..7a7a0103 100644 --- a/lib/SP/DataModel/ItemPresetData.php +++ b/lib/SP/DataModel/ItemPresetData.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -24,6 +24,9 @@ namespace SP\DataModel; +use SP\Domain\Common\Out\DataModelBase; +use SP\Domain\Common\Out\HydratableInterface; + /** * Class ItemPresetData * diff --git a/lib/SP/DataModel/NotificationData.php b/lib/SP/DataModel/NotificationData.php index 4be152a9..bd27e111 100644 --- a/lib/SP/DataModel/NotificationData.php +++ b/lib/SP/DataModel/NotificationData.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -25,6 +25,7 @@ namespace SP\DataModel; use SP\Core\Messages\MessageInterface; +use SP\Domain\Common\Out\DataModelInterface; /** * Class NoticeData diff --git a/lib/SP/DataModel/PublicLinkData.php b/lib/SP/DataModel/PublicLinkData.php index ec4aa9e3..6bd92a25 100644 --- a/lib/SP/DataModel/PublicLinkData.php +++ b/lib/SP/DataModel/PublicLinkData.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -24,6 +24,9 @@ namespace SP\DataModel; +use SP\Domain\Common\Out\DataModelBase; +use SP\Domain\Common\Out\DataModelInterface; + defined('APP_ROOT') || die(); /** diff --git a/lib/SP/DataModel/SerializedModel.php b/lib/SP/DataModel/SerializedModel.php index 6f7ee60e..85a12a05 100644 --- a/lib/SP/DataModel/SerializedModel.php +++ b/lib/SP/DataModel/SerializedModel.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -42,18 +42,18 @@ trait SerializedModel * @return mixed|null * @throws NoSuchPropertyException */ - public function hydrate(string $class = null, string $property = 'data') + public function hydrate(?string $class = null, string $property = 'data') { if (property_exists($this, $property)) { - if ($this->$property === null) { + if ($this->{$property} === null) { return null; } if ($class !== null) { - return Util::unserialize($class, $this->$property); + return Util::unserialize($class, $this->{$property}); } - return unserialize($this->$property); + return unserialize($this->{$property}); } throw new NoSuchPropertyException($property); diff --git a/lib/SP/DataModel/TagData.php b/lib/SP/DataModel/TagData.php index ea598ed1..7d3e022f 100644 --- a/lib/SP/DataModel/TagData.php +++ b/lib/SP/DataModel/TagData.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -24,6 +24,9 @@ namespace SP\DataModel; +use SP\Domain\Common\Out\DataModelBase; +use SP\Domain\Common\Out\DataModelInterface; + defined('APP_ROOT') || die(); /** diff --git a/lib/SP/DataModel/TrackData.php b/lib/SP/DataModel/TrackData.php index 33f8b90b..698d0ef4 100644 --- a/lib/SP/DataModel/TrackData.php +++ b/lib/SP/DataModel/TrackData.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -25,6 +25,7 @@ namespace SP\DataModel; use SP\Core\Exceptions\InvalidArgumentException; +use SP\Domain\Common\Out\DataModelBase; use SP\Http\Address; /** diff --git a/lib/SP/DataModel/UserData.php b/lib/SP/DataModel/UserData.php index 838a31c2..dd700245 100644 --- a/lib/SP/DataModel/UserData.php +++ b/lib/SP/DataModel/UserData.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -24,6 +24,8 @@ namespace SP\DataModel; +use SP\Domain\Common\Out\DataModelInterface; + defined('APP_ROOT') || die(); /** diff --git a/lib/SP/DataModel/UserGroupData.php b/lib/SP/DataModel/UserGroupData.php index f14e8525..37c77c99 100644 --- a/lib/SP/DataModel/UserGroupData.php +++ b/lib/SP/DataModel/UserGroupData.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -24,6 +24,9 @@ namespace SP\DataModel; +use SP\Domain\Common\Out\DataModelBase; +use SP\Domain\Common\Out\DataModelInterface; + defined('APP_ROOT') || die(); /** diff --git a/lib/SP/DataModel/UserPassData.php b/lib/SP/DataModel/UserPassData.php index 028d2d9e..43f1b3be 100644 --- a/lib/SP/DataModel/UserPassData.php +++ b/lib/SP/DataModel/UserPassData.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -24,6 +24,8 @@ namespace SP\DataModel; +use SP\Domain\Common\Out\DataModelBase; + /** * Class UserPassData * diff --git a/lib/SP/DataModel/UserPassRecoverData.php b/lib/SP/DataModel/UserPassRecoverData.php index 52c1610f..a5908bec 100644 --- a/lib/SP/DataModel/UserPassRecoverData.php +++ b/lib/SP/DataModel/UserPassRecoverData.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -24,6 +24,8 @@ namespace SP\DataModel; +use SP\Domain\Common\Out\DataModelBase; + /** * Class UserPassRecoverData * diff --git a/lib/SP/DataModel/UserProfileData.php b/lib/SP/DataModel/UserProfileData.php index 295954e9..141be4e5 100644 --- a/lib/SP/DataModel/UserProfileData.php +++ b/lib/SP/DataModel/UserProfileData.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -24,6 +24,9 @@ namespace SP\DataModel; +use SP\Domain\Common\Out\DataModelBase; +use SP\Domain\Common\Out\DataModelInterface; + defined('APP_ROOT') || die(); /** diff --git a/lib/SP/DataModel/UserToUserGroupData.php b/lib/SP/DataModel/UserToUserGroupData.php index 30b67931..4791d7dd 100644 --- a/lib/SP/DataModel/UserToUserGroupData.php +++ b/lib/SP/DataModel/UserToUserGroupData.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -24,6 +24,8 @@ namespace SP\DataModel; +use SP\Domain\Common\Out\DataModelBase; + defined('APP_ROOT') || die(); /** diff --git a/lib/SP/Domain/Account/In/AccountRepositoryInterface.php b/lib/SP/Domain/Account/In/AccountRepositoryInterface.php index 4f2a4a7a..c27ac77f 100644 --- a/lib/SP/Domain/Account/In/AccountRepositoryInterface.php +++ b/lib/SP/Domain/Account/In/AccountRepositoryInterface.php @@ -32,9 +32,9 @@ use SP\Domain\Account\Services\AccountPasswordRequest; use SP\Domain\Account\Services\AccountRequest; use SP\Domain\Account\Services\AccountSearchFilter; use SP\Domain\Common\In\RepositoryInterface; +use SP\Domain\Common\Out\SimpleModel; use SP\Infrastructure\Database\QueryResult; use SP\Mvc\Model\QueryCondition; -use stdClass; /** * Class AccountRepository @@ -46,11 +46,10 @@ interface AccountRepositoryInterface extends RepositoryInterface /** * Devolver el número total de cuentas * - * @return stdClass * @throws QueryException * @throws ConstraintException */ - public function getTotalNumAccounts(): stdClass; + public function getTotalNumAccounts(): SimpleModel; /** * @param int $id diff --git a/lib/SP/Domain/Account/Out/AccountData.php b/lib/SP/Domain/Account/Out/AccountData.php new file mode 100644 index 00000000..58acbcab --- /dev/null +++ b/lib/SP/Domain/Account/Out/AccountData.php @@ -0,0 +1,199 @@ +. + */ + +namespace SP\Domain\Account\Out; + +defined('APP_ROOT') || die(); + +use JsonSerializable; +use SP\Domain\Common\Out\DataModelBase; +use SP\Domain\Common\Out\DataModelInterface; +use SP\Http\Json; + +/** + * Class AccountData + */ +class AccountData extends DataModelBase implements JsonSerializable, DataModelInterface +{ + private int $id; + private ?int $userId = null; + private ?int $userGroupId = null; + private ?int $userEditId = null; + private ?string $name = null; + private ?int $clientId = null; + private ?int $categoryId = null; + private ?string $login = null; + private ?string $url = null; + private ?string $pass = null; + private ?string $key = null; + private ?string $notes = null; + private ?int $dateAdd = 0; + private ?int $dateEdit = 0; + private ?int $countView = 0; + private ?int $countDecrypt = 0; + private ?int $isPrivate = 0; + private ?int $isPrivateGroup = 0; + private ?int $passDate = 0; + private ?int $passDateChange = 0; + private ?int $parentId = 0; + + public function __construct(int $accountId = 0, ?array $properties = []) + { + parent::__construct($properties); + + $this->id = $accountId; + } + + public function getDateAdd(): ?int + { + return $this->dateAdd; + } + + public function getDateEdit(): ?int + { + return $this->dateEdit; + } + + public function getUserEditId(): ?int + { + return $this->userEditId; + } + + public function getPass(): ?string + { + return $this->pass; + } + + public function setPass(string $pass) + { + $this->pass = $pass; + } + + public function getKey(): ?string + { + return $this->key; + } + + public function setKey(string $key) + { + $this->key = $key; + } + + public function getId(): ?int + { + return $this->id; + } + + public function getUserId(): ?int + { + return $this->userId; + } + + public function getUserGroupId(): ?int + { + return $this->userGroupId; + } + + public function getName(): ?string + { + return $this->name; + } + + public function getCategoryId(): ?int + { + return $this->categoryId; + } + + public function getClientId(): ?int + { + return $this->clientId; + } + + public function getLogin(): ?string + { + return $this->login; + } + + public function getUrl(): ?string + { + return $this->url; + } + + public function getNotes(): ?string + { + return $this->notes; + } + + /** + * Specify data which should be serialized to JSON + * + * @link http://php.net/manual/en/jsonserializable.jsonserialize.php + * @return mixed data which can be serialized by json_encode, + * which is a value of any type other than a resource. + * @since 5.4.0 + */ + public function jsonSerialize() + { + $data = get_object_vars($this); + + unset($data['accountPass'], $data['accountIV']); + + return Json::safeJson($data); + } + + public function getCountView(): ?int + { + return $this->countView; + } + + public function getCountDecrypt(): ?int + { + return $this->countDecrypt; + } + + public function getIsPrivate(): ?int + { + return $this->isPrivate; + } + + public function getPassDate(): ?int + { + return $this->passDate; + } + + public function getPassDateChange(): ?int + { + return $this->passDateChange; + } + + public function getParentId(): ?int + { + return $this->parentId; + } + + public function getIsPrivateGroup(): ?int + { + return $this->isPrivateGroup; + } +} \ No newline at end of file diff --git a/lib/SP/Domain/Account/Out/AccountPassData.php b/lib/SP/Domain/Account/Out/AccountPassData.php new file mode 100644 index 00000000..604d0671 --- /dev/null +++ b/lib/SP/Domain/Account/Out/AccountPassData.php @@ -0,0 +1,77 @@ +. + */ + +namespace SP\Domain\Account\Out; + +use SP\Domain\Common\Out\DataModelBase; +use SP\Domain\Common\Out\DataModelInterface; + +/** + * Class AccountPassData + */ +class AccountPassData extends DataModelBase implements DataModelInterface +{ + private ?int $id = null; + private ?string $name = null; + private ?string $login = null; + private ?string $pass = null; + private ?string $key = null; + private ?int $parentId = null; + private ?string $mPassHash = null; + + public function getId(): ?int + { + return $this->id; + } + + public function getName(): ?string + { + return $this->name; + } + + public function getLogin(): ?string + { + return $this->login; + } + + public function getPass(): ?string + { + return $this->pass; + } + + public function getKey(): ?string + { + return $this->key; + } + + public function getParentId(): ?int + { + return $this->parentId; + } + + public function getMPassHash(): ?string + { + return $this->mPassHash; + } +} \ No newline at end of file diff --git a/lib/SP/Domain/Account/Services/AccountService.php b/lib/SP/Domain/Account/Services/AccountService.php index 95dfd4d2..25e539d9 100644 --- a/lib/SP/Domain/Account/Services/AccountService.php +++ b/lib/SP/Domain/Account/Services/AccountService.php @@ -31,10 +31,8 @@ use SP\Core\Exceptions\ConstraintException; use SP\Core\Exceptions\NoSuchPropertyException; use SP\Core\Exceptions\QueryException; use SP\Core\Exceptions\SPException; -use SP\DataModel\AccountData; use SP\DataModel\AccountExtData; use SP\DataModel\AccountHistoryData; -use SP\DataModel\AccountPassData; use SP\DataModel\Dto\AccountDetailsResponse; use SP\DataModel\Dto\AccountHistoryCreateDto; use SP\DataModel\ItemPreset\AccountPermission; @@ -47,6 +45,8 @@ use SP\Domain\Account\In\AccountRepositoryInterface; use SP\Domain\Account\In\AccountToTagRepositoryInterface; use SP\Domain\Account\In\AccountToUserGroupRepositoryInterface; use SP\Domain\Account\In\AccountToUserRepositoryInterface; +use SP\Domain\Account\Out\AccountData; +use SP\Domain\Account\Out\AccountPassData; use SP\Domain\Common\Services\Service; use SP\Domain\Common\Services\ServiceException; use SP\Domain\Common\Services\ServiceItemTrait; diff --git a/lib/SP/Domain/Common/Out/DataModelBase.php b/lib/SP/Domain/Common/Out/DataModelBase.php new file mode 100644 index 00000000..ce0c5243 --- /dev/null +++ b/lib/SP/Domain/Common/Out/DataModelBase.php @@ -0,0 +1,71 @@ +. + */ + +namespace SP\Domain\Common\Out; + +/** + * Class DataModelBase + */ +abstract class DataModelBase +{ + private array $properties; + + public function __construct(?array $properties = []) + { + foreach ($properties as $property => $value) { + $this->{$property} = $value; + } + } + + /** + * @param string $name + * + * @return mixed|null + */ + public function __get(string $name) + { + if (property_exists($this, $name)) { + return $this->{$name}; + } + + if (array_key_exists($name, $this->properties)) { + return $this->properties[$name]; + } + + return null; + } + + public function __set(string $name, ?string $value = null): void + { + if (is_numeric($value)) { + $value = (int)$value; + } + + if (property_exists($this, $name)) { + $this->{$name} = $value; + } else { + $this->properties[$name] = $value; + } + } +} \ No newline at end of file diff --git a/lib/SP/DataModel/DataModelInterface.php b/lib/SP/Domain/Common/Out/DataModelInterface.php similarity index 83% rename from lib/SP/DataModel/DataModelInterface.php rename to lib/SP/Domain/Common/Out/DataModelInterface.php index ec590ece..7130ce1c 100644 --- a/lib/SP/DataModel/DataModelInterface.php +++ b/lib/SP/Domain/Common/Out/DataModelInterface.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -22,7 +22,7 @@ * along with sysPass. If not, see . */ -namespace SP\DataModel; +namespace SP\Domain\Common\Out; /** @@ -32,7 +32,7 @@ namespace SP\DataModel; */ interface DataModelInterface { - public function getId(); + public function getId(): ?int; - public function getName(); + public function getName(): ?string; } \ No newline at end of file diff --git a/lib/SP/DataModel/HydratableInterface.php b/lib/SP/Domain/Common/Out/HydratableInterface.php similarity index 85% rename from lib/SP/DataModel/HydratableInterface.php rename to lib/SP/Domain/Common/Out/HydratableInterface.php index 02a46b75..fc7a0700 100644 --- a/lib/SP/DataModel/HydratableInterface.php +++ b/lib/SP/Domain/Common/Out/HydratableInterface.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -22,7 +22,7 @@ * along with sysPass. If not, see . */ -namespace SP\DataModel; +namespace SP\Domain\Common\Out; /** * Interface HydratableInterface @@ -32,9 +32,8 @@ namespace SP\DataModel; interface HydratableInterface { /** - * @param string $class - * - * @param string $property + * @param string|null $class + * @param string $property * * @return mixed|null */ diff --git a/lib/SP/DataModel/DataModelBase.php b/lib/SP/Domain/Common/Out/SimpleModel.php similarity index 57% rename from lib/SP/DataModel/DataModelBase.php rename to lib/SP/Domain/Common/Out/SimpleModel.php index cb68758c..924e0936 100644 --- a/lib/SP/DataModel/DataModelBase.php +++ b/lib/SP/Domain/Common/Out/SimpleModel.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -22,30 +22,16 @@ * along with sysPass. If not, see . */ -namespace SP\DataModel; +namespace SP\Domain\Common\Out; /** - * Class DataModelBase + * Class SimpleModel * - * @package SP\DataModel + * This model does not contain any properties, they will be set on $properties property. + * + * It's intended to be used when returned non-well defined objects from the repository. */ -abstract class DataModelBase +final class SimpleModel extends DataModelBase { - /** - * is utilized for reading data from inaccessible members. - * - * @param $name string - * - * @return mixed - * @link https://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members - */ - public function __get($name) - { - if (property_exists($this, $name)) { - return $this->{$name}; - } - - return null; - } } \ No newline at end of file diff --git a/lib/SP/Infrastructure/Account/Repositories/AccountRepository.php b/lib/SP/Infrastructure/Account/Repositories/AccountRepository.php index 04e3be3e..9446621c 100644 --- a/lib/SP/Infrastructure/Account/Repositories/AccountRepository.php +++ b/lib/SP/Infrastructure/Account/Repositories/AccountRepository.php @@ -28,17 +28,18 @@ use RuntimeException; use SP\Core\Exceptions\ConstraintException; use SP\Core\Exceptions\QueryException; use SP\Core\Exceptions\SPException; -use SP\DataModel\AccountData; use SP\DataModel\AccountExtData; -use SP\DataModel\AccountPassData; use SP\DataModel\AccountSearchVData; use SP\DataModel\AccountVData; use SP\DataModel\ItemData; use SP\DataModel\ItemSearchData; use SP\Domain\Account\In\AccountRepositoryInterface; +use SP\Domain\Account\Out\AccountData; +use SP\Domain\Account\Out\AccountPassData; use SP\Domain\Account\Services\AccountPasswordRequest; use SP\Domain\Account\Services\AccountRequest; use SP\Domain\Account\Services\AccountSearchFilter; +use SP\Domain\Common\Out\SimpleModel; use SP\Infrastructure\Common\Repositories\Repository; use SP\Infrastructure\Common\Repositories\RepositoryItemTrait; use SP\Infrastructure\Database\QueryData; @@ -46,7 +47,6 @@ use SP\Infrastructure\Database\QueryResult; use SP\Mvc\Model\QueryAssignment; use SP\Mvc\Model\QueryCondition; use SP\Mvc\Model\QueryJoin; -use stdClass; /** * Class AccountRepository @@ -59,18 +59,15 @@ final class AccountRepository extends Repository implements AccountRepositoryInt /** * Devolver el número total de cuentas - * - * @return stdClass - * @throws QueryException - * @throws ConstraintException */ - public function getTotalNumAccounts(): stdClass + public function getTotalNumAccounts(): SimpleModel { $query = /** @lang SQL */ 'SELECT SUM(n) AS num FROM (SELECT COUNT(*) AS n FROM Account UNION SELECT COUNT(*) AS n FROM AccountHistory) a'; $queryData = new QueryData(); + $queryData->setMapClassName(SimpleModel::class); $queryData->setQuery($query); return $this->db->doSelect($queryData)->getData(); @@ -81,8 +78,6 @@ final class AccountRepository extends Repository implements AccountRepositoryInt * @param QueryCondition $queryCondition * * @return QueryResult - * @throws \SP\Core\Exceptions\ConstraintException - * @throws \SP\Core\Exceptions\QueryException */ public function getPasswordForId(int $id, QueryCondition $queryCondition): QueryResult { @@ -103,8 +98,6 @@ final class AccountRepository extends Repository implements AccountRepositoryInt * @param QueryCondition $queryCondition * * @return QueryResult - * @throws ConstraintException - * @throws QueryException */ public function getPasswordHistoryForId(QueryCondition $queryCondition): QueryResult { diff --git a/lib/SP/Infrastructure/Common/Repositories/RepositoryItemTrait.php b/lib/SP/Infrastructure/Common/Repositories/RepositoryItemTrait.php index 195c0b7b..539ba80f 100644 --- a/lib/SP/Infrastructure/Common/Repositories/RepositoryItemTrait.php +++ b/lib/SP/Infrastructure/Common/Repositories/RepositoryItemTrait.php @@ -26,8 +26,8 @@ namespace SP\Infrastructure\Common\Repositories; use Exception; use RuntimeException; -use SP\DataModel\DataModelInterface; use SP\Domain\Common\In\RepositoryInterface; +use SP\Domain\Common\Out\DataModelInterface; use SP\Infrastructure\Database\DatabaseUtil; use SP\Infrastructure\Database\DbStorageInterface; diff --git a/lib/SP/Infrastructure/Database/Database.php b/lib/SP/Infrastructure/Database/Database.php index 02990889..eb2e837f 100644 --- a/lib/SP/Infrastructure/Database/Database.php +++ b/lib/SP/Infrastructure/Database/Database.php @@ -91,16 +91,10 @@ final class Database implements DatabaseInterface * @throws ConstraintException * @throws QueryException */ - public function doSelect( - QueryData $queryData, - bool $fullCount = false - ): QueryResult { + public function doSelect(QueryData $queryData, bool $fullCount = false): QueryResult + { if ($queryData->getQuery() === '') { - throw new QueryException( - $queryData->getOnErrorMessage(), - SPException::ERROR, - __u('Blank query') - ); + throw new QueryException($queryData->getOnErrorMessage(), SPException::ERROR, __u('Blank query')); } try { @@ -140,11 +134,7 @@ final class Database implements DatabaseInterface $this->eventDispatcher->notifyEvent( 'database.query', - new Event( - $this, - EventMessage::factory() - ->addDescription($queryData->getQuery()) - ) + new Event($this, EventMessage::factory()->addDescription($queryData->getQuery())) ); if (preg_match("/^(select|show)\s/i", $queryData->getQuery())) { diff --git a/lib/SP/Infrastructure/Database/DatabaseInterface.php b/lib/SP/Infrastructure/Database/DatabaseInterface.php index c8ea663b..2f036f1d 100644 --- a/lib/SP/Infrastructure/Database/DatabaseInterface.php +++ b/lib/SP/Infrastructure/Database/DatabaseInterface.php @@ -35,6 +35,8 @@ use SP\Core\Exceptions\QueryException; */ interface DatabaseInterface { + public function doSelect(QueryData $queryData, bool $fullCount = false): QueryResult; + /** * Performs a DB query * diff --git a/lib/SP/Infrastructure/Plugin/Repositories/PluginDataModel.php b/lib/SP/Infrastructure/Plugin/Repositories/PluginDataModel.php index 99663afc..999b6886 100644 --- a/lib/SP/Infrastructure/Plugin/Repositories/PluginDataModel.php +++ b/lib/SP/Infrastructure/Plugin/Repositories/PluginDataModel.php @@ -25,8 +25,8 @@ namespace SP\Infrastructure\Plugin\Repositories; use SP\DataModel\EncryptedModel; -use SP\DataModel\HydratableInterface; use SP\DataModel\SerializedModel; +use SP\Domain\Common\Out\HydratableInterface; /** * Class PluginData diff --git a/lib/SP/Infrastructure/Plugin/Repositories/PluginModel.php b/lib/SP/Infrastructure/Plugin/Repositories/PluginModel.php index f834d1e0..0aecff6c 100644 --- a/lib/SP/Infrastructure/Plugin/Repositories/PluginModel.php +++ b/lib/SP/Infrastructure/Plugin/Repositories/PluginModel.php @@ -24,8 +24,8 @@ namespace SP\Infrastructure\Plugin\Repositories; -use SP\DataModel\DataModelBase; -use SP\DataModel\DataModelInterface; +use SP\Domain\Common\Out\DataModelBase; +use SP\Domain\Common\Out\DataModelInterface; /** * Class PluginData diff --git a/lib/SP/Mvc/View/Components/SelectItemAdapter.php b/lib/SP/Mvc/View/Components/SelectItemAdapter.php index dec59e6f..64975fcb 100644 --- a/lib/SP/Mvc/View/Components/SelectItemAdapter.php +++ b/lib/SP/Mvc/View/Components/SelectItemAdapter.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2021, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -26,7 +26,7 @@ namespace SP\Mvc\View\Components; use RuntimeException; use SP\Core\Exceptions\SPException; -use SP\DataModel\DataModelInterface; +use SP\Domain\Common\Out\DataModelInterface; use SP\Http\Json; /** diff --git a/tests/SP/Repositories/AccountRepositoryTest.php b/tests/SP/Repositories/AccountRepositoryTest.php index 09e5cd99..7043ff17 100644 --- a/tests/SP/Repositories/AccountRepositoryTest.php +++ b/tests/SP/Repositories/AccountRepositoryTest.php @@ -1,10 +1,10 @@ . + * along with sysPass. If not, see . */ namespace SP\Tests\Repositories; -use Defuse\Crypto\Exception\CryptoException; -use DI\DependencyException; -use DI\NotFoundException; -use SP\Core\Context\ContextException; -use SP\Core\Crypt\Crypt; -use SP\Core\Exceptions\ConstraintException; -use SP\Core\Exceptions\QueryException; -use SP\Core\Exceptions\SPException; -use SP\DataModel\AccountData; -use SP\DataModel\AccountSearchVData; +use PHPUnit\Framework\Constraint\Callback; use SP\DataModel\AccountVData; -use SP\DataModel\ItemSearchData; -use SP\Domain\Account\In\AccountRepositoryInterface; +use SP\Domain\Account\Out\AccountPassData; use SP\Domain\Account\Services\AccountPasswordRequest; use SP\Domain\Account\Services\AccountRequest; -use SP\Domain\Account\Services\AccountSearchFilter; +use SP\Domain\Common\Out\SimpleModel; use SP\Infrastructure\Account\Repositories\AccountRepository; +use SP\Infrastructure\Database\DatabaseInterface; +use SP\Infrastructure\Database\QueryData; +use SP\Infrastructure\Database\QueryResult; use SP\Mvc\Model\QueryCondition; -use SP\Tests\DatabaseTestCase; -use stdClass; -use function SP\Tests\setupContext; +use SP\Tests\UnitaryTestCase; /** * Class AccountRepositoryTest @@ -53,599 +44,444 @@ use function SP\Tests\setupContext; * * @package SP\Tests */ -class AccountRepositoryTest extends DatabaseTestCase +class AccountRepositoryTest extends UnitaryTestCase { - const SECURE_KEY_PASSWORD = 'syspass123'; - /** - * @var AccountRepositoryInterface - */ - private static $repository; + private DatabaseInterface $databaseInterface; + private AccountRepository $accountRepository; /** - * @throws DependencyException - * @throws NotFoundException - * @throws ContextException + * @noinspection ClassMockingCorrectnessInspection */ - public static function setUpBeforeClass(): void + protected function setUp(): void { - $dic = setupContext(); + parent::setUp(); - self::$loadFixtures = true; - - // Inicializar el repositorio - self::$repository = $dic->get(AccountRepository::class); + $this->databaseInterface = $this->createMock(DatabaseInterface::class); + $this->accountRepository = new AccountRepository($this->databaseInterface, $this->context); } - /** - * Comprobar la eliminación de registros - * - * @throws SPException - */ - public function testDelete() + public function testGetTotalNumAccounts(): void { - // Comprobar registros iniciales - $this->assertEquals(4, self::getRowCount('Account')); + $expected = new QueryResult([new SimpleModel(['num' => 1])]); - // Eliminar un registro y comprobar el total de registros - $this->assertEquals(1, self::$repository->delete(1)); - $this->assertEquals(3, self::getRowCount('Account')); + $callback = new Callback( + static function (QueryData $arg) { + return $arg->getMapClassName() === SimpleModel::class && !empty($arg->getQuery()); + } + ); - // Eliminar un registro no existente - $this->assertEquals(0, self::$repository->delete(100)); + $this->databaseInterface->expects(self::once()) + ->method('doSelect') + ->with($callback, false) + ->willReturn($expected); - // Eliminar un registro y comprobar el total de registros - $this->assertEquals(1, self::$repository->delete(2)); - $this->assertEquals(2, self::getRowCount('Account')); + $this->assertEquals($expected->getData(), $this->accountRepository->getTotalNumAccounts()); } - /** - * @throws ConstraintException - * @throws QueryException - */ - public function testEditRestore() + public function testGetPasswordForId(): void { - $this->assertTrue(self::$repository->editRestore(3, 1)); + $expected = new QueryResult(); - $this->assertEquals(5, self::getRowCount('AccountHistory')); + $callback = new Callback( + static function (QueryData $arg) { + return $arg->getMapClassName() === AccountPassData::class && !empty($arg->getQuery()); + } + ); - $this->assertEquals(0, self::$repository->editRestore(1, 1)); + $this->databaseInterface->expects(self::once()) + ->method('doSelect') + ->with($callback, false) + ->willReturn($expected); + + $this->assertEquals($expected, $this->accountRepository->getPasswordForId(1, new QueryCondition())); } - /** - * @throws ConstraintException - * @throws QueryException - */ - public function testEditRestoreUnknownUser() + public function testGetPasswordHistoryForId(): void { - $this->expectException(ConstraintException::class); + $expected = new QueryResult(); - self::$repository->editRestore(3, 10); + $callback = new Callback( + static function (QueryData $arg) { + return $arg->getMapClassName() === AccountPassData::class && !empty($arg->getQuery()); + } + ); + + $this->databaseInterface->expects(self::once()) + ->method('doSelect') + ->with($callback, false) + ->willReturn($expected); + + $this->assertEquals($expected, $this->accountRepository->getPasswordHistoryForId(new QueryCondition())); } - /** - * Comprobar la modificación de una clave de cuenta - * - * @throws SPException - * @throws CryptoException - * @throws ConstraintException - */ - public function testEditPassword() + public function testIncrementDecryptCounter(): void + { + $id = 1; + + $expected = new QueryResult(); + $expected->setAffectedNumRows(1); + + $callback = new Callback( + function (QueryData $arg) use ($id) { + $params = $arg->getParams(); + + return $params[0] === $id && !empty($arg->getQuery()); + } + ); + + $this->databaseInterface->expects(self::once()) + ->method('doQuery') + ->with($callback) + ->willReturn($expected); + + $this->assertTrue($this->accountRepository->incrementDecryptCounter($id)); + } + + public function testIncrementDecryptCounterNoRows(): void + { + $id = 1; + + $expected = new QueryResult(); + $expected->setAffectedNumRows(0); + + $callback = new Callback( + function (QueryData $arg) use ($id) { + return $arg->getParams()[0] === $id && !empty($arg->getQuery()); + } + ); + + $this->databaseInterface->expects(self::once()) + ->method('doQuery') + ->with($callback) + ->willReturn($expected); + + $this->assertFalse($this->accountRepository->incrementDecryptCounter($id)); + } + + public function testCreate(): void + { + $accountRequest = $this->buildAccountRequest(); + + $expected = new QueryResult(); + $expected->setLastId(1); + + $callback = new Callback( + function (QueryData $arg) use ($accountRequest) { + $params = $arg->getParams(); + + return $params[0] === $accountRequest->clientId + && $params[1] === $accountRequest->categoryId + && $params[2] === $accountRequest->name + && $params[3] === $accountRequest->login + && $params[4] === $accountRequest->url + && $params[5] === $accountRequest->pass + && $params[6] === $accountRequest->key + && $params[7] === $accountRequest->notes + && $params[8] === $accountRequest->userId + && $params[9] === $accountRequest->userGroupId + && $params[10] === $accountRequest->userId + && $params[11] === $accountRequest->isPrivate + && $params[12] === $accountRequest->isPrivateGroup + && $params[13] === $accountRequest->passDateChange + && $params[14] === $accountRequest->parentId + && !empty($arg->getQuery()); + } + ); + + $this->databaseInterface->expects(self::once()) + ->method('doQuery') + ->with($callback) + ->willReturn($expected); + + $this->assertEquals($expected->getLastId(), $this->accountRepository->create($accountRequest)); + } + + private function buildAccountRequest(): AccountRequest { $accountRequest = new AccountRequest(); - $accountRequest->key = Crypt::makeSecuredKey(self::SECURE_KEY_PASSWORD); - $accountRequest->pass = Crypt::encrypt('1234', $accountRequest->key, self::SECURE_KEY_PASSWORD); - $accountRequest->id = 2; - $accountRequest->userEditId = 1; - $accountRequest->passDateChange = time() + 3600; + $accountRequest->id = self::$faker->randomNumber(); + $accountRequest->name = self::$faker->name; + $accountRequest->login = self::$faker->userName; + $accountRequest->url = self::$faker->url; + $accountRequest->notes = self::$faker->text; + $accountRequest->userEditId = self::$faker->randomNumber(); + $accountRequest->passDateChange = self::$faker->unixTime; + $accountRequest->clientId = self::$faker->randomNumber(); + $accountRequest->categoryId = self::$faker->randomNumber(); + $accountRequest->isPrivate = self::$faker->numberBetween(0, 1); + $accountRequest->isPrivateGroup = self::$faker->numberBetween(0, 1); + $accountRequest->parentId = self::$faker->randomNumber(); + $accountRequest->userId = self::$faker->randomNumber(); + $accountRequest->userGroupId = self::$faker->randomNumber(); + $accountRequest->key = self::$faker->text; + $accountRequest->pass = self::$faker->text; - // Comprobar que la modificación de la clave es correcta - $this->assertEquals(1, self::$repository->editPassword($accountRequest)); - - $accountPassData = self::$repository->getPasswordForId(2, new QueryCondition())->getData(); - $clearPassword = Crypt::decrypt($accountPassData->pass, $accountPassData->key, self::SECURE_KEY_PASSWORD); - - // Comprobar que la clave obtenida es igual a la encriptada anteriormente - $this->assertEquals('1234', $clearPassword); - - // Comprobar que no devuelve resultados - $this->assertEquals(0, self::$repository->getPasswordForId(10, new QueryCondition())->getNumRows()); + return $accountRequest; } - /** - * Comprobar la obtención de cuentas - * - * @throws SPException - */ - public function testGetById() + public function testEditPassword(): void { - $result = self::$repository->getById(1); + $accountRequest = $this->buildAccountRequest(); - $this->assertEquals(1, $result->getNumRows()); + $expected = new QueryResult(); + $expected->setAffectedNumRows(1); - /** @var AccountVData $data */ - $data = $result->getData(); + $callback = new Callback( + function (QueryData $arg) use ($accountRequest) { + $params = $arg->getParams(); - $this->assertInstanceOf(AccountVData::class, $data); - $this->assertEquals(1, $data->getId()); + return $params[0] === $accountRequest->pass + && $params[1] === $accountRequest->key + && $params[2] === $accountRequest->userEditId + && $params[3] === $accountRequest->passDateChange + && $params[4] === $accountRequest->id + && !empty($arg->getQuery()); + } + ); - $this->assertEquals(0, self::$repository->getById(10)->getNumRows()); + $this->databaseInterface->expects(self::once()) + ->method('doQuery') + ->with($callback) + ->willReturn($expected); + + $this->assertEquals($expected->getAffectedNumRows(), $this->accountRepository->editPassword($accountRequest)); } - /** - * @throws SPException - */ - public function testUpdate() + public function testUpdatePassword(): void { - $accountRequest = new AccountRequest(); - $accountRequest->id = 1; - $accountRequest->name = 'Prueba 1'; - $accountRequest->login = 'admin'; - $accountRequest->url = 'http://syspass.org'; - $accountRequest->notes = 'notas'; - $accountRequest->userEditId = 1; - $accountRequest->passDateChange = time() + 3600; - $accountRequest->clientId = 1; - $accountRequest->categoryId = 1; - $accountRequest->isPrivate = 0; - $accountRequest->isPrivateGroup = 0; - $accountRequest->parentId = 0; - $accountRequest->userGroupId = 2; + $accountPasswordRequest = new AccountPasswordRequest(); + $accountPasswordRequest->pass = self::$faker->password; + $accountPasswordRequest->key = self::$faker->password; + $accountPasswordRequest->id = self::$faker->randomNumber(); - $this->assertEquals(1, self::$repository->update($accountRequest)); + $expected = new QueryResult(); + $expected->setAffectedNumRows(1); - $result = self::$repository->getById(1); + $callback = new Callback( + function (QueryData $arg) use ($accountPasswordRequest) { + $params = $arg->getParams(); - $this->assertEquals(1, $result->getNumRows()); + return $params[0] === $accountPasswordRequest->pass + && $params[1] === $accountPasswordRequest->key + && $params[2] === $accountPasswordRequest->id + && !empty($arg->getQuery()); + } + ); - /** @var AccountVData $data */ - $data = $result->getData(); + $this->databaseInterface->expects(self::once()) + ->method('doQuery') + ->with($callback) + ->willReturn($expected); - $this->assertEquals(1, $data->getId()); - $this->assertEquals($accountRequest->name, $data->getName()); - $this->assertEquals($accountRequest->login, $data->getLogin()); - $this->assertEquals($accountRequest->url, $data->getUrl()); - $this->assertEquals($accountRequest->notes, $data->getNotes()); - $this->assertEquals($accountRequest->userEditId, $data->getUserEditId()); - $this->assertEquals($accountRequest->passDateChange, $data->getPassDateChange()); - $this->assertEquals($accountRequest->clientId, $data->getClientId()); - $this->assertEquals($accountRequest->categoryId, $data->getCategoryId()); - $this->assertEquals($accountRequest->isPrivate, $data->getIsPrivate()); - $this->assertEquals($accountRequest->isPrivateGroup, $data->getIsPrivateGroup()); - $this->assertEquals($accountRequest->parentId, $data->getParentId()); - - // El grupo no debe de cambiar si el usuario no tiene permisos - $this->assertNotEquals($accountRequest->userGroupId, $data->getUserGroupId()); - $this->assertEquals(1, $data->getUserGroupId()); + $this->assertTrue($this->accountRepository->updatePassword($accountPasswordRequest)); } - /** - * Not implemented - */ - public function testCheckDuplicatedOnAdd() + public function testEditRestore(): void { - $this->markTestSkipped('Not implemented'); + $historyId = 1; + $userId = 1; + + $expected = new QueryResult(); + $expected->setAffectedNumRows(1); + + $callback = new Callback( + function (QueryData $arg) use ($historyId, $userId) { + $params = $arg->getParams(); + + return $params[0] === $historyId + && $params[1] === $userId + && !empty($arg->getQuery()); + } + ); + + $this->databaseInterface->expects(self::once()) + ->method('doQuery') + ->with($callback) + ->willReturn($expected); + + $this->assertTrue($this->accountRepository->editRestore($historyId, $userId)); } - /** - * Comprobar la eliminación en lotes - * - * @throws ConstraintException - * @throws QueryException - */ - public function testDeleteByIdBatch() + public function testDelete(): void { - // Comprobar registros iniciales - $this->assertEquals(4, self::getRowCount('Account')); + $id = 1; + $expected = new QueryResult(); + $expected->setAffectedNumRows(1); - $this->assertEquals(2, self::$repository->deleteByIdBatch([1, 2, 100])); + $callback = new Callback( + function (QueryData $arg) use ($id) { + $params = $arg->getParams(); - // Comprobar registros tras eliminación - $this->assertEquals(2, self::getRowCount('Account')); + return $params[0] === $id + && !empty($arg->getQuery()); + } + ); + + $this->databaseInterface->expects(self::once()) + ->method('doQuery') + ->with($callback) + ->willReturn($expected); + + $this->assertEquals($expected->getAffectedNumRows(), $this->accountRepository->delete($id)); } - /** - * Comprobar la búsqueda de cuentas - * - * @throws ConstraintException - * @throws QueryException - */ - public function testSearch() + public function testUpdate(): void { - // Comprobar búsqueda con el texto Google Inc - $itemSearchData = new ItemSearchData(); - $itemSearchData->setSeachString('Google'); - $itemSearchData->setLimitCount(10); + $accountRequest = $this->buildAccountRequest(); - $result = self::$repository->search($itemSearchData); - $data = $result->getDataAsArray(); + $expected = new QueryResult(); + $expected->setAffectedNumRows(1); - $this->assertCount(1, $data); - $this->assertEquals(1, $result->getNumRows()); - $this->assertInstanceOf(stdClass::class, $data[0]); - $this->assertEquals(1, $data[0]->id); - $this->assertEquals('Google', $data[0]->name); + $callback = new Callback( + function (QueryData $arg) use ($accountRequest) { + $params = $arg->getParams(); - // Comprobar búsqueda con el texto Apple - $itemSearchData = new ItemSearchData(); - $itemSearchData->setSeachString('Apple'); - $itemSearchData->setLimitCount(1); + return $params[0] === $accountRequest->clientId + && $params[1] === $accountRequest->categoryId + && $params[2] === $accountRequest->name + && $params[3] === $accountRequest->login + && $params[4] === $accountRequest->url + && $params[5] === $accountRequest->notes + && $params[6] === $accountRequest->userEditId + && $params[7] === $accountRequest->passDateChange + && $params[8] === $accountRequest->isPrivate + && $params[9] === $accountRequest->isPrivateGroup + && $params[10] === $accountRequest->parentId + && !empty($arg->getQuery()); + } + ); - $result = self::$repository->search($itemSearchData); - $data = $result->getDataAsArray(); + $this->databaseInterface->expects(self::once()) + ->method('doQuery') + ->with($callback) + ->willReturn($expected); - $this->assertCount(1, $data); - $this->assertEquals(1, $result->getNumRows()); - $this->assertInstanceOf(stdClass::class, $data[0]); - $this->assertEquals(2, $data[0]->id); - $this->assertEquals('Apple', $data[0]->name); + $this->assertEquals($expected->getAffectedNumRows(), $this->accountRepository->update($accountRequest)); } - /** - * Comprobar las cuentas enlazadas - * - * @throws ConstraintException - * @throws QueryException - */ - public function testGetLinked() + public function testUpdateWithChangeGroup(): void { - $filter = new QueryCondition(); - $filter->addFilter('Account.parentId = 1'); + $accountRequest = $this->buildAccountRequest(); + $accountRequest->changeUserGroup = true; - $this->assertEquals(1, self::$repository->getLinked($filter)->getNumRows()); + $expected = new QueryResult(); + $expected->setAffectedNumRows(1); + + $callback = new Callback( + function (QueryData $arg) use ($accountRequest) { + $params = $arg->getParams(); + + return $params[0] === $accountRequest->clientId + && $params[1] === $accountRequest->categoryId + && $params[2] === $accountRequest->name + && $params[3] === $accountRequest->login + && $params[4] === $accountRequest->url + && $params[5] === $accountRequest->notes + && $params[6] === $accountRequest->userEditId + && $params[7] === $accountRequest->passDateChange + && $params[8] === $accountRequest->isPrivate + && $params[9] === $accountRequest->isPrivateGroup + && $params[10] === $accountRequest->parentId + && $params[11] === $accountRequest->userGroupId + && !empty($arg->getQuery()); + } + ); + + $this->databaseInterface->expects(self::once()) + ->method('doQuery') + ->with($callback) + ->willReturn($expected); + + $this->assertEquals($expected->getAffectedNumRows(), $this->accountRepository->update($accountRequest)); } - /** - * Comprobar en incremento del contador de vistas - * - * @throws ConstraintException - * @throws QueryException - * @throws SPException - */ - public function testIncrementViewCounter() + public function testUpdateWithChangeOwner(): void { - /** @var AccountVData $accountBefore */ - $accountBefore = self::$repository->getById(1)->getData(); + $accountRequest = $this->buildAccountRequest(); + $accountRequest->changeOwner = true; - $this->assertTrue(self::$repository->incrementViewCounter(1)); + $expected = new QueryResult(); + $expected->setAffectedNumRows(1); - /** @var AccountVData $accountAfter */ - $accountAfter = self::$repository->getById(1)->getData(); + $callback = new Callback( + function (QueryData $arg) use ($accountRequest) { + $params = $arg->getParams(); - $this->assertEquals($accountBefore->getCountView() + 1, $accountAfter->getCountView()); + return $params[0] === $accountRequest->clientId + && $params[1] === $accountRequest->categoryId + && $params[2] === $accountRequest->name + && $params[3] === $accountRequest->login + && $params[4] === $accountRequest->url + && $params[5] === $accountRequest->notes + && $params[6] === $accountRequest->userEditId + && $params[7] === $accountRequest->passDateChange + && $params[8] === $accountRequest->isPrivate + && $params[9] === $accountRequest->isPrivateGroup + && $params[10] === $accountRequest->parentId + && $params[11] === $accountRequest->userId + && !empty($arg->getQuery()); + } + ); + + $this->databaseInterface->expects(self::once()) + ->method('doQuery') + ->with($callback) + ->willReturn($expected); + + $this->assertEquals($expected->getAffectedNumRows(), $this->accountRepository->update($accountRequest)); } - /** - * Obtener todas las cuentas - * - * @throws ConstraintException - * @throws QueryException - */ - public function testGetAll() - { - $result = self::$repository->getAll(); - - $this->assertEquals(4, $result->getNumRows()); - - /** @var AccountData[] $data */ - $data = $result->getDataAsArray(); - - $this->assertCount(4, $data); - $this->assertInstanceOf(AccountData::class, $data[0]); - $this->assertEquals(1, $data[0]->getId()); - $this->assertInstanceOf(AccountData::class, $data[1]); - $this->assertEquals(2, $data[1]->getId()); - $this->assertInstanceOf(AccountData::class, $data[2]); - $this->assertEquals(3, $data[2]->getId()); - $this->assertInstanceOf(AccountData::class, $data[3]); - $this->assertEquals(4, $data[3]->getId()); - } - - /** - * @throws SPException - * @throws CryptoException - * @throws ConstraintException - */ - public function testUpdatePassword() - { - $accountRequest = new AccountPasswordRequest(); - $accountRequest->id = 2; - $accountRequest->key = Crypt::makeSecuredKey(self::SECURE_KEY_PASSWORD); - $accountRequest->pass = Crypt::encrypt('1234', $accountRequest->key, self::SECURE_KEY_PASSWORD); - - // Comprobar que la modificación de la clave es correcta - $this->assertTrue(self::$repository->updatePassword($accountRequest)); - - $accountPassData = self::$repository->getPasswordForId(2, new QueryCondition())->getData(); - $clearPassword = Crypt::decrypt($accountPassData->pass, $accountPassData->key, self::SECURE_KEY_PASSWORD); - - // Comprobar que la clave obtenida es igual a la encriptada anteriormente - $this->assertEquals('1234', $clearPassword); - } - - /** - * Comprobar en incremento del contador de desencriptado - * - * @throws SPException - * @throws ConstraintException - * @throws QueryException - */ - public function testIncrementDecryptCounter() - { - /** @var AccountVData $accountBefore */ - $accountBefore = self::$repository->getById(1)->getData(); - - $this->assertTrue(self::$repository->incrementDecryptCounter(1)); - - /** @var AccountVData $accountAfter */ - $accountAfter = self::$repository->getById(1)->getData(); - - $this->assertEquals($accountBefore->getCountDecrypt() + 1, $accountAfter->getCountDecrypt()); - } - - /** - * Comprobar el número total de cuentas - * - * @throws ConstraintException - * @throws QueryException - */ - public function testGetTotalNumAccounts() - { - $this->assertEquals(9, self::$repository->getTotalNumAccounts()->num); - } - - /** - * @throws ConstraintException - * @throws QueryException - */ - public function testGetDataForLink() - { - $result = self::$repository->getDataForLink(1); - - $this->assertEquals(1, $result->getNumRows()); - - $data = $result->getData(); - - $this->assertEquals(1, $data->getId()); - $this->assertEquals(1, $data->getId()); - $this->assertEquals('Google', $data->getName()); - $this->assertEquals('admin', $data->getLogin()); - $this->assertEquals(pack('H*', '6465663530323030656135663361636362366237656462653536343938666234313231616635323237363539663162346532383963386361346565323732656530636238333632316436393736353665373631393435623033353236616164333730336662306531333535626437333638653033666137623565633364306365323634663863643436393436633365353234316534373338376130393133663935303736396364613365313234643432306636393834386434613262316231306138'), $data->getPass()); - $this->assertEquals(pack('H*', '6465663130303030646566353032303065646434636466636231333437613739616166313734343462343839626362643364353664376664356562373233363235653130316261666432323539343633336664626639326630613135373461653562613562323535353230393236353237623863633534313862653363376361376536366139356366353366356162663031623064343236613234336162643533643837643239636633643165326532663732626664396433366133653061343534656664373134633661366237616338363966636263366435303166613964316338386365623264303861333438626633656638653135356538633865353838623938636465653061306463313835646636366535393138393831653366303464323139386236383738333539616563653034376434643637663835313235636661313237633138373865643530616630393434613934616363356265316130323566623065633362663831613933626365366365343734336164363562656638353131343466343332323837356438323339303236656363613866643862376330396563356465373233666466313636656166386336356539666537353436333535333664393766383366316366663931396530386339373730636166633136376661656364306366656262323931666334343831333238333662366432'), $data->getKey()); - $this->assertEquals('http://google.com', $data->getUrl()); - $this->assertEquals('aaaa', $data->getNotes()); - $this->assertEquals('Google', $data->getClientName()); - $this->assertEquals('Web', $data->getCategoryName()); - - $this->assertEquals(0, self::$repository->getDataForLink(10)->getNumRows()); - } - - /** - * Comprobar las cuentas devueltas para un filtro de usuario - * - * @throws ConstraintException - * @throws QueryException - */ - public function testGetForUser() - { - $queryCondition = new QueryCondition(); - $queryCondition->addFilter('Account.isPrivate = 1'); - - $this->assertCount(1, self::$repository->getForUser($queryCondition)->getDataAsArray()); - } - - /** - * Comprobar las cuentas devueltas para obtener los datos de las claves - * - * @throws ConstraintException - * @throws QueryException - */ - public function testGetAccountsPassData(): void - { - $this->assertCount(4, self::$repository->getAccountsPassData()->getDataAsArray()); - } - - /** - * Comprobar la creación de una cuenta - * - * @throws SPException - * @throws CryptoException - * @throws ConstraintException - * @throws QueryException - */ - public function testCreate() - { - $accountRequest = new AccountRequest(); - $accountRequest->name = 'Prueba 2'; - $accountRequest->login = 'admin'; - $accountRequest->url = 'http://syspass.org'; - $accountRequest->notes = 'notas'; - $accountRequest->userEditId = 1; - $accountRequest->passDateChange = time() + 3600; - $accountRequest->clientId = 1; - $accountRequest->categoryId = 1; - $accountRequest->isPrivate = 0; - $accountRequest->isPrivateGroup = 0; - $accountRequest->parentId = 0; - $accountRequest->userId = 1; - $accountRequest->userGroupId = 2; - $accountRequest->key = Crypt::makeSecuredKey(self::SECURE_KEY_PASSWORD); - $accountRequest->pass = Crypt::encrypt('1234', $accountRequest->key, self::SECURE_KEY_PASSWORD); - - // Comprobar registros iniciales - $this->assertEquals(4, self::getRowCount('Account')); - - self::$repository->create($accountRequest); - - // Comprobar registros finales - $this->assertEquals(5, self::getRowCount('Account')); - } - - /** - * Not implemented - */ - public function testGetByIdBatch() - { - $this->markTestSkipped('Not implemented'); - } - - /** - * Not implemented - */ - public function testCheckDuplicatedOnUpdate() - { - $this->markTestSkipped('Not implemented'); - } - - /** - * @throws ConstraintException - * @throws QueryException - */ - public function testGetPasswordHistoryForId() - { - $condition = new QueryCondition(); - $condition->addFilter('AccountHistory.id = 3'); - - $result = self::$repository->getPasswordHistoryForId($condition); - - $this->assertEquals(1, $result->getNumRows()); - - $data = $result->getData(); - - $this->assertEquals(3, $data->getId()); - $this->assertEquals('Google', $data->getName()); - $this->assertEquals('admin', $data->getLogin()); - $this->assertNull($data->getParentId()); - $this->assertEquals(pack('H*', '646566353032303064396362643366376662646536326637663732663861383732623430613839386131643134333933663662623033316664343362366461643762626564643634386437363964346634616234386638336636653236396166623734636261383134313363626162326461393733343934613231653934666331616664633637313732316562356666396562646132613665313937626233333563613632383830393934333863643731333230383132316430366433303838'), $data->getPass()); - $this->assertEquals(pack('H*', '6465663130303030646566353032303032636635623034396437656539356531653838663166613438643061616132663133613163663766346238316165663837326134373665316461653661353865316666626438346130383166303062633138646136373265653935643234626564336565303063333262646262303433336633356534323263616337613238363532336233313666316137333462616337343839346631333632643863376430373861373862396135633064396239653061353537626562666336636566623766363166376330393734356461623536373762303436313865343936383434663932666364303634316330303935636239363938336361336631363161623134663339643536636233653938333833613062396464356365383736333334376364363933313563306436343362623937366139383831376632346431303364316533353133306262393862353034353262346334663934663162323531383632356530653331346438343430323362666334306264616265376437386238663632326535353338636537663431626261616461613138646333333662623762636565333030656565333734616537356365303131363731323239383132383964346634383661376635303136303835336138663335653366393230383632386162373332343335633037656432616234'), $data->getKey()); - $this->assertEquals(pack('H*', '24327924313024787473754E325055766753482F306D7266426C73624F4163745667436A596371447143364C3354395172614E785A43345258475961'), $data->getMPassHash()); - - - $condition = new QueryCondition(); - $condition->addFilter('AccountHistory.id = 1'); - - $this->assertEquals(0, self::$repository->getPasswordHistoryForId($condition)->getNumRows()); - } - - /** - * Comprobar la búsqueda de cuentas mediante filtros - * - * @throws SPException - * @throws ConstraintException - * @throws QueryException - */ - public function testGetByFilter() - { - $searchFilter = new AccountSearchFilter(); - $searchFilter->setLimitCount(10); - $searchFilter->setCategoryId(1); - - // Comprobar un Id de categoría - $response = self::$repository->getByFilter($searchFilter, new QueryCondition()); - - $this->assertEquals(1, $response->getNumRows()); - - // Comprobar un Id de categoría no existente - $searchFilter->reset(); - $searchFilter->setLimitCount(10); - $searchFilter->setCategoryId(10); - - $response = self::$repository->getByFilter($searchFilter, new QueryCondition()); - - $this->assertEquals(0, $response->getNumRows()); - - // Comprobar un Id de cliente - $searchFilter->reset(); - $searchFilter->setLimitCount(10); - $searchFilter->setClientId(1); - - $response = self::$repository->getByFilter($searchFilter, new QueryCondition()); - - $this->assertEquals(1, $response->getNumRows()); - - // Comprobar un Id de cliente no existente - $searchFilter->reset(); - $searchFilter->setLimitCount(10); - $searchFilter->setClientId(10); - - $response = self::$repository->getByFilter($searchFilter, new QueryCondition()); - - $this->assertEquals(0, $response->getNumRows()); - - // Comprobar una cadena de texto - $searchFilter->reset(); - $searchFilter->setLimitCount(10); - $searchFilter->setCleanTxtSearch('apple.com'); - - $response = self::$repository->getByFilter($searchFilter, new QueryCondition()); - - $this->assertEquals(1, $response->getNumRows()); - - /** @var AccountSearchVData[] $data */ - $data = $response->getDataAsArray(); - - $this->assertEquals(2, $data[0]->getId()); - - // Comprobar los favoritos - $searchFilter->reset(); - $searchFilter->setLimitCount(10); - $searchFilter->setSearchFavorites(true); - - $response = self::$repository->getByFilter($searchFilter, new QueryCondition()); - - $this->assertEquals(1, $response->getNumRows()); - - // Comprobar las etiquetas - $searchFilter->reset(); - $searchFilter->setLimitCount(10); - $searchFilter->setTagsId([1]); - - $response = self::$repository->getByFilter($searchFilter, new QueryCondition()); - - $this->assertEquals(2, $response->getNumRows()); - - /** @var AccountSearchVData[] $data */ - $data = $response->getDataAsArray(); - - $this->assertEquals(2, $data[0]->getId()); - $this->assertEquals(1, $data[1]->getId()); - } - - /** - * @throws ConstraintException - * @throws SPException - * @throws QueryException - */ public function testUpdateBulk() { - $accountRequest = new AccountRequest(); - $accountRequest->id = 1; - $accountRequest->userEditId = 1; - $accountRequest->passDateChange = time() + 3600; - $accountRequest->clientId = 1; - $accountRequest->categoryId = 1; - $accountRequest->userId = 1; - $accountRequest->userGroupId = 2; + $accountRequest = $this->buildAccountRequest(); - $this->assertEquals(1, self::$repository->updateBulk($accountRequest)); + $expected = new QueryResult(); + $expected->setAffectedNumRows(1); - $result = self::$repository->getById($accountRequest->id); + $callback = new Callback( + function (QueryData $arg) use ($accountRequest) { + $params = $arg->getParams(); - $this->assertEquals(1, $result->getNumRows()); + return $params[0] === $accountRequest->userEditId + && $params[1] === $accountRequest->clientId + && $params[2] === $accountRequest->categoryId + && $params[3] === $accountRequest->userId + && $params[4] === $accountRequest->userGroupId + && $params[5] === $accountRequest->passDateChange + && $params[6] === $accountRequest->id + && !empty($arg->getQuery()); + } + ); - /** @var AccountVData $data */ - $data = $result->getData(); + $this->databaseInterface->expects(self::once()) + ->method('doQuery') + ->with($callback) + ->willReturn($expected); - $this->assertEquals($accountRequest->id, $data->getId()); - $this->assertEquals($accountRequest->userId, $data->getUserId()); - $this->assertEquals($accountRequest->userGroupId, $data->getUserGroupId()); - $this->assertEquals($accountRequest->passDateChange, $data->getPassDateChange()); - $this->assertEquals($accountRequest->clientId, $data->getClientId()); - $this->assertEquals($accountRequest->categoryId, $data->getCategoryId()); - $this->assertEquals($accountRequest->userEditId, $data->getUserEditId()); + $this->assertEquals($expected->getAffectedNumRows(), $this->accountRepository->updateBulk($accountRequest)); + } - $accountRequest = new AccountRequest(); - $accountRequest->id = 10; + public function testgetById() + { + $id = self::$faker->randomNumber(); - $this->assertEquals(0, self::$repository->updateBulk($accountRequest)); + $expected = new QueryResult(); + + $callback = new Callback( + function (QueryData $arg) use ($id) { + $params = $arg->getParams(); + + return $params[0] === $id + && $arg->getMapClassName() === AccountVData::class + && !empty($arg->getQuery()); + } + ); + + $this->databaseInterface->expects(self::once()) + ->method('doSelect') + ->with($callback) + ->willReturn($expected); + + $this->assertEquals($expected, $this->accountRepository->getById($id)); } } diff --git a/tests/SP/Services/Account/AccountServiceTest.php b/tests/SP/Services/Account/AccountServiceTest.php index d42f648f..29e171f7 100644 --- a/tests/SP/Services/Account/AccountServiceTest.php +++ b/tests/SP/Services/Account/AccountServiceTest.php @@ -1,10 +1,10 @@ . + * along with sysPass. If not, see . */ namespace SP\Tests\Services\Account; @@ -35,12 +35,12 @@ use SP\Core\Exceptions\ConstraintException; use SP\Core\Exceptions\NoSuchPropertyException; use SP\Core\Exceptions\QueryException; use SP\Core\Exceptions\SPException; -use SP\DataModel\AccountData; use SP\DataModel\AccountSearchVData; use SP\DataModel\AccountVData; use SP\DataModel\ItemSearchData; use SP\DataModel\ProfileData; use SP\Domain\Account\AccountHistoryServiceInterface; +use SP\Domain\Account\Out\AccountData; use SP\Domain\Account\Services\AccountBulkRequest; use SP\Domain\Account\Services\AccountHistoryService; use SP\Domain\Account\Services\AccountPasswordRequest; diff --git a/tests/SP/UnitaryTestCase.php b/tests/SP/UnitaryTestCase.php index 40764464..200e79b6 100644 --- a/tests/SP/UnitaryTestCase.php +++ b/tests/SP/UnitaryTestCase.php @@ -30,6 +30,7 @@ use Faker\Factory; use Faker\Generator; use PHPUnit\Framework\TestCase; use SP\Core\Application; +use SP\Core\Context\ContextInterface; use SP\Core\Context\StatelessContext; use SP\Core\Events\EventDispatcher; use SP\Domain\Config\Services\ConfigBackupService; @@ -46,6 +47,7 @@ abstract class UnitaryTestCase extends TestCase protected static Generator $faker; protected ConfigFileService $config; protected Application $application; + protected ContextInterface $context; public static function setUpBeforeClass(): void { @@ -78,17 +80,17 @@ abstract class UnitaryTestCase extends TestCase $userLogin = new UserLoginResponse(); $userLogin->setLogin(self::$faker->userName); - $context = new StatelessContext(); - $context->initialize(); - $context->setUserData($userLogin); + $this->context = new StatelessContext(); + $this->context->initialize(); + $this->context->setUserData($userLogin); $config = new ConfigFileService( $this->createStub(XmlHandler::class), $this->createStub(FileCache::class), - $context, + $this->context, $this->createStub(ConfigBackupService::class) ); - return new Application($config, $this->createStub(EventDispatcher::class), $context); + return new Application($config, $this->createStub(EventDispatcher::class), $this->context); } } \ No newline at end of file