Skip to content

Commit

Permalink
Merge pull request #1 from amsprost/improvements
Browse files Browse the repository at this point in the history
Improvements
  • Loading branch information
joostfaassen authored Aug 12, 2019
2 parents 179b2dd + b39979f commit 6fb5056
Show file tree
Hide file tree
Showing 26 changed files with 750 additions and 302 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"symfony/cache": "^4.3",
"symfony/property-access": "^4.3",
"symfony/property-info": "^4.3",
"symfony/serializer": "^4.3"
"symfony/serializer": "^4.3",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^8"
Expand Down
13 changes: 13 additions & 0 deletions src/Services/Camunda/CamundaGroupMapper.php
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']);
}
}
45 changes: 45 additions & 0 deletions src/Services/Camunda/CamundaGroupMemberProvider.php
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;
}
}
43 changes: 43 additions & 0 deletions src/Services/Camunda/CamundaGroupProvider.php
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;
}
}
23 changes: 23 additions & 0 deletions src/Services/Camunda/CamundaUserMapper.php
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,
]
);
}
}
43 changes: 43 additions & 0 deletions src/Services/Camunda/CamundaUserProvider.php
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;
}
}
4 changes: 2 additions & 2 deletions src/Services/PasswordHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public function __construct(?string $salt)
$this->salt = $salt;
}

public function getDefaultPassword(string $username)
public function getDefaultPassword(string $username): string
{
return substr(md5($username . $this->salt), 0, 8);
}
}
}
118 changes: 118 additions & 0 deletions src/Services/SyncRemover/CamundaSyncRemover.php
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()));
}
}
10 changes: 10 additions & 0 deletions src/Services/SyncRemover/SyncRemoverInterface.php
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace LinkORB\OrgSync\SynchronizationAdapter\AdapterFactory;

use LinkORB\OrgSync\DTO\Target;
use LinkORB\OrgSync\Services\SyncRemover\SyncRemoverInterface;
use LinkORB\OrgSync\SynchronizationAdapter\GroupPush\GroupPushInterface;
use LinkORB\OrgSync\SynchronizationAdapter\OrganizationPull\OrganizationPullInterface;
use LinkORB\OrgSync\SynchronizationAdapter\OrganizationPush\OrganizationPushInterface;
Expand All @@ -22,4 +23,6 @@ public function createOrganizationPushAdapter(): OrganizationPushInterface;
public function createSetPasswordAdapter(): SetPasswordInterface;

public function createUserPushAdapter(): UserPushInterface;

public function createSyncRemover(): SyncRemoverInterface;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@
use LinkORB\OrgSync\DTO\Group;
use LinkORB\OrgSync\DTO\Target;
use LinkORB\OrgSync\DTO\User;
use LinkORB\OrgSync\Services\Camunda\CamundaGroupMapper;
use LinkORB\OrgSync\Services\Camunda\CamundaGroupMemberProvider;
use LinkORB\OrgSync\Services\Camunda\CamundaGroupProvider;
use LinkORB\OrgSync\Services\Camunda\CamundaUserMapper;
use LinkORB\OrgSync\Services\Camunda\CamundaUserProvider;
use LinkORB\OrgSync\Services\Camunda\ResponseChecker;
use LinkORB\OrgSync\Services\PasswordHelper;
use LinkORB\OrgSync\Services\SyncRemover\CamundaSyncRemover;
use LinkORB\OrgSync\Services\SyncRemover\SyncRemoverInterface;
use LinkORB\OrgSync\SynchronizationAdapter\GroupPush\CamundaGroupPushAdapter;
use LinkORB\OrgSync\SynchronizationAdapter\GroupPush\GroupPushInterface;
use LinkORB\OrgSync\SynchronizationAdapter\OrganizationPull\OrganizationPullInterface;
Expand All @@ -27,9 +34,12 @@ class CamundaAdapterFactory implements AdapterFactoryInterface
/** @var PasswordHelper */
private $passwordHelper;

/** @var string|null */
private $defaultPassSalt;

public function __construct(?string $defaultPassSalt)
{
$this->passwordHelper = $this->getPasswordHelper($defaultPassSalt);
$this->defaultPassSalt = $defaultPassSalt;
}

public function setTarget(Target $target): AdapterFactoryInterface
Expand All @@ -46,6 +56,7 @@ public function setTarget(Target $target): AdapterFactoryInterface
}

$this->camundaClient = $this->getClient($clientOptions);
$this->passwordHelper = $this->getPasswordHelper($this->defaultPassSalt . $target->getName());

return $this;
}
Expand Down Expand Up @@ -83,6 +94,16 @@ public function createOrganizationPushAdapter(): OrganizationPushInterface
return new CamundaOrganizationPushAdapter();
}

public function createSyncRemover(): SyncRemoverInterface
{
$userProvider = new CamundaUserProvider($this->camundaClient, new CamundaUserMapper());
$groupMapper = new CamundaGroupMapper();
$groupProvider = new CamundaGroupProvider($this->camundaClient, $groupMapper);
$userGroupProvider = new CamundaGroupMemberProvider($this->camundaClient, $groupMapper);

return new CamundaSyncRemover($userProvider, $groupProvider, $userGroupProvider, $this->camundaClient);
}

protected function getClient(array $options): Client
{
return new Client($options);
Expand Down
Loading

0 comments on commit 6fb5056

Please sign in to comment.