Skip to content

Commit

Permalink
chore(entity): move access collections related functions into trait
Browse files Browse the repository at this point in the history
  • Loading branch information
jeabakker committed Aug 1, 2024
1 parent 9939128 commit 30e0daa
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 93 deletions.
104 changes: 104 additions & 0 deletions engine/classes/Elgg/Traits/Entity/AccessCollections.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Elgg\Traits\Entity;

use Elgg\Exceptions\InvalidArgumentException as ElggInvalidArgumentException;

/**
* Bundle all access_collections related functions for an \ElggEntity
*
* @since 6.1
*/
trait AccessCollections {

/**
* Returns the ACLs owned by the entity
*
* @param array $options additional options to get the access collections with
*
* @return \ElggAccessCollection[]
*
* @see elgg_get_access_collections()
* @since 3.0
*/
public function getOwnedAccessCollections(array $options = []): array {
$options['owner_guid'] = $this->guid;
return _elgg_services()->accessCollections->getEntityCollections($options);
}

/**
* Returns the first ACL owned by the entity with a given subtype
*
* @param string $subtype subtype of the ACL
*
* @return \ElggAccessCollection|null
* @throws \Elgg\Exceptions\InvalidArgumentException
*
* @since 3.0
*/
public function getOwnedAccessCollection(string $subtype): ?\ElggAccessCollection {
if ($subtype === '') {
throw new ElggInvalidArgumentException(__METHOD__ . ' requires $subtype to be non empty');
}

$acls = $this->getOwnedAccessCollections([
'subtype' => $subtype,
]);

return elgg_extract(0, $acls);
}

/**
* Remove the membership of all access collections for this entity (if the entity is a user)
*
* @return bool
* @since 1.11
*/
public function deleteAccessCollectionMemberships() {
if (!$this->guid) {
return false;
}

if ($this->type !== 'user') {
return true;
}

$ac = _elgg_services()->accessCollections;

$collections = $ac->getCollectionsByMember($this->guid);
if (empty($collections)) {
return true;
}

$result = true;
foreach ($collections as $collection) {
$result &= $ac->removeUser($this->guid, $collection->id);
}

return $result;
}

/**
* Remove all access collections owned by this entity
*
* @return bool
* @since 1.11
*/
public function deleteOwnedAccessCollections() {
if (!$this->guid) {
return false;
}

$collections = $this->getOwnedAccessCollections();
if (empty($collections)) {
return true;
}

$result = true;
foreach ($collections as $collection) {
$result = $result & $collection->delete();
}

return $result;
}
}
95 changes: 2 additions & 93 deletions engine/classes/ElggEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Elgg\Exceptions\Filesystem\IOException;
use Elgg\Exceptions\DomainException as ElggDomainException;
use Elgg\Exceptions\InvalidArgumentException as ElggInvalidArgumentException;
use Elgg\Traits\Entity\AccessCollections;
use Elgg\Traits\Entity\Annotations;
use Elgg\Traits\Entity\Icons;
use Elgg\Traits\Entity\Metadata;
Expand Down Expand Up @@ -50,6 +51,7 @@
*/
abstract class ElggEntity extends \ElggData {

use AccessCollections;
use Annotations;
use Icons;
use Metadata;
Expand Down Expand Up @@ -371,43 +373,6 @@ public function countComments(): int {
return \Elgg\Comments\DataService::instance()->getCommentsCount($this);
}

/**
* Returns the ACLs owned by the entity
*
* @param array $options additional options to get the access collections with
*
* @return \ElggAccessCollection[]
*
* @see elgg_get_access_collections()
* @since 3.0
*/
public function getOwnedAccessCollections(array $options = []): array {
$options['owner_guid'] = $this->guid;
return _elgg_services()->accessCollections->getEntityCollections($options);
}

/**
* Returns the first ACL owned by the entity with a given subtype
*
* @param string $subtype subtype of the ACL
*
* @return \ElggAccessCollection|null
* @throws \Elgg\Exceptions\InvalidArgumentException
*
* @since 3.0
*/
public function getOwnedAccessCollection(string $subtype): ?\ElggAccessCollection {
if ($subtype === '') {
throw new ElggInvalidArgumentException(__METHOD__ . ' requires $subtype to be non empty');
}

$acls = $this->getOwnedAccessCollections([
'subtype' => $subtype,
]);

return elgg_extract(0, $acls);
}

/**
* Check if the given user has access to this entity
*
Expand Down Expand Up @@ -1263,62 +1228,6 @@ public function getObjectFromID(int $id): ?\ElggEntity {
return get_entity($id);
}

/**
* Remove the membership of all access collections for this entity (if the entity is a user)
*
* @return bool
* @since 1.11
*/
public function deleteAccessCollectionMemberships() {

if (!$this->guid) {
return false;
}

if ($this->type !== 'user') {
return true;
}

$ac = _elgg_services()->accessCollections;

$collections = $ac->getCollectionsByMember($this->guid);
if (empty($collections)) {
return true;
}

$result = true;
foreach ($collections as $collection) {
$result &= $ac->removeUser($this->guid, $collection->id);
}

return $result;
}

/**
* Remove all access collections owned by this entity
*
* @return bool
* @since 1.11
*/
public function deleteOwnedAccessCollections() {

if (!$this->guid) {
return false;
}

$collections = $this->getOwnedAccessCollections();
if (empty($collections)) {
return true;
}

$result = true;
foreach ($collections as $collection) {
$result = $result & $collection->delete();
}

return $result;
}

/**
* Update the last_action column in the entities table.
*
Expand Down

0 comments on commit 30e0daa

Please sign in to comment.