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 #30 from burzum/develop
Browse files Browse the repository at this point in the history
Merging develop to master for 1.0.4
  • Loading branch information
burzum committed Feb 17, 2016
2 parents 288bf5b + 4c066c5 commit f33602d
Show file tree
Hide file tree
Showing 5 changed files with 416 additions and 74 deletions.
70 changes: 70 additions & 0 deletions src/Controller/Component/FlashAndRedirectTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* FlashAndRedirectTrait
*
* @author Florian Krämer
* @copyright 2013 - 2016 Florian Krämer
* @license MIT
*/
namespace Burzum\UserTools\Controller\Component;

trait FlashAndRedirectTrait {

/**
* Helper property to detect a redirect
*
* @see UserToolComponent::handleFlashAndRedirect();
* @var \Cake\Network\Response
*/
protected $_redirectResponse = null;

/**
* Handles flashes and redirects
*
* @param string $type Prefix for the array key, mostly "success" or "error"
* @param array $options Options
* @return mixed
*/
public function handleFlashAndRedirect($type, $options) {
$this->_handleFlash($type, $options);
return $this->_handleRedirect($type, $options);
}

/**
* Handles the redirect options.
*
* @param string $type Prefix for the array key, mostly "success" or "error"
* @param array $options Options
* @return mixed
*/
protected function _handleRedirect($type, $options) {
if (isset($options[$type . 'RedirectUrl']) && $options[$type . 'RedirectUrl'] !== false) {
$controller = $this->_registry->getController();
$result = $controller->redirect($options[$type . 'RedirectUrl']);
$this->_redirectResponse = $result;
return $result;
}
return false;
}

/**
* Handles the flash options.
*
* @param string $type Prefix for the array key, mostly "success" or "error"
* @param array $options Options
* @return boolean
*/
protected function _handleFlash($type, $options) {
if (isset($options[$type . 'Message']) && $options[$type . 'Message'] !== false) {
if (is_string($options[$type . 'Message'])) {
$flashOptions = [];
if (isset($options[$type . 'FlashOptions'])) {
$flashOptions = $options[$type . 'FlashOptions'];
}
$this->Flash->$type($options[$type . 'Message'], $flashOptions);
return true;
}
}
return false;
}
}
99 changes: 33 additions & 66 deletions src/Controller/Component/UserToolComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
class UserToolComponent extends Component {

use EventManagerTrait;
use FlashAndRedirectTrait;

/**
* Components
Expand Down Expand Up @@ -82,7 +83,7 @@ class UserToolComponent extends Component {
],
'logout' => [
'successFlashOptions' => [],
'successRedirectUrl' => '/',
'successRedirectUrl' => null,
],
'verifyEmailToken' => [
'queryParam' => 'token',
Expand Down Expand Up @@ -201,14 +202,6 @@ class UserToolComponent extends Component {
*/
public $response = null;

/**
* Helper property to detect a redirect
*
* @see UserToolComponent::handleFlashAndRedirect();
* @var \Cake\Network\Response
*/
protected $_redirectResponse = null;

/**
* Convenience property to avoid the need to go through the registry all time.
*
Expand Down Expand Up @@ -364,6 +357,10 @@ public function mapAction() {
return $this->_mapAction($action);
}

/**
* @param string $action
* @return \Cake\Network\Response A response object containing the rendered view.
*/
protected function _directMapping($action) {
if (!method_exists($this, $action)) {
return false;
Expand All @@ -375,6 +372,12 @@ protected function _directMapping($action) {
return $this->_controller->render($action);
}

/**
* Maps an action of the controller to the component
*
* @param string $action
* @return bool|\Cake\Network\Response
*/
protected function _mapAction($action) {
$actionMap = $this->config('actionMap');
if (isset($actionMap[$action]) && method_exists($this, $actionMap[$action]['method'])) {
Expand Down Expand Up @@ -463,7 +466,7 @@ protected function _afterLogin($user, array $options) {
public function login($options = []) {
$options = Hash::merge($this->config('login'), $options);
$this->_handleUserBeingAlreadyLoggedIn($options);
$entity = $this->UserTable->newEntity([], ['validate' => false]);
$entity = $this->UserTable->newEntity(null, ['validate' => false]);
if ($this->request->is('post')) {
$user = $this->_beforeLogin($entity, $options);
if ($user) {
Expand Down Expand Up @@ -545,19 +548,23 @@ protected function _getUserEntity($userId) {
* Logout
*
* @param array $options Options array.
* @return void
* @return \Cake\Network\Response
*/
public function logout($options = []) {
$options = Hash::merge($this->config('logout'), $options);
$Auth = $this->_getAuthObject();
$user = $Auth->user();

if (empty($user)) {
$this->_controller->redirect($this->_controller->referer());
return;
return $this->_controller->redirect($this->_controller->referer());
}
$this->handleFlashAndRedirect('success', $options);
$this->_controller->redirect($Auth->logout());
return;

$logoutRedirect = $Auth->logout();
if (is_null($options['successRedirectUrl'])) {
$options['successRedirectUrl'] = $logoutRedirect;
}

return $this->handleFlashAndRedirect('success', $options);
}

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

if ($this->request->is('post')) {
$entity = $this->UserTable->patchEntity($entity, $this->request->data, ['validate' => 'requestPassword']);
Expand All @@ -641,12 +648,20 @@ public function requestPassword($options = []) {
unset($this->request->data[$options['field']]);
return false;
}

if ($options['setEntity']) {
$this->_controller->set('userEntity', $entity);
}
}

protected function _initPasswordReset($entity, $options) {
/**
* Initializes the password reset and handles a possible errors.
*
* @param \Cake\Datasource\EntityInterface
* @param array $options Options array
* @return bool
*/
protected function _initPasswordReset(Entity $entity, $options) {
try {
$this->UserTable->initPasswordReset($this->request->data[$options['field']]);
$this->handleFlashAndRedirect('success', $options);
Expand Down Expand Up @@ -769,54 +784,6 @@ public function verifyToken($options = []) {
return $result;
}

/**
* Handles flashes and redirects
*
* @param string $type Prefix for the array key, mostly "success" or "error"
* @param array $options Options
* @return mixed
*/
public function handleFlashAndRedirect($type, $options) {
$this->_handleFlash($type, $options);
$this->_handleRedirect($type, $options);
}

/**
* Handles the redirect options.
*
* @param string $type Prefix for the array key, mostly "success" or "error"
* @param array $options Options
* @return mixed
*/
protected function _handleRedirect($type, $options) {
if (isset($options[$type . 'RedirectUrl']) && $options[$type . 'RedirectUrl'] !== false) {
$result = $this->_controller->redirect($options[$type . 'RedirectUrl']);
return $this->_redirectResponse = $result;
}
return false;
}

/**
* Handles the flash options.
*
* @param string $type Prefix for the array key, mostly "success" or "error"
* @param array $options Options
* @return boolean
*/
protected function _handleFlash($type, $options) {
if (isset($options[$type . 'Message']) && $options[$type . 'Message'] !== false) {
if (is_string($options[$type . 'Message'])) {
$flashOptions = [];
if (isset($options[$type . 'FlashOptions'])) {
$flashOptions = $options[$type . 'FlashOptions'];
}
$this->Flash->set($options[$type . 'Message'], $flashOptions);
return true;
}
}
return false;
}

/**
* Gets the auth component object
*
Expand Down
Loading

0 comments on commit f33602d

Please sign in to comment.