Users: Refactor user creation, expecting a creator or the current user

pull/3904/head
Julio Montoya 4 years ago
parent 5df045cb4f
commit 80b197c25d
  1. 3
      src/CoreBundle/DataFixtures/AccessUserFixtures.php
  2. 22
      src/CoreBundle/Entity/AbstractResource.php
  3. 22
      src/CoreBundle/Entity/Listener/UserListener.php
  4. 36
      src/CoreBundle/Entity/User.php
  5. 12
      src/CoreBundle/Entity/Usergroup.php
  6. 5
      src/CoreBundle/Repository/Node/UserRepository.php
  7. 37
      src/CoreBundle/Traits/UserCreatorTrait.php
  8. 5
      src/CoreBundle/Traits/UserTrait.php

@ -19,6 +19,7 @@ class AccessUserFixtures extends Fixture implements ContainerAwareInterface
public const ADMIN_USER_REFERENCE = 'admin';
public const ANON_USER_REFERENCE = 'anon';
public const ACCESS_URL_REFERENCE = 'accessUrl';
private ContainerInterface $container;
public function setContainer(ContainerInterface $container = null): void
@ -46,7 +47,6 @@ class AccessUserFixtures extends Fixture implements ContainerAwareInterface
->setEmail('admin@example.org')
->setOfficialCode('ADMIN')
->setCreatorId(1)
->setAuthSource(PLATFORM_AUTH_SOURCE)
->setTimezone($timezone)
->addUserAsAdmin()
->addRole('ROLE_GLOBAL_ADMIN')
@ -69,7 +69,6 @@ class AccessUserFixtures extends Fixture implements ContainerAwareInterface
->setEmail('anonymous@localhost')
->setOfficialCode('anonymous')
->setCreatorId(1)
->setAuthSource(PLATFORM_AUTH_SOURCE)
->setTimezone($timezone)
;
$manager->persist($anon);

@ -9,6 +9,7 @@ namespace Chamilo\CoreBundle\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiSubresource;
use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter;
use Chamilo\CoreBundle\Traits\UserCreatorTrait;
use Chamilo\CourseBundle\Entity\CGroup;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
@ -25,6 +26,8 @@ use Symfony\Component\Validator\Constraints as Assert;
*/
abstract class AbstractResource
{
use UserCreatorTrait;
/**
* @ApiProperty(iri="http://schema.org/contentUrl")
* @Groups({"resource_file:read", "resource_node:read", "document:read", "media_object_read"})
@ -89,31 +92,12 @@ abstract class AbstractResource
*/
public array $resourceLinkEntityList = [];
public ?User $resourceNodeCreator = null;
abstract public function getResourceName(): string;
abstract public function setResourceName(string $name);
//abstract public function setResourceProperties(FormInterface $form, $course, $session, $fileType);
public function getCreator()
{
return $this->getResourceNode()->getCreator();
}
public function setCreator(User $user): self
{
$this->resourceNodeCreator = $user;
return $this;
}
public function getResourceNodeCreator(): ?User
{
return $this->resourceNodeCreator;
}
public function getResourceLinkEntityList()
{
return $this->resourceLinkEntityList;

@ -11,6 +11,7 @@ use Chamilo\CoreBundle\Entity\User;
use Chamilo\CoreBundle\Repository\Node\UserRepository;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Exception;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\Security;
class UserListener
@ -29,7 +30,7 @@ class UserListener
*/
public function prePersist(User $user, LifecycleEventArgs $args): void
{
error_log('User listener prePersist');
//error_log('User listener prePersist');
if ($user) {
$this->userRepository->updateCanonicalFields($user);
@ -40,17 +41,26 @@ class UserListener
}
if (!$user->hasResourceNode()) {
// @todo use the creator id.
$token = $this->security->getToken();
if (null === $token) {
throw new Exception('A user creator is needed, to adding the user in a ResourceNode');
// Check if creator is set with $resource->setCreator()
$creator = $user->getResourceNodeCreator();
if (null === $creator) {
/** @var User|null $creator */
$defaultCreator = $this->security->getUser();
if (null !== $defaultCreator) {
$creator = $defaultCreator;
}
}
if (null === $creator) {
throw new UserNotFoundException('User creator not found, use $resource->setCreator();');
}
$em = $args->getEntityManager();
$resourceNode = new ResourceNode();
$resourceNode
->setTitle($user->getUsername())
->setCreator($this->security->getUser())
->setCreator($creator)
->setResourceType($this->userRepository->getResourceType())
;
$em->persist($resourceNode);

@ -12,6 +12,7 @@ use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiSubresource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\BooleanFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use Chamilo\CoreBundle\Traits\UserCreatorTrait;
use Chamilo\CourseBundle\Entity\CGroupRelTutor;
use Chamilo\CourseBundle\Entity\CGroupRelUser;
use DateTime;
@ -64,6 +65,7 @@ use UserManager;
class User implements UserInterface, EquatableInterface, ResourceInterface, ResourceIllustrationInterface, PasswordAuthenticatedUserInterface
{
use TimestampableEntity;
use UserCreatorTrait;
public const ROLE_DEFAULT = 'ROLE_USER';
public const ROLE_SUPER_ADMIN = 'ROLE_SUPER_ADMIN';
@ -74,6 +76,16 @@ class User implements UserInterface, EquatableInterface, ResourceInterface, Reso
public const STUDENT = 5;
public const ANONYMOUS = 6;
/**
* @Groups({"user_json:read"})
*
* @ORM\OneToOne(
* targetEntity="Chamilo\CoreBundle\Entity\ResourceNode", cascade={"remove"}, orphanRemoval=true
* )
* @ORM\JoinColumn(name="resource_node_id", onDelete="CASCADE")
*/
public ?ResourceNode $resourceNode = null;
/**
* @Groups({"user:read", "resource_node:read", "user_json:read"})
* @ORM\Column(name="id", type="integer")
@ -329,16 +341,6 @@ class User implements UserInterface, EquatableInterface, ResourceInterface, Reso
*/
protected Collection $sessionsAsGeneralCoach;
/**
* @Groups({"user_json:read"})
*
* @ORM\OneToOne(
* targetEntity="Chamilo\CoreBundle\Entity\ResourceNode", cascade={"remove"}, orphanRemoval=true
* )
* @ORM\JoinColumn(name="resource_node_id", onDelete="CASCADE")
*/
protected ?ResourceNode $resourceNode = null;
/**
* @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\ResourceNode", mappedBy="creator")
*
@ -696,17 +698,22 @@ class User implements UserInterface, EquatableInterface, ResourceInterface, Reso
public function __construct()
{
$this->skipResourceNode = false;
$this->uuid = Uuid::v4();
$this->apiToken = null;
$this->biography = '';
$this->website = '';
$this->locale = 'en';
$this->timezone = 'Europe\Paris';
$this->authSource = 'platform';
$this->status = self::STUDENT;
$this->salt = sha1(uniqid('', true));
$this->active = true;
$this->registrationDate = new DateTime();
$this->authSource = 'platform';
$this->skipResourceNode = false;
$this->enabled = false;
$this->locked = false;
$this->expired = false;
$this->courses = new ArrayCollection();
$this->classes = new ArrayCollection();
$this->curriculumItems = new ArrayCollection();
@ -750,9 +757,6 @@ class User implements UserInterface, EquatableInterface, ResourceInterface, Reso
$this->updatedAt = new DateTime();
$this->registrationDate = new DateTime();
$this->enabled = false;
$this->locked = false;
$this->expired = false;
$this->roles = [];
$this->credentialsExpired = false;
$this->credentialsExpireAt = new DateTime();

@ -28,12 +28,15 @@ class Usergroup extends AbstractResource implements ResourceInterface, ResourceI
{
use TimestampableEntity;
public const SOCIAL_CLASS = 1;
public const NORMAL_CLASS = 0;
/**
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue
*/
protected int $id;
protected ?int $id = null;
/**
* @Assert\NotBlank()
@ -48,6 +51,7 @@ class Usergroup extends AbstractResource implements ResourceInterface, ResourceI
protected ?string $description = null;
/**
* @Assert\NotNull()
* @ORM\Column(name="group_type", type="integer", nullable=false)
*/
protected int $groupType;
@ -63,6 +67,8 @@ class Usergroup extends AbstractResource implements ResourceInterface, ResourceI
protected ?string $url = null;
/**
* @Assert\NotNull()
*
* @ORM\Column(name="visibility", type="string", length=255, nullable=false)
*/
protected string $visibility;
@ -73,6 +79,7 @@ class Usergroup extends AbstractResource implements ResourceInterface, ResourceI
protected ?string $authorId = null;
/**
* @Assert\NotNull()
* @ORM\Column(name="allow_members_leave_group", type="integer")
*/
protected int $allowMembersToLeaveGroup;
@ -113,6 +120,9 @@ class Usergroup extends AbstractResource implements ResourceInterface, ResourceI
public function __construct()
{
$this->groupType = self::NORMAL_CLASS;
$this->visibility = GROUP_PERMISSION_OPEN;
$this->allowMembersToLeaveGroup = 0;
$this->users = new ArrayCollection();
$this->urls = new ArrayCollection();
$this->courses = new ArrayCollection();

@ -86,6 +86,11 @@ class UserRepository extends ResourceRepository implements PasswordUpgraderInter
$this->hasher = $hasher;
}
public function createUser(): User
{
return new User();
}
public function updateUser(User $user, bool $andFlush = true): void
{
$this->updateCanonicalFields($user);

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Traits;
use Chamilo\CoreBundle\Entity\ResourceNode;
use Chamilo\CoreBundle\Entity\User;
trait UserCreatorTrait
{
public ?ResourceNode $resourceNode = null;
public ?User $resourceNodeCreator = null;
public function getCreator(): ?User
{
if (null === $this->resourceNode) {
return null;
}
return $this->resourceNode->getCreator();
}
public function setCreator(User $user)
{
$this->resourceNodeCreator = $user;
return $this;
}
public function getResourceNodeCreator(): ?User
{
return $this->resourceNodeCreator;
}
}

@ -10,10 +10,7 @@ use Chamilo\CoreBundle\Entity\User;
trait UserTrait
{
/**
* @return User
*/
public function getUser()
public function getUser(): User
{
return $this->user;
}

Loading…
Cancel
Save