Add user in the resource_node table, change "admin" tool to "global".

Create user repo using in the resource repository factory.
pull/3064/head
Julio 6 years ago
parent 99d196df37
commit bf9357cf5a
  1. 19
      public/main/inc/lib/usermanager.lib.php
  2. 23
      src/CoreBundle/Controller/ResourceController.php
  3. 5
      src/CoreBundle/Migrations/Schema/V200/Version20.php
  4. 2
      src/CoreBundle/Repository/ResourceRepository.php
  5. 13
      src/CoreBundle/Resources/config/tools.yml
  6. 4
      src/CoreBundle/Tool/GlobalTool.php
  7. 64
      src/UserBundle/Entity/User.php
  8. 14
      src/UserBundle/Repository/UserRepository.php
  9. 8
      src/UserBundle/Resources/config/services.yml

@ -2,6 +2,7 @@
/* For licensing terms, see /license.txt */ /* For licensing terms, see /license.txt */
use Chamilo\CoreBundle\Entity\ExtraField as EntityExtraField; use Chamilo\CoreBundle\Entity\ExtraField as EntityExtraField;
use Chamilo\CoreBundle\Entity\Resource\ResourceNode;
use Chamilo\CoreBundle\Entity\SkillRelUser; use Chamilo\CoreBundle\Entity\SkillRelUser;
use Chamilo\CoreBundle\Entity\SkillRelUserComment; use Chamilo\CoreBundle\Entity\SkillRelUserComment;
use Chamilo\CoreBundle\Framework\Container; use Chamilo\CoreBundle\Framework\Container;
@ -336,7 +337,25 @@ class UserManager
if (!empty($expirationDate)) { if (!empty($expirationDate)) {
$user->setExpirationDate($expirationDate); $user->setExpirationDate($expirationDate);
} }
try { try {
$factory = Container::$container->get('Chamilo\CoreBundle\Repository\ResourceFactory');
$repo = $factory->createRepository('global', 'users');
// Add user as a node:
$url = api_get_url_entity($access_url_id);
$resourceNode = new ResourceNode();
$resourceNode
->setSlug($loginName)
->setCreator(api_get_user_entity($creatorId))
->setResourceType($repo->getResourceType())
->setParent($url->getResourceNode())
;
$repo->getEntityManager()->persist($resourceNode);
$user->setResourceNode($resourceNode);
$repo->getEntityManager()->persist($user);
$userManager->updateUser($user); $userManager->updateUser($user);
$userId = $user->getId(); $userId = $user->getId();

@ -17,6 +17,7 @@ use Chamilo\CoreBundle\Entity\Resource\ResourceNode;
use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter; use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter;
use Chamilo\CourseBundle\Controller\CourseControllerInterface; use Chamilo\CourseBundle\Controller\CourseControllerInterface;
use Chamilo\CourseBundle\Controller\CourseControllerTrait; use Chamilo\CourseBundle\Controller\CourseControllerTrait;
use Chamilo\UserBundle\Entity\User;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria; use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
@ -59,8 +60,17 @@ class ResourceController extends AbstractResourceController implements CourseCon
$tool = $request->get('tool'); $tool = $request->get('tool');
$type = $request->get('type'); $type = $request->get('type');
$course = $this->getCourse(); $repository = $this->getRepositoryFromRequest($request);
$grid = $this->getGrid($request, $grid, $course->getResourceNode()->getId());
$entityName = $repository->getResourceType()->getEntityName();
$parentResource = $this->getCourse();
if (null === $parentResource) {
/** @var User $user */
$parentResource = $this->getUser();
}
$grid = $this->getGrid($request, $grid, $parentResource->getResourceNode()->getId());
$breadcrumb = $this->getBreadCrumb(); $breadcrumb = $this->getBreadCrumb();
$breadcrumb->addChild( $breadcrumb->addChild(
@ -939,12 +949,15 @@ class ResourceController extends AbstractResourceController implements CourseCon
} }
/** /**
* @param string $mode show or download * @param Request $request
* @param string $filter * @param ResourceNode $resourceNode
* @param string $mode
* @param Glide|null $glide
* @param string $filter
* *
* @return mixed|StreamedResponse * @return mixed|StreamedResponse
*/ */
private function showFile(Request $request, ResourceNode $resourceNode, string $mode = 'show', Glide $glide = null, $filter = '') private function showFile(Request $request, ResourceNode $resourceNode, $mode = 'show', Glide $glide = null, $filter = '')
{ {
$this->denyAccessUnlessGranted( $this->denyAccessUnlessGranted(
ResourceNodeVoter::VIEW, ResourceNodeVoter::VIEW,

@ -965,6 +965,11 @@ class Version20 extends AbstractMigrationChamilo
$this->addSql("ALTER TABLE c_link ADD CONSTRAINT FK_9209C2A01BAD783F FOREIGN KEY (resource_node_id) REFERENCES resource_node (id) ON DELETE CASCADE;"); $this->addSql("ALTER TABLE c_link ADD CONSTRAINT FK_9209C2A01BAD783F FOREIGN KEY (resource_node_id) REFERENCES resource_node (id) ON DELETE CASCADE;");
$this->addSql("CREATE UNIQUE INDEX UNIQ_9209C2A01BAD783F ON c_link (resource_node_id);"); $this->addSql("CREATE UNIQUE INDEX UNIQ_9209C2A01BAD783F ON c_link (resource_node_id);");
$this->addSql("ALTER TABLE user ADD resource_node_id INT DEFAULT NULL;");
$this->addSql("ALTER TABLE user ADD CONSTRAINT FK_8D93D6491BAD783F FOREIGN KEY (resource_node_id) REFERENCES resource_node (id) ON DELETE CASCADE;");
$this->addSql("CREATE UNIQUE INDEX UNIQ_8D93D6491BAD783F ON user (resource_node_id);");
$this->addSql("ALTER TABLE user_audit ADD resource_node_id INT DEFAULT NULL;");
/* $this->addSql(""); /* $this->addSql("");
$this->addSql(""); $this->addSql("");
$this->addSql("");*/ $this->addSql("");*/

@ -166,7 +166,7 @@ class ResourceRepository extends EntityRepository
* @param null $lockMode * @param null $lockMode
* @param null $lockVersion * @param null $lockVersion
*/ */
public function find($id, $lockMode = null, $lockVersion = null): ?AbstractResource public function find($id, $lockMode = null, $lockVersion = null) //: ?AbstractResource
{ {
return $this->getRepository()->find($id); return $this->getRepository()->find($id);
} }

@ -9,12 +9,12 @@ services:
# Tool chain is filled in file DependencyInjection/Compiler/ToolCompilerClass.php # Tool chain is filled in file DependencyInjection/Compiler/ToolCompilerClass.php
Chamilo\CoreBundle\ToolChain: ~ Chamilo\CoreBundle\ToolChain: ~
# Admin # Admin
chamilo_core.tool.admin: chamilo_core.tool.global:
class: Chamilo\CoreBundle\Tool\Admin class: Chamilo\CoreBundle\Tool\GlobalTool
arguments: arguments:
- 'admin' - 'global'
- 'admin' - ''
- 'admin' - ''
- '' - ''
- urls: - urls:
entity: Chamilo\CoreBundle\Entity\AccessUrl entity: Chamilo\CoreBundle\Entity\AccessUrl
@ -22,6 +22,9 @@ services:
courses: courses:
entity: Chamilo\CoreBundle\Entity\Course entity: Chamilo\CoreBundle\Entity\Course
repository: Chamilo\CoreBundle\Repository\CourseRepository repository: Chamilo\CoreBundle\Repository\CourseRepository
users:
entity: Chamilo\UserBundle\Entity\User
repository: Chamilo\UserBundle\Repository\UserRepository
- 1 - 1
tags: tags:
- {name: chamilo_core.tool} - {name: chamilo_core.tool}

@ -4,8 +4,8 @@
namespace Chamilo\CoreBundle\Tool; namespace Chamilo\CoreBundle\Tool;
/** /**
* Class Admin. * Class GlobalTool.
*/ */
class Admin extends AbstractTool class GlobalTool extends AbstractTool
{ {
} }

@ -6,6 +6,8 @@ namespace Chamilo\UserBundle\Entity;
use Chamilo\CoreBundle\Entity\AccessUrl; use Chamilo\CoreBundle\Entity\AccessUrl;
use Chamilo\CoreBundle\Entity\AccessUrlRelUser; use Chamilo\CoreBundle\Entity\AccessUrlRelUser;
use Chamilo\CoreBundle\Entity\Course; use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\Resource\AbstractResource;
use Chamilo\CoreBundle\Entity\Resource\ResourceNode;
use Chamilo\CoreBundle\Entity\Skill; use Chamilo\CoreBundle\Entity\Skill;
use Chamilo\CoreBundle\Entity\UsergroupRelUser; use Chamilo\CoreBundle\Entity\UsergroupRelUser;
use Chamilo\ThemeBundle\Model\UserInterface as ThemeUser; use Chamilo\ThemeBundle\Model\UserInterface as ThemeUser;
@ -16,7 +18,6 @@ use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Sonata\UserBundle\Entity\BaseUser; use Sonata\UserBundle\Entity\BaseUser;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Security\Core\User\EquatableInterface; use Symfony\Component\Security\Core\User\EquatableInterface;
use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
@ -178,15 +179,6 @@ class User extends BaseUser implements ThemeUser, EquatableInterface //implement
*/ */
protected $address; protected $address;
/**
* Vich\UploadableField(mapping="user_image", fileNameProperty="picture_uri").
*
* note This is not a mapped field of entity metadata, just a simple property.
*
* @var File
*/
protected $imageFile;
/** /**
* @var AccessUrl * @var AccessUrl
*/ */
@ -506,6 +498,30 @@ class User extends BaseUser implements ThemeUser, EquatableInterface //implement
*/ */
protected $receivedMessages; protected $receivedMessages;
/**
*
* @ORM\OneToOne(
* targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceNode", cascade={"remove"}, orphanRemoval=true
* )
* @ORM\JoinColumn(name="resource_node_id", referencedColumnName="id", onDelete="CASCADE")
*/
public $resourceNode;
/**
* @return $this
*/
public function setResourceNode(ResourceNode $resourceNode): self
{
$this->resourceNode = $resourceNode;
return $this;
}
public function getResourceNode(): ResourceNode
{
return $this->resourceNode;
}
/** /**
* Constructor. * Constructor.
*/ */
@ -1432,34 +1448,6 @@ class User extends BaseUser implements ThemeUser, EquatableInterface //implement
return $this->getId(); return $this->getId();
} }
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
*/
public function setImageFile(File $image)
{
$this->imageFile = $image;
if ($image) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new \DateTime('now');
}
}
/**
* @return File
*/
public function getImageFile()
{
return $this->imageFile;
}
/** /**
* @return string * @return string
*/ */

@ -27,6 +27,8 @@ use Chamilo\CoreBundle\Entity\UserApiKey;
use Chamilo\CoreBundle\Entity\UserCourseCategory; use Chamilo\CoreBundle\Entity\UserCourseCategory;
use Chamilo\CoreBundle\Entity\UsergroupRelUser; use Chamilo\CoreBundle\Entity\UsergroupRelUser;
use Chamilo\CoreBundle\Entity\UserRelCourseVote; use Chamilo\CoreBundle\Entity\UserRelCourseVote;
use Chamilo\CoreBundle\Repository\ResourceRepository;
use Chamilo\CoreBundle\Repository\ResourceRepositoryInterface;
use Chamilo\CourseBundle\Entity\CAttendanceResult; use Chamilo\CourseBundle\Entity\CAttendanceResult;
use Chamilo\CourseBundle\Entity\CAttendanceSheet; use Chamilo\CourseBundle\Entity\CAttendanceSheet;
use Chamilo\CourseBundle\Entity\CBlogPost; use Chamilo\CourseBundle\Entity\CBlogPost;
@ -59,19 +61,19 @@ use Symfony\Component\Serializer\Serializer;
* All functions that query the database (selects) * All functions that query the database (selects)
* Functions should return query builders. * Functions should return query builders.
*/ */
class UserRepository class UserRepository extends ResourceRepository
{ {
/** /**
* @var EntityRepository * @var EntityRepository
*/ */
private $repository; //private $repository;
public function __construct(EntityManagerInterface $entityManager) /*public function __construct(EntityManagerInterface $entityManager)
{ {
$this->repository = $entityManager->getRepository(User::class); $this->repository = $entityManager->getRepository(User::class);
} }*/
public function find($id): ?User /*public function find($id)
{ {
$user = $this->repository->find($id); $user = $this->repository->find($id);
@ -80,7 +82,7 @@ class UserRepository
} }
return $user; return $user;
} }*/
public function findByUsername(string $username): ?User public function findByUsername(string $username): ?User
{ {

@ -12,5 +12,11 @@ services:
chamilo_user.security.login_form_authenticator: chamilo_user.security.login_form_authenticator:
class: Chamilo\UserBundle\Security\LoginFormAuthenticator class: Chamilo\UserBundle\Security\LoginFormAuthenticator
Chamilo\UserBundle\Repository\UserRepository: ~ Chamilo\UserBundle\Repository\UserRepository:
arguments:
$tool: 'global'
$type: 'users'
# call a method on the specified factory service
factory: ['@Chamilo\CoreBundle\Repository\ResourceFactory', 'createRepository']
Chamilo\UserBundle\Repository\GroupRepository: ~ Chamilo\UserBundle\Repository\GroupRepository: ~

Loading…
Cancel
Save