new acl usage

This commit is contained in:
Yuri Kuznetsov
2021-04-17 12:47:31 +03:00
parent 33ec8d0041
commit 4497c4be66
10 changed files with 364 additions and 75 deletions
@@ -41,6 +41,7 @@ class OwnershipChecker implements OwnershipOwnChecker
{
public function checkOwn(User $user, Entity $entity): bool
{
if ($entity->get('type') === 'Post' && $user->getId() === $entity->get('createdById')) {
return true;
}
@@ -27,22 +27,45 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\AclPortal;
namespace Espo\Classes\AclPortal\Attachment;
use Espo\Entities\User as EntityUser;
use Espo\Entities\Note;
use Espo\Entities\{
User,
Note,
};
use Espo\ORM\Entity;
use Espo\Core\{
ORM\EntityManager,
Portal\AclManager,
Acl\ScopeData,
Acl\Table,
AclPortal\Acl as Acl,
Acl\AccessEntityCREDChecker,
Portal\Acl\DefaultAccessChecker,
Portal\Acl\Traits\DefaultAccessCheckerDependency,
};
class Attachment extends Acl
class AccessChecker implements AccessEntityCREDChecker
{
public function checkEntityRead(EntityUser $user, Entity $entity, ScopeData $data): bool
use DefaultAccessCheckerDependency;
private $defaultAccessChecker;
private $aclManager;
private $entityManager;
public function __construct(
DefaultAccessChecker $defaultAccessChecker,
AclManager $aclManager,
EntityManager $entityManager
) {
$this->defaultAccessChecker = $defaultAccessChecker;
$this->aclManager = $aclManager;
$this->entityManager = $entityManager;
}
public function checkEntityRead(User $user, Entity $entity, ScopeData $data): bool
{
if ($entity->get('parentType') === 'Settings') {
return true;
@@ -77,14 +100,14 @@ class Attachment extends Acl
return true;
}
if ($this->checkEntity($user, $entity, $data, Table::ACTION_READ)) {
if ($this->defaultAccessChecker->checkEntityRead($user, $entity, $data)) {
return true;
}
return false;
}
protected function checkEntityReadNoteParent(EntityUser $user, Note $note): ?bool
private function checkEntityReadNoteParent(User $user, Note $note): ?bool
{
if ($note->isInternal()) {
return false;
@@ -128,13 +151,4 @@ class Attachment extends Acl
return null;
}
public function checkIsOwner(EntityUser $user, Entity $entity)
{
if ($user->getId() === $entity->get('createdById')) {
return true;
}
return false;
}
}
@@ -27,50 +27,26 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\AclPortal;
namespace Espo\Classes\AclPortal\Attachment;
use Espo\Entities\User;
use Espo\Entities\User as EntityUser;
use Espo\ORM\Entity;
use Espo\Core\{
Acl\ScopeData,
Acl\Table,
AclPortal\Acl as Acl,
Acl\OwnershipOwnChecker,
};
class Note extends Acl
class OwnershipChecker implements OwnershipOwnChecker
{
public function checkIsOwner(EntityUser $user, Entity $entity)
private const ATTR_CREATED_BY_ID = 'createdById';
public function checkOwn(User $user, Entity $entity): bool
{
if ($entity->get('type') === 'Post' && $user->id === $entity->get('createdById')) {
if ($user->getId() === $entity->get(self::ATTR_CREATED_BY_ID)) {
return true;
}
return false;
}
public function checkEntityCreate(EntityUser $user, Entity $entity, ScopeData $data): bool
{
if ($entity->get('type') !== 'Post') {
return false;
}
if ($entity->get('type') === 'Post' && $entity->get('targetType')) {
return false;
}
if (!$entity->get('parentId') || !$entity->get('parentType')) {
return false;
}
$parent = $this->entityManager->getEntity($entity->get('parentType'), $entity->get('parentId'));
if ($parent) {
if ($this->aclManager->checkEntity($user, $parent, Table::ACTION_STREAM)) {
return true;
}
}
return false;
}
}
@@ -27,23 +27,40 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\AclPortal;
namespace Espo\Classes\AclPortal\Email;
use Espo\Entities\User as EntityUser;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Core\{
Acl\ScopeData,
Portal\AclManager,
Acl\Table,
AclPortal\Acl as Acl,
Acl\ScopeData,
Acl\AccessEntityCREDChecker,
Portal\Acl\DefaultAccessChecker,
Portal\Acl\Traits\DefaultAccessCheckerDependency,
};
class Email extends Acl
class AccessChecker implements AccessEntityCREDChecker
{
public function checkEntityRead(EntityUser $user, Entity $entity, ScopeData $data): bool
use DefaultAccessCheckerDependency;
private $defaultAccessChecker;
private $aclManager;
public function __construct(
DefaultAccessChecker $defaultAccessChecker,
AclManager $aclManager
) {
$this->defaultAccessChecker = $defaultAccessChecker;
$this->aclManager = $aclManager;
}
public function checkEntityRead(User $user, Entity $entity, ScopeData $data): bool
{
if ($this->checkEntity($user, $entity, $data, Table::ACTION_READ)) {
if ($this->defaultAccessChecker->checkEntityRead($user, $entity, $data)) {
return true;
}
@@ -55,11 +72,7 @@ class Email extends Acl
return false;
}
if (!$entity->has('usersIds')) {
$entity->loadLinkMultipleField('users');
}
$userIdList = $entity->get('usersIds');
$userIdList = $entity->getLinkMultipleIdLIst('users');
if (is_array($userIdList) && in_array($user->getId(), $userIdList)) {
return true;
@@ -67,14 +80,4 @@ class Email extends Acl
return false;
}
public function checkIsOwner(EntityUser $user, Entity $entity)
{
if ($user->getId() === $entity->get('createdById')) {
return true;
}
return false;
}
}
@@ -0,0 +1,50 @@
<?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 Espo\Classes\AclPortal\Email;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Core\{
Acl\OwnershipOwnChecker,
};
class OwnershipChecker implements OwnershipOwnChecker
{
public function checkOwn(User $user, Entity $entity): bool
{
if ($user->getId() === $entity->get('createdById')) {
return true;
}
return false;
}
}
@@ -0,0 +1,189 @@
<?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 Espo\Classes\AclPortal\Note;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Core\{
Portal\AclManager,
Acl\ScopeData,
Acl\AccessEntityCREDChecker,
Portal\Acl\DefaultAccessChecker,
Portal\Acl\Traits\DefaultAccessCheckerDependency,
ORM\EntityManager,
Utils\Config,
};
use DateTime;
use Exception;
class AccessChecker implements AccessEntityCREDChecker
{
use DefaultAccessCheckerDependency;
private const EDIT_PERIOD = '7 days';
private const DELETE_PERIOD = '1 month';
private $defaultAccessChecker;
private $aclManager;
private $entityManager;
private $config;
public function __construct(
DefaultAccessChecker $defaultAccessChecker,
AclManager $aclManager,
EntityManager $entityManager,
Config $config
) {
$this->defaultAccessChecker = $defaultAccessChecker;
$this->aclManager = $aclManager;
$this->entityManager = $entityManager;
$this->config = $config;
}
public function checkEntityCreate(User $user, Entity $entity, ScopeData $data): bool
{
$parentId = $entity->get('parentId');
$parentType = $entity->get('parentType');
if (!$parentId || !$parentType) {
return $this->defaultAccessChecker->checkEntityCreate($user, $entity, $data);
}
$parent = $this->entityManager->getEntity($parentType, $parentId);
if ($parent && $this->aclManager->checkEntityStream($user, $parent)) {
return true;
}
return $this->defaultAccessChecker->checkEntityCreate($user, $entity, $data);
}
public function checkEntityRead(User $user, Entity $entity, ScopeData $data): bool
{
if ($entity->get('type') !== 'Post') {
return false;
}
if ($entity->get('type') === 'Post' && $entity->get('targetType')) {
return false;
}
if (!$entity->get('parentId') || !$entity->get('parentType')) {
return false;
}
$parent = $this->entityManager->getEntity($entity->get('parentType'), $entity->get('parentId'));
if ($parent) {
if ($this->aclManager->checkEntityStream($user, $parent)) {
return true;
}
}
return false;
}
public function checkEntityEdit(User $user, Entity $entity, ScopeData $data): bool
{
if (!$this->defaultAccessChecker->checkEntityEdit($user, $entity, $data)) {
return false;
}
if (!$this->aclManager->checkOwnershipOwn($user, $entity)) {
return true;
}
$createdAt = $entity->get('createdAt');
if (!$createdAt) {
return true;
}
$noteEditThresholdPeriod =
'-' . $this->config->get('noteEditThresholdPeriod', self::EDIT_PERIOD);
$dt = new DateTime();
$dt->modify($noteEditThresholdPeriod);
try {
if ($dt->format('U') > (new DateTime($createdAt))->format('U')) {
return false;
}
}
catch (Exception $e) {
return false;
}
return true;
}
public function checkEntityDelete(User $user, Entity $entity, ScopeData $data): bool
{
if (!$this->defaultAccessChecker->checkEntityDelete($user, $entity, $data)) {
return false;
}
if (!$this->aclManager->checkOwnershipOwn($user, $entity)) {
return true;
}
$createdAt = $entity->get('createdAt');
if (!$createdAt) {
return true;
}
$deleteThresholdPeriod =
'-' . $this->config->get('noteDeleteThresholdPeriod', self::DELETE_PERIOD);
$dt = new DateTime();
$dt->modify($deleteThresholdPeriod);
try {
if ($dt->format('U') > (new DateTime($createdAt))->format('U')) {
return false;
}
}
catch (Exception $e) {
return false;
}
return true;
}
}
@@ -0,0 +1,50 @@
<?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 Espo\Classes\AclPortal\Note;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Core\{
Acl\OwnershipOwnChecker,
};
class OwnershipChecker implements OwnershipOwnChecker
{
public function checkOwn(User $user, Entity $entity): bool
{
if ($entity->get('type') === 'Post' && $user->getId() === $entity->get('createdById')) {
return true;
}
return false;
}
}
@@ -1,4 +1,6 @@
{
"accessCheckerClassName": "Espo\\Classes\\Acl\\Attachment\\AccessChecker",
"ownershipCheckerClassName": "Espo\\Classes\\Acl\\Attachment\\OwnershipChecker"
"ownershipCheckerClassName": "Espo\\Classes\\Acl\\Attachment\\OwnershipChecker",
"portalAccessCheckerClassName": "Espo\\Classes\\AclPortal\\Attachment\\AccessChecker",
"portalOwnershipCheckerClassName": "Espo\\Classes\\AclPortal\\Attachment\\OwnershipChecker"
}
@@ -1,5 +1,7 @@
{
"accessCheckerClassName": "Espo\\Classes\\Acl\\Email\\AccessChecker",
"ownershipCheckerClassName": "Espo\\Classes\\Acl\\Email\\OwnershipChecker",
"portalAccessCheckerClassName": "Espo\\Classes\\AclPortal\\Email\\AccessChecker",
"portalOwnershipCheckerClassName": "Espo\\Classes\\AclPortal\\Email\\OwnershipChecker",
"readOwnerUserField": "users"
}
@@ -1,4 +1,6 @@
{
"accessCheckerClassName": "Espo\\Classes\\Acl\\Note\\AccessChecker",
"ownershipCheckerClassName": "Espo\\Classes\\Acl\\Note\\OwnershipChecker"
"ownershipCheckerClassName": "Espo\\Classes\\Acl\\Note\\OwnershipChecker",
"portalAccessCheckerClassName": "Espo\\Classes\\AclPortal\\Note\\AccessChecker",
"portalOwnershipCheckerClassName": "Espo\\Classes\\AclPortal\\Note\\OwnershipChecker"
}