-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(entity): move access collections related functions into trait
- Loading branch information
Showing
2 changed files
with
106 additions
and
93 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
engine/classes/Elgg/Traits/Entity/AccessCollections.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters