/**
  * Resets all pwds to a simple pwd (for local development).
  *
  * @param string|null $pwd
  * @return void
  */
 public function pwd($pwd = null)
 {
     if (!empty($pwd)) {
         $pwdToHash = $pwd;
     }
     while (empty($pwdToHash) || mb_strlen($pwdToHash) < 2) {
         $pwdToHash = $this->in(__('Password to Hash (2 characters at least)'));
     }
     $this->hr();
     $this->out('Password:'******'Default';
     $hashType = Configure::read('Passwordable.passwordHasher');
     if ($hashType) {
         $hasher = $hashType;
     }
     $passwordHasher = PasswordHasherFactory::build($hasher);
     $pwd = $passwordHasher->hash($pwdToHash);
     $this->hr();
     $this->out('Hash:');
     $this->out($pwd);
     $this->hr();
     $this->out('resetting...');
     $this->Users = TableRegistry::get(CLASS_USERS);
     if (!$this->Users->hasField('password')) {
         $this->abort(CLASS_USERS . ' table doesnt have a password field!');
     }
     if (empty($this->params['dry-run'])) {
         $count = $this->Users->updateAll(['password' => $pwd], ['password !=' => $pwd]);
     } else {
         $count = $this->Users->find('all', ['conditions' => [CLASS_USERS . '.password !=' => $pwd]])->count();
     }
     $this->out($count . ' pwds resetted - DONE');
 }
 /**
  * Constructor
  *
  * @param array $config configuration options for this object. Requires the
  * `hashers` key to be present in the array with a list of other hashers to be
  * used
  */
 public function __construct(array $config = array())
 {
     parent::__construct($config);
     foreach ($this->_config['hashers'] as $hasher) {
         $this->_hashers[] = PasswordHasherFactory::build($hasher);
     }
 }
 public function passwordHasher()
 {
     if ($this->_passwordHasher) {
         return $this->_passwordHasher;
     }
     $passwordHasher = 'default';
     return $this->_passwordHasher = PasswordHasherFactory::build($passwordHasher);
 }
 /**
  * SetUp method
  *
  * @return void
  */
 public function setUp()
 {
     parent::setUp();
     Configure::write('App.namespace', 'TestApp');
     Configure::delete('Passwordable');
     Configure::write('Passwordable.auth', 'AuthTest');
     $this->Users = TableRegistry::get('ToolsUsers');
     $this->hasher = PasswordHasherFactory::build('Default');
     Router::setRequestInfo(new Request());
 }
 /**
  * Constructor
  *
  * @param array $config configuration options for this object. Requires the
  * `hashers` key to be present in the array with a list of other hashers to be
  * used
  */
 public function __construct(array $config = [])
 {
     parent::__construct($config);
     foreach ($this->_config['hashers'] as $key => $hasher) {
         if (!is_string($hasher)) {
             $hasher += ['className' => $key];
         }
         $this->_hashers[] = PasswordHasherFactory::build($hasher);
     }
 }
 public function run()
 {
     $data = [['title' => 'Guest', 'slug' => 'guest', 'admin' => false, 'core' => true], ['title' => 'Registered', 'slug' => 'registered', 'admin' => false, 'core' => true], ['title' => 'Manager', 'slug' => 'manager', 'admin' => true, 'core' => true], ['title' => 'Admin', 'slug' => 'admin', 'admin' => true, 'core' => true]];
     $roles = $this->table('passengers_roles');
     $roles->insert($data)->save();
     $adminRole = $this->fetchRow('SELECT * FROM passengers_roles WHERE slug LIKE \'admin\'');
     $passwordHasher = PasswordHasherFactory::build('Default');
     $data = [['role_id' => $adminRole['id'], 'username' => 'admin', 'password' => $passwordHasher->hash('qwerty1234'), 'email' => '*****@*****.**', 'active' => true]];
     $users = $this->table('passengers_users');
     $users->insert($data)->save();
 }
 /**
  * test
  *
  * @return void
  */
 public function testChangePasswordHappy()
 {
     $this->assertEquals('12345', $this->table->get('00000000-0000-0000-0000-000000000001')->password);
     $this->_mockRequestPost();
     $this->_mockAuthLoggedIn();
     $this->_mockFlash();
     $this->Trait->request->expects($this->once())->method('data')->will($this->returnValue(['password' => 'new', 'password_confirm' => 'new']));
     $this->Trait->expects($this->once())->method('redirect')->with(['plugin' => 'CakeDC/Users', 'controller' => 'Users', 'action' => 'profile']);
     $this->Trait->Flash->expects($this->any())->method('success')->with('Password has been changed successfully');
     $this->Trait->changePassword();
     $hasher = PasswordHasherFactory::build('Default');
     $this->assertTrue($hasher->check('new', $this->table->get('00000000-0000-0000-0000-000000000001')->password));
 }
 /**
  * SetUp method
  *
  * @return void
  */
 public function setUp()
 {
     parent::setUp();
     Configure::write('App.namespace', 'TestApp');
     Configure::delete('Passwordable');
     Configure::write('Passwordable.auth', 'AuthTest');
     $this->Users = TableRegistry::get('ToolsUsers');
     $this->hasher = PasswordHasherFactory::build('Default');
     $user = $this->Users->newEntity();
     $data = ['id' => '5', 'name' => 'admin', 'password' => $this->hasher->hash('somepwd'), 'role_id' => '1'];
     $this->Users->patchEntity($user, $data);
     $result = $this->Users->save($user);
     $this->assertTrue((bool) $result);
     Router::setRequestInfo(new Request());
 }
 /**
  * Migrate Up.
  */
 public function up()
 {
     //Create roles table
     $roles = $this->table('passengers_roles');
     $roles->addColumn('title', 'string', ['limit' => 255])->addColumn('slug', 'string', ['limit' => 255])->addColumn('admin', 'boolean', ['null' => true])->addColumn('core', 'boolean', ['null' => true])->addColumn('created', 'datetime', ['null' => true])->addColumn('modified', 'datetime', ['null' => true])->addColumn('user_count', 'integer', ['null' => true, 'default' => 0])->addIndex(['slug'], array('unique' => true, 'name' => 'passengers_roles_slug_idx'))->save();
     //Create users table
     $users = $this->table('passengers_users');
     $users->addColumn('role_id', 'integer', ['default' => 2])->addColumn('username', 'string', ['limit' => 60])->addColumn('password', 'string', ['limit' => 60])->addColumn('email', 'string', ['limit' => 250])->addColumn('created', 'datetime', ['null' => true])->addColumn('modified', 'datetime', ['null' => true])->addColumn('active', 'boolean', ['default' => 0])->addColumn('activation_code', 'string', ['limit' => 32, 'null' => true])->addColumn('update_required', 'boolean', ['null' => true, 'default' => 0])->addColumn('profile', 'text', ['null' => true])->addColumn('options', 'text', ['null' => true])->addIndex(['role_id'], array('unique' => false, 'name' => 'passengers_users_role_id_idx'))->save();
     //Create sessions table
     $sessionsExists = $this->hasTable('sessions');
     if (!$sessionsExists) {
         $sessions = $this->table('sessions', ['id' => false, 'primary_key' => ['id']]);
         $sessions->addColumn('id', 'string', ['limit' => 40])->addColumn('data', 'text', ['null' => true])->addColumn('expires', 'integer', ['null' => true])->save();
     }
     //Seed roles table by default data
     $roles = TableRegistry::get('Passengers.Roles');
     foreach ($this->roles as $role) {
         $role = $roles->newEntity($role);
         $roles->save($role);
     }
     //Seed users table by default data
     $users = TableRegistry::get('Passengers.Users');
     $passwordHasher = PasswordHasherFactory::build('Default');
     foreach ($this->users as $user) {
         $user['password'] = $passwordHasher->hash($user['password']);
         $user = $users->newEntity($user);
         $users->save($user);
     }
     $exists = $this->hasTable('rear_engine_cells');
     if ($exists) {
         //Seed roles table by default data
         $cells = TableRegistry::get('RearEngine.Cells');
         $blocks = TableRegistry::get('RearEngine.Blocks');
         foreach ($this->cells as $cell) {
             if (!is_int($cell['block_id'])) {
                 $block = $blocks->findAllBySlug($cell['block_id'])->first();
                 $cell['block_id'] = 1;
                 if (isset($block->id)) {
                     $cell['block_id'] = $block->id;
                 }
             }
             $cell = $cells->newEntity($cell);
             $cells->save($cell);
         }
     }
 }
 /**
  * Return password hasher object
  *
  * @return AbstractPasswordHasher Password hasher instance
  * @throws \RuntimeException If password hasher class not found or
  *   it does not extend AbstractPasswordHasher
  */
 public function passwordHasher()
 {
     if ($this->_passwordHasher) {
         return $this->_passwordHasher;
     }
     $passwordHasher = $this->_config['passwordHasher'];
     return $this->_passwordHasher = PasswordHasherFactory::build($passwordHasher);
 }
 /**
  * Reset password
  *
  * @param null|string $key
  *
  * @return \Cake\Network\Response|void
  */
 public function reset($key = null)
 {
     $this->set('title', __('Reset Password'));
     /** @var $usersTable UsersTable */
     $usersTable = TableRegistry::get('Pie/Users.Users');
     /** @var $user User */
     $user = $usersTable->find()->where(['status' => 1])->matching('UserDetails', function (Query $query) use($key) {
         return $query->where(['key' => 'reset_key', 'value' => $key]);
     })->contain('UserDetails')->first();
     if (!$user || is_null($key)) {
         throw new NotFoundException();
     }
     if ($this->request->is(['post', 'put'])) {
         $validator = new Validator();
         $validator->add('new_password', ['minLengthPassword' => ['rule' => ['minLength', 8], 'message' => __d('users', 'Minimum length of password is 8 characters.')]])->add('confirm_password', ['equalToPassword' => ['rule' => function ($value, $context) {
             if ($value === $context['data']['new_password']) {
                 return true;
             } else {
                 return false;
             }
         }, 'message' => __d('users', 'Entered passwords do not match.')]]);
         $errors = $validator->errors($this->request->data, $user->isNew());
         $user->errors($errors);
         if (empty($errors)) {
             $user->set('password', PasswordHasherFactory::build(Configure::read('pie.users.passwordHasher'))->hash($this->request->data('new_password')));
             if ($usersTable->save($user)) {
                 /** @var $userDetailsTable UserDetailsTable */
                 $userDetailsTable = TableRegistry::get('Pie/Users.UserDetails');
                 $userDetailsTable->delete($user->getDetails()['reset_key']);
                 $this->Flash->set(__('Your password has been reset successfully.'), ['element' => 'success']);
                 return $this->redirect(['action' => 'login']);
             }
         }
         $this->Flash->set(__('An error occurred. Please try again.'), ['element' => 'error']);
     }
     $this->set(compact('user'));
 }
 /**
  * PasswordableBehavior::_getPasswordHasher()
  *
  * @param string|array $hasher Name or options array.
  * @param array $options
  * @return \Cake\Auth\AbstractPasswordHasher
  */
 protected function _getPasswordHasher($hasher, array $options = [])
 {
     if ($this->_passwordHasher) {
         return $this->_passwordHasher;
     }
     $config = [];
     if (is_string($hasher)) {
         $class = $hasher;
     } else {
         $class = $hasher['className'];
         $config = $hasher;
         unset($config['className']);
     }
     $config['className'] = $class;
     $config += $options;
     return $this->_passwordHasher = PasswordHasherFactory::build($config);
 }
 /**
  * test build() throws exception for non existent hasher
  *
  * @expectedException \RuntimeException
  * @expectedExceptionMessage Password hasher class "FooBar" was not found.
  * @return void
  */
 public function testBuildException()
 {
     $hasher = PasswordHasherFactory::build('FooBar');
 }
 /**
  * PasswordableBehavior::_getPasswordHasher()
  *
  * @param mixed $hasher Name or options array.
  * @return PasswordHasher
  */
 protected function _getPasswordHasher($hasher)
 {
     if ($this->_passwordHasher) {
         return $this->_passwordHasher;
     }
     return $this->_passwordHasher = PasswordHasherFactory::build($hasher);
 }
 public function change($data = [])
 {
     $passwordHasher = PasswordHasherFactory::build('Default');
     $data['password'] = $passwordHasher->hash($data['password']);
     return $data;
 }