From 9ccf69f35a57ddca5cfd0f2e35d196d71c3da1af Mon Sep 17 00:00:00 2001 From: iglocska Date: Wed, 8 Apr 2015 13:10:52 +0200 Subject: [PATCH] User edit fixed - choose organisation from a list as expected - fixed refreshauth --- app/Controller/AppController.php | 11 --------- app/Controller/UsersController.php | 39 ++++++++++++++++++------------ app/View/Users/admin_edit.ctp | 5 +++- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/app/Controller/AppController.php b/app/Controller/AppController.php index b9674b3ca..f5b494dd3 100755 --- a/app/Controller/AppController.php +++ b/app/Controller/AppController.php @@ -237,17 +237,6 @@ class AppController extends Controller { return $this->Auth->user('org'); } -/** - * Refreshes the Auth session with new/updated data - * @return void - */ - protected function _refreshAuth() { - $this->loadModel('User'); - $this->User->recursive = -1; - $user = $this->User->findById($this->Auth->user('id')); - $this->Auth->login($user['User']); - } - /** * * @param $action diff --git a/app/Controller/UsersController.php b/app/Controller/UsersController.php index 5c3a40da1..b2f47463e 100755 --- a/app/Controller/UsersController.php +++ b/app/Controller/UsersController.php @@ -365,16 +365,11 @@ class UsersController extends AppController { $this->Session->setFlash(__('The user could not be saved. Please, try again.')); } } else { - // generate auth key for a new user - $temp = $this->User->Organisation->find('all', array( - 'fields' => array('name', 'id'), - 'recursive' => -1, + $orgs = $this->User->Organisation->find('list', array( + 'conditions' => array('local' => 1), )); - $orgs = array(); - foreach ($temp as $org) { - $orgs[$org['Organisation']['id']] = $org['Organisation']['name']; - } $this->set('orgs', $orgs); + // generate auth key for a new user $this->newkey = $this->User->generateAuthKey(); $this->set('authkey', $this->newkey); } @@ -419,15 +414,8 @@ class UsersController extends AppController { 'id' => $allowedRole, ) )); - $paramsOrgs = array('conditions' => array( - 'id' => $this->Auth->user('organisation_id'), - 'fields' => array('Organisation.id', 'Organisation.name'), - )); } - $roles = $this->User->Role->find('list', $params); - $organisations = $this->User->Organisation->find('list', $paramsOrgs); - debug($organisations); $this->set('currentId', $id); if ($this->request->is('post') || $this->request->is('put')) { $fields = array(); @@ -492,13 +480,18 @@ class UsersController extends AppController { $this->Session->setFlash(__('The user could not be saved. Please, try again.')); } } else { - $this->User->recursive = 0; $this->User->read(null, $id); if (!$this->_isSiteAdmin() && $this->Auth->user('organisation_id') != $this->User->data['User']['organisation_id']) $this->redirect(array('controller' => 'users', 'action' => 'index', 'admin' => true)); $this->User->set('password', ''); $this->request->data = $this->User->data; // TODO CHECK } + if ($this->_isSiteAdmin()) { + $orgs = $this->User->Organisation->find('list', array( + 'conditions' => array('local' => 1), + )); + } + $this->set('orgs', $orgs); $this->set('id', $id); $this->set(compact('roles')); } @@ -1047,4 +1040,18 @@ class UsersController extends AppController { $user_results = $this->User->verifyGPG(); $this->set('users', $user_results); } + + /** + * Refreshes the Auth session with new/updated data + * @return void + */ + protected function _refreshAuth() { + $oldUser = $this->Auth->user(); + $newUser = $this->User->find('first', array('conditions' => array('User.id' => $oldUser['id']), 'recursive' => -1,'contain' => array('Organisation', 'Role'))); + // Rearrange it a bit to match the Auth object created during the login + $newUser['User']['Role'] = $newUser['Role']; + $newUser['User']['Organisation'] = $newUser['Organisation']; + unset($newUser['Organisation'], $newUser['Role']); + $this->Auth->login($newUser['User']); + } } diff --git a/app/View/Users/admin_edit.ctp b/app/View/Users/admin_edit.ctp index 85703d9d7..90c50e930 100755 --- a/app/View/Users/admin_edit.ctp +++ b/app/View/Users/admin_edit.ctp @@ -7,7 +7,10 @@ echo $this->Form->input('password'); echo $this->Form->input('confirm_password', array('type' => 'password', 'div' => array('class' => 'input password required'))); if ($isSiteAdmin) { - echo $this->Form->input('org', array('label' => 'Organisation')); + echo $this->Form->input('organisation_id', array( + 'options' => $orgs, + 'label' => 'Organisation', + )); } echo $this->Form->input('role_id', array('label' => 'Role', 'div' => 'input clear')); // TODO ACL, User edit role_id. echo $this->Form->input('authkey', array('disabled' => 'disabled', 'label' => 'Authentication key'));