Skip to content

Commit

Permalink
onUserDeleted
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Apr 14, 2020
1 parent 91d5c03 commit fe742d7
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 78 deletions.
1 change: 0 additions & 1 deletion lib/Db/CirclesRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ public function forceGetCircleByName($name) {

/**
* @param string $userId
* @param string $instanceId
* @param int $type
* @param string $name
* @param int $level
Expand Down
14 changes: 10 additions & 4 deletions lib/Db/MembersRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,20 +598,26 @@ public function removeAllFromCircle($uniqueCircleId) {
*
* remove All membership from a User. Used when removing a User from the Cloud.
*
* @param string $userId
* @param Member $member
*/
public function removeAllMembershipsFromUser($userId) {
if ($userId === '') {
public function removeAllMembershipsFromUser(Member $member) {
if ($member->getUserId() === '') {
return;
}

$instance = $member->getInstance();
if ($instance === $this->configService->getLocalCloudId()) {
$instance = '';
}

$qb = $this->getMembersDeleteSql();
$expr = $qb->expr();

/** @noinspection PhpMethodParametersCountMismatchInspection */
$qb->where(
$expr->andX(
$expr->eq('user_id', $qb->createNamedParameter($userId)),
$expr->eq('user_id', $qb->createNamedParameter($member->getUserId())),
$expr->eq('instance', $qb->createNamedParameter($instance)),
$expr->eq('user_type', $qb->createNamedParameter(Member::TYPE_USER))
)
);
Expand Down
7 changes: 5 additions & 2 deletions lib/Events/UserEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ public function __construct(
*/
public function onUserDeleted(array $params) {
$userId = $params['uid'];
$this->circlesService->onUserRemoved($userId);
$this->membersService->onUserRemoved($userId);
try {
$this->membersService->onUserRemoved($userId);
} catch (\Exception $e) {
$this->miscService->log('exception while onUserDeleted ' . $e->getMessage());
}
}


Expand Down
12 changes: 0 additions & 12 deletions lib/GlobalScale/MemberLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ public function verify(GSEvent $event, bool $localCheck = false, bool $mustBeChe
$this->verifyMemberLevel($event, $circle, $member, $level);
}

$this->miscService->log('$$$$ 3 ' . json_encode($event));

}


Expand All @@ -100,31 +98,21 @@ public function manage(GSEvent $event): void {
$level = $event->getData()
->gInt('level');

$this->miscService->log('$$$$ 4 ' . json_encode($event));

$member = $event->getMember();
$this->cleanMember($member);
$this->miscService->log('$$$$ 5 ' . json_encode($event));

$member->setLevel($level);
$this->membersRequest->updateMember($member);

$this->miscService->log('#### ' . json_encode($event));
if ($level === Member::LEVEL_OWNER) {
$circle = $event->getCircle();
$isMod = $circle->getOwner();
if ($isMod->getInstance() === '') {
$isMod->setInstance($event->getSource());
}

$this->miscService->log('???? ' . $isMod->getInstance());

$isMod->setLevel(Member::LEVEL_ADMIN);
$this->miscService->log('#### 001 ' . json_encode($event));

$this->membersRequest->updateMember($isMod);
$this->miscService->log('#### 002 ' . json_encode($event));

}
}

Expand Down
160 changes: 160 additions & 0 deletions lib/GlobalScale/UserDeleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php declare(strict_types=1);


/**
* Circles - Bring cloud-users closer together.
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@artificial-owl.com>
* @copyright 2017
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/


namespace OCA\Circles\GlobalScale;


use OCA\Circles\Exceptions\CircleDoesNotExistException;
use OCA\Circles\Exceptions\ConfigNoCircleAvailableException;
use OCA\Circles\Exceptions\GlobalScaleDSyncException;
use OCA\Circles\Exceptions\GlobalScaleEventException;
use OCA\Circles\Exceptions\MemberDoesNotExistException;
use OCA\Circles\Model\Circle;
use OCA\Circles\Model\GlobalScale\GSEvent;
use OCA\Circles\Model\Member;


/**
* Class MemberDelete
*
* @package OCA\Circles\GlobalScale
*/
class UserDeleted extends AGlobalScaleEvent {


/**
* @param GSEvent $event
* @param bool $localCheck
* @param bool $mustBeChecked
*
* @throws CircleDoesNotExistException
* @throws ConfigNoCircleAvailableException
* @throws GlobalScaleDSyncException
* @throws GlobalScaleEventException
*/
public function verify(GSEvent $event, bool $localCheck = false, bool $mustBeChecked = false): void {
parent::verify($event, $localCheck, true);

$member = $event->getMember();
$circles = $this->circlesRequest->getCircles($member->getUserId(), 0, '', Member::LEVEL_OWNER);

$destroyedCircles = [];
$promotedAdmins = [];
foreach ($circles as $circle) {
$members =
$this->membersRequest->forceGetMembers($circle->getUniqueId(), Member::LEVEL_MEMBER);

if ($circle->getType() === Circle::CIRCLES_PERSONAL || sizeof($members) === 1) {
$destroyedCircles[] = $circle->getUniqueId();
continue;
}

$promotedAdmins[] = $this->getOlderAdmin($members);
}

$event->getData()
->sArray('destroyedCircles', $destroyedCircles)
->sArray('promotedAdmins', $promotedAdmins);

$this->miscService->log(json_encode($event->getData()));
}


/**
* @param GSEvent[] $events
*/
public function result(array $events): void {
}


/**
* @param GSEvent $event
*/
public function manage(GSEvent $event): void {
$member = $event->getMember();

$this->membersRequest->removeAllMembershipsFromUser($member);

$data = $event->getData();
$this->destroyCircles($data->gArray('destroyedCircles'));
$this->promotedAdmins($data->gArray('promotedAdmins'));
}


/**
* @param Member[] $members
*
* @return string
*/
private function getOlderAdmin(array $members) {
foreach ($members as $member) {
if ($member->getLevel() === Member::LEVEL_ADMIN) {
return $member->getMemberId();
}
}
foreach ($members as $member) {
if ($member->getLevel() === Member::LEVEL_MODERATOR) {
return $member->getMemberId();
}
}
foreach ($members as $member) {
if ($member->getLevel() === Member::LEVEL_MEMBER) {
return $member->getMemberId();
}
}
}


/**
* @param array $circleIds
*/
private function destroyCircles(array $circleIds) {
foreach ($circleIds as $circleId) {
$this->circlesRequest->destroyCircle($circleId);
$this->membersRequest->removeAllFromCircle($circleId);
}
}


/**
* @param array $memberIds
*/
private function promotedAdmins(array $memberIds) {
foreach ($memberIds as $memberId) {
try {
$member = $this->membersRequest->forceGetMemberById($memberId);
$member->setLevel(Member::LEVEL_OWNER);
$this->membersRequest->updateMember($member);
} catch (MemberDoesNotExistException $e) {
}
}
}

}

1 change: 1 addition & 0 deletions lib/Model/GlobalScale/GSEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class GSEvent implements JsonSerializable {
const MEMBER_LEVEL = 'GlobalScale\MemberLevel';
const MEMBER_UPDATE = 'GlobalScale\MemberUpdate';
const MEMBER_REMOVE = 'GlobalScale\MemberRemove';
const USER_DELETED = 'GlobalScale\UserDeleted';

const FILE_SHARE = 'GlobalScale\FileShare';

Expand Down
50 changes: 1 addition & 49 deletions lib/Service/CirclesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,54 +442,6 @@ public function infoCircleByName($circleName) {
}


/**
* // TODO - check this on GS setup
* When a user is removed.
* Before deleting a user from the cloud, we assign a new owner to his Circles.
* Remove the Circle if it has no admin.
*
* @param string $userId
*/
public function onUserRemoved($userId) {
$circles = $this->circlesRequest->getCircles($userId, 0, '', Member::LEVEL_OWNER);

foreach ($circles as $circle) {

$members =
$this->membersRequest->forceGetMembers($circle->getUniqueId(), Member::LEVEL_ADMIN);

if (sizeof($members) === 1) {
$this->circlesRequest->destroyCircle($circle->getUniqueId());
continue;
}

$this->switchOlderAdminToOwner($circle, $members);
}
}


/**
* // TODO - check this on GS setup
* switchOlderAdminToOwner();
*
* @param Circle $circle
* @param Member[] $members
*/
private function switchOlderAdminToOwner(Circle $circle, $members) {

foreach ($members as $member) {
if ($member->getLevel() === Member::LEVEL_ADMIN) {
$member->setLevel(Member::LEVEL_OWNER);
$this->membersRequest->updateMember($member);
$this->eventsService->onMemberOwner($circle, $member);

return;
}
}

}


/**
* Convert a Type in String to its Bit Value
*
Expand Down Expand Up @@ -587,7 +539,7 @@ public function checkThatCircleIsNotFull(Circle $circle) {
$circle->getUniqueId(), Member::LEVEL_MEMBER, true
);

$limit = (int) $circle->getSetting('members_limit');
$limit = (int)$circle->getSetting('members_limit');
if ($limit === -1) {
return;
}
Expand Down
6 changes: 1 addition & 5 deletions lib/Service/GSDownstreamService.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,8 @@ public function requestedEvent(GSEvent $event) {

$gs = $this->globalScaleService->getGlobalScaleEvent($event);
$gs->verify($event, true);

$this->miscService->log('&&&& 1 ' . json_encode($event));

$gs->manage($event);

$this->miscService->log('&&&& 2 ' . json_encode($event));

$this->globalScaleService->asyncBroadcast($event);
}

Expand Down Expand Up @@ -157,6 +152,7 @@ public function onNewEvent(GSEvent $event) {
$gs = $this->globalScaleService->getGlobalScaleEvent($event);
$gs->manage($event);
} catch (Exception $e) {
$this->miscService->log('issue onNewEvent - ' . $e->getMessage());
}
}

Expand Down
18 changes: 13 additions & 5 deletions lib/Service/MembersService.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ private function addGroupMembers(Circle $circle, $groupId) {

/**
* // TODO - check this on GS setup
*
* @param Circle $circle
* @param string $mails
*
Expand Down Expand Up @@ -588,14 +589,21 @@ public function removeMember(


/**
* // TODO - check this on GS setup
* When a user is removed, remove him from all Circles
*
* @param $userId
* @param string $userId
*
* @throws Exception
*/
public function onUserRemoved($userId) {
// TODO: broadcast the event to all instances
$this->membersRequest->removeAllMembershipsFromUser($userId);
public function onUserRemoved(string $userId) {
$event = new GSEvent(GSEvent::USER_DELETED, true, true);

$member = new Member($userId);
$event->setMember($member);
$event->getData()
->s('userId', $userId);

$this->gsUpstreamService->newEvent($event);
}


Expand Down

0 comments on commit fe742d7

Please sign in to comment.