Skip to content

Commit

Permalink
Groups hfix
Browse files Browse the repository at this point in the history
  • Loading branch information
satanio committed Sep 16, 2021
1 parent 6f065bc commit 469ca3a
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 17 deletions.
67 changes: 61 additions & 6 deletions app/Endpoints/AuthorizeEndpoints/UserGroupController.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Controllers;

use App\Repositories\Authorization\UserRepository;
use App\Entity\{
Authorization\User,
Authorization\UserGroup,
Expand Down Expand Up @@ -54,7 +55,13 @@ protected function setData(IdentifiedObject $userGroup, ArgumentParser $body): v
{
/** @var UserGroup $userGroup */
!$body->hasKey('name') ?: $userGroup->setName($body->getString('name'));
!$body->hasKey('type') ?: $userGroup->setType($body->getString('type'));
if ($body->hasKey('type')) {
$type = $body->getString('type');
if (array_key_exists($type, UserGroup::SPACES)) {
$type = UserGroup::SPACES[$type];
}
$userGroup->setType($type);
}
!$body->hasKey('description') ?: $userGroup->setDescription($body->getString('description'));
}

Expand All @@ -68,12 +75,13 @@ protected function createObject(ArgumentParser $body): IdentifiedObject

protected function checkInsertObject(IdentifiedObject $userGroup): void
{
//TODO
$userGroup->setIsPublic(intval($userGroup->getIsPublic()));
}


public function delete(Request $request, Response $response, ArgumentParser $args): Response
{
// TODO: Should not forcefully delete
// TODO: verify user dependencies
return parent::delete($request, $response, $args);
}
Expand All @@ -84,7 +92,7 @@ protected function getValidator(): Assert\Collection
return new Assert\Collection([
'name' => new Assert\Type(['type' => 'string']),
'description' => new Assert\Type(['type' => 'string']),
'type' => new Assert\Type(['type' => 'integer']),
'type' => new Assert\Type(['type' => 'string']),
]);
}

Expand Down Expand Up @@ -139,9 +147,56 @@ public function getAccessFilter(array $userGroups): ?array
foreach (array_flip($accObj) as $id => $trash) {
$userGroups[$id] = $dql;
}
$quasiFilter = array_map(function () use ($dql) { return $dql; }, $userGroups);
unset($quasiFilter[UserGroup::PUBLIC_SPACE]);
return $quasiFilter;
return array_map(function () use ($dql) { return $dql; }, $userGroups);
}

public function addUsers(Request $request, Response $response, ArgumentParser $args): Response
{
$this->isAuthorized($request->getAttribute('oauth_user_id'));
$body = new ArgumentParser($request->getParsedBody());
if ($body->hasKey('emails')) {
$this->validate($body, new Assert\Collection([
'emails' => new Assert\Type(['type' => 'array'])]));
$emails = $body->getArray('emails');
/** @var User[] $usersToAdd */
$usersToAdd = $this->orm->getRepository(User::class)->findBy(['email' => $emails]);
/** @var UserGroup $group */
$group = $this->getObject($this->getModifyId($args));
$usersToInviteCount = 0;
$usersAddedCount = 0;
foreach ($usersToAdd as $u) {
if (in_array($u->getEmail(), $emails))
{
$usersToInviteCount++;
}
if (!$group->getAllUsers()->contains($u)) {
$linkToMySpace = new UserGroupToUser();
$linkToMySpace->setUserId($u);
$linkToMySpace->setUserGroupId($group);
$linkToMySpace->setRoleId(User::SPECTATOR);
$this->orm->persist($linkToMySpace);
$usersAddedCount++;
}
}
$this->orm->flush();
return self::formatOk($response, [
'users' => $emails,
'added' => $usersAddedCount,
'invited' => $usersToInviteCount]);
}

}

/**
* @param int|null $id
* @throws InvalidAuthenticationException
*/
private function isAuthorized(?int $id)
{
if(is_null($id)) {
throw new InvalidAuthenticationException('User not authorized.', 'This endpoint is accessible' .
' only with valid token.');
}
}

public function canAdd(int $role, int $id): bool
Expand Down
1 change: 1 addition & 0 deletions app/Entity/Authorization/User.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class User implements UserEntityInterface, IdentifiedObject
const CAN_EDIT = [2,3,6,7,8];
const CAN_DELETE = [1,3,5,7,8];
const OWNER_ROLE = 8;
const SPECTATOR = 10;

const PASSWORD_ALGORITHM = PASSWORD_DEFAULT;

Expand Down
33 changes: 22 additions & 11 deletions app/Entity/Authorization/UserGroup.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@
class UserGroup implements IdentifiedObject
{

const PUBLIC_SPACE = 1;
const ADMIN_SPACE = 2;
const USER_SPACE = 3;
const WORK_SPACE = 4;

const SPACES = [
'user space' => self::USER_SPACE,
'work space' => self::WORK_SPACE
];

use Identifier;

Expand All @@ -28,7 +33,7 @@ class UserGroup implements IdentifiedObject
private $name;

/**
* @ORM\OneToMany(targetEntity="UserGroupToUser", mappedBy="userGroupId")
* @ORM\OneToMany(targetEntity="UserGroupToUser", mappedBy="userGroupId", cascade={"remove"})
*/
private $users;

Expand All @@ -42,13 +47,13 @@ class UserGroup implements IdentifiedObject
* @var string
* @ORM\Column
*/
private $type;
private $type = self::USER_SPACE;

/**
* @var boolean
* @var bool
* @ORM\Column(name="is_public")
*/
private $isPublic;
private $isPublic = false;

public function getIdentifier()
{
Expand All @@ -62,11 +67,19 @@ public function getName()
}


public function getUsers()
//FIXME: refactor, this returns only links
public function getUsers(): Collection
{
return $this->users;
}

public function getAllUsers(): Collection
{
return $this->getUsers()->map(function (UserGroupToUser $userGroupToUser) {
return $userGroupToUser->getUserId();
});
}


public function getType()
{
Expand All @@ -89,21 +102,19 @@ public function setIdentifier($identifier)
public function setName(string $name)
{
$this->name = $name;
return $name;
}


public function setType(int $type)
public function setType(string $type)
{
$this->type = $type;
return $type;

}


public function setDescription(string $description)
{
$this->description = $description;
return $description;
}


Expand Down
3 changes: 3 additions & 0 deletions app/routes.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@ public function addAnnotationsRoutes(): RouteHelper
->setRoute(Ctl\UserGroupController::class, '/userGroups')
->setAuthMask(true)
->register();
// Group management
$app->post('/userGroups/{id:\\d+}/addUsers', Ctl\UserGroupController::class . ':addUsers')
->add(RouteHelper::$authMiddleware);
(new RouteHelper)
->setRoute(Ctl\UserGroupRoleController::class, '/userGroupRoles')
->setMask(RouteHelper::LIST | RouteHelper::DETAIL)
Expand Down

0 comments on commit 469ca3a

Please sign in to comment.