diff --git a/application/Espo/Core/Acl.php b/application/Espo/Core/Acl.php index 36352cb75b..e70f275721 100644 --- a/application/Espo/Core/Acl.php +++ b/application/Espo/Core/Acl.php @@ -118,5 +118,15 @@ class Acl { return $this->getAclManager()->getScopeForbiddenFieldList($this->getUser(), $scope, $action, $thresholdLevel); } + + public function checkUserPermission($target, $permissionType = 'userPermission') + { + return $this->getAclManager()->checkUserPermission($this->getUser(), $target, $permissionType); + } + + public function checkAssignmentPermission($target) + { + return $this->getAclManager()->checkAssignmentPermission($this->getUser(), $target); + } } diff --git a/application/Espo/Core/AclManager.php b/application/Espo/Core/AclManager.php index 51aeb0e051..08b0d60a31 100644 --- a/application/Espo/Core/AclManager.php +++ b/application/Espo/Core/AclManager.php @@ -235,5 +235,40 @@ class AclManager if ($user->isAdmin()) return []; return $this->getTable($user)->getScopeForbiddenFieldList($scope, $action, $thresholdLevel); } + + public function checkUserPermission(User $user, $target, $permissionType = 'userPermission') + { + $permission = $this->get($user, $permissionType); + + if (is_object($target)) { + $userId = $target->id; + } else { + $userId = $target; + } + + if ($user->id === $userId) return true; + + if ($permission === 'no') { + return false; + } + + if ($permission === 'yes') { + return true; + } + + if ($permission === 'team') { + $teamIdList = $user->getLinkMultipleIdList('teams'); + if (!$this->getContainer()->get('entityManager')->getRepository('User')->checkBelongsToAnyOfTeams($userId, $teamIdList)) { + return false; + } + } + + return true; + } + + public function checkAssignmentPermission(User $user, $target) + { + return $this->checkUserPermission($user, $target, 'assignmentPermission'); + } } diff --git a/application/Espo/Modules/Crm/Repositories/Meeting.php b/application/Espo/Modules/Crm/Repositories/Meeting.php index 387e6eb0ab..a3cb95ab4f 100644 --- a/application/Espo/Modules/Crm/Repositories/Meeting.php +++ b/application/Espo/Modules/Crm/Repositories/Meeting.php @@ -61,19 +61,23 @@ class Meeting extends \Espo\Core\ORM\Repositories\RDB } $assignedUserId = $entity->get('assignedUserId'); - if ($assignedUserId && $entity->has('usersIds')) { - $usersIds = $entity->get('usersIds'); - if (!is_array($usersIds)) { - $usersIds = array(); - } - if (!in_array($assignedUserId, $usersIds)) { - $usersIds[] = $assignedUserId; - $entity->set('usersIds', $usersIds); - $hash = $entity->get('usersNames'); - if ($hash instanceof \StdClass) { - $hash->$assignedUserId = $entity->get('assignedUserName'); - $entity->set('usersNames', $hash); + if ($assignedUserId) { + if ($entity->has('usersIds')) { + $usersIds = $entity->get('usersIds'); + if (!is_array($usersIds)) { + $usersIds = []; } + if (!in_array($assignedUserId, $usersIds)) { + $usersIds[] = $assignedUserId; + $entity->set('usersIds', $usersIds); + $hash = $entity->get('usersNames'); + if ($hash instanceof \StdClass) { + $hash->$assignedUserId = $entity->get('assignedUserName'); + $entity->set('usersNames', $hash); + } + } + } else { + $entity->addLinkMultipleId('users', $assignedUserId); } if ($entity->isNew()) { $currentUserId = $this->getEntityManager()->getUser()->id; diff --git a/application/Espo/Modules/Crm/Services/Meeting.php b/application/Espo/Modules/Crm/Services/Meeting.php index c0b5619233..9cd23d3589 100644 --- a/application/Espo/Modules/Crm/Services/Meeting.php +++ b/application/Espo/Modules/Crm/Services/Meeting.php @@ -76,6 +76,39 @@ class Meeting extends \Espo\Services\Record return $this->getInjection('dateTime'); } + public function checkAssignment(Entity $entity) + { + $result = parent::checkAssignment($entity); + if (!$result) return false; + + $userIdList = $entity->get('usersIds'); + if (!is_array($userIdList)) { + $userIdList = []; + } + + $newIdList = []; + if (!$entity->isNew()) { + $existingIdList = []; + foreach ($entity->get('users') as $user) { + $existingIdList[] = $user->id; + } + foreach ($userIdList as $id) { + if (!in_array($id, $existingIdList)) { + $newIdList[] = $id; + } + } + } else { + $newIdList = $userIdList; + } + + foreach ($newIdList as $userId) { + if (!$this->getAcl()->checkAssignmentPermission($userId)) { + return false; + } + } + + return true; + } protected function getInvitationManager() { diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index 3b0738859e..cf34382049 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -391,8 +391,8 @@ class Record extends \Espo\Core\Services\Base return false; } } else if ($assignmentPermission == 'team') { - $teamIds = $this->getUser()->get('teamsIds'); - if (!$this->getEntityManager()->getRepository('User')->checkBelongsToAnyOfTeams($assignedUserId, $teamIds)) { + $teamIdList = $this->getUser()->get('teamsIds'); + if (!$this->getEntityManager()->getRepository('User')->checkBelongsToAnyOfTeams($assignedUserId, $teamIdList)) { return false; } } diff --git a/client/modules/crm/src/views/meeting/fields/users.js b/client/modules/crm/src/views/meeting/fields/users.js index 04c7791c6c..aedaa437d7 100644 --- a/client/modules/crm/src/views/meeting/fields/users.js +++ b/client/modules/crm/src/views/meeting/fields/users.js @@ -30,7 +30,21 @@ Espo.define('crm:views/meeting/fields/users', 'crm:views/meeting/fields/attendee return Dep.extend({ - selectPrimaryFilterName: 'active' + selectPrimaryFilterName: 'active', + + init: function () { + this.assignmentPermission = this.getAcl().get('assignmentPermission'); + if (this.assignmentPermission == 'no') { + this.readOnly = true; + } + Dep.prototype.init.call(this); + }, + + getSelectBoolFilterList: function () { + if (this.assignmentPermission == 'team') { + return ['onlyMyTeam']; + } + } });