Bolt\AccessControl\Password::resetPasswordRequest PHP Method

resetPasswordRequest() public method

Sends email with password request. Accepts email or username.
public resetPasswordRequest ( string $username, string $remoteIP, Symfony\Component\EventDispatcher\Event $event ) : boolean
$username string
$remoteIP string
$event Symfony\Component\EventDispatcher\Event
return boolean
    public function resetPasswordRequest($username, $remoteIP, Event $event)
    {
        /** @var UsersRepository $repo */
        $repo = $this->app['storage']->getRepository('Bolt\\Storage\\Entity\\Users');
        /** @var Entity\Users $userEntity */
        $userEntity = $repo->getUser($username);
        if (!$userEntity) {
            // For safety, this is the message we display, regardless of whether user exists.
            $this->app['logger.flash']->clear();
            $this->app['logger.flash']->info(Trans::__('page.login.password-reset-link-sent', ['%user%' => $username]));
            $this->app['dispatcher']->dispatch(AccessControlEvents::RESET_FAILURE, $event);
            return false;
        }
        // Generate shadow password and hash
        $shadowPassword = $this->app['randomgenerator']->generateString(12);
        $shadowPasswordHash = $this->app['password_factory']->createHash($shadowPassword, '$2y$');
        // Generate shadow token and hash
        $shadowToken = $this->app['randomgenerator']->generateString(32);
        $shadowTokenHash = md5($shadowToken . '-' . str_replace('.', '-', $remoteIP));
        // Set the shadow password and related stuff in the database.
        $userEntity->setShadowpassword($shadowPasswordHash);
        $userEntity->setShadowtoken($shadowTokenHash);
        $userEntity->setShadowvalidity(Carbon::create()->addHours(2));
        $this->app['storage']->getRepository('Bolt\\Storage\\Entity\\Users')->save($userEntity);
        $mailoptions = $this->app['config']->get('general/mailoptions');
        // PHP 5.4 compatibility
        if (empty($mailoptions)) {
            $this->app['logger.flash']->danger(Trans::__('general.phrase.error-mail-options-not-set'));
        }
        // Sent the password reset notification
        $this->resetPasswordNotification($userEntity, $shadowPassword, $shadowToken);
        $this->app['dispatcher']->dispatch(AccessControlEvents::RESET_REQUEST, $event);
        return true;
    }

Usage Example

Exemplo n.º 1
0
 public function testResetPasswordRequestSendFailure()
 {
     $app = $this->getApp();
     $this->addDefaultUser($app);
     $app['config']->set('general/mailoptions', ['transport' => 'smtp', 'spool' => true, 'host' => 'localhost', 'port' => '25']);
     $logger = $this->getMock('\\Bolt\\Logger\\FlashLogger', ['error']);
     $logger->expects($this->atLeastOnce())->method('error')->with($this->equalTo('Failed to send password request. Please check the email settings.'));
     $app['logger.flash'] = $logger;
     $logger = $this->getMock('\\Monolog\\Logger', ['error'], ['testlogger']);
     $logger->expects($this->atLeastOnce())->method('error')->with($this->equalTo("Failed to send password request sent to 'Admin'."));
     $app['logger.system'] = $logger;
     $mailer = $this->getMock('\\Swift_Mailer', ['send'], [$app['swiftmailer.transport']]);
     $mailer->expects($this->atLeastOnce())->method('send')->will($this->returnValue(false));
     $app['mailer'] = $mailer;
     $event = new AccessControlEvent(Request::createFromGlobals());
     $password = new Password($app);
     $result = $password->resetPasswordRequest('admin', '8.8.8.8', $event);
     $this->assertTrue($result);
 }
All Usage Examples Of Bolt\AccessControl\Password::resetPasswordRequest