Esempio n. 1
0
 public function validate($value, Constraint $constraint)
 {
     $users = UserQuery::create()->filterByUserName($value)->count();
     if ($users > 0) {
         $this->context->buildViolation($constraint->message)->setParameter('username', $value)->addViolation();
     }
 }
Esempio n. 2
0
 /**
  * Automatically generated run method
  * 
  * @param Request $request
  * @return Response
  */
 public function run(Request $request)
 {
     $body = $request->getContent();
     if (!isset($body['data'])) {
         throw new InvalidParameterException();
     }
     $data = $body['data'];
     $id = $this->getParam('id');
     $group = GroupQuery::create()->findOneById($id);
     if ($group === null) {
         throw new ResourceNotFoundException('group with id ' . $id . ' does not exist');
     }
     // remove all relationships before
     UserGroupQuery::create()->filterByGroup($group)->deleteAll();
     // add them
     foreach ($data as $entry) {
         if (!isset($entry['id'])) {
             throw new InvalidParameterException();
         }
         $user = UserQuery::create()->findOneById($entry['id']);
         $group->addUser($user);
         $group->save();
     }
     // run response
     return $this->response->run($request, $group);
 }
Esempio n. 3
0
 /**
  * Get the associated ChildUser object
  *
  * @param  ConnectionInterface $con Optional Connection object.
  * @return ChildUser The associated ChildUser object.
  * @throws PropelException
  */
 public function getUser(ConnectionInterface $con = null)
 {
     if ($this->aUser === null && $this->user_id !== null) {
         $this->aUser = ChildUserQuery::create()->findPk($this->user_id, $con);
         /* The following can be used additionally to
               guarantee the related object contains a reference
               to this object.  This level of coupling may, however, be
               undesirable since it could result in an only partially populated collection
               in the referenced object.
               $this->aUser->addSessions($this);
            */
     }
     return $this->aUser;
 }
Esempio n. 4
0
 /**
  * Returns one User with the given id from cache
  * 
  * @param mixed $id
  * @return User|null
  */
 protected function get($id)
 {
     if ($this->pool === null) {
         $this->pool = new Map();
     } else {
         if ($this->pool->has($id)) {
             return $this->pool->get($id);
         }
     }
     $model = UserQuery::create()->findOneById($id);
     $this->pool->set($id, $model);
     return $model;
 }
Esempio n. 5
0
 /**
  * Performs an INSERT on the database, given a User or Criteria object.
  *
  * @param mixed               $criteria Criteria or User object containing data that is used to create the INSERT statement.
  * @param ConnectionInterface $con the ConnectionInterface connection to use
  * @return mixed           The new primary key.
  * @throws PropelException Any exceptions caught during processing will be
  *                         rethrown wrapped into a PropelException.
  */
 public static function doInsert($criteria, ConnectionInterface $con = null)
 {
     if (null === $con) {
         $con = Propel::getServiceContainer()->getWriteConnection(UserTableMap::DATABASE_NAME);
     }
     if ($criteria instanceof Criteria) {
         $criteria = clone $criteria;
         // rename for clarity
     } else {
         $criteria = $criteria->buildCriteria();
         // build Criteria from User object
     }
     if ($criteria->containsKey(UserTableMap::COL_ID) && $criteria->keyContainsValue(UserTableMap::COL_ID)) {
         throw new PropelException('Cannot insert a value for auto-increment primary key (' . UserTableMap::COL_ID . ')');
     }
     // Set the correct dbName
     $query = UserQuery::create()->mergeWith($criteria);
     // use transaction because $criteria could contain info
     // for more than one table (I guess, conceivably)
     return $con->transaction(function () use($con, $query) {
         return $query->doInsert($con);
     });
 }
Esempio n. 6
0
File: User.php Progetto: keeko/core
 /**
  * Builds a Criteria object containing the primary key for this object.
  *
  * Unlike buildCriteria() this method includes the primary key values regardless
  * of whether or not they have been modified.
  *
  * @throws LogicException if no primary key is defined
  *
  * @return Criteria The Criteria object containing value(s) for primary key(s).
  */
 public function buildPkeyCriteria()
 {
     $criteria = ChildUserQuery::create();
     $criteria->add(UserTableMap::COL_ID, $this->id);
     return $criteria;
 }
Esempio n. 7
0
 /**
  * Automatically generated run method
  *
  * @param Request $request
  * @return Response
  */
 public function run(Request $request)
 {
     $token = $this->getParam('token');
     $auth = $this->getServiceContainer()->getAuthManager();
     $translator = $this->getServiceContainer()->getTranslator();
     $page = $this->getServiceContainer()->getKernel()->getApplication()->getPage();
     $page->setTitle($translator->trans('forgot_password'));
     if ($request->isMethod('POST')) {
         // reset password
         if (!empty($token)) {
             $post = $request->request;
             $user = $auth->getUser();
             $pwA = $post->get('new_password');
             $pwB = $post->get('new_password_confirm');
             if ($pwA == $pwB) {
                 $user->setPassword($auth->encryptPassword($pwA));
                 $user->setPasswordRecoverToken(null);
                 $user->setPasswordRecoverTime(null);
                 $user->save();
                 $payload = new Updated();
             } else {
                 $payload = new Found(['token' => $token, 'destination' => $this->getTargetLocation($token), 'error' => $translator->trans('change_password_nomatch')]);
             }
         } else {
             $login = $request->request->get('login');
             $user = $auth->findUser($login);
             if ($user === null) {
                 $message = $translator->trans('error.user_not_found', [], 'keeko.account');
                 $payload = new Blank(['error' => $message, 'login' => $login]);
             } else {
                 $token = AuthManager::generateToken();
                 $user->setPasswordRecoverToken($token);
                 $user->setPasswordRecoverTime(new DateTime());
                 $user->save();
                 // send mail
                 $prefs = $this->getServiceContainer()->getPreferenceLoader()->getSystemPreferences();
                 $localeService = $this->getServiceContainer()->getLocaleService();
                 $file = $localeService->findLocaleFile('/keeko/account/locales/{locale}/mail/forgot-password.twig');
                 $body = $this->render($file, ['user' => $user->getDisplayName(), 'plattform' => $prefs->getPlattformName(), 'link' => $this->getTargetLocation($token)]);
                 $mailer = $this->getServiceContainer()->getMailer();
                 $message = $this->getServiceContainer()->createMessage();
                 $message->setTo($user->getEmail());
                 $message->setSubject($translator->trans('forgot_password.subject'));
                 $message->setBody($body);
                 $mailer->send($message);
                 $payload = new Created(['mail' => $user->getEmail()]);
             }
         }
     } else {
         if (!empty($token)) {
             $user = UserQuery::create()->findOneByPasswordRecoverToken($token);
             if ($user !== null) {
                 $now = new DateTime();
                 $time = $user->getPasswordRecoverTime();
                 $diff = $now->diff($time);
                 if ($diff->h >= 1) {
                     $payload = new Failed(['error' => $translator->trans('error.token_timeout')]);
                 } else {
                     $payload = new Found(['token' => $token, 'destination' => $this->getTargetLocation($token)]);
                 }
             } else {
                 $payload = new Failed(['error' => $translator->trans('error.token_invalid')]);
             }
         } else {
             $payload = new Blank();
         }
     }
     return $this->responder->run($request, $payload);
 }
Esempio n. 8
0
 /**
  *
  * @param string $login
  * @return User|null
  */
 public function findUser($login)
 {
     $query = UserQuery::create();
     $prefs = $this->service->getPreferenceLoader()->getSystemPreferences();
     $mode = $prefs->getUserLogin();
     // login with username
     if ($mode == SystemPreferences::LOGIN_USERNAME) {
         $query = $query->filterByUserName($login);
     } else {
         if ($mode == SystemPreferences::LOGIN_EMAIL) {
             $query = $query->filterByEmail($login);
         } else {
             if ($mode == SystemPreferences::LOGIN_USERNAME_EMAIL) {
                 $query = $query->filterByEmail($login)->_or()->filterByUserName($login);
             } else {
                 return null;
             }
         }
     }
     return $query->findOne();
 }
Esempio n. 9
0
 /**
  * Internal update mechanism of Users on Group
  * 
  * @param Group $model
  * @param mixed $data
  */
 protected function doUpdateUsers(Group $model, $data)
 {
     // remove all relationships before
     UserGroupQuery::create()->filterByGroup($model)->delete();
     // add them
     $errors = [];
     foreach ($data as $entry) {
         if (!isset($entry['id'])) {
             $errors[] = 'Missing id for User';
         } else {
             $related = UserQuery::create()->findOneById($entry['id']);
             $model->addUser($related);
         }
     }
     if (count($errors) > 0) {
         throw new ErrorsException($errors);
     }
 }
Esempio n. 10
0
File: Group.php Progetto: keeko/core
 /**
  * Gets the number of User objects related by a many-to-many relationship
  * to the current object by way of the kk_user_group cross-reference table.
  *
  * @param      Criteria $criteria Optional query object to filter the query
  * @param      boolean $distinct Set to true to force count distinct
  * @param      ConnectionInterface $con Optional connection object
  *
  * @return int the number of related User objects
  */
 public function countUsers(Criteria $criteria = null, $distinct = false, ConnectionInterface $con = null)
 {
     $partial = $this->collUsersPartial && !$this->isNew();
     if (null === $this->collUsers || null !== $criteria || $partial) {
         if ($this->isNew() && null === $this->collUsers) {
             return 0;
         } else {
             if ($partial && !$criteria) {
                 return count($this->getUsers());
             }
             $query = ChildUserQuery::create(null, $criteria);
             if ($distinct) {
                 $query->distinct();
             }
             return $query->filterByGroup($this)->count($con);
         }
     } else {
         return count($this->collUsers);
     }
 }