/** * @param string $entityType * @param integer $entityId * @param User $user * @return bool */ public function unsubscribe(User $user, $entityType, $entityId) { /** @var $em EntityManager */ $em = $this->doctrine->getManager(); $em->createQueryBuilder()->delete('EtuCoreBundle:Subscription', 's')->andWhere('s.entityId = :entityId')->andWhere('s.entityType = :entityType')->andWhere('s.user = :user')->setParameter('entityType', $entityType)->setParameter('entityId', $entityId)->setParameter('user', $user->getId())->getQuery()->execute(); return true; }
/** * @param User $user * @return Course[] */ public function getUserNextCourses(User $user) { /** @var Course[] $todayCourses */ $todayCourses = $this->createQueryBuilder('c')->where('c.user = :user')->andWhere('c.day = :day')->orderBy('c.start', 'ASC')->setParameter('user', $user->getId())->setParameter('day', Course::getTodayConstant())->getQuery()->getResult(); $nextCourses = []; foreach ($todayCourses as $course) { if ($course->getStartAsInt() >= (int) date('Hi') - 15) { $nextCourses[$course->getStart()][] = $course; } } return array_slice($nextCourses, 0, 5); }
/** * Update the element in the database * * @return DbUser */ public function update() { /* * Update: * - formation * - niveau * - filiere * - uvs * - semesters history */ $persist = false; $user = $this->database; $history = $user->addCureentSemesterToHistory(); $niveau = null; $branch = $this->ldap->getNiveau(); preg_match('/^[^0-9]+/i', $this->ldap->getNiveau(), $match); if (isset($match[0])) { $branch = $match[0]; $niveau = str_replace($branch, '', $this->ldap->getNiveau()); } // Updates if (ucfirst(strtolower($this->ldap->getFormation())) != $this->database->getFormation()) { $persist = true; $user->setFormation(ucfirst(strtolower($this->ldap->getFormation()))); } if ($niveau != $this->database->getNiveau()) { $persist = true; $user->setNiveau($niveau); } if ($branch != $this->database->getBranch()) { $persist = true; $user->setBranch($branch); } if ($this->ldap->getFiliere() != $this->database->getFiliere()) { $persist = true; $user->setFiliere($this->ldap->getFiliere()); } if (implode('|', $this->ldap->getUvs()) != $this->database->getUvs()) { $persist = true; $user->setUvs(implode('|', $this->ldap->getUvs())); } /* * Add badges */ if (substr($history['niveau'], 0, 2) == 'TC' && substr($user->getNiveau(), 0, 2) != 'TC') { BadgesManager::userAddBadge($user, 'tc_survivor'); BadgesManager::userPersistBadges($user); } if ($persist) { $this->doctrine->getManager()->persist($user); } return $persist; }
/** * @param User $user * @param EmbedBag $includes * @return array */ private function getIncludes(User $user, EmbedBag $includes) { $embed = ['badges' => []]; if ($includes->has('badges')) { foreach ($user->getBadges() as $userBadge) { $embed['badges'][] = $this->badgeTransformer->transform($userBadge->getBadge(), new EmbedBag()); } } else { foreach ($user->getBadges() as $userBadge) { $embed['badges'][] = $userBadge->getBadge()->getId(); } } return ['_embed' => $embed]; }
/** * @return User[] */ public function getBirthdays() { if ($this->stopwatch) { $this->stopwatch->start('block_birthdays', 'home_blocks'); } $query = $this->manager->createQueryBuilder()->select('u, m, o')->from('EtuUserBundle:User', 'u')->leftJoin('u.memberships', 'm')->leftJoin('m.organization', 'o')->where('DAY(u.birthday) = DAY(CURRENT_TIMESTAMP())')->andWhere('MONTH(u.birthday) = MONTH(CURRENT_TIMESTAMP())')->andWhere('u.birthday IS NOT NULL')->andWhere('u.birthdayPrivacy = :privacy')->setParameter('privacy', User::PRIVACY_PUBLIC)->andWhere('u.id != :me')->setParameter('me', $this->user->getId())->getQuery(); $query->useResultCache(true, 3600); /** @var User[] $users */ $users = $query->getResult(); // Find more interesting birthdays : same promotion (SRT4), same branch (SRT), others $usersWeights = []; foreach ($users as $key => $user) { $usersWeights[$key] = 0; if ($user->getBranch() == $this->user->getBranch()) { $usersWeights[$key]++; } if ($user->getNiveau() == $this->user->getNiveau()) { $usersWeights[$key]++; } } array_multisort($usersWeights, SORT_DESC, SORT_NUMERIC, $users); $result = array_slice($users, 0, 3); if ($this->stopwatch) { $this->stopwatch->stop('block_birthdays'); } return $result; }
/** * {@inheritDoc} */ public function load(ObjectManager $manager) { $admin = new User(); $admin->setFullName('Admin ADMIN'); $admin->setLogin('admin'); $admin->setMail('*****@*****.**'); $admin->setIsAdmin(true); $admin->setAvatar('admin.png'); $admin->setBirthday(new \DateTime()); $admin->setLastVisitHome(new \DateTime()); $admin->setReadOnlyExpirationDate(new \DateTime()); $user = new User(); $user->setFullName('User USER'); $user->setLogin('user'); $user->setMail('*****@*****.**'); $user->setIsAdmin(false); $user->setAvatar('user.png'); $user->setBirthday(new \DateTime()); $user->setLastVisitHome(new \DateTime()); $user->setReadOnlyExpirationDate(new \DateTime()); $orga = new Organization(); $orga->setName('Orga ORGA'); $orga->setLogin('orga'); $orga->setContactMail('*****@*****.**'); $manager->persist($admin); $manager->persist($user); $manager->persist($orga); $membership = new Member(); $membership->setOrganization($orga); $membership->setUser($user); $membership->addPermission('daymail'); $manager->persist($membership); $manager->flush(); $this->addReference('user_admin', $admin); $this->addReference('user_user', $user); $this->addReference('user_orga', $orga); $this->addReference('user_membership', $orga); }
/** * @return User */ public static function createAdminUser() { $user = new User(); $user->setFullName('Admin ADMIN'); $user->setLogin('admin'); $user->setMail('*****@*****.**'); $user->setIsAdmin(true); $user->setAvatar('admin.png'); $user->testingContext = true; $reflection = new \ReflectionObject($user); $property = $reflection->getProperty('id'); $property->setAccessible(true); $property->setValue($user, 1); return $user; }
/** * @param Category $category * @return bool */ public function canDelete(Category $category) { if ($this->user instanceof Organization) { return false; } if ($this->user->getIsAdmin()) { return true; } if (!$this->user->getIsReadOnly()) { $permissions = new Permissions(); foreach ($category->getPermissions() as $value) { if ($value->getType() == 1) { $permissions = $value; } } if ($permissions->getDelete()) { return true; } foreach ($category->getPermissions() as $value) { if ($value->getType() == 2) { foreach ($this->memberships as $membership) { if ($value->getOrganization() == $membership->getOrganization()) { $permissions = $value; if ($permissions->getDelete()) { return true; } } } } if ($value->getType() == 3) { if ($value->getUser() == $this->user) { $permissions = $value; if ($permissions->getDelete()) { return true; } } } } } }
/** * Keep the user in the database and set a password for external connexion */ public function keep($encryptedPassword) { $this->element->setPassword($encryptedPassword); $this->element->setKeepActive(true); $this->doctrine->getManager()->persist($this->element); }
/** * @param User $user * @return bool */ public function hasUser(User $user) { if ($this->author->getId() == $user->getId()) { return true; } foreach ($this->subscriptions as $subscription) { if ($subscription->getUser()->getId() == $user->getId()) { return true; } } return false; }
/** * Connected user as external * * @return bool */ public function isExternal() { return $this->isUser() && $this->user->getKeepActive(); }
/** * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @return void * @throws \RuntimeException */ protected function execute(InputInterface $input, OutputInterface $output) { $dialog = $this->getHelperSet()->get('dialog'); /** @var EntityManager $em */ $em = $this->getContainer()->get('doctrine')->getManager(); $login = $input->getOption('login') !== null ? $input->getOption('login') : $dialog->ask($output, 'Login: '******'firstName') !== null ? $input->getOption('firstName') : $dialog->ask($output, 'First name: '); $lastName = $input->getOption('lastName') !== null ? $input->getOption('lastName') : $dialog->ask($output, 'Last name: '); $password = $input->getOption('password') !== null ? $input->getOption('password') : $dialog->ask($output, 'Password: '******'email') !== null ? $input->getOption('email') : $dialog->ask($output, 'Public e-mail: '); $user = new User(); $user->setKeepActive(true); $user->setLogin($login); $user->setFirstName($firstName); $user->setLastName($lastName); $user->setFullName($user->getFirstName() . ' ' . $user->getLastName()); $user->setPassword($this->getContainer()->get('etu.user.crypting')->encrypt($password)); $user->setMail($email); $em->persist($user); $em->flush(); $output->writeln("\nDone.\n"); }
/** * @deprecated Deprecated since version 10.0 Bêta1, to be removed in 10.1. Use * {@link User::currentSemester()} instead. * @return string */ public static function currentSemester() { return User::currentSemester(); }
/** * @Route("/{slug}-{name}/send-review", name="uvs_view_send_review") * @Template() */ public function sendReviewAction(Request $request, $slug, $name) { if (!$this->getUserLayer()->isUser()) { return $this->createAccessDeniedResponse(); } /** @var EntityManager $em */ $em = $this->getDoctrine()->getManager(); /** @var UV $uv */ $uv = $em->getRepository('EtuModuleUVBundle:UV')->findOneBy(array('slug' => $slug)); if (!$uv) { throw $this->createNotFoundException(sprintf('UV for slug %s not found', $slug)); } if (StringManipulationExtension::slugify($uv->getName()) != $name) { return $this->redirect($this->generateUrl('uvs_view_send_review', array('slug' => $uv->getSlug(), 'name' => StringManipulationExtension::slugify($uv->getName()))), 301); } $review = new Review(); $review->setUv($uv)->setSender($this->getUser())->setSemester(User::currentSemester()); $form = $this->createFormBuilder($review)->add('type', 'choice', array('choices' => Review::$types, 'required' => true))->add('semester', 'choice', array('choices' => Review::availableSemesters(), 'required' => true))->add('file', null, array('required' => true))->getForm(); if ($request->getMethod() == 'POST' && $form->submit($request)->isValid()) { $review->upload(); $em->persist($review); $em->flush(); // Notify subscribers $notif = new Notification(); $review->file = null; $notif->setModule($this->getCurrentBundle()->getIdentifier())->setHelper('uv_new_review')->setAuthorId($this->getUser()->getId())->setEntityType('uv')->setEntityId($uv->getId())->addEntity($review); $this->getNotificationsSender()->send($notif); // Add badges $count = $em->createQueryBuilder()->select('COUNT(r) as nb')->from('EtuModuleUVBundle:Review', 'r')->where('r.sender = :user')->setParameter('user', $this->getUser()->getId())->getQuery()->getSingleScalarResult(); $user = $this->getUser(); if ($count >= 1) { BadgesManager::userAddBadge($user, 'uvs_reviews', 1); } else { BadgesManager::userRemoveBadge($user, 'uvs_reviews', 1); } if ($count >= 2) { BadgesManager::userAddBadge($user, 'uvs_reviews', 2); } else { BadgesManager::userRemoveBadge($user, 'uvs_reviews', 2); } if ($count >= 4) { BadgesManager::userAddBadge($user, 'uvs_reviews', 3); } else { BadgesManager::userRemoveBadge($user, 'uvs_reviews', 3); } if ($count >= 10) { BadgesManager::userAddBadge($user, 'uvs_reviews', 4); } else { BadgesManager::userRemoveBadge($user, 'uvs_reviews', 4); } BadgesManager::userPersistBadges($user); $em->persist($user); $em->flush(); $this->get('session')->getFlashBag()->set('message', array('type' => 'success', 'message' => 'uvs.main.sendReview.confirm')); return $this->redirect($this->generateUrl('uvs_view', array('slug' => $slug, 'name' => $name))); } return array('uv' => $uv, 'form' => $form->createView()); }
/** * @ApiDoc( * description = "Badges list of a given user (scope: public)", * section = "User - Public data", * parameters={ * { "name"="login", "dataType"="string", "required"=true, "description"="User login" } * } * ) * * @Route("/public/users/{login}/badges", name="api_public_users_badges") * @Method("GET") */ public function badgesAction(User $user) { $badges = []; foreach ($user->getBadges() as $userBadge) { $badges[] = $userBadge->getBadge(); } return $this->format(['data' => $this->get('etu.api.badge.transformer')->transform($badges)]); }
/** * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @return void * @throws \RuntimeException */ protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln("\nConnecting to database ..."); $output->writeln('----------------------------------------'); $dialog = $this->getHelperSet()->get('dialog'); $host = $dialog->ask($output, 'Host: '); $name = $dialog->ask($output, 'Name: '); $user = $dialog->ask($output, 'User: '******'Pass: '******'mysql:host=' . $host . ';dbname=' . $name, $user, $pass); $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); $output->writeln("\nFinding users differences ..."); $output->writeln('----------------------------------------'); /** @var EntityManager $em */ $em = $this->getContainer()->get('doctrine')->getManager(); /* * Imported */ $imported = $pdo->query('SELECT * FROM admis')->fetchAll(\PDO::FETCH_OBJ); $importedLogins = []; $importedRegistry = []; foreach ($imported as $import) { $importedRegistry[$import->login] = $import; $importedLogins[] = $import->login; } /* * Database */ $qb = $em->createQueryBuilder(); $dbUsers = $qb->select('u.login')->from('EtuUserBundle:User', 'u')->where($qb->expr()->in('u.login', $importedLogins))->getQuery()->getArrayResult(); $dbLogins = []; foreach ($dbUsers as $dbUser) { $dbLogins[] = $dbUser['login']; } $toImport = array_diff($importedLogins, $dbLogins); $output->writeln(sprintf('%s users to import', count($toImport))); $output->writeln("\nImporting users ..."); if (count($toImport) > 0) { $bar = new ProgressBar('%fraction% [%bar%] %percent%', '=>', ' ', 80, count($toImport)); $bar->update(0); $i = 1; foreach ($toImport as $loginToImport) { $import = $importedRegistry[$loginToImport]; $user = new User(); $user->setLogin($import->login); $user->setPassword($this->getContainer()->get('etu.user.crypting')->encrypt($import->password)); $user->setFullName($import->prenom . ' ' . $import->nom); $user->setSex($import->sexe == 'M' ? User::SEX_MALE : User::SEX_FEMALE); $user->setSexPrivacy(User::PRIVACY_PRIVATE); $user->setCity(ucfirst(strtolower($import->ville))); $user->setCityPrivacy(User::PRIVACY_PRIVATE); $user->setPostalCode($import->codePostal); $user->setPostalCodePrivacy(User::PRIVACY_PRIVATE); $user->setCountry(ucfirst(strtolower($import->pays))); $user->setCountryPrivacy(User::PRIVACY_PRIVATE); $user->setPersonnalMail($import->email); $user->setPersonnalMailPrivacy(User::PRIVACY_PRIVATE); $user->setBranch($import->branche); $user->setFiliere($import->specialite); $user->setNiveau(intval($import->niveau)); $user->setFormation('Inconnue'); $user->setLanguage('fr'); $user->setKeepActive(false); $em->persist($user); $em->flush(); $bar->update($i); $i++; } } $output->write("\nDone\n"); }