From efdc09b8ff5afa8806f3782be3b16339ac595e6f Mon Sep 17 00:00:00 2001 From: Andrzej Grzegorz Borkowski Date: Tue, 11 Jan 2011 15:58:39 +0100 Subject: [PATCH 1/2] patches --- .../001_initialize_users_schema.php | 5 +- config/migrations/map.php | 0 config/schema/users.php | 2 +- controllers/details_controller.php | 0 controllers/users_controller.php | 345 ++++++++---------- license.txt | 0 locale/fre/LC_MESSAGES/users.po | 0 locale/users.pot | 0 models/detail.php | 3 + models/user.php | 16 +- readme.md | 3 + .../controllers/details_controller.test.php | 0 .../controllers/users_controller.test.php | 66 +--- tests/cases/models/detail.test.php | 0 tests/cases/models/user.test.php | 0 tests/fixtures/detail_fixture.php | 0 tests/fixtures/user_fixture.php | 0 users_app_controller.php | 18 +- users_app_model.php | 0 views/details/add.ctp | 0 views/details/admin_add.ctp | 0 views/details/admin_edit.ctp | 0 views/details/admin_index.ctp | 0 views/details/admin_view.ctp | 0 views/details/edit.ctp | 0 views/details/index.ctp | 0 views/details/view.ctp | 0 .../email/text/account_verification.ctp | 0 .../email/text/password_reset_request.ctp | 0 views/elements/login.ctp | 0 views/users/add.ctp | 36 +- views/users/admin_add.ctp | 0 views/users/admin_edit.ctp | 0 views/users/admin_index.ctp | 0 views/users/admin_view.ctp | 0 views/users/change_password.ctp | 0 views/users/dashboard.ctp | 17 +- views/users/edit.ctp | 0 views/users/groups.ctp | 0 views/users/index.ctp | 59 --- views/users/login.ctp | 35 +- views/users/register.ctp | 0 views/users/request_password_change.ctp | 0 views/users/reset_password.ctp | 0 views/users/search.ctp | 0 views/users/view.ctp | 0 46 files changed, 269 insertions(+), 336 deletions(-) mode change 100644 => 100755 config/migrations/001_initialize_users_schema.php mode change 100644 => 100755 config/migrations/map.php mode change 100644 => 100755 config/schema/users.php mode change 100644 => 100755 controllers/details_controller.php mode change 100644 => 100755 controllers/users_controller.php mode change 100644 => 100755 license.txt mode change 100644 => 100755 locale/fre/LC_MESSAGES/users.po mode change 100644 => 100755 locale/users.pot mode change 100644 => 100755 models/detail.php mode change 100644 => 100755 models/user.php mode change 100644 => 100755 readme.md mode change 100644 => 100755 tests/cases/controllers/details_controller.test.php mode change 100644 => 100755 tests/cases/controllers/users_controller.test.php mode change 100644 => 100755 tests/cases/models/detail.test.php mode change 100644 => 100755 tests/cases/models/user.test.php mode change 100644 => 100755 tests/fixtures/detail_fixture.php mode change 100644 => 100755 tests/fixtures/user_fixture.php mode change 100644 => 100755 users_app_controller.php mode change 100644 => 100755 users_app_model.php mode change 100644 => 100755 views/details/add.ctp mode change 100644 => 100755 views/details/admin_add.ctp mode change 100644 => 100755 views/details/admin_edit.ctp mode change 100644 => 100755 views/details/admin_index.ctp mode change 100644 => 100755 views/details/admin_view.ctp mode change 100644 => 100755 views/details/edit.ctp mode change 100644 => 100755 views/details/index.ctp mode change 100644 => 100755 views/details/view.ctp mode change 100644 => 100755 views/elements/email/text/account_verification.ctp mode change 100644 => 100755 views/elements/email/text/password_reset_request.ctp mode change 100644 => 100755 views/elements/login.ctp mode change 100644 => 100755 views/users/add.ctp mode change 100644 => 100755 views/users/admin_add.ctp mode change 100644 => 100755 views/users/admin_edit.ctp mode change 100644 => 100755 views/users/admin_index.ctp mode change 100644 => 100755 views/users/admin_view.ctp mode change 100644 => 100755 views/users/change_password.ctp mode change 100644 => 100755 views/users/dashboard.ctp mode change 100644 => 100755 views/users/edit.ctp mode change 100644 => 100755 views/users/groups.ctp delete mode 100644 views/users/index.ctp mode change 100644 => 100755 views/users/login.ctp mode change 100644 => 100755 views/users/register.ctp mode change 100644 => 100755 views/users/request_password_change.ctp mode change 100644 => 100755 views/users/reset_password.ctp mode change 100644 => 100755 views/users/search.ctp mode change 100644 => 100755 views/users/view.ctp diff --git a/config/migrations/001_initialize_users_schema.php b/config/migrations/001_initialize_users_schema.php old mode 100644 new mode 100755 index 9383dcea0..a7e5356e3 --- a/config/migrations/001_initialize_users_schema.php +++ b/config/migrations/001_initialize_users_schema.php @@ -66,8 +66,9 @@ class M49c3417a54874a9d276811502cedc421 extends CakeMigration { 'modified' => array('type'=>'datetime', 'null' => true, 'default' => NULL), 'indexes' => array( 'PRIMARY' => array('column' => 'id', 'unique' => 1), - 'BY_USERNAME' => array('column' => array('username', 'passwd'), 'unique' => 0), - 'BY_EMAIL' => array('column' => array('email', 'passwd'), 'unique' => 0)), + 'BY_USERNAME' => array('column' => array('username'), 'unique' => 0), + 'BY_EMAIL' => array('column' => array('email'), 'unique' => 0) + ), ), ), ), diff --git a/config/migrations/map.php b/config/migrations/map.php old mode 100644 new mode 100755 diff --git a/config/schema/users.php b/config/schema/users.php old mode 100644 new mode 100755 index f5096f6fa..343ba9346 --- a/config/schema/users.php +++ b/config/schema/users.php @@ -55,6 +55,6 @@ function after($event = array()) { 'role' => array('type' => 'string', 'null' => true, 'default' => NULL), 'created' => array('type' => 'datetime', 'null' => true, 'default' => NULL), 'modified' => array('type' => 'datetime', 'null' => true, 'default' => NULL), - 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1), 'BY_USERNAME' => array('column' => array('username', 'passwd'), 'unique' => 0), 'BY_EMAIL' => array('column' => array('email', 'passwd'), 'unique' => 0)) + 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1), 'BY_USERNAME' => array('column' => array('username'), 'unique' => 0), 'BY_EMAIL' => array('column' => array('email'), 'unique' => 0)) ); } diff --git a/controllers/details_controller.php b/controllers/details_controller.php old mode 100644 new mode 100755 diff --git a/controllers/users_controller.php b/controllers/users_controller.php old mode 100644 new mode 100755 index 3ba3e6f08..1f5d86a53 --- a/controllers/users_controller.php +++ b/controllers/users_controller.php @@ -29,14 +29,26 @@ class UsersController extends UsersAppController { * * @var array */ - public $helpers = array('Html', 'Form', 'Session', 'Time', 'Text', 'Utils.Gravatar'); + public $helpers = array( + 'Form', + 'Html', + 'Session', + 'Text', + 'Time', + ); /** * Components * * @var array */ - public $components = array('Auth', 'Session', 'Email', 'Cookie', 'Search.Prg'); + public $components = array( + 'Cookie', + 'Email', + 'Search.Prg', + 'Session', + 'Users.UsersAuth', + ); /** * $presetVars @@ -55,60 +67,31 @@ class UsersController extends UsersAppController { */ public function beforeFilter() { parent::beforeFilter(); - $this->Auth->allow('register', 'reset', 'verify', 'logout', 'index', 'view', 'reset_password'); + $this->Auth->allow('add', 'reset', 'verify', 'logout', 'index', 'view', 'reset_password', 'session', 'login'); - if ($this->action == 'register') { + if ($this->action == 'add') { $this->Auth->enabled = false; } if ($this->action == 'login') { + if ($this->Auth->user()) { + $this->Session->setFlash(__d('users', 'You are already logged in.', true)); + return $this->redirect($this->Auth->loginRedirect); + } $this->Auth->autoRedirect = false; } $this->set('model', $this->modelClass); - - if (!Configure::read('App.defaultEmail')) { - Configure::write('App.defaultEmail', 'noreply@' . env('HTTP_HOST')); - } } /** - * List of all users - * - * @return void + * redirect to dashboard + * @return void */ public function index() { - //$this->User->contain('Detail'); - $searchTerm = ''; - $this->Prg->commonProcess($this->modelClass, $this->modelClass, 'index', false); - - if (!empty($this->params['named']['search'])) { - if (!empty($this->params['named']['search'])) { - $searchTerm = $this->params['named']['search']; - } - $this->data[$this->modelClass]['search'] = $searchTerm; - } - - $this->paginate = array( - 'search', - 'limit' => 12, - 'order' => $this->modelClass . '.username ASC', - 'by' => $searchTerm, - 'conditions' => array( - 'OR' => array( - 'AND' => array( - $this->modelClass . '.active' => 1, - $this->modelClass . '.email_authenticated' => 1)))); - - - $this->set('users', $this->paginate($this->modelClass)); - $this->set('searchTerm', $searchTerm); - - if (!isset($this->params['named']['sort'])) { - $this->params['named']['sort'] = 'username'; - } + $this->redirect(array('action'=>'dashboard')); } - + /** * The homepage of a users giving him an overview about everything * @@ -140,7 +123,7 @@ public function view($slug = null) { * @param string $id User ID * @return void */ - public function edit() { + public function edit($slug) { if (!empty($this->data)) { if ($this->User->Detail->saveSection($this->Auth->user('id'), $this->data, 'User')) { $this->Session->setFlash(__d('users', 'Profile saved.', true)); @@ -148,9 +131,17 @@ public function edit() { $this->Session->setFlash(__d('users', 'Could not save your profile.', true)); } } else { + try { + $this->data = $this->User->view($slug); + if($this->Auth->user('slug') != $this->data[$this->modelClass]['slug']){ + $this->redirect('/'); + } + } catch (Exception $e) { + $this->Session->setFlash($e->getMessage()); + $this->redirect('/'); + } $this->data = $this->User->read(null, $this->Auth->user('id')); } - $this->_setLanguages(); } @@ -180,7 +171,7 @@ public function admin_index() { public function admin_view($id = null) { if (!$id) { $this->Session->setFlash(__d('users', 'Invalid User.', true)); - $this->redirect(array('action'=>'index')); + return $this->redirect(array('action' => 'index')); } $this->set('user', $this->User->read(null, $id)); } @@ -192,7 +183,7 @@ public function admin_view($id = null) { */ public function admin_add() { if ($this->User->add($this->data)) { - $this->Session->setFlash(__d('users', 'The User has been saved', true)); + $this->Session->setFlash(__d('users', 'User saved.', true)); $this->redirect(array('action' => 'index')); } } @@ -207,7 +198,7 @@ public function admin_edit($userId = null) { try { $result = $this->User->edit($userId, $this->data); if ($result === true) { - $this->Session->setFlash(__d('users', 'User saved', true)); + $this->Session->setFlash(__d('users', 'User saved.', true)); $this->redirect(array('action' => 'index')); } else { $this->data = $result; @@ -230,9 +221,9 @@ public function admin_edit($userId = null) { */ public function admin_delete($userId = null) { if ($this->User->delete($userId)) { - $this->Session->setFlash(__d('users', 'User deleted', true)); + $this->Session->setFlash(__d('users', 'User deleted.', true)); } else { - $this->Session->setFlash(__d('users', 'Invalid User', true)); + $this->Session->setFlash(__d('users', 'Invalid User.', true)); } $this->redirect(array('action' => 'index')); @@ -252,10 +243,10 @@ public function admin_search() { * * @return void */ - public function register() { + public function add() { if ($this->Auth->user()) { - $this->Session->setFlash(__d('users', 'You are already registered and logged in!', true)); - $this->redirect('/'); + $this->Session->setFlash(__d('users', 'You are already registered and logged in.', true)); + return $this->redirect($this->Auth->loginRedirect); } if (!empty($this->data)) { @@ -264,7 +255,7 @@ public function register() { $this->set('user', $user); $this->_sendVerificationEmail($user[$this->modelClass]['email']); $this->Session->setFlash(__d('users', 'Your account has been created. You should receive an e-mail shortly to authenticate your account. Once validated you will be able to login.', true)); - $this->redirect(array('action'=> 'login')); + return $this->redirect(array('action' => 'login')); } else { unset($this->data[$this->modelClass]['passwd']); unset($this->data[$this->modelClass]['temppassword']); @@ -273,39 +264,35 @@ public function register() { } $this->_setLanguages(); + + // Render the OpenID form if that data is present + $oid = $this->Session->read('openIdAuthData'); + if ($oid) { + $this->autoRender = false; + $this->set('openIdAuthData', $oid); + $this->render('openid_add'); + } } /** - * Common login action + * Login action + * + * Checks for posted login information, as well as processing cookie + * information if present, to automatically login the user. * * @return void */ public function login() { - if ($this->Auth->user()) { - $this->User->id = $this->Auth->user('id'); - $this->User->saveField('last_login', date('Y-m-d H:i:s')); - + if ($this->Auth->user() || $this->Auth->login()) { if ($this->here == $this->Auth->loginRedirect) { $this->Auth->loginRedirect = '/'; } - - $this->Session->setFlash(sprintf(__d('users', '%s you have successfully logged in', true), $this->Auth->user('username'))); - if (!empty($this->data)) { - $data = $this->data[$this->modelClass]; - $this->_setCookie(); - } - - if (empty($data['return_to'])) { - $data['return_to'] = null; - } - $this->redirect($this->Auth->redirect($data['return_to'])); - } - - if (isset($this->params['named']['return_to'])) { - $this->set('return_to', urldecode($this->params['named']['return_to'])); - } else { - $this->set('return_to', false); + $data = $this->data[$this->modelClass]; + $url = !empty($data['return_to']) ? $data['return_to'] : null; + return $this->redirect($this->Auth->redirect($url)); } + $return_to = isset($this->params['named']['return_to']) ? urldecode($this->params['named']['return_to']) : false; + $this->set('return_to', $return_to); } /** @@ -337,92 +324,81 @@ public function search() { 'by' => $by, 'search' => $searchTerm, 'conditions' => array( - 'AND' => array( - $this->modelClass . '.active' => 1, - $this->modelClass . '.email_authenticated' => 1))); + $this->modelClass . '.active' => 1, + $this->modelClass . '.email_authenticated' => 1)); $this->set('users', $this->paginate($this->modelClass)); $this->set('searchTerm', $searchTerm); } /** - * Common logout action + * Logout action * * @return void */ public function logout() { - $message = sprintf(__d('users', '%s you have successfully logged out', true), $this->Auth->user('username')); + // Message is created first, to grab authentication information before destroying session + $message = sprintf(__d('users', '%s, you have successfully logged out.', true), $this->Auth->user('username')); + $this->Session->destroy(); - $this->Cookie->destroy(); - $this->Session->setFlash($message); $this->redirect($this->Auth->logout()); } /** - * Confirm email action + * Confirm email action and password reset action * * @param string $type Type * @return void */ - public function verify($type = 'email') { - if (isset($this->passedArgs['1'])){ - $token = $this->passedArgs['1']; - } else { - $this->redirect(array('action' => 'login'), null, true); + public function verify($type = 'email', $token = null) { + $verifyTypes = array('email', 'reset'); + if (!$token || !in_array($type, $verifyTypes)) { + $this->Session->setFlash(__d('users', 'The url you have accessed is no longer valid.', true)); } - if ($type === 'email') { - $data = $this->User->validateToken($token); - } elseif($type === 'reset') { - $data = $this->User->validateToken($token, true); - } else { - $this->Session->setFlash(__d('users', 'There url you accessed is not longer valid', true)); - $this->redirect('/'); + $data = $this->User->validateToken($token, $type === 'reset'); + if (!$data) { + $this->Session->setFlash(__d('users', 'The url you have accessed is no longer valid', true)); + return $this->redirect('/'); } - if ($data !== false) { - $email = $data[$this->modelClass]['email']; - unset($data[$this->modelClass]['email']); - - if ($type === 'reset') { - $newPassword = $data[$this->modelClass]['passwd']; - $data[$this->modelClass]['passwd'] = $this->Auth->password($newPassword); - } + $email = $data[$this->modelClass]['email']; + unset($data[$this->modelClass]['email']); - if ($type === 'email') { - $data[$this->modelClass]['active'] = 1; - } + if ($type === 'reset') { + $newPassword = $data[$this->modelClass]['passwd']; + $data[$this->modelClass]['passwd'] = $this->Auth->password($newPassword); + } + if ($type === 'email') { + $data[$this->modelClass]['active'] = 1; + } - if ($this->User->save($data, false)) { - if ($type === 'reset') { - $this->Email->to = $email; - $this->Email->from = Configure::read('App.defaultEmail'); - $this->Email->replyTo = Configure::read('App.defaultEmail'); - $this->Email->return = Configure::read('App.defaultEmail'); - $this->Email->subject = env('HTTP_HOST') . ' ' . __d('users', 'Password Reset', true); - $this->Email->template = null; - $content[] = __d('users', 'Your password has been reset', true); - $content[] = __d('users', 'Please login using this password and change your password', true); - $content[] = $newPassword; - $this->Email->send($content); - $this->Session->setFlash(__d('users', 'Your password was sent to your registered email account', true)); - $this->redirect(array('action' => 'login')); - } else { - unset($data); - $data[$this->modelClass]['active'] = 1; - $this->User->save($data); - $this->Session->setFlash(__d('users', 'Your e-mail has been validated!', true)); - $this->redirect(array('action' => 'login')); - } + if ($this->User->save($data, false)) { + if ($type === 'reset') { + + $content[] = __d('users', 'Your password has been reset.', true); + $content[] = __d('users', 'Please login using this password and change your password.', true); + $content[] = $newPassword; + + $this->__sendEmail($email, array( + 'subject' => env('HTTP_HOST') . ' ' . __d('users', 'Password Reset', true), + 'template' => null, + 'content' => $content + )); + + $this->Session->setFlash(__d('users', 'Your password has been sent to your registered email address.', true)); } else { - $this->Session->setFlash(__d('users', 'There was an error trying to validate your e-mail address. Please check your e-mail for the URL you should use to verify your e-mail address.', true)); - $this->redirect('/'); + unset($data); + $data[$this->modelClass]['active'] = 1; + $this->User->save($data); + $this->Session->setFlash(__d('users', 'Your e-mail has been validated. You may now login.', true)); } - } else { - $this->Session->setFlash(__d('users', 'The url you accessed is not longer valid', true)); - $this->redirect('/'); + return $this->redirect(array('action' => 'login')); } + + $this->Session->setFlash(__d('users', 'There was an error verifying your account. Please check the email you were sent, and retry the verification link.', true)); + $this->redirect('/'); } /** @@ -434,8 +410,8 @@ public function change_password() { if (!empty($this->data)) { $this->data[$this->modelClass]['id'] = $this->Auth->user('id'); if ($this->User->changePassword($this->data)) { - $this->Session->setFlash(__d('users', 'Password changed.', true)); - $this->redirect('/'); + $this->Session->setFlash(__d('users', 'Your password was successfully changed.', true)); + $this->redirect(array('action' => 'dashboard')); } } } @@ -488,19 +464,13 @@ protected function _setLanguages($viewVar = 'languages') { */ protected function _sendVerificationEmail($to = null, $options = array()) { $defaults = array( - 'from' => 'noreply@' . env('HTTP_HOST'), 'subject' => __d('users', 'Account verification', true), - 'template' => 'account_verification'); - + 'template' => 'account_verification' + ); $options = array_merge($defaults, $options); - - $this->Email->to = $to; - $this->Email->from = $options['from']; - $this->Email->subject = $options['subject']; - $this->Email->template = $options['template']; - - return $this->Email->send(); + return $this->__sendEmail($to, $options); } + /** * Checks if the email is in the system and authenticated, if yes create the token @@ -512,10 +482,8 @@ protected function _sendVerificationEmail($to = null, $options = array()) { */ protected function _sendPasswordReset($admin = null, $options = array()) { $defaults = array( - 'from' => 'noreply@' . env('HTTP_HOST'), 'subject' => __d('users', 'Password Reset', true), 'template' => 'password_reset_request'); - $options = array_merge($defaults, $options); if (!empty($this->data)) { @@ -523,60 +491,24 @@ protected function _sendPasswordReset($admin = null, $options = array()) { if (!empty($user)) { $this->set('token', $user[$this->modelClass]['password_token']); - $this->Email->to = $user[$this->modelClass]['email']; - $this->Email->from = $options['from']; - $this->Email->subject = $options['subject']; - $this->Email->template = $options['template']; - - $this->Email->send(); + $this->__sendEmail( $user[$this->modelClass]['email'], $options); if ($admin) { $this->Session->setFlash(sprintf( - __d('users', '%s has been sent an email with instruction to reset their password.', true), + __d('users', '%s has been sent an email with instructions to reset their password.', true), $user[$this->modelClass]['email'])); - $this->redirect(array('action' => 'index', 'admin' => true)); + return $this->redirect(array('action' => 'index', 'admin' => true)); } else { - $this->Session->setFlash(__d('users', 'You should receive an email with further instructions shortly', true)); - $this->redirect(array('action' => 'login')); + $this->Session->setFlash(__d('users', 'Password reset requested. Please check your email for further instructions.', true)); + return $this->redirect(array('action' => 'login')); } } else { $this->Session->setFlash(__d('users', 'No user was found with that email.', true)); - $this->redirect('/'); + return $this->redirect(array('action' => 'reset_password')); } } $this->render('request_password_change'); } -/** - * Sets the cookie to remember the user - * - * @param array Cookie properties - * @return void - * @access protected - * @link http://api13.cakephp.org/class/cookie-component - */ - protected function _setCookie($options = array()) { - if (!isset($this->data[$this->modelClass]['remember_me'])) { - $this->Cookie->delete($this->modelClass); - } else { - $validProperties = array('domain', 'key', 'name', 'path', 'secure', 'time'); - $defaults = array( - 'name' => 'rememberMe'); - - $options = array_merge($defaults, $options); - foreach ($options as $key => $value) { - if (in_array($key, $validProperties)) { - $this->Cookie->{$key} = $value; - } - } - - $cookie = array(); - $cookie[$this->Auth->fields['username']] = $this->data[$this->modelClass][$this->Auth->fields['username']]; - $cookie[$this->Auth->fields['password']] = $this->data[$this->modelClass][$this->Auth->fields['password']]; - $this->Cookie->write($this->modelClass, $cookie, true, '1 Month'); - } - unset($this->data[$this->modelClass]['remember_me']); - } - /** * This method allows the user to change his password if the reset token is correct * @@ -586,7 +518,7 @@ protected function _setCookie($options = array()) { private function __resetPassword($token) { $user = $this->User->checkPasswordToken($token); if (empty($user)) { - $this->Session->setFlash(__d('users', 'Invalid password reset token, try again.', true)); + $this->Session->setFlash(__d('users', 'Invalid password reset token, please try again.', true)); $this->redirect(array('action' => 'reset_password')); } @@ -599,4 +531,41 @@ private function __resetPassword($token) { $this->set('token', $token); } + + private function __sendEmail($to, $options = array()){ + if(empty($to)) + trigger_error(__d('users','Invalid email address, please set email and try again.')); + $defaultOptions = array( + 'sendAs' => 'both', + 'subject' => 'system email', + 'template' => null, + 'layout' => null, + 'delivery' => 'smtp', + 'data' => null, + 'content' => null + ); + $options = array_merge($defaultOptions,$options); + Configure::load ( 'smtp' ); + $this->Email->reset (); + $this->Email->to = $to; + $this->Email->sendAs = $options['sendAs']; // because we like to send pretty mail + $this->Email->smtpOptions = Configure::read ( 'Email' ); + $this->Email->replyTo = Configure::read ( 'Email.fromHeader' ); + $this->Email->from = Configure::read ( 'Email.fromHeader' ); + $this->Email->return = Configure::read ( 'Email.from' ); + //regular email sending + $this->Email->subject = $options['subject']; + $this->Email->delivery = $options['delivery']; + //Set the body of the mail as we send it. + //Note: the text can be an array, each element will appear as a + //seperate line in the message body. + return $this->Email->send ($options['content'],$options['template'],$options['layout']); + } + + public function kill_session() { + if (Configure::read('debug')) { + $this->Session->destroy(); + return $this->redirect('/'); + } + } } diff --git a/license.txt b/license.txt old mode 100644 new mode 100755 diff --git a/locale/fre/LC_MESSAGES/users.po b/locale/fre/LC_MESSAGES/users.po old mode 100644 new mode 100755 diff --git a/locale/users.pot b/locale/users.pot old mode 100644 new mode 100755 diff --git a/models/detail.php b/models/detail.php old mode 100644 new mode 100755 index ec269006d..d0a3273cf --- a/models/detail.php +++ b/models/detail.php @@ -217,6 +217,9 @@ public function saveSection($userId = null, $data = null, $section = null) { $newDetail[$model]['field'] = $field; $newDetail[$model]['value'] = $value; + $newDetail[$model]['input'] = ''; + $newDetail[$model]['data_type'] = ''; + $newDetail[$model]['label'] = ''; $this->save($newDetail, false); } } elseif (isset($this->{$model})) { diff --git a/models/user.php b/models/user.php old mode 100644 new mode 100755 index 49d19a89b..fd7c626a9 --- a/models/user.php +++ b/models/user.php @@ -75,6 +75,13 @@ class User extends UsersAppModel { */ public $validate = array(); +/** + * Detail model + * + * @var Detail + */ + public $Detail = null; + /** * Constructor * @@ -406,16 +413,13 @@ public function view($slug = null) { 'Detail'), 'conditions' => array( $this->alias . '.slug' => $slug, - 'OR' => array( - 'AND' => - array($this->alias . '.active' => 1, $this->alias . '.email_authenticated' => 1), - //array($this->alias . '.active' => 1, $this->alias . '.account_type' => 'remote') - )))); + $this->alias . '.active' => 1, + $this->alias . '.email_authenticated' => 1), + )); if (empty($user)) { throw new Exception(__d('users', 'The user does not exist.', true)); } - return $user; } diff --git a/readme.md b/readme.md old mode 100644 new mode 100755 index 1e8752bc7..0ae7aba9e --- a/readme.md +++ b/readme.md @@ -15,6 +15,9 @@ or cake migration all -plugin users You will also need the [CakeDC Search plugin](http://github.com/CakeDC/search), just grab it and put it into your application's plugin folder. +and the [CakeDC Utils plugin](http://github.com/CakeDC/utils) + +just grab them and put it into your application's plugin folder. ## How to use it ## diff --git a/tests/cases/controllers/details_controller.test.php b/tests/cases/controllers/details_controller.test.php old mode 100644 new mode 100755 diff --git a/tests/cases/controllers/users_controller.test.php b/tests/cases/controllers/users_controller.test.php old mode 100644 new mode 100755 index a6fe795f2..9b6722b5e --- a/tests/cases/controllers/users_controller.test.php +++ b/tests/cases/controllers/users_controller.test.php @@ -33,28 +33,6 @@ class TestUsersController extends UsersController { */ public $uses = array('Users.User'); -/** - * beforeFilter Callback - * - * @return void - */ - public function beforeFilter() { - parent::beforeFilter(); - $this->Auth->authorize = 'controller'; - $this->Auth->fields = array('username' => 'email', 'password' => 'passwd'); - $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'prefix' => 'admin', 'admin' => false, 'plugin' => 'users'); - $this->Auth->loginRedirect = $this->Session->read('Auth.redirect'); - $this->Auth->logoutRedirect = '/'; - $this->Auth->authError = __d('users', 'Sorry, but you need to login to access this location.', true); - $this->Auth->loginError = __d('users', 'Invalid e-mail / password combination. Please try again', true); - $this->Auth->autoRedirect = true; - $this->Auth->userModel = 'User'; - $this->Auth->userScope = array( - 'OR' => array( - 'AND' => - array('User.active' => 1, 'User.email_authenticated' => 1))); - } - /** * Public interface to _setCookie */ @@ -179,21 +157,21 @@ public function testUserLogin() { $this->Users->Component->startup($this->Users); $this->Users->login(); - $this->assertEqual($this->Users->Session->read('Message.flash.message'), __d('users', 'testuser you have successfully logged in', true)); + $this->assertEqual($this->Users->Session->read('Message.flash.message'), __d('users', 'testuser, you have successfully logged in.', true)); $this->assertEqual(Router::normalize($this->Users->redirectUrl), Router::normalize(Router::url($this->Users->Auth->loginRedirect))); $this->__setPost(array('User' => $this->usersData['invalidUser'])); $this->Users->beforeFilter(); $this->Users->login(); - $this->assertEqual($this->Users->Session->read('Message.auth.message'), __d('users', 'Invalid e-mail / password combination. Please try again', true)); + $this->assertEqual($this->Users->Session->read('Message.auth.message'), __d('users', 'Invalid e-mail / password combination. Please try again', true)); } /** * Test user registration * */ - public function testRegister() { - $this->Users->params['action'] = 'register'; + public function testAdd() { + $this->Users->params['action'] = 'add'; $this->__setPost(array( 'User' => array( @@ -203,7 +181,7 @@ public function testRegister() { 'temppassword' => 'password', 'tos' => 1))); $this->Users->beforeFilter(); - $this->Users->register(); + $this->Users->add(); $this->assertEqual($this->Users->Session->read('Message.flash.message'), __d('users', 'Your account has been created. You should receive an e-mail shortly to authenticate your account. Once validated you will be able to login.', true)); $this->__setPost(array( @@ -214,7 +192,7 @@ public function testRegister() { 'temppassword' => '', 'tos' => 0))); $this->Users->beforeFilter(); - $this->Users->register(); + $this->Users->add(); $this->assertEqual($this->Users->Session->read('Message.flash.message'), __d('users', 'Your account could not be created. Please, try again.', true)); } @@ -224,16 +202,14 @@ public function testRegister() { */ public function testVerify() { $this->Users->beforeFilter(); - $this->Users->passedArgs[1] = 'testtoken2'; $this->Users->User->id = '37ea303a-3bdc-4251-b315-1316c0b300fa'; $this->Users->User->saveField('email_token_expires', date('Y-m-d H:i:s', strtotime('+1 year'))); - $this->Users->verify($type = 'email'); - $this->assertEqual($this->Users->Session->read('Message.flash.message'), __d('users', 'Your e-mail has been validated!', true)); + $this->Users->verify('email', 'testtoken2'); + $this->assertEqual($this->Users->Session->read('Message.flash.message'), __d('users', 'Your e-mail has been validated. You may now login.', true)); $this->Users->beforeFilter(); - $this->Users->passedArgs[1] = 'invalid-token'; - $this->Users->verify($type = 'email'); - $this->assertEqual($this->Users->Session->read('Message.flash.message'), __d('users', 'The url you accessed is not longer valid', true)); + $this->Users->verify('email', 'invalid-token'); + $this->assertEqual($this->Users->Session->read('Message.flash.message'), __d('users', 'The url you have accessed is no longer valid', true)); } /** @@ -245,7 +221,7 @@ public function testLogout() { $this->Users->beforeFilter(); $this->Users->Session->write('Auth.User', $this->usersData['validUser']); $this->Users->logout(); - $this->assertEqual($this->Users->Session->read('Message.flash.message'), __d('users', 'floriank you have successfully logged out', true)); + $this->assertEqual($this->Users->Session->read('Message.flash.message'), __d('users', 'floriank, you have successfully logged out.', true)); $this->assertEqual($this->Users->redirectUrl, '/'); } @@ -381,31 +357,13 @@ public function testAdminDelete() { $this->assertEqual($this->Users->redirectUrl, array('action' => 'index')); } -/** - * Test setting the cookie - * - */ - public function testSetCookie() { - $this->Users->data['User'] = array( - 'remember_me' => 1, - 'username' => 'test', - 'password' => 'testtest'); - $this->Users->setCookie(array( - 'name' => 'userTestCookie')); - $this->Users->Cookie->name = 'userTestCookie'; - $result = $this->Users->Cookie->read('User'); - $this->assertEqual($result, array( - 'username' => 'test', - 'password' => 'testtest')); - } - /** * Test * */ private function __setPost($data = array()) { $_SERVER['REQUEST_METHOD'] = 'POST'; - $this->Users->data = am($data, array('_method' => 'POST')); + $this->Users->data = array_merge($data, array('_method' => 'POST')); } /** diff --git a/tests/cases/models/detail.test.php b/tests/cases/models/detail.test.php old mode 100644 new mode 100755 diff --git a/tests/cases/models/user.test.php b/tests/cases/models/user.test.php old mode 100644 new mode 100755 diff --git a/tests/fixtures/detail_fixture.php b/tests/fixtures/detail_fixture.php old mode 100644 new mode 100755 diff --git a/tests/fixtures/user_fixture.php b/tests/fixtures/user_fixture.php old mode 100644 new mode 100755 diff --git a/users_app_controller.php b/users_app_controller.php old mode 100644 new mode 100755 index 92b5400ae..7209b0ac2 --- a/users_app_controller.php +++ b/users_app_controller.php @@ -13,7 +13,23 @@ * Users App Controller * * @package users - * @subpackage users.controllers */ class UsersAppController extends AppController { + +/** + * Determine if the user is authorized to view the requested action + * + * Inspect the URL and return true if the user is authorized + * + * @return boolean Authorized to view action + */ + public function isAuthorized() { + $authorized = true; + + // Restrict "admin" prefix routes to users with the role "admin". + if (isset($this->params['prefix']) && $this->params['prefix'] == 'admin') { + $authorized = $this->Auth->user('role') === 'admin'; + } + return $authorized; + } } diff --git a/users_app_model.php b/users_app_model.php old mode 100644 new mode 100755 diff --git a/views/details/add.ctp b/views/details/add.ctp old mode 100644 new mode 100755 diff --git a/views/details/admin_add.ctp b/views/details/admin_add.ctp old mode 100644 new mode 100755 diff --git a/views/details/admin_edit.ctp b/views/details/admin_edit.ctp old mode 100644 new mode 100755 diff --git a/views/details/admin_index.ctp b/views/details/admin_index.ctp old mode 100644 new mode 100755 diff --git a/views/details/admin_view.ctp b/views/details/admin_view.ctp old mode 100644 new mode 100755 diff --git a/views/details/edit.ctp b/views/details/edit.ctp old mode 100644 new mode 100755 diff --git a/views/details/index.ctp b/views/details/index.ctp old mode 100644 new mode 100755 diff --git a/views/details/view.ctp b/views/details/view.ctp old mode 100644 new mode 100755 diff --git a/views/elements/email/text/account_verification.ctp b/views/elements/email/text/account_verification.ctp old mode 100644 new mode 100755 diff --git a/views/elements/email/text/password_reset_request.ctp b/views/elements/email/text/password_reset_request.ctp old mode 100644 new mode 100755 diff --git a/views/elements/login.ctp b/views/elements/login.ctp old mode 100644 new mode 100755 diff --git a/views/users/add.ctp b/views/users/add.ctp old mode 100644 new mode 100755 index 12a2f6e4f..a75e70d84 --- a/views/users/add.ctp +++ b/views/users/add.ctp @@ -10,16 +10,38 @@ */ ?>
-Form->create($model);?>
- Form->input('username'); - echo $this->Form->input('passwd'); - echo $this->Form->input('password_token'); - ?> + Form->create($model); + echo $this->Form->input('username', array( + 'error' => array( + 'unique_username' => __d('users', 'Please select a username that is not already in use', true), + 'username_min' => __d('users', 'Must be at least 3 characters', true), + 'alpha' => __d('users', 'Username must contain numbers and letters only', true), + 'required' => __d('users', 'Please choose username', true)))); + echo $this->Form->input('email', array( + 'label' => __d('users', 'E-mail (used as login)',true), + 'error' => array('isValid' => __d('users', 'Must be a valid email address', true), + 'isUnique' => __d('users', 'An account with that email already exists', true)))); + echo $this->Form->input('passwd', array( + 'label' => __d('users', 'Password',true), + 'type' => 'password', + 'error' => __d('users', 'Must be at least 5 characters long', true))); + echo $this->Form->input('temppassword', array( + 'label' => __d('users', 'Password (confirm)', true), + 'type' => 'password', + 'error' => __d('users', 'Passwords must match', true) + ) + ); + echo $this->Form->input('tos', array( + 'label' => __d('users', 'I have read and agreed to ', true) . $this->Html->link(__d('users', 'Terms of Service', true), array('controller' => 'pages', 'action' => 'tos')), + 'error' => __d('users', 'You must verify you have read the Terms of Service', true) + ) + ); + echo $this->Form->end(__d('users', 'Submit',true)); + ?>
-Form->end('Submit');?>