예제 #1
0
 /**
  * After validation generate new pwd, recovery token and send email
  * @throws SyntaxException
  * @throws \Ffcms\Core\Exception\NativeException
  */
 public function make()
 {
     $user = App::$User->getIdentityViaEmail($this->email);
     if ($user === null) {
         throw new SyntaxException('Email not found');
     }
     if ($user->approve_token !== '0' && Str::length($user->approve_token) > 0) {
         throw new SyntaxException('You must approve your account');
     }
     $rows = UserRecovery::where('user_id', '=', $user->getId())->orderBy('id', 'DESC')->first();
     if ($rows !== null && $rows !== false) {
         // prevent spam of recovery messages
         if (Date::convertToTimestamp($rows->created_at) > time() - self::DELAY) {
             return;
         }
     }
     // generate pwd, token and pwdCrypt
     $newPwd = Str::randomLatinNumeric(mt_rand(8, 16));
     $pwdCrypt = App::$Security->password_hash($newPwd);
     $token = Str::randomLatinNumeric(mt_rand(64, 128));
     // write new data to recovery table
     $rObject = new UserRecovery();
     $rObject->user_id = $user->id;
     $rObject->password = $pwdCrypt;
     $rObject->token = $token;
     $rObject->save();
     // write logs data
     $log = new UserLog();
     $log->user_id = $user->id;
     $log->type = 'RECOVERY';
     $log->message = __('Password recovery is initialized from: %ip%', ['ip' => App::$Request->getClientIp()]);
     $log->save();
     // generate mail template
     $mailTemplate = App::$View->render('user/mail/recovery', ['login' => $user->login, 'email' => $this->email, 'password' => $newPwd, 'token' => $token, 'id' => $rObject->id]);
     $sender = App::$Properties->get('adminEmail');
     // format SWIFTMailer format
     $mailMessage = \Swift_Message::newInstance(App::$Translate->get('Profile', 'Account recovery on %site%', ['site' => App::$Request->getHost()]))->setFrom([$sender])->setTo([$this->email])->setBody($mailTemplate, 'text/html');
     // send message
     App::$Mailer->send($mailMessage);
 }
예제 #2
0
 /**
  * Open session and store data token to db
  * @param iUser $userObject
  * @return bool
  */
 public function openSession(iUser $userObject)
 {
     if ($userObject === null || $userObject->id < 1) {
         return false;
     }
     // write session data
     App::$Session->set('ff_user_id', $userObject->id);
     // write user log
     $log = new UserLog();
     $log->user_id = $userObject->id;
     $log->type = 'AUTH';
     $log->message = __('Successful authorization from ip: %ip%', ['ip' => App::$Request->getClientIp()]);
     $log->save();
     return true;
 }