private function buildGroup($name) { $group = new Group(); $group->setName($name); $this->om->persist($group); return $group; }
public function group($name) { $group = new Group(); $group->setGuid($this->container->get('claroline.utilities.misc')->generateGuid()); $group->setName($name); $this->om->persist($group); return $group; }
public function load(ObjectManager $manager) { $groupManager = $this->container->get('claroline.manager.group_manager'); foreach ($this->groups as $properties) { $group = new Group(); $group->setName($properties['name']); $role = $manager->getRepository('ClarolineCoreBundle:Role')->findOneByName($properties['role']); $group->addRole($role); $groupManager->insertGroup($group); } }
/** * {@inheritDoc} */ public function load(ObjectManager $manager) { foreach ($this->groups as $name => $users) { $group = new Group(); $group->setName($name); foreach ($users as $username) { $user = $this->getReference('user/' . $username); $group->addUser($user); } $manager->persist($group); $this->addReference('group/' . $name, $group); } $manager->flush(); }
public function manageGroupCreation(array $datas) { $logs = array(); $identicalGroupTxt = $this->translator->trans('identical_group_name', array(), 'platform'); $groupTxt = $this->translator->trans('group', array(), 'platform'); $createdTxt = $this->translator->trans('has_been_created', array(), 'platform'); $existedTxt = $this->translator->trans('already_exists', array(), 'platform'); $this->om->startFlushSuite(); // Checks for double entries foreach ($datas as $lineNb => $lineDatas) { if (!isset($lineDatas['error'])) { $groupName = strtolower($lineDatas['name']); foreach ($datas as $lineNbBis => $lineDatasBis) { if ($lineNb !== $lineNbBis && !isset($lineDatasBis['error']) && $groupName === strtolower($lineDatasBis['name'])) { $groupNameBis = $lineDatasBis['name']; $datas[$lineNb]['error'] = "[{$lineNb}] {$identicalGroupTxt} [{$groupName}]"; $datas[$lineNbBis]['error'] = "[{$lineNbBis}] {$identicalGroupTxt} [{$groupNameBis}]"; } } } } foreach ($datas as $lineNb => $lineDatas) { if (isset($lineDatas['error'])) { $logs[] = $lineDatas['error']; } else { $groupName = $lineDatas['name']; $group = $this->groupManager->getGroupByName($groupName); if (is_null($group)) { $group = new Group(); $group->setName($groupName); $this->om->persist($group); $logs[] = "{$groupTxt} [{$groupName}] {$createdTxt}"; } else { $logs[] = "{$groupTxt} [{$groupName}] {$existedTxt}"; } } } $this->om->endFlushSuite(); return $logs; }
protected function persistGroup($name) { $group = new Group(); $group->setName($name); $group->setGuid($name); $this->om->persist($group); return $group; }
/** * Import users from an array. * There is the array format:. * * @todo some batch processing * * array( * array(firstname, lastname, username, pwd, email, code, phone), * array(firstname2, lastname2, username2, pwd2, email2, code2, phone2), * array(firstname3, lastname3, username3, pwd3, email3, code3, phone3), * ) * * @param array $users * @param string $authentication an authentication source * @param bool $mail do the users need to be mailed * @param \Closure $logger an anonymous function allowing to log actions * * @return array */ public function importUsers(array $users, $sendMail = true, $logger = null, $additionalRoles = [], $enableEmailNotifaction = false, $options = []) { //build options if (!isset($options['ignore-update'])) { $options['ignore-update'] = false; } $returnValues = []; $skipped = []; //keep these roles before the clear() will mess everything up. It's not what we want. $tmpRoles = $additionalRoles; $additionalRoles = []; //I need to do that to import roles from models. Please don't ask why, I have no f*****g idea. $this->objectManager->clear(); foreach ($tmpRoles as $role) { if ($role) { $additionalRoles[] = $this->objectManager->merge($role); } } $roleUser = $this->roleManager->getRoleByName('ROLE_USER'); $max = $roleUser->getMaxUsers(); $total = $this->countUsersByRoleIncludingGroup($roleUser); if ($total + count($users) > $max) { throw new AddRoleException(); } $lg = $this->platformConfigHandler->getParameter('locale_language'); $this->objectManager->startFlushSuite(); $i = 1; $j = 0; $countCreated = 0; $countUpdated = 0; foreach ($users as $user) { $firstName = $user[0]; $lastName = $user[1]; $username = $user[2]; $pwd = $user[3]; $email = trim($user[4]); if (isset($user[5])) { $code = trim($user[5]) === '' ? null : $user[5]; } else { $code = null; } if (isset($user[6])) { $phone = trim($user[6]) === '' ? null : $user[6]; } else { $phone = null; } if (isset($user[7])) { $authentication = trim($user[7]) === '' ? null : $user[7]; } else { $authentication = null; } if (isset($user[8])) { $modelName = trim($user[8]) === '' ? null : $user[8]; } else { $modelName = null; } if (isset($user[9])) { $groupName = trim($user[9]) === '' ? null : $user[9]; } else { $groupName = null; } if (isset($user[10])) { $organizationName = trim($user[10]) === '' ? null : $user[10]; } else { $organizationName = null; } $hasPersonalWorkspace = isset($user[11]) ? (bool) $user[11] : false; $isMailValidated = isset($user[12]) ? (bool) $user[12] : false; $isMailNotified = isset($user[13]) ? (bool) $user[13] : $enableEmailNotifaction; if ($modelName) { $model = $this->objectManager->getRepository('Claroline\\CoreBundle\\Entity\\Model\\WorkspaceModel')->findOneByName($modelName); } else { $model = null; } if ($organizationName) { $organizations = [$this->objectManager->getRepository('Claroline\\CoreBundle\\Entity\\Organization\\Organization')->findOneByName($organizationName)]; } else { $organizations = []; } $group = $groupName ? $this->groupManager->getGroupByName($groupName) : null; if ($groupName) { $group = $this->groupManager->getGroupByNameAndScheduledForInsert($groupName); if (!$group) { $group = new Group(); $group->setName($groupName); $group = $this->groupManager->insertGroup($group); } } else { $group = null; } $userEntity = $this->userRepo->findOneByMail($email); if (!$userEntity) { $userEntity = $this->userRepo->findOneByUsername($username); if (!$userEntity && $code !== null) { //the code isn't required afaik $userEntity = $this->userRepo->findOneByAdministrativeCode($code); } } if ($userEntity && $options['ignore-update']) { if ($logger) { $logger(" Skipping {$userEntity->getUsername()}..."); } continue; } $isNew = false; if (!$userEntity) { $isNew = true; $userEntity = new User(); $userEntity->setPlainPassword($pwd); ++$countCreated; } else { if (!empty($pwd)) { $userEntity->setPlainPassword($pwd); } ++$countUpdated; } $userEntity->setUsername($username); $userEntity->setMail($email); $userEntity->setFirstName($firstName); $userEntity->setLastName($lastName); $userEntity->setAdministrativeCode($code); $userEntity->setPhone($phone); $userEntity->setLocale($lg); $userEntity->setAuthentication($authentication); $userEntity->setIsMailNotified($isMailNotified); $userEntity->setIsMailValidated($isMailValidated); if ($options['single-validate']) { $errors = $this->validator->validate($userEntity); if (count($errors) > 0) { $skipped[$i] = $userEntity; if ($isNew) { --$countCreated; } else { --$countUpdated; } continue; } } if (!$isNew && $logger) { $logger(" User {$j} ({$username}) being updated..."); $this->roleManager->associateRoles($userEntity, $additionalRoles); } if ($isNew) { if ($logger) { $logger(" User {$j} ({$username}) being created..."); } $this->createUser($userEntity, $sendMail, $additionalRoles, $model, $username . uniqid(), $organizations, $hasPersonalWorkspace, false); } $this->objectManager->persist($userEntity); $returnValues[] = $firstName . ' ' . $lastName; if ($group) { $this->groupManager->addUsersToGroup($group, [$userEntity]); } if ($logger) { $logger(' [UOW size: ' . $this->objectManager->getUnitOfWork()->size() . ']'); } ++$i; ++$j; if ($i % self::MAX_USER_BATCH_SIZE === 0) { if ($logger) { $logger(' [UOW size: ' . $this->objectManager->getUnitOfWork()->size() . ']'); } $this->objectManager->forceFlush(); if ($logger) { $logger(' flushing users...'); } $tmpRoles = $additionalRoles; $this->objectManager->clear(); $additionalRoles = []; foreach ($tmpRoles as $toAdd) { if ($toAdd) { $additionalRoles[] = $this->objectManager->merge($toAdd); } } if ($this->tokenStorage->getToken()) { $this->objectManager->merge($this->tokenStorage->getToken()->getUser()); } } } $this->objectManager->endFlushSuite(); if ($logger) { $logger($countCreated . ' users created.'); $logger($countUpdated . ' users updated.'); } foreach ($skipped as $key => $user) { $logger('The user ' . $user . ' was skipped at line ' . $key . ' because it failed the validation pass.'); } return $returnValues; }
protected static function createGroup($name, array $users = array(), array $roles = array()) { $group = new Group(); $group->setName($name); foreach ($users as $user) { $group->addUser($user); } foreach ($roles as $role) { $group->addRole($role); } self::create($name, $group); }