Пример #1
0
 /**
  * Sends email message to user.
  *
  * @return bool True on success, false otherwise
  * @throws \BadMethodCallException When "name" or "email" properties are missing
  *  for the provided User entity
  */
 public function send()
 {
     if (!$this->_user->has('email') || !$this->_user->has('name')) {
         throw new BadMethodCallException(__d('user', 'Missing "name" or "email" property when trying to send the email.'));
     }
     if ($this->config('updateToken') === true) {
         $this->loadModel('User.Users');
         $this->_user->updateToken();
     }
     $subject = $this->_parseVariables((string) $this->subject());
     $body = $this->_parseVariables((string) $this->body());
     if (empty($subject) || empty($body)) {
         return false;
     }
     $sender = new Email($this->config('emailConfig'));
     $sent = false;
     try {
         $sent = $sender->to($this->_user->get('email'), $this->_user->get('name'))->subject($subject)->send($body);
     } catch (\Exception $e) {
         return false;
     }
     return $sent;
 }
Пример #2
0
 /**
  * Generates a unique token for the given user entity. The generated token is
  * automatically persisted on DB.
  *
  * Tokens are unique within the entire DB and follows the pattern below:
  *
  *     <32-random-letters-and-numbers>
  *
  * @param \User\Model\Entity\User $user The user for which generate the token
  * @return \User\Model\Entity\User The user entity with a the new token property
  * @throws \Cake\Error\FatalErrorException When an invalid user entity was given
  */
 public function updateToken(User $user)
 {
     if (!$user->has('id')) {
         throw new FatalErrorException(__d('user', 'UsersTable::updateToken(), no ID was found for the given entity.'));
     }
     $token = md5(uniqid($user->id, true));
     $count = $this->find()->where(['Users.token' => $token])->limit(1)->count();
     while ($count > 0) {
         $token = str_shuffle(md5(uniqid($user->id, true) . rand(1, 9999)));
         $count = $this->find()->where(['Users.token' => $token])->limit(1)->count();
     }
     $user->set('token', $token);
     $user->set('token_expiration', time() + USER_TOKEN_EXPIRATION);
     $this->updateAll(['token' => $user->get('token'), 'token_expiration' => $user->get('token_expiration')], ['id' => $user->id]);
     return $user;
 }