Пример #1
0
 /**
  * Create a user.
  * Its basic properties (name, username,... ) must already be set.
  *
  * @param \Claroline\CoreBundle\Entity\User $user
  * @param boolean                           $sendMail         do we need to mail the new user ?
  * @param array                             $additionnalRoles a list of additionalRoles
  * @param Model                             $model            a model to create workspace
  *
  * @return \Claroline\CoreBundle\Entity\User
  */
 public function createUser(User $user, $sendMail = true, $rolesToAdd = array(), $model = null, $publicUrl = null, $organizations = array())
 {
     $additionnalRoles = [];
     foreach ($rolesToAdd as $roleToAdd) {
         if (is_string($roleToAdd)) {
             $additionnalRoles[] = $this->roleManager->getRoleByName($roleToAdd);
         }
     }
     if (count($organizations) === 0 && count($user->getOrganizations()) === 0) {
         $organizations = array($this->organizationManager->getDefault());
         $user->setOrganizations($organizations);
     }
     $this->objectManager->startFlushSuite();
     if ($this->personalWorkspaceAllowed($additionnalRoles)) {
         $this->setPersonalWorkspace($user, $model);
     }
     $user->setGuid($this->container->get('claroline.utilities.misc')->generateGuid());
     $user->setEmailValidationHash($this->container->get('claroline.utilities.misc')->generateGuid());
     $this->objectManager->persist($user);
     $publicUrl ? $user->setPublicUrl($publicUrl) : $user->setPublicUrl($this->generatePublicUrl($user));
     $this->toolManager->addRequiredToolsToUser($user, 0);
     $this->toolManager->addRequiredToolsToUser($user, 1);
     $this->roleManager->setRoleToRoleSubject($user, PlatformRoles::USER);
     $this->objectManager->persist($user);
     $this->strictEventDispatcher->dispatch('log', 'Log\\LogUserCreate', array($user));
     $this->roleManager->createUserRole($user);
     foreach ($additionnalRoles as $role) {
         if ($role) {
             $this->roleManager->associateRole($user, $role);
         }
     }
     if ($this->mailManager->isMailerAvailable() && $sendMail) {
         //send a validation by hash
         $mailValidation = $this->platformConfigHandler->getParameter('registration_mail_validation');
         if ($mailValidation === PlatformConfiguration::REGISTRATION_MAIL_VALIDATION_FULL) {
             $password = sha1(rand(1000, 10000) . $user->getUsername() . $user->getSalt());
             $user->setResetPasswordHash($password);
             $user->setIsEnabled(false);
             $this->objectManager->persist($user);
             $this->objectManager->flush();
             $this->mailManager->sendEnableAccountMessage($user);
         } elseif ($mailValidation === PlatformConfiguration::REGISTRATION_MAIL_VALIDATION_PARTIAL) {
             //don't change anything
             $this->mailManager->sendCreationMessage($user);
         }
     }
     $this->container->get('claroline.event.event_dispatcher')->dispatch('user_created_event', 'UserCreated', array('user' => $user));
     $this->objectManager->endFlushSuite();
     return $user;
 }