-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from amsprost/improvements
Improvements
- Loading branch information
Showing
26 changed files
with
750 additions
and
302 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace LinkORB\OrgSync\Services\Camunda; | ||
|
||
use LinkORB\OrgSync\DTO\Group; | ||
|
||
class CamundaGroupMapper | ||
{ | ||
public function map(array $data) | ||
{ | ||
return new Group($data['id'], $data['name']); | ||
} | ||
} |
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,45 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace LinkORB\OrgSync\Services\Camunda; | ||
|
||
use GuzzleHttp\Client; | ||
use LinkORB\OrgSync\DTO\Group; | ||
use LinkORB\OrgSync\DTO\User; | ||
|
||
class CamundaGroupMemberProvider | ||
{ | ||
/** | ||
* @var Client | ||
*/ | ||
private $httpClient; | ||
|
||
/** | ||
* @var CamundaGroupMapper | ||
*/ | ||
private $mapper; | ||
|
||
public function __construct(Client $httpClient, CamundaGroupMapper $mapper) | ||
{ | ||
$this->httpClient = $httpClient; | ||
$this->mapper = $mapper; | ||
} | ||
|
||
/** | ||
* @param User $user | ||
* @return Group[] | ||
*/ | ||
public function getGroupsForUser(User $user): array | ||
{ | ||
$response = $this->httpClient->get(sprintf('group?member=%s', $user->getUsername())); | ||
|
||
$groups = []; | ||
|
||
foreach (json_decode($response->getBody()->getContents(), true) as $group) { | ||
$groupDto = $this->mapper->map($group); | ||
|
||
$groups[$groupDto->getName()] = $groupDto; | ||
} | ||
|
||
return $groups; | ||
} | ||
} |
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,43 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace LinkORB\OrgSync\Services\Camunda; | ||
|
||
use GuzzleHttp\Client; | ||
use LinkORB\OrgSync\DTO\Group; | ||
|
||
class CamundaGroupProvider | ||
{ | ||
/** | ||
* @var Client | ||
*/ | ||
private $httpClient; | ||
|
||
/** | ||
* @var CamundaGroupMapper | ||
*/ | ||
private $mapper; | ||
|
||
public function __construct(Client $httpClient, CamundaGroupMapper $mapper) | ||
{ | ||
$this->httpClient = $httpClient; | ||
$this->mapper = $mapper; | ||
} | ||
|
||
/** | ||
* @return Group[] | ||
*/ | ||
public function getGroups(): array | ||
{ | ||
$response = $this->httpClient->get('group'); | ||
|
||
$groups = []; | ||
|
||
foreach (json_decode($response->getBody()->getContents(), true) as $group) { | ||
$groupDto = $this->mapper->map($group); | ||
|
||
$groups[$groupDto->getName()] = $groupDto; | ||
} | ||
|
||
return $groups; | ||
} | ||
} |
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,23 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace LinkORB\OrgSync\Services\Camunda; | ||
|
||
use LinkORB\OrgSync\DTO\User; | ||
|
||
final class CamundaUserMapper | ||
{ | ||
public function map(array $data): User | ||
{ | ||
return new User( | ||
$data['id'], | ||
null, | ||
$data['email'] ?? null, | ||
null, | ||
null, | ||
[ | ||
'firstName' => $data['firstName'] ?? null, | ||
'lastName' => $data['lastName'] ?? null, | ||
] | ||
); | ||
} | ||
} |
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,43 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace LinkORB\OrgSync\Services\Camunda; | ||
|
||
use GuzzleHttp\Client; | ||
use LinkORB\OrgSync\DTO\User; | ||
|
||
class CamundaUserProvider | ||
{ | ||
/** | ||
* @var Client | ||
*/ | ||
private $httpClient; | ||
|
||
/** | ||
* @var CamundaUserMapper | ||
*/ | ||
private $mapper; | ||
|
||
public function __construct(Client $httpClient, CamundaUserMapper $mapper) | ||
{ | ||
$this->httpClient = $httpClient; | ||
$this->mapper = $mapper; | ||
} | ||
|
||
/** | ||
* @return User[] | ||
*/ | ||
public function getUsers(): array | ||
{ | ||
$response = $this->httpClient->get('user'); | ||
|
||
$users = []; | ||
|
||
foreach (json_decode($response->getBody()->getContents(), true) as $user) { | ||
$userDto = $this->mapper->map($user); | ||
|
||
$users[$userDto->getUsername()] = $userDto; | ||
} | ||
|
||
return $users; | ||
} | ||
} |
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,118 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace LinkORB\OrgSync\Services\SyncRemover; | ||
|
||
use GuzzleHttp\Client; | ||
use LinkORB\OrgSync\DTO\Group; | ||
use LinkORB\OrgSync\DTO\Organization; | ||
use LinkORB\OrgSync\DTO\User; | ||
use LinkORB\OrgSync\Services\Camunda\CamundaGroupMemberProvider; | ||
use LinkORB\OrgSync\Services\Camunda\CamundaGroupProvider; | ||
use LinkORB\OrgSync\Services\Camunda\CamundaUserProvider; | ||
|
||
class CamundaSyncRemover implements SyncRemoverInterface | ||
{ | ||
/** | ||
* @var CamundaUserProvider | ||
*/ | ||
private $usersProvider; | ||
|
||
/** | ||
* @var CamundaGroupProvider | ||
*/ | ||
private $groupsProvider; | ||
|
||
/** | ||
* @var CamundaGroupMemberProvider | ||
*/ | ||
private $userGroupsProvider; | ||
|
||
/** | ||
* @var Client | ||
*/ | ||
private $httpClient; | ||
|
||
public function __construct( | ||
CamundaUserProvider $usersProvider, | ||
CamundaGroupProvider $groupsProvider, | ||
CamundaGroupMemberProvider $userGroupsProvider, | ||
Client $httpClient | ||
) { | ||
$this->usersProvider = $usersProvider; | ||
$this->groupsProvider = $groupsProvider; | ||
$this->userGroupsProvider = $userGroupsProvider; | ||
$this->httpClient = $httpClient; | ||
} | ||
|
||
public function removeNonExists(Organization $organization): void | ||
{ | ||
$existingUsers = $this->usersProvider->getUsers(); | ||
$existingGroups = $this->groupsProvider->getGroups(); | ||
|
||
// Removing users | ||
$syncUsers = []; | ||
foreach ($organization->getUsers() as $user) { | ||
$syncUsers[$user->getUsername()] = $user; | ||
} | ||
|
||
foreach ($existingUsers as $existingUser) { | ||
if (!isset($syncUsers[$existingUser->getUsername()])) { | ||
$this->removeUser($existingUser); | ||
} | ||
} | ||
|
||
// Removing groups | ||
$syncGroups = []; | ||
foreach ($organization->getGroups() as $group) { | ||
$syncGroups[$group->getName()] = $group; | ||
} | ||
|
||
/** @var Group $existingGroup */ | ||
foreach ($existingGroups as $existingGroup) { | ||
if (!isset($syncGroups[$existingGroup->getName()])) { | ||
$this->removeGroup($existingGroup); | ||
} | ||
} | ||
|
||
// Removing users from groups | ||
$syncGroupUsers = []; | ||
foreach ($organization->getGroups() as $group) { | ||
foreach ($group->getMembers() as $member) { | ||
$syncGroupUsers[$group->getName()][$member->getUsername()] = true; | ||
} | ||
} | ||
|
||
foreach ($existingUsers as $user) { | ||
if (!isset($syncUsers[$user->getUsername()])) { | ||
continue; | ||
} | ||
|
||
$userGroups = $this->userGroupsProvider->getGroupsForUser($user); | ||
|
||
foreach ($userGroups as $group) { | ||
if (!isset($syncGroups[$group->getName()])) { | ||
continue; | ||
} | ||
|
||
if (!($syncGroupUsers[$group->getName()][$user->getUsername()] ?? false)) { | ||
$this->removeMemberFromGroup($user, $group); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private function removeMemberFromGroup(User $user, Group $group): void | ||
{ | ||
$this->httpClient->delete(sprintf('group/%s/members/%s', $group->getName(), $user->getUsername())); | ||
} | ||
|
||
private function removeGroup(Group $group): void | ||
{ | ||
$this->httpClient->delete(sprintf('group/%s', $group->getName())); | ||
} | ||
|
||
private function removeUser(User $user): void | ||
{ | ||
$this->httpClient->delete(sprintf('user/%s', $user->getUsername())); | ||
} | ||
} |
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,10 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace LinkORB\OrgSync\Services\SyncRemover; | ||
|
||
use LinkORB\OrgSync\DTO\Organization; | ||
|
||
interface SyncRemoverInterface | ||
{ | ||
public function removeNonExists(Organization $organization) :void; | ||
} |
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
Oops, something went wrong.