/** * Create a user. * Its basic properties (name, username,... ) must already be set. * * @param \Claroline\CoreBundle\Entity\User $user * @param bool $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 = [], $model = null, $publicUrl = null, $organizations = [], $forcePersonalWorkspace = null, $forceRoleValidation = true) { $additionnalRoles = []; foreach ($rolesToAdd as $roleToAdd) { $additionnalRoles[] = is_string($roleToAdd) ? $this->roleManager->getRoleByName($roleToAdd) : $roleToAdd; } if (count($organizations) === 0 && count($user->getOrganizations()) === 0) { $organizations = [$this->organizationManager->getDefault(true)]; $user->setOrganizations($organizations); } $this->objectManager->startFlushSuite(); $user->setGuid($this->container->get('claroline.utilities.misc')->generateGuid()); $user->setEmailValidationHash($this->container->get('claroline.utilities.misc')->generateGuid()); $user->setOrganizations($organizations); $publicUrl ? $user->setPublicUrl($publicUrl) : $user->setPublicUrl($this->generatePublicUrl($user)); $this->toolManager->addRequiredToolsToUser($user, 0); $this->toolManager->addRequiredToolsToUser($user, 1); $roleUser = $this->roleManager->getRoleByName(PlatformRoles::USER); $user->addRole($roleUser); $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->mailManager->sendEnableAccountMessage($user); } elseif ($mailValidation === PlatformConfiguration::REGISTRATION_MAIL_VALIDATION_PARTIAL) { //don't change anything $this->mailManager->sendCreationMessage($user); } } if ($forcePersonalWorkspace !== null) { if ($forcePersonalWorkspace) { $this->setPersonalWorkspace($user, $model); } } else { if ($this->personalWorkspaceAllowed($additionnalRoles)) { $this->setPersonalWorkspace($user, $model); } } $this->objectManager->persist($user); $this->strictEventDispatcher->dispatch('user_created_event', 'UserCreated', ['user' => $user]); $this->strictEventDispatcher->dispatch('log', 'Log\\LogUserCreate', [$user]); $this->objectManager->endFlushSuite(); return $user; }