Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(access): the friends access is now an access collection
- Loading branch information
Showing
26 changed files
with
418 additions
and
130 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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,133 @@ | ||
<?php | ||
|
||
namespace Elgg\Upgrades; | ||
|
||
use Elgg\Upgrade\Batch; | ||
use Elgg\Upgrade\Result; | ||
|
||
/** | ||
* Creates user friends access collection and migrates entity access_id | ||
*/ | ||
class MigrateFriendsACL implements Batch { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getVersion() { | ||
return 2017121200; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function needsIncrementOffset() { | ||
return false; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function shouldBeSkipped() { | ||
return empty($this->countItems()); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function countItems() { | ||
// users without a friends acl | ||
$db = elgg()->getDb(); | ||
|
||
return elgg_get_entities([ | ||
'type' => 'user', | ||
'count' => true, | ||
'wheres' => [ | ||
"e.guid NOT IN ( | ||
SELECT acl.owner_guid FROM {$db->prefix}access_collections acl WHERE acl.subtype = 'friends' | ||
)", | ||
], | ||
]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function run(Result $result, $offset) { | ||
|
||
$db = elgg()->getDb(); | ||
|
||
$users = elgg_get_entities([ | ||
'type' => 'user', | ||
'limit' => 1, | ||
'offset' => $offset, | ||
'wheres' => [ | ||
"e.guid NOT IN ( | ||
SELECT acl.owner_guid FROM {$db->prefix}access_collections acl WHERE acl.subtype = 'friends' | ||
)", | ||
], | ||
]); | ||
|
||
if (empty($users)) { | ||
// mark as complete | ||
$result->addSuccesses(1); | ||
return $result; | ||
} | ||
|
||
$user = $users[0]; | ||
|
||
// create acl | ||
$acl_id = create_access_collection('friends', $user->guid, 'friends'); | ||
if (!$acl_id) { | ||
$result->addError('Failed to create an ACL for user'); | ||
return $result; | ||
} | ||
|
||
$acl = get_access_collection($acl_id); | ||
|
||
$this->addFriendsToACL($user, $acl); | ||
$this->updateEntities($user, $acl); | ||
$this->updateAnnotations($user, $acl); | ||
|
||
$result->addSuccesses(1); | ||
|
||
return $result; | ||
} | ||
|
||
protected function addFriendsToACL(\ElggUser $user, \ElggAccessCollection $acl) { | ||
$friends = $user->getFriends(['batch' => true]); | ||
foreach ($friends as $friend) { | ||
$acl->addMember($friend->guid); | ||
} | ||
} | ||
|
||
protected function updateEntities(\ElggUser $user, \ElggAccessCollection $acl) { | ||
$entities = elgg_get_entities([ | ||
'type' => 'object', | ||
'owner_guid' => $user->guid, | ||
'access_id' => ACCESS_FRIENDS, | ||
'limit' => false, | ||
'batch' => true, | ||
]); | ||
|
||
foreach ($entities as $entity) { | ||
$entity->access_id = $acl->id; | ||
$entity->save(); | ||
} | ||
} | ||
|
||
protected function updateAnnotations(\ElggUser $user, \ElggAccessCollection $acl) { | ||
$annotations = elgg_get_annotations([ | ||
'type' => 'object', | ||
'owner_guid' => $user->guid, | ||
'access_id' => ACCESS_FRIENDS, | ||
'limit' => false, | ||
'batch' => true, | ||
]); | ||
|
||
foreach ($annotations as $annotation) { | ||
$annotation->access_id = $acl->id; | ||
$annotation->save(); | ||
} | ||
} | ||
|
||
} |
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
Oops, something went wrong.