user webhook: registration and delete

This commit is contained in:
aynsix
2019-09-20 18:01:23 +04:00
parent 861785e7ab
commit 5d8132b08d
11 changed files with 61 additions and 18 deletions

View File

@@ -167,11 +167,11 @@ class AccountService
* @param string $login * @param string $login
* @throws AccountException * @throws AccountException
*/ */
public function deleteAccount($login = null) public function deleteAccount($login = null, array $grantedBaseIdList = array())
{ {
$user = $this->getUserOrCurrentUser($login); $user = $this->getUserOrCurrentUser($login);
$this->userManipulator->delete($user); $this->userManipulator->delete($user, $grantedBaseIdList);
} }
/** /**

View File

@@ -328,11 +328,9 @@ class RegistrationService
$autoReg = $acl->get_granted_base(); $autoReg = $acl->get_granted_base();
$granted = [];
foreach ($autoReg as $baseId => $collection) { foreach ($autoReg as $baseId => $collection) {
$granted[$baseId] = $collection->get_label($this->app['locale']); $granted[$baseId] = $collection->get_label($this->app['locale']);
}
if(count($granted) > 0) {
$this->app['manipulator.webhook-event']->create( $this->app['manipulator.webhook-event']->create(
WebhookEvent::USER_REGISTRATION_GRANTED, WebhookEvent::USER_REGISTRATION_GRANTED,
WebhookEvent::USER_REGISTRATION_TYPE, WebhookEvent::USER_REGISTRATION_TYPE,
@@ -340,8 +338,11 @@ class RegistrationService
'user_id' => $user->getId(), 'user_id' => $user->getId(),
'granted' => $granted, 'granted' => $granted,
'rejected' => [] 'rejected' => []
] ],
[$baseId]
); );
unset($granted);
} }

View File

@@ -515,9 +515,9 @@ class UserController extends Controller
$denyColl[] = $label; $denyColl[] = $label;
$hookData['rejected'][$bas] = $label; $hookData['rejected'][$bas] = $label;
} }
}
$this->app['manipulator.webhook-event']->create($hookName, $hookType, $hookData); $this->app['manipulator.webhook-event']->create($hookName, $hookType, $hookData, [$bas]);
}
if ($user->hasMailNotificationsActivated() && (0 !== count($acceptColl) || 0 !== count($denyColl))) { if ($user->hasMailNotificationsActivated() && (0 !== count($acceptColl) || 0 !== count($denyColl))) {
$message = ''; $message = '';

View File

@@ -518,7 +518,9 @@ class AccountController extends Controller
$this->getApiApplicationManipulator()->deleteApiApplications($applications); $this->getApiApplicationManipulator()->deleteApiApplications($applications);
// revoke access and delete phraseanet user account // get list of old granted base_id then revoke access and delete phraseanet user account
$oldGrantedBaseIds = array_keys($this->app->getAclForUser($user)->get_granted_base());
$list = array_keys($this->app['repo.collections-registry']->getBaseIdMap()); $list = array_keys($this->app['repo.collections-registry']->getBaseIdMap());
@@ -535,7 +537,7 @@ class AccountController extends Controller
$mail = MailSuccessAccountDelete::create($this->app, $receiver); $mail = MailSuccessAccountDelete::create($this->app, $receiver);
$this->app['manipulator.user']->delete($user); $this->app['manipulator.user']->delete($user, [$user->getId() => $oldGrantedBaseIds]);
$this->deliver($mail); $this->deliver($mail);
} }

View File

@@ -45,7 +45,7 @@ class WebhookUserEventSubscriber implements EventSubscriberInterface
'user_id' => $event->getUserId(), 'user_id' => $event->getUserId(),
'email' => $event->getEmailAddress(), 'email' => $event->getEmailAddress(),
'login' => $event->getLogin() 'login' => $event->getLogin()
]); ], $event->getGrantedBaseIds());
} }
public static function getSubscribedEvents() public static function getSubscribedEvents()

View File

@@ -36,4 +36,12 @@ class DeletedEvent extends UserEvent
{ {
return $this->args['email']; return $this->args['email'];
} }
/**
* @return array
*/
public function getGrantedBaseIds()
{
return $this->args['grantedBaseIds'];
}
} }

View File

@@ -16,7 +16,7 @@ class Version
/** /**
* @var string * @var string
*/ */
private $number = '4.1.0-alpha.15a'; private $number = '4.1.0-alpha.16a';
/** /**
* @var string * @var string

View File

@@ -73,10 +73,12 @@ class Edit extends \Alchemy\Phrasea\Helper\Helper
{ {
$list = array_keys($this->app->getAclForUser($this->app->getAuthenticatedUser())->get_granted_base([\ACL::CANADMIN])); $list = array_keys($this->app->getAclForUser($this->app->getAuthenticatedUser())->get_granted_base([\ACL::CANADMIN]));
$oldGrantedBaseIds = array_keys($this->app->getAclForUser($user)->get_granted_base());
$this->app->getAclForUser($user)->revoke_access_from_bases($list); $this->app->getAclForUser($user)->revoke_access_from_bases($list);
if ($this->app->getAclForUser($user)->is_phantom()) { if ($this->app->getAclForUser($user)->is_phantom()) {
$this->app['manipulator.user']->delete($user); $this->app['manipulator.user']->delete($user, [$user->getId() => $oldGrantedBaseIds]);
} }
return $this; return $this;

View File

@@ -68,6 +68,14 @@ class WebhookEvent
*/ */
private $created; private $created;
/**
* List of collection base_id concerned
* @var array
*
* @ORM\Column(name="collection_base_ids", type="json_array", nullable=true)
*/
private $collectionBaseIds;
/** /**
* @param \DateTime $created * @param \DateTime $created
* *
@@ -175,4 +183,24 @@ class WebhookEvent
return $this; return $this;
} }
/**
* @param array $collectionBaseIds
*
* @return $this
*/
public function setCollectionBaseIds(array $collectionBaseIds)
{
$this->collectionBaseIds = $collectionBaseIds;
return $this;
}
/**
* @return array
*/
public function getCollectionBaseIds()
{
return $this->collectionBaseIds;
}
} }

View File

@@ -126,8 +126,9 @@ class UserManipulator implements ManipulatorInterface
* Deletes a user. * Deletes a user.
* *
* @param User|User[] $users * @param User|User[] $users
* @param array $grantedBaseIdList List of the old granted base_id per userId [user_id => [base_id, ...] ]
*/ */
public function delete($users) public function delete($users, array $grantedBaseIdList = array())
{ {
/** @var User $user */ /** @var User $user */
foreach ($this->makeTraversable($users) as $user) { foreach ($this->makeTraversable($users) as $user) {
@@ -146,9 +147,10 @@ class UserManipulator implements ManipulatorInterface
new DeletedEvent( new DeletedEvent(
null, null,
array( array(
'user_id'=>$old_id, 'user_id' => $old_id,
'login'=>$old_login, 'login' => $old_login,
'email'=>$old_email 'email' => $old_email,
'grantedBaseIds' => isset($grantedBaseIdList[$old_id]) ? $grantedBaseIdList[$old_id] : []
) )
) )
); );

View File

@@ -79,7 +79,7 @@ class UserDeletionTest extends \PhraseanetAuthenticatedWebTestCase
$apiLog = $apiLogManipulator->create($account, new Request(), new Response()); $apiLog = $apiLogManipulator->create($account, new Request(), new Response());
$apiLogId = $apiLog->getId(); $apiLogId = $apiLog->getId();
$this->userManipulator->delete($this->user, true); $this->userManipulator->delete($this->user);
$this->assertTrue($this->user->isDeleted(), 'User was not properly deleted'); $this->assertTrue($this->user->isDeleted(), 'User was not properly deleted');
$apiLogRepository->clear(); $apiLogRepository->clear();