Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Commit

Permalink
Merge pull request #45 from burzum/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
burzum authored Jul 17, 2017
2 parents 3a3091a + 8121303 commit c7b309c
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 71 deletions.
10 changes: 4 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ language: php
php:
- 5.6
- 7.0
- 7.1

matrix:
allow_failures:
- php: 7.0

fast_finish: true

include:
- php: 5.6
- php: 7.1
env:
- COVERALLS=1

Expand All @@ -22,8 +20,8 @@ before_script:
- sh -c "if [ '$COVERALLS' = '1' ]; then mkdir -p build/logs; fi"

script:
- phpunit
- sh -c "if [ '$COVERALLS' = '1' ]; then phpunit --stderr --coverage-clover build/logs/clover.xml; fi"
- vendor/bin/phpunit
- sh -c "if [ '$COVERALLS' = '1' ]; then vendor/bin/phpunit --stderr --coverage-clover build/logs/clover.xml; fi"
- sh -c "if [ '$COVERALLS' = '1' ]; then php vendor/bin/coveralls -c .coveralls.yml -v; fi"

notifications:
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Requirements
------------

* CakePHP 3.0+
* PHP 5.4.19

Complementary plugins
---------------------
Expand Down
2 changes: 1 addition & 1 deletion src/Auth/DefaultAuthSetupTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* UserToolComponent
*
* @author Florian Krämer
* @copyright 2013 - 2016 Florian Krämer
* @copyright 2013 - 2017 Florian Krämer
* @license MIT
*/
namespace Burzum\UserTools\Auth;
Expand Down
15 changes: 12 additions & 3 deletions src/Controller/Component/FlashAndRedirectTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* FlashAndRedirectTrait
*
* @author Florian Krämer
* @copyright 2013 - 2016 Florian Krämer
* @copyright 2013 - 2017 Florian Krämer
* @license MIT
*/
namespace Burzum\UserTools\Controller\Component;
Expand All @@ -14,7 +14,7 @@ trait FlashAndRedirectTrait {
* Helper property to detect a redirect
*
* @see UserToolComponent::handleFlashAndRedirect();
* @var \Cake\Network\Response
* @var \Cake\Http\Response|null
*/
protected $_redirectResponse = null;

Expand Down Expand Up @@ -44,6 +44,9 @@ protected function _handleRedirect($type, $options) {
$this->_redirectResponse = $result;
return $result;
}

$this->_redirectResponse = null;

return false;
}

Expand All @@ -61,10 +64,16 @@ protected function _handleFlash($type, $options) {
if (isset($options[$type . 'FlashOptions'])) {
$flashOptions = $options[$type . 'FlashOptions'];
}
$this->Flash->$type($options[$type . 'Message'], $flashOptions);

if (!isset($flashOptions['element'])) {
$flashOptions['element'] = $type;
$this->Flash->set($options[$type . 'Message'], $flashOptions);
}

return true;
}
}

return false;
}
}
103 changes: 72 additions & 31 deletions src/Controller/Component/UserToolComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* UserToolComponent
*
* @author Florian Krämer
* @copyright 2013 - 2016 Florian Krämer
* @copyright 2013 - 2017 Florian Krämer
* @license MIT
*/
namespace Burzum\UserTools\Controller\Component;
Expand All @@ -13,16 +13,17 @@
use Cake\Controller\ComponentRegistry;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\Exception\RecordNotFoundException;
use Cake\Event\EventManagerTrait;
use Cake\Event\EventDispatcherTrait;
use Cake\Event\Event;
use Cake\Http\Response;
use Cake\Network\Exception\NotFoundException;
use Cake\Network\Response;
use Cake\ORM\TableRegistry;
use Cake\Utility\Hash;
use Cake\View\Exception\MissingTemplateException;

class UserToolComponent extends Component {

use EventManagerTrait;
use EventDispatcherTrait;
use FlashAndRedirectTrait;

/**
Expand Down Expand Up @@ -101,16 +102,24 @@ class UserToolComponent extends Component {
'setEntity' => true,
],
'resetPassword' => [
'queryParam' => 'token',
'tokenOptions' => [],
// Success
'successFlashOptions' => [],
'successRedirectUrl' => '/',
// Normal error
'errorFlashOptions' => [],
'errorRedirectUrl' => false,
'invalidErrorFlashOptions' => [],
// Invalid Token error
'invalidErrorFlashOptions' => [
'element' => 'Flash/error'
],
'invalidErrorRedirectUrl' => '/',
'expiredErrorFlashOptions' => [],
'expiredErrorRedirectUrl' => '/',
'queryParam' => 'token',
'tokenOptions' => [],
// Token expired error
'expiredErrorFlashOptions' => [
'element' => 'Flash/error'
],
'expiredErrorRedirectUrl' => '/'
],
'changePassword' => [
'successFlashOptions' => [],
Expand Down Expand Up @@ -198,7 +207,7 @@ class UserToolComponent extends Component {
/**
* Response object
*
* @var \Cake\Network\Response
* @var \Cake\Http\Response
*/
public $response = null;

Expand Down Expand Up @@ -359,16 +368,20 @@ public function mapAction() {

/**
* @param string $action
* @return \Cake\Network\Response A response object containing the rendered view.
* @return \Cake\Http\Response|bool A response object containing the rendered view.
*/
protected function _directMapping($action) {
if (!method_exists($this, $action)) {
return false;
}
$result = $this->{$action}();

$pass = (array)$this->request->getParam('pass');
$result = call_user_func_array([$this, $action], $pass);

if ($result instanceof Response) {
return $result;
}

return $this->_controller->render($action);
}

Expand All @@ -381,16 +394,26 @@ protected function _directMapping($action) {
protected function _mapAction($action) {
$actionMap = $this->config('actionMap');
if (isset($actionMap[$action]) && method_exists($this, $actionMap[$action]['method'])) {
$this->{$actionMap[$action]['method']}();
$pass = (array)$this->request->getParam('pass');
call_user_func_array([$this, $actionMap[$action]['method']], $pass);

if ($this->_redirectResponse instanceof Response) {
return $this->_redirectResponse;
}

if (is_string($actionMap[$action]['view'])) {
return $this->_controller->render($actionMap[$action]['view']);
try {
return $this->_controller->render($this->_controller->request->getParam('action'));
} catch (MissingTemplateException $e) {
return $this->_controller->render($actionMap[$action]['view']);
}

return true;
} else {
return $this->response;
}
}

return false;
}

Expand Down Expand Up @@ -639,10 +662,14 @@ public function verifyEmailToken($options = []) {
*/
public function requestPassword($options = []) {
$options = Hash::merge($this->config('requestPassword'), $options);
$entity = $this->UserTable->newEntity(null, ['validate' => 'requestPassword']);
$entity = $this->UserTable->newEntity(null, [
'validate' => 'requestPassword'
]);

if ($this->request->is('post')) {
$entity = $this->UserTable->patchEntity($entity, $this->request->data, ['validate' => 'requestPassword']);
$entity = $this->UserTable->patchEntity($entity, $this->request->data, [
'validate' => 'requestPassword'
]);

if (!$entity->errors($options['field']) && $this->_initPasswordReset($entity, $options)) {
return true;
Expand Down Expand Up @@ -677,10 +704,12 @@ protected function _initPasswordReset(EntityInterface $entity, $options) {
if ($options['setEntity']) {
$this->_setViewVar('userEntity', $entity);
}

return true;
} catch (RecordNotFoundException $e) {
$this->handleFlashAndRedirect('error', $options);
}

return false;
}

Expand All @@ -692,38 +721,53 @@ protected function _initPasswordReset(EntityInterface $entity, $options) {
* @return void
*/
public function resetPassword($token = null, $options = []) {
$options = (Hash::merge($this->config('resetPassword'), $options));
$options = Hash::merge($this->config('resetPassword'), $options);

if (!empty($this->request->query[$options['queryParam']])) {
$token = $this->request->query[$options['queryParam']];
}

// Check of the token exists
try {
$entity = $this->UserTable->verifyPasswordResetToken($token, $options['tokenOptions']);
} catch (RecordNotFoundException $e) {
if (empty($options['invalidErrorMessage'])) {
$options['invalidErrorMessage'] = $e->getMessage();
if (empty($options['errorMessage']) && $options['errorMessage'] !== false) {
$options['errorMessage'] = $e->getMessage();
}

$redirect = $this->handleFlashAndRedirect('invalidError', $options);
if ($redirect instanceof Response) {
return $redirect;
}
$this->handleFlashAndRedirect('invalidError', $options);
$entity = $this->UserTable->newEntity();
}

if (isset($entity->token_is_expired) && $entity->token_is_expired === true) {
// Check if the token has expired
if ($entity->get('token_is_expired') === true) {
if (empty($options['invalidErrorMessage'])) {
$options['invalidErrorMessage'] = $e->getMessage();
}
$this->handleFlashAndRedirect('expiredError', $options);
$redirect = $this->handleFlashAndRedirect('expiredError', $options);
if ($redirect instanceof Response) {
return $redirect;
}
}

// Handle the POST
if ($this->request->is('post')) {
$entity = $this->UserTable->patchEntity($entity, $this->request->data);
if ($this->UserTable->resetPassword($entity)) {
$this->handleFlashAndRedirect('success', $options);
$redirect = $this->handleFlashAndRedirect('success', $options);
} else {
$this->handleFlashAndRedirect('error', $options);
$redirect = $this->handleFlashAndRedirect('error', $options);
}
if ($redirect instanceof Response) {
return $redirect;
}
} else {
$entity = $this->UserTable->newEntity();
}

$this->_setViewVar('entity', $entity);
}

Expand All @@ -734,34 +778,31 @@ public function resetPassword($token = null, $options = []) {
* @return void
*/
public function changePassword($options = []) {
$options = (Hash::merge($this->config('changePassword'), $options));
$options = Hash::merge($this->config('changePassword'), $options);

$entity = $this->UserTable->newEntity();
$entity->accessible([
'id',
'old_password',
'password',
'new_password',
'confirm_password'
], true);

if ($this->request->is(['post', 'put'])) {
$this->request->data['id'] = $this->_getAuthObject()->user('id');
$entity = $this->UserTable->get($this->_getAuthObject()->user('id'));
$entity = $this->UserTable->patchEntity($entity, $this->request->data, [
'validate' => 'changePassword'
]);
$entity->id = $this->_controller->Auth->user('id');
$entity->isNew(false);

if ($this->UserTable->changePassword($entity)) {
$this->request->data = [];
$entity = $this->UserTable->newEntity();
$entity->id = $this->_controller->Auth->user('id');
$entity->isNew(false);
$this->handleFlashAndRedirect('success', $options);
} else {
$this->handleFlashAndRedirect('error', $options);
}
}

$this->_setViewVar('entity', $entity);
}

Expand Down
Loading

0 comments on commit c7b309c

Please sign in to comment.