Add user listener to create a resource node when calling /api/users

#3811
pull/3890/head
Julio Montoya 4 years ago
parent 55040c27be
commit df36be9960
  1. 64
      src/CoreBundle/Entity/Listener/UserListener.php
  2. 17
      src/CoreBundle/Entity/User.php
  3. 8
      src/CoreBundle/Repository/Node/UserRepository.php
  4. 8
      src/CoreBundle/Resources/config/services.yml

@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Entity\Listener;
use Chamilo\CoreBundle\Entity\ResourceNode;
use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\Repository\Node\UserRepository;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Exception;
use Symfony\Component\Security\Core\Security;
class UserListener
{
protected UserRepository $userRepository;
protected Security $security;
public function __construct(UserRepository $userRepository, Security $security)
{
$this->userRepository = $userRepository;
$this->security = $security;
}
/**
* This code is executed when a new user is created.
*/
public function prePersist(User $user, LifecycleEventArgs $args): void
{
error_log('User listener prePersist');
if ($user) {
$this->userRepository->updateCanonicalFields($user);
$this->userRepository->updatePassword($user);
if (!$user->hasResourceNode()) {
$em = $args->getEntityManager();
$resourceNode = new ResourceNode();
$resourceNode
->setTitle($user->getUsername())
->setCreator($this->security->getUser())
->setResourceType($this->userRepository->getResourceType())
;
$em->persist($resourceNode);
$user->setResourceNode($resourceNode);
}
}
}
/**
* This code is executed when a user is updated.
*
* @throws Exception
*/
public function preUpdate(User $user, LifecycleEventArgs $args): void
{
error_log('User listener preUpdate');
if ($user) {
$this->userRepository->updatePassword($user);
$this->userRepository->updateCanonicalFields($user);
}
}
}

@ -48,10 +48,9 @@ use UserManager;
* },
* )
*
* @ApiFilter(SearchFilter::class, properties={"username":"partial", "firstname":"partial"})
* @ApiFilter(SearchFilter::class, properties={"username":"partial", "firstname":"partial", "lastname":"partial"})
* @ApiFilter(BooleanFilter::class, properties={"isActive"})
*
* @ORM\HasLifecycleCallbacks
* @ORM\Table(
* name="user",
* indexes={
@ -60,6 +59,7 @@ use UserManager;
* )
* @UniqueEntity("username")
* @ORM\Entity
* @ORM\EntityListeners({"Chamilo\CoreBundle\Entity\Listener\UserListener"})
*/
class User implements UserInterface, EquatableInterface, ResourceInterface, ResourceIllustrationInterface
{
@ -126,6 +126,9 @@ class User implements UserInterface, EquatableInterface, ResourceInterface, Reso
*/
protected string $username;
/**
* @Groups({"user:write"})
*/
protected ?string $plainPassword = null;
/**
@ -222,7 +225,7 @@ class User implements UserInterface, EquatableInterface, ResourceInterface, Reso
protected ?string $gender = null;
/**
* @Groups({"user:read", "user:write"})
* @Groups({"user:read"})
* @ORM\Column(name="last_login", type="datetime", nullable=true)
*/
protected ?DateTime $lastLogin;
@ -266,6 +269,7 @@ class User implements UserInterface, EquatableInterface, ResourceInterface, Reso
protected Collection $dropBoxSentFiles;
/**
* An array of roles. Example: ROLE_USER, ROLE_TEACHER, ROLE_ADMIN
* @Groups({"user:read", "user:write", "user_json:read"})
* @ORM\Column(type="array")
*
@ -857,13 +861,6 @@ class User implements UserInterface, EquatableInterface, ResourceInterface, Reso
return $this;
}
/**
* @ORM\PostPersist()
*/
public function postPersist(LifecycleEventArgs $args): void
{
}
public function getDropBoxSentFiles(): Collection
{
return $this->dropBoxSentFiles;

@ -145,9 +145,11 @@ class UserRepository extends ResourceRepository implements UserLoaderInterface,
'r'
)
;
$qb->where('r.creator = u');
$qb->andWhere('r.parent IS NULL');
$qb->getFirstResult();
$qb
->where('r.creator = u')
->andWhere('r.parent IS NULL')
->getFirstResult()
;
$rootUser = $qb->getQuery()->getSingleResult();

@ -121,12 +121,18 @@ services:
tags:
- {name: kernel.event_listener, event: chamilo_course.course.session, method: onSessionAccessEvent}
# When Course entity is loaded
# When the Course entity is created/updated
Chamilo\CoreBundle\Entity\Listener\CourseListener:
arguments: ['@Chamilo\CoreBundle\ToolChain', '@chamilo.settings.manager']
tags:
- {name: doctrine.orm.entity_listener, entity_manager: default, lazy: true}
# When the User entity is created/updated -
Chamilo\CoreBundle\Entity\Listener\UserListener:
arguments: [ '@Chamilo\CoreBundle\Repository\Node\UserRepository']
tags:
- { name: doctrine.orm.entity_listener, entity_manager: default, lazy: true }
# Setting user
Chamilo\CoreBundle\EventListener\LegacyListener:
calls:

Loading…
Cancel
Save