diff --git a/src/Controller/Component/FlashAndRedirectTrait.php b/src/Controller/Component/FlashAndRedirectTrait.php new file mode 100644 index 0000000..2f576ad --- /dev/null +++ b/src/Controller/Component/FlashAndRedirectTrait.php @@ -0,0 +1,70 @@ +_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; + } +} diff --git a/src/Controller/Component/UserToolComponent.php b/src/Controller/Component/UserToolComponent.php index 666dc5d..46ee4ab 100644 --- a/src/Controller/Component/UserToolComponent.php +++ b/src/Controller/Component/UserToolComponent.php @@ -23,6 +23,7 @@ class UserToolComponent extends Component { use EventManagerTrait; + use FlashAndRedirectTrait; /** * Components @@ -82,7 +83,7 @@ class UserToolComponent extends Component { ], 'logout' => [ 'successFlashOptions' => [], - 'successRedirectUrl' => '/', + 'successRedirectUrl' => null, ], 'verifyEmailToken' => [ 'queryParam' => 'token', @@ -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. * @@ -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; @@ -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'])) { @@ -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) { @@ -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); } /** @@ -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']); @@ -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); @@ -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 * diff --git a/src/Locale/user_tools.pot b/src/Locale/user_tools.pot new file mode 100644 index 0000000..5c24354 --- /dev/null +++ b/src/Locale/user_tools.pot @@ -0,0 +1,301 @@ +# LANGUAGE translation of CakePHP Application +# Copyright YEAR NAME +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: PROJECT VERSION\n" +"POT-Creation-Date: 2016-02-17 00:30+0000\n" +"PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ\n" +"Last-Translator: NAME \n" +"Language-Team: LANGUAGE \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=utf-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Controller/Component/UserToolComponent.php:244 +msgid "An email was send to your address, please check your inbox." +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Controller/Component/UserToolComponent.php:245 +msgid "Invalid user." +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Controller/Component/UserToolComponent.php:248 +msgid "Your password has been reset, you can now login." +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Controller/Component/UserToolComponent.php:249 +msgid "Please check your inputs." +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Controller/Component/UserToolComponent.php:250 +msgid "Invalid token!" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Controller/Component/UserToolComponent.php:251 +msgid "The token has expired!" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Controller/Component/UserToolComponent.php:254 +msgid "Your password has been updated." +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Controller/Component/UserToolComponent.php:255 +msgid "Could not update your password, please check for errors and try again." +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Controller/Component/UserToolComponent.php:258 +msgid "Thank you for signing up!" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Controller/Component/UserToolComponent.php:259 +msgid "Please check your inputs" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Controller/Component/UserToolComponent.php:262 +msgid "You are logged in!" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Controller/Component/UserToolComponent.php:263 +msgid "Invalid login credentials." +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Controller/Component/UserToolComponent.php:266 +msgid "You are logged out!" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Controller/Component/UserToolComponent.php:269 +msgid "Email verified, you can now login!" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Controller/Component/UserToolComponent.php:270 +msgid "Invalid email token!" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Controller/Component/UserToolComponent.php:273 +msgid "Token verified!" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Controller/Component/UserToolComponent.php:776 +msgid "No token present!" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Model/Behavior/UserBehavior.php:136 +msgid "Invalid field \"%s\"!" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Model/Behavior/UserBehavior.php:373 +msgid "Invalid token." +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Model/Behavior/UserBehavior.php:598 +msgid "Enter your old password." +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Model/Behavior/UserBehavior.php:603 +msgid "Wrong password, please try again." +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Model/Behavior/UserBehavior.php:668;705 +msgid "User not found." +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Model/Behavior/UserBehavior.php:751 +msgid "Invalid user" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Model/Behavior/UserBehavior.php:808 +msgid "Your password reset" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Model/Behavior/UserBehavior.php:825 +msgid "Your new password" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Model/Behavior/UserBehavior.php:842 +msgid "Please verify your Email" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Model/Behavior/UserBehavior.php:878 +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Validation/UsersValidator.php:37 +msgid "An username is required." +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Model/Behavior/UserBehavior.php:882 +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Validation/UsersValidator.php:41 +msgid "The username must be between 3 and 32 characters." +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Model/Behavior/UserBehavior.php:887 +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Validation/UsersValidator.php:46 +msgid "The username is already in use." +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Model/Behavior/UserBehavior.php:891 +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Validation/UsersValidator.php:50 +msgid "The username must be alpha numeric." +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Model/Behavior/UserBehavior.php:911 +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Validation/UsersValidator.php:66 +msgid "An email is required." +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Model/Behavior/UserBehavior.php:918 +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Validation/UsersValidator.php:71 +msgid "The email is already in use." +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Model/Behavior/UserBehavior.php:922 +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Validation/UsersValidator.php:75 +msgid "Must be a valid email address." +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Model/Behavior/UserBehavior.php:942;971 +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Validation/UsersValidator.php:91;116 +msgid "A password is required." +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Model/Behavior/UserBehavior.php:946;975 +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Validation/UsersValidator.php:95;120 +msgid "The password must have at least 6 characters." +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Model/Behavior/UserBehavior.php:950;979 +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Validation/UsersValidator.php:99;124 +msgid "The passwords don't match!" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Shell/UserShell.php:65 +msgid "You need to call this command with at least tow arguments." +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Shell/UserShell.php:47 +msgid "Removed {0,number,integer} expired registration." +msgid_plural "Removed {0,number,integer} expired registrations." +msgstr[0] "" +msgstr[1] "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/Element/login_form.ctp:4 +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/Element/request_password_form.ctp:4 +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/Element/table.ctp:4 +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/UserTools/view.ctp:5 +msgid "Email" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/Element/login_form.ctp:9 +msgid "Password" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/Element/login_form.ctp:15 +msgid "Register" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/Element/login_form.ctp:17 +msgid "Reset Password" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/Element/login_form.ctp:21 +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/UserTools/login.ctp:2 +msgid "Login" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/Element/registration_form.ctp:9 +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/Element/table.ctp:3 +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/UserTools/view.ctp:3 +msgid "Username" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/Element/registration_form.ctp:16 +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/UserTools/register.ctp:2 +msgid "Sign up" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/Element/request_password_form.ctp:7 +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/Element/reset_password_form.ctp:12 +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/UserTools/change_password.ctp:21 +msgid "Submit" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/Element/reset_password_form.ctp:4 +msgid "New Password" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/Element/reset_password_form.ctp:9 +msgid "Repeat Password" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/Element/table.ctp:5 +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/UserTools/view.ctp:9 +msgid "Email Verified" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/Element/table.ctp:6 +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/UserTools/view.ctp:11 +msgid "Created" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/Element/table.ctp:17 +msgid "Yes" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/Element/table.ctp:17 +msgid "No" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/Element/table.ctp:22 +msgid "N/A" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/Email/text/Users/new_password.ctp:1 +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/Email/text/Users/password_reset.ctp:1 +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/Email/text/Users/verification_email.ctp:1 +msgid "Hello {0}!" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/Email/text/Users/new_password.ctp:3 +msgid "Your new password is: %s" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/Email/text/Users/password_reset.ctp:3 +msgid "Please click this link to reset your password." +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/Email/text/Users/verification_email.ctp:3 +msgid "Please click this link to activate your account." +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/UserTools/change_password.ctp:2 +msgid "Change password" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/UserTools/change_password.ctp:8 +msgid "Old password" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/UserTools/change_password.ctp:15 +msgid "New password" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/UserTools/change_password.ctp:19 +msgid "Confirm password" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/UserTools/index.ctp:2 +msgid "Users" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/UserTools/request_password.ctp:2 +msgid "Request a new password" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/UserTools/reset_password.ctp:2 +msgid "Reset your password" +msgstr "" + +#: C:/xampp/htdocs/hema/vendor/burzum/cakephp-user-tools/src/Template/UserTools/view.ctp:7 +msgid "Active" +msgstr "" + diff --git a/src/Model/Behavior/UserBehavior.php b/src/Model/Behavior/UserBehavior.php index 93e0e81..987d7f2 100644 --- a/src/Model/Behavior/UserBehavior.php +++ b/src/Model/Behavior/UserBehavior.php @@ -595,7 +595,7 @@ protected function validationOldPassword($validator) { $validator->provider('userTable', $this->_table); $validator->add('old_password', 'notBlank', [ 'rule' => 'notBlank', - 'message' => __d('userTools', 'Enter your old password.') + 'message' => __d('user_tools', 'Enter your old password.') ]); $validator->add('old_password', 'oldPassword', [ 'rule' => ['validateOldPassword', 'password'], @@ -702,7 +702,7 @@ public function getUser($value, $options = []) { */ protected function _getUser($value, $options = []) { $defaults = [ - 'notFoundErrorMessage' => __d('user_tools', 'User not found'), + 'notFoundErrorMessage' => __d('user_tools', 'User not found.'), 'field' => $this->_table->alias() . '.' . $this->_table->primaryKey() ]; $options = Hash::merge($defaults, $options); @@ -717,6 +717,10 @@ protected function _getUser($value, $options = []) { $query->where([$options['field'] => $value]); } + if (isset($options['queryCallback']) && is_callable($options['queryCallback'])) { + $query = $options['queryCallback']($query, $options); + } + $result = $query->first(); if (empty($result)) { diff --git a/src/Template/UserTools/view.ctp b/src/Template/UserTools/view.ctp index d4613e9..fd6b743 100644 --- a/src/Template/UserTools/view.ctp +++ b/src/Template/UserTools/view.ctp @@ -1,13 +1,13 @@

username) ?>

-
+
username) ?>
-
+
email) ?>
-
+
active) ?>
-
+
email_verified) ?>
-
+
created) ?>
-
\ No newline at end of file +