Fix user delete when using resource nodes.

User children will be set to the root user in order to avoid
resourceNode.creator to be null.
pull/3064/head
Julio 5 years ago
parent a9409daf06
commit aa1e6e35e9
  1. 17
      public/main/inc/lib/usermanager.lib.php
  2. 10
      src/CoreBundle/Framework/Container.php
  3. 18
      src/UserBundle/Entity/User.php
  4. 46
      src/UserBundle/Repository/UserRepository.php

@ -758,8 +758,13 @@ class UserManager
$table_work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
$userInfo = api_get_user_info($user_id);
$repository = Container::getUserRepository();
/** @var User $user */
$user = Container::getUserManager()->find($user_id);
$user = $repository->find($user_id);
$repository->deleteUser($user);
// Unsubscribe the user from all groups in all his courses
$sql = "SELECT c.id
@ -916,17 +921,7 @@ class UserManager
$app_plugin = new AppPlugin();
$app_plugin->performActionsWhenDeletingItem('user', $user_id);
$em->remove($user->getResourceNode());
foreach ($user->getGroups() as $group) {
$user->removeGroup($group);
}
// $em->flush();
// Delete user from database
$em->remove($user);
$em->flush();
// Add event to system log
$authorId = api_get_user_id();

@ -17,6 +17,7 @@ use Chamilo\CourseBundle\Repository\CQuizQuestionRepository;
use Chamilo\CourseBundle\Repository\CQuizRepository;
use Chamilo\PageBundle\Entity\Page;
use Chamilo\SettingsBundle\Manager\SettingsManager;
use Chamilo\UserBundle\Repository\UserRepository;
use Sonata\PageBundle\Entity\SiteManager;
use Sonata\UserBundle\Entity\UserManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
@ -337,6 +338,15 @@ class Container
return self::$container->get('Chamilo\CoreBundle\Repository\CourseRepository');
}
/**
* @return UserRepository
*/
public static function getUserRepository()
{
return self::$container->get('Chamilo\UserBundle\Repository\UserRepository');
}
/**
* @return CourseCategoryRepository|object|null
*/

@ -79,14 +79,6 @@ class User extends BaseUser implements ThemeUser, EquatableInterface //implement
public const STUDENT = 5;
public const ANONYMOUS = 6;
/**
* @ORM\OneToOne(
* targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceNode", cascade={"remove"}, orphanRemoval=true
* )
* @ORM\JoinColumn(name="resource_node_id", referencedColumnName="id", onDelete="CASCADE")
*/
public $resourceNode;
/**
* @var int
*
@ -296,6 +288,14 @@ class User extends BaseUser implements ThemeUser, EquatableInterface //implement
*/
protected $sessionAsGeneralCoach;
/**
* @ORM\OneToOne(
* targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceNode", cascade={"remove"}, orphanRemoval=true
* )
* @ORM\JoinColumn(name="resource_node_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $resourceNode;
/**
* @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceNode", mappedBy="creator")
*/
@ -565,7 +565,7 @@ class User extends BaseUser implements ThemeUser, EquatableInterface //implement
}
/**
* @return mixed
* @return ArrayCollection|ResourceNode[]
*/
public function getResourceNodes()
{

@ -47,7 +47,9 @@ use Chamilo\CourseBundle\Entity\CSurveyAnswer;
use Chamilo\CourseBundle\Entity\CWiki;
use Chamilo\TicketBundle\Entity\Ticket;
use Chamilo\UserBundle\Entity\User;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Query\Expr\Join;
use FOS\UserBundle\Model\UserInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
@ -62,6 +64,50 @@ use Symfony\Component\Serializer\Serializer;
*/
class UserRepository extends ResourceRepository
{
public function getRootUser(): User
{
$qb = $this->getRepository()->createQueryBuilder('u');
$qb
->innerJoin(
'u.resourceNode',
'r'
);
$qb->where('r.creator = u');
$qb->andWhere('r.parent IS NULL');
$qb->getFirstResult();
return $qb->getQuery()->getSingleResult();
}
public function deleteUser(User $user)
{
$em = $this->getEntityManager();
$type = $user->getResourceNode()->getResourceType();
$rootUser = $this->getRootUser();
if (null === $rootUser) {
throw new UsernameNotFoundException('Root user not found');
}
// User children will be set to the root user.
$criteria = Criteria::create()->where(Criteria::expr()->eq('resourceType', $type));
$userNodeCreatedList = $user->getResourceNodes()->matching($criteria);
/** @var ResourceNode $userCreated */
foreach ($userNodeCreatedList as $userCreated) {
$userCreated->setCreator($rootUser);
}
$em->remove($user->getResourceNode());
foreach ($user->getGroups() as $group) {
$user->removeGroup($group);
}
$em->remove($user);
$em->flush();
}
public function addUserToResourceNode(int $userId, int $creatorId): ResourceNode
{
/** @var User $user */

Loading…
Cancel
Save