From 608949339240d9e12b87de299c95a9ed4312c8dd Mon Sep 17 00:00:00 2001 From: Ale Vilar Date: Thu, 11 Sep 2014 12:31:06 -0300 Subject: [PATCH 001/134] composer ristorantino --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 994be689f..3e78ac5d1 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "cakedc/users", + "name": "ristorantino/users", "description": "Users Plugin for CakePHP", "type": "cakephp-plugin", "keywords": ["cakephp","users","auth"], From 4d3c36ff1a4fae268781d0946baeca8c5fc12686 Mon Sep 17 00:00:00 2001 From: Ale Vilar Date: Sat, 13 Sep 2014 02:21:12 -0300 Subject: [PATCH 002/134] usuarios adaptados al ristorantino --- Config/Schema/schema.php | 26 +++-- Controller/RolesController.php | 97 ++++++++++++++++++ Controller/UsersController.php | 174 +++++++++++++++++++++++++++++---- Model/Rol.php | 81 +++++++++++++++ Model/SocialProfile.php | 12 +++ Model/User.php | 59 ++++++++++- View/Elements/boxlogin.ctp | 44 +++++++++ View/Roles/edit.ctp | 22 +++++ View/Roles/index.ctp | 50 ++++++++++ View/Roles/view.ctp | 92 +++++++++++++++++ View/Users/edit.ctp | 4 +- View/Users/login.ctp | 53 ++++++---- 12 files changed, 659 insertions(+), 55 deletions(-) create mode 100755 Controller/RolesController.php create mode 100755 Model/Rol.php create mode 100644 Model/SocialProfile.php create mode 100644 View/Elements/boxlogin.ctp create mode 100755 View/Roles/edit.ctp create mode 100755 View/Roles/index.ctp create mode 100755 View/Roles/view.ctp diff --git a/Config/Schema/schema.php b/Config/Schema/schema.php index 827c14344..ca9dd8581 100644 --- a/Config/Schema/schema.php +++ b/Config/Schema/schema.php @@ -25,19 +25,25 @@ public function before($event = array()) { public function after($event = array()) { } - public $user_details = array( + public $social_profiles = array( 'id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36, 'key' => 'primary'), - 'user_id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36), - 'position' => array('type' => 'float', 'null' => false, 'default' => '1'), - 'field' => array('type' => 'string', 'null' => false, 'default' => null, 'key' => 'index'), - 'value' => array('type' => 'text', 'null' => true, 'default' => null), - 'input' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 16), - 'data_type' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 16), - 'label' => array('type' => 'string', 'null' => false, 'length' => 128), + 'user_id' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 36), + 'oid' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 136), + 'email' => array('type' => 'string', 'null' => false, 'default' => null, 'key' => 'index'), + 'family_name' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 128), + 'given_name' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 128), + 'gender' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 128), + 'locale' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 128), + 'birthday' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 128), + 'picture' => array('type' => 'string', 'null' => false, 'length' => 128), + 'provider' => array('type' => 'string', 'null' => false, 'length' => 128), + 'raw' => array('type' => 'text', '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), - 'UNIQUE_PROFILE_PROPERTY' => array('column' => array('field', 'user_id'), 'unique' => 1) + 'indexes' => array( + 'PRIMARY' => array('column' => 'id', 'unique' => 1), + 'BY_USERNAME' => array('column' => array('user_id'), 'unique' => 0), + 'BY_OID' => array('column' => array('oid'), 'unique' => 0) ) ); diff --git a/Controller/RolesController.php b/Controller/RolesController.php new file mode 100755 index 000000000..4e2b24a28 --- /dev/null +++ b/Controller/RolesController.php @@ -0,0 +1,97 @@ +Rol->recursive = 0; + $this->set('roles', $this->paginate()); + } + +/** + * view method + * + * @param string $id + * @return void + */ + public function view($id = null) { + $this->Rol->id = $id; + if (!$this->Rol->exists()) { + throw new NotFoundException(__('Invalid rol')); + } + $this->set('rol', $this->Rol->read(null, $id)); + } + +/** + * add method + * + * @return void + */ + public function add() { + if ($this->request->is('post')) { + $this->Rol->create(); + if ($this->Rol->save($this->request->data)) { + $this->Session->setFlash(__('The rol has been saved'),'Risto.flash_success'); + $this->redirect(array('action' => 'index')); + } else { + $this->Session->setFlash(__('The rol could not be saved. Please, try again.'),'Risto.flash_error'); + } + } + $this->render('edit'); + } + +/** + * edit method + * + * @param string $id + * @return void + */ + public function edit($id = null) { + $this->Rol->id = $id; + if (!$this->Rol->exists()) { + throw new NotFoundException(__('Invalid rol')); + } + if ($this->request->is('post') || $this->request->is('put')) { + if ($this->Rol->save($this->request->data)) { + $this->Session->setFlash(__('The rol has been saved'),'Risto.flash_success'); + $this->redirect(array('action' => 'index')); + } else { + $this->Session->setFlash(__('The rol could not be saved. Please, try again.'),'Risto.flash_error'); + } + } else { + $this->request->data = $this->Rol->read(null, $id); + } + } + +/** + * delete method + * + * @param string $id + * @return void + */ + public function delete($id = null) { + if (!$this->request->is('post')) { + throw new MethodNotAllowedException(); + } + $this->Rol->id = $id; + if (!$this->Rol->exists()) { + throw new NotFoundException(__('Invalid rol')); + } + if ($this->Rol->delete()) { + $this->Session->setFlash(__('Rol deleted'),'Risto.flash_success'); + $this->redirect(array('action' => 'index')); + } + $this->Session->setFlash(__('Rol was not deleted'),'Risto.flash_error'); + $this->redirect(array('action' => 'index')); + } +} diff --git a/Controller/UsersController.php b/Controller/UsersController.php index 74f1df63d..e78a4d904 100644 --- a/Controller/UsersController.php +++ b/Controller/UsersController.php @@ -41,18 +41,6 @@ class UsersController extends UsersAppController { */ public $plugin = null; -/** - * Helpers - * - * @var array - */ - public $helpers = array( - 'Html', - 'Form', - 'Session', - 'Time', - 'Text' - ); /** * Components @@ -60,7 +48,7 @@ class UsersController extends UsersAppController { * @var array */ public $components = array( - 'Auth', + //'Auth', 'Session', 'Cookie', 'Paginator', @@ -220,7 +208,7 @@ protected function _setupAuth() { return; } - $this->Auth->allow('add', 'reset', 'verify', 'logout', 'view', 'reset_password', 'login', 'resend_verification'); + $this->Auth->allow('add', 'reset', 'verify', 'logout', 'view', 'reset_password', 'login', 'resend_verification', 'auth_login', 'auth_callback'); if (!is_null(Configure::read('Users.allowRegistration')) && !Configure::read('Users.allowRegistration')) { $this->Auth->deny('add'); @@ -232,6 +220,7 @@ protected function _setupAuth() { $this->Auth->authenticate = array( 'Form' => array( + 'recursive' => 1, 'fields' => array( 'username' => 'email', 'password' => 'password'), @@ -242,10 +231,6 @@ protected function _setupAuth() { ) ) ); - - $this->Auth->loginRedirect = '/'; - $this->Auth->logoutRedirect = array('plugin' => Inflector::underscore($this->plugin), 'controller' => 'users', 'action' => 'login'); - $this->Auth->loginAction = array('admin' => false, 'plugin' => Inflector::underscore($this->plugin), 'controller' => 'users', 'action' => 'login'); } /** @@ -293,6 +278,21 @@ public function view($slug = null) { * @return void */ public function edit() { + $user = $this->Auth->user(); + if ( empty($user) ) { + throw new ForbiddenException(__("You must be logged in")); + } + if ( $this->request->is('post') ) { + if ($this->User->save( $this->request->data) ) { + $this->Session->setFlash(__('Se ha guardado la información correctamente')); + $this->User->recursive = 1; + $user = $this->User->read(null, $id); + $this->Session->write('Auth.User', $user); + } else { + $this->Session->setFlash(__('El usuario no pudo ser guardado. Por favor, intente nuevamente.')); + } + } + $this->request->data = $user; } /** @@ -504,6 +504,7 @@ public function login() { $this->redirect($this->Auth->redirect($data[$this->modelClass]['return_to'])); } } else { + $this->Auth->flash['element'] = 'Risto.flash_error'; $this->Auth->flash(__d('users', 'Invalid e-mail / password combination. Please try again')); } } @@ -877,4 +878,141 @@ public function isAuthorized($user = null) { return parent::isAuthorized($user); } + + + + /** + * + * ------------------------------------------------------------------------------------------- + * + * OAUTH + */ + + public function auth_login($provider) { + $result = $this->ExtAuth->login($provider); + if ($result['success']) { + $Event = new CakeEvent( + 'Users.Controller.Users.afterLogin', + $this, + array( + 'data' => $this->request->data, + 'isFirstLogin' => !$this->Auth->user('last_login') + ) + ); + + $this->getEventManager()->dispatch($Event); + + $this->Auth->loginRedirect = $result['redirectURL']; + $this->redirect( $this->Auth->redirectUrl() ); + + } else { + $this->Session->setFlash($result['message']); + $this->redirect($this->Auth->redirectUrl()); + } + } + + public function auth_callback($provider) { + $result = $this->ExtAuth->loginCallback($provider); + if ($result['success']) { + $this->__successfulExtAuth($result['profile'], $result['accessToken']); + + } else { + $this->Session->setFlash($result['message']); + $this->redirect($this->Auth->redirectUrl()); + } + } + + + private function __successfulExtAuth($incomingProfile, $accessToken) { + + // search for profile + $this->User->SocialProfile->recursive = -1; + $existingProfile = $this->User->SocialProfile->find('first', array( + 'conditions' => array('oid' => $incomingProfile['oid']) + )); + + if ($existingProfile) { + + // Existing profile? log the associated user in. + $user = $this->User->find('first', array( + 'conditions' => array('id' => $existingProfile['SocialProfile']['user_id']) + )); + + $this->__doAuthLogin($user); + } else { + // verificar que no se haya registrado con otra credencial + $existingUser = $this->User->find('first', array( + 'conditions' => array('email' => $incomingProfile['email']) + )); + if ( $existingUser ) { + // User exists but never (logged using oauth) saved UserProfile => save UserProfile + $incomingProfile['user_id'] = $existingUser['User']['id']; + $incomingProfile['last_login'] = date('Y-m-d h:i:s'); + $incomingProfile['access_token'] = serialize($accessToken); + $this->User->SocialProfile->save($incomingProfile); + $this->Session->setFlash(__('Your %s account has been linked to user %s.', $incomingProfile['provider'], $existingUser['User']['username'])); + + // log in + $this->__doAuthLogin($existingUser); + + $this->redirect($this->Auth->redirectUrl()); + } + + + + // New profile. + if ($this->Auth->loggedIn()) { + + // user logged in already, attach profile to logged in user. + // create social profile linked to current user + $incomingProfile['user_id'] = $this->Auth->user('id'); + $this->User->SocialProfile->save($incomingProfile); + $this->Session->setFlash('Your ' . $incomingProfile['provider'] . ' account has been linked.'); + $this->redirect($this->Auth->redirectUrl()); + + } else { + + // no-one logged in, must be a registration. + unset($incomingProfile['id']); + $user = $this->User->register(array('User' => $incomingProfile), array('emailVerification'=>false)); + if (!$user) { + debug($this->User->validationErrors); + throw new CakeException(__d('users', 'Error registering users')); + } + // create social profile linked to new user + $incomingProfile['user_id'] = $user['User']['id']; + $incomingProfile['last_login'] = date('Y-m-d h:i:s'); + $incomingProfile['access_token'] = serialize($accessToken); + $this->User->SocialProfile->save($incomingProfile); + + // log in + $this->__doAuthLogin($user); + } + } + } + + private function __doAuthLogin($user) { + if ($this->Auth->login($user['User'])) { + $user['last_login'] = date('Y-m-d H:i:s'); + $this->User->save(array('User' => $user)); + + $this->Session->setFlash(sprintf(__d('users', '%s you have successfully logged in'), $this->Auth->user('username'))); + + $Event = new CakeEvent( + 'Users.Controller.Users.afterLogin', + $this, + array( + 'data' => $this->request->data, + 'isFirstLogin' => !$this->Auth->user('last_login') + ) + ); + + $this->getEventManager()->dispatch($Event); + + $this->redirect($this->Auth->redirectUrl()); + } + } + + + } diff --git a/Model/Rol.php b/Model/Rol.php new file mode 100755 index 000000000..5f2b1ff47 --- /dev/null +++ b/Model/Rol.php @@ -0,0 +1,81 @@ + array('type' => 'requester')); + + +/** + * Validation rules + * + * @var array + */ + public $validate = array( + 'name' => array( + 'notempty' => array( + 'rule' => array('notempty'), + //'message' => 'Your custom message here', + //'allowEmpty' => false, + //'required' => false, + //'last' => false, // Stop validation after this rule + //'on' => 'create', // Limit validation to 'create' or 'update' operations + ), + ), + ); + + //The Associations below have been created with all possible keys, those that are not needed can be removed + +/** + * hasMany associations + * + * @var array + */ + public $hasAndBelongsToMany = array( + 'User' => array( + 'className' => 'Users.User', + 'foreignKey' => 'rol_id', + 'dependent' => false, + 'conditions' => '', + 'fields' => '', + 'order' => '', + 'limit' => '', + 'offset' => '', + 'exclusive' => '', + 'finderQuery' => '', + 'counterQuery' => '' + ) + ); + + public function beforeSave($options = array()) { + $this->data['Rol']['machin_name'] = strtolower( Inflector::slug( $this->data['Rol']['name'])) ; + return true; + } + + public function afterSave($created, $options = Array()) + { + // colocar el nombre del rol como alias den Aro ACL + if ( $this->Aro->saveField('alias', $this->data['Rol']['machin_name']) ) { + return parent::afterSave($created); + } else { + return false; + } + } + + public function parentNode(){ + return; + } + + +} diff --git a/Model/SocialProfile.php b/Model/SocialProfile.php new file mode 100644 index 000000000..d3354584d --- /dev/null +++ b/Model/SocialProfile.php @@ -0,0 +1,12 @@ + array('type' => 'like'), - 'email' => array('type' => 'value') + 'email' => array('type' => 'value'), + 'txt_buscar' => array( + 'type' => 'query', + 'method' => '__searchTextGeneric' + ), + 'site_alias' => array( + 'type' => 'query', + 'method' => '__searchFromSite', + 'field' => 'User.id', + ), + ); + + +/** + * hasMany associations + * + * @var array + */ + public $hasMany = array( + 'SocialProfile' => array( + 'className' => 'Users.SocialProfile', + 'foreignKey' => 'user_id', + 'unique' => 'keepExisting', + 'dependent' => true, + ) + ); + + +/** + * hasAndBelongsToMany associations + * + * @var array + */ + public $hasAndBelongsToMany = array( + 'Site' => array( + 'className' => 'MtSites.Site', + 'joinTable' => 'sites_users', + 'foreignKey' => 'user_id', + 'associationForeignKey' => 'site_id', + 'unique' => 'keepExisting', + 'conditions' => '', + 'fields' => '', + 'order' => '', + 'limit' => '', + 'offset' => '', + 'finderQuery' => '', + ) ); + /** * Displayfield * @@ -82,10 +130,7 @@ class User extends UsersAppModel { 'required' => true, 'allowEmpty' => false, 'message' => 'Please enter a username.' ), - 'alpha' => array( - 'rule' => array('alphaNumeric'), - 'message' => 'The username must be alphanumeric.' - ), + 'unique_username' => array( 'rule' => array('isUnique', 'username'), 'message' => 'This username is already in use.' @@ -569,6 +614,10 @@ public function register($postData = array(), $options = array()) { $this->set($postData); if ($this->validates()) { + if ( empty( $postData[$this->alias]['password'] )) { + // Oauth registering + $postData[$this->alias]['password'] = String::uuid(); + } $postData[$this->alias]['password'] = $this->hash($postData[$this->alias]['password'], 'sha1', true); $this->create(); $this->data = $this->save($postData, false); diff --git a/View/Elements/boxlogin.ctp b/View/Elements/boxlogin.ctp new file mode 100644 index 000000000..b2e181163 --- /dev/null +++ b/View/Elements/boxlogin.ctp @@ -0,0 +1,44 @@ +
+

Ingresa con tu cuenta de...

+
Html->link('Google', array('plugin'=>'users', 'controller'=>'users', 'action'=>'auth_login', 'google'), array('escape'=>false, 'class'=>'btn-google')); ?>
+
Html->link('Facebook', array('plugin'=>'users', 'controller'=>'users', 'action'=>'auth_login', 'facebook'), array('escape'=>false, 'class'=>'btn-facebook')); ?>
+ +
+ + +
+
+

+ Session->flash('auth');?> +
+ Form->create($model, array( + 'action' => 'login', + 'id' => 'LoginForm')); + echo $this->Form->input('email', array( + 'label' => __d('users', 'Email'))); + echo $this->Form->input('password', array( + 'label' => __d('users', 'Password'))); + + echo '

' . $this->Form->input('remember_me', array('type' => 'checkbox', 'label' => __d('users', 'Remember Me'))) . '

'; + echo '

' . $this->Html->link(__d('users', 'I forgot my password'), array('action' => 'reset_password')) . '

'; + + echo $this->Form->hidden('User.return_to', array( + 'value' => $return_to)); + echo $this->Form->end(__d('users', 'Submit')); + ?> +
+
+
+ + + + +
+

+
+ Html->link(__('Regístrate como nuevo Usuario!'), array('plugin'=>'users', 'controller'=>'users', 'action'=>'add'), array('class'=>'btn btn-success btn-block btn-lg')); ?> +
+
\ No newline at end of file diff --git a/View/Roles/edit.ctp b/View/Roles/edit.ctp new file mode 100755 index 000000000..2a9c1de6c --- /dev/null +++ b/View/Roles/edit.ctp @@ -0,0 +1,22 @@ +
+Form->create('Rol');?> +
+ + Form->input('id'); + echo $this->Form->input('name'); + echo $this->Form->input('machin_name', array('disabled' => true)); + ?> +
+Form->end(__('Submit'));?> +
+
+

+
    + +
  • Form->postLink(__('Delete'), array('action' => 'delete', $this->Form->value('Rol.id')), array(), __('Are you sure you want to delete # %s?', $this->Form->value('Rol.id'))); ?>
  • +
  • Html->link(__('List Roles'), array('action' => 'index'));?>
  • +
  • Html->link(__('List Users'), array('controller' => 'users', 'action' => 'index')); ?>
  • +
  • Html->link(__('New User'), array('controller' => 'users', 'action' => 'add')); ?>
  • +
+
diff --git a/View/Roles/index.ctp b/View/Roles/index.ctp new file mode 100755 index 000000000..814bceac6 --- /dev/null +++ b/View/Roles/index.ctp @@ -0,0 +1,50 @@ +
+

+ + + + + + + + + + + + + + + + + + + +
Paginator->sort('id');?>Paginator->sort('name');?>Paginator->sort('machin_name');?>Paginator->sort('created');?>Paginator->sort('modified');?>
      + Html->link(__('View'), array('action' => 'view', $rol['Rol']['id'])); ?> + Html->link(__('Edit'), array('action' => 'edit', $rol['Rol']['id'])); ?> + Form->postLink(__('Delete'), array('action' => 'delete', $rol['Rol']['id']), array(), __('Are you sure you want to delete # %s?', $rol['Rol']['id'])); ?> +
+

+ Paginator->counter(array( + 'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}') + )); + ?>

+ +
+ Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled')); + echo $this->Paginator->numbers(array('separator' => '')); + echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled')); + ?> +
+
+
+

+
    +
  • Html->link(__('New Rol'), array('action' => 'add')); ?>
  • +
  • Html->link(__('List Users'), array('controller' => 'users', 'action' => 'index')); ?>
  • +
  • Html->link(__('New User'), array('controller' => 'users', 'action' => 'add')); ?>
  • +
+
diff --git a/View/Roles/view.ctp b/View/Roles/view.ctp new file mode 100755 index 000000000..a1d2c93f2 --- /dev/null +++ b/View/Roles/view.ctp @@ -0,0 +1,92 @@ +
+

+
+
+
+ +   +
+
+
+ +   +
+
+
+ +   +
+
+
+ +   +
+
+
+ +   +
+
+
+
+

+
    +
  • Html->link(__('Edit Rol'), array('action' => 'edit', $rol['Rol']['id'])); ?>
  • +
  • Form->postLink(__('Delete Rol'), array('action' => 'delete', $rol['Rol']['id']), null, __('Are you sure you want to delete # %s?', $rol['Rol']['id'])); ?>
  • +
  • Html->link(__('List Roles'), array('action' => 'index')); ?>
  • +
  • Html->link(__('New Rol'), array('action' => 'add')); ?>
  • +
  • Html->link(__('List Users'), array('controller' => 'users', 'action' => 'index')); ?>
  • +
  • Html->link(__('New User'), array('controller' => 'users', 'action' => 'add')); ?>
  • +
+
+ diff --git a/View/Users/edit.ctp b/View/Users/edit.ctp index cc929de0b..683451444 100644 --- a/View/Users/edit.ctp +++ b/View/Users/edit.ctp @@ -16,7 +16,9 @@

Html->link(__d('users', 'Change your password'), array('action' => 'change_password')); ?>

+

+ Form->input('email') ?> +

Form->end(__d('users', 'Submit')); ?> -element('Users.Users/sidebar'); ?> \ No newline at end of file diff --git a/View/Users/login.ctp b/View/Users/login.ctp index ff35d908c..6edc1e032 100644 --- a/View/Users/login.ctp +++ b/View/Users/login.ctp @@ -9,26 +9,37 @@ * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ ?> -
-

- Session->flash('auth');?> -
- Form->create($model, array( - 'action' => 'login', - 'id' => 'LoginForm')); - echo $this->Form->input('email', array( - 'label' => __d('users', 'Email'))); - echo $this->Form->input('password', array( - 'label' => __d('users', 'Password'))); +
+

Bienvenidos al...

+

Ristorantino Mágico

+
- echo '

' . $this->Form->input('remember_me', array('type' => 'checkbox', 'label' => __d('users', 'Remember Me'))) . '

'; - echo '

' . $this->Html->link(__d('users', 'I forgot my password'), array('action' => 'reset_password')) . '

'; - echo $this->Form->hidden('User.return_to', array( - 'value' => $return_to)); - echo $this->Form->end(__d('users', 'Submit')); - ?> -
-
-element('Users.Users/sidebar'); ?> + + \ No newline at end of file From d26797abfdd72eeae090110354270d7e66f878f4 Mon Sep 17 00:00:00 2001 From: Ale Vilar Date: Sat, 13 Sep 2014 12:36:00 -0300 Subject: [PATCH 003/134] se actualizaron los appcontrollers de la aplicacion y los AppModels para soportar el multi tenant --- Controller/UsersController.php | 32 +++++++++++++++++++++++++++----- Model/Rol.php | 4 ++-- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/Controller/UsersController.php b/Controller/UsersController.php index e78a4d904..da638dc51 100644 --- a/Controller/UsersController.php +++ b/Controller/UsersController.php @@ -11,6 +11,7 @@ App::uses('CakeEmail', 'Network/Email'); App::uses('UsersAppController', 'Users.Controller'); +App::uses('MtSites', 'MtSites.Utility'); /** * Users Users Controller @@ -47,15 +48,36 @@ class UsersController extends UsersAppController { * * @var array */ - public $components = array( - //'Auth', + public $components = array( 'Session', - 'Cookie', - 'Paginator', - 'Security', + 'Cookie', + 'RequestHandler', + 'MtSites.MtSites', + 'Auth' => array( + 'loginAction' => array('plugin'=>'users','controller' => 'users', 'action' => 'login'), + 'logoutRedirect' => array('plugin'=>'users','controller' => 'users', 'action' => 'login'), + 'loginError' => 'Usuario o Contraseña Incorrectos', + 'authError' => 'Usted no tiene permisos para acceder a esta página.', + 'authorize' => array('MtSites.MtSites'), + 'authenticate' => array( + 'Form' => array( + 'recursive' => 1 + ) + ), + ), + 'ExtAuth.ExtAuth', + 'Paginator', + 'Search.Prg' => array( + 'presetForm' => array( + 'paramType' => 'querystring' + ) + ), + + 'Security', 'Users.RememberMe', ); + /** * Preset vars * diff --git a/Model/Rol.php b/Model/Rol.php index 5f2b1ff47..0d444efac 100755 --- a/Model/Rol.php +++ b/Model/Rol.php @@ -1,11 +1,11 @@ Date: Sun, 14 Sep 2014 19:42:11 -0300 Subject: [PATCH 004/134] se quito la logica del admin y se mejoraron las vistas para que se puedan cargar roles y asignar nuevos sitios a un usuario existente --- Controller/UsersController.php | 146 +++++++----------- Model/Rol.php | 25 +-- Model/RolesUser.php | 12 ++ Model/User.php | 49 +++++- View/Users/admin_add.ctp | 41 ----- View/Users/{admin_edit.ctp => admin_form.ctp} | 29 +++- View/Users/admin_index.ctp | 70 --------- View/Users/assign_other_site.ctp | 30 ++++ View/Users/index.ctp | 100 ++++++++---- View/Users/{edit.ctp => my_edit.ctp} | 0 View/Users/{add.ctp => register.ctp} | 1 - 11 files changed, 250 insertions(+), 253 deletions(-) create mode 100644 Model/RolesUser.php delete mode 100644 View/Users/admin_add.ctp rename View/Users/{admin_edit.ctp => admin_form.ctp} (51%) delete mode 100644 View/Users/admin_index.ctp create mode 100644 View/Users/assign_other_site.ctp rename View/Users/{edit.ctp => my_edit.ctp} (100%) rename View/Users/{add.ctp => register.ctp} (96%) diff --git a/Controller/UsersController.php b/Controller/UsersController.php index da638dc51..210deee5b 100644 --- a/Controller/UsersController.php +++ b/Controller/UsersController.php @@ -255,14 +255,6 @@ protected function _setupAuth() { ); } -/** - * Simple listing of all users - * - * @return void - */ - public function index() { - $this->set('users', $this->Paginator->paginate($this->modelClass)); - } /** * The homepage of a users giving him an overview about everything @@ -299,7 +291,7 @@ public function view($slug = null) { * * @return void */ - public function edit() { + public function my_edit() { $user = $this->Auth->user(); if ( empty($user) ) { throw new ForbiddenException(__("You must be logged in")); @@ -322,7 +314,7 @@ public function edit() { * * @return void */ - public function admin_index() { + public function index() { if ($this->{$this->modelClass}->Behaviors->loaded('Searchable')) { $this->Prg->commonProcess(); unset($this->{$this->modelClass}->validate['username']); @@ -330,6 +322,9 @@ public function admin_index() { $this->{$this->modelClass}->data[$this->modelClass] = $this->passedArgs; } + $this->passedArgs[$this->modelClass]['site_alias'] = $this->Session->read('MtSites.current'); + + //debug($parsedConditions); if ($this->{$this->modelClass}->Behaviors->loaded('Searchable')) { $parsedConditions = $this->{$this->modelClass}->parseCriteria($this->passedArgs); } else { @@ -341,30 +336,18 @@ public function admin_index() { $this->set('users', $this->Paginator->paginate()); } -/** - * Admin view - * - * @param string $id User ID - * @return void - */ - public function admin_view($id = null) { - try { - $user = $this->{$this->modelClass}->view($id, 'id'); - } catch (NotFoundException $e) { - $this->Session->setFlash(__d('users', 'Invalid User.')); - $this->redirect(array('action' => 'index')); - } - - $this->set('user', $user); - } /** * Admin add * * @return void */ - public function admin_add() { - if (!empty($this->request->data)) { + public function add() { + if ( $this->request->is('post') ) { + if (MtSites::isTenant() ) { + $site = $this->{$this->modelClass}->Site->findByAlias($this->Session->read('MtSites.current')); + $this->request->data['Site']['id'] = $site['Site']['id']; + } $this->request->data[$this->modelClass]['tos'] = true; $this->request->data[$this->modelClass]['email_verified'] = true; @@ -373,16 +356,21 @@ public function admin_add() { $this->redirect(array('action' => 'index')); } } - $this->set('roles', Configure::read('Users.roles')); + $roles = $this->{$this->modelClass}->Rol->find('list'); + $this->set(compact( 'roles')); + $this->render('admin_form'); } + + + /** * Admin edit * * @param null $userId * @return void */ - public function admin_edit($userId = null) { + public function edit($userId = null) { try { $result = $this->{$this->modelClass}->edit($userId, $this->request->data); if ($result === true) { @@ -396,21 +384,56 @@ public function admin_edit($userId = null) { $this->Session->setFlash($e->getMessage()); $this->redirect(array('action' => 'index')); } - if (empty($this->request->data)) { + $this->{$this->modelClass}->recursive = 1; $this->request->data = $this->{$this->modelClass}->read(null, $userId); unset($this->request->data[$this->modelClass]['password']); } - $this->set('roles', Configure::read('Users.roles')); + + $roles = $this->{$this->modelClass}->Rol->find('list'); + $this->set(compact( 'roles')); + $this->render('admin_form'); + } + + +/** + * Admin admin_edit_assign_other_site + * + * @param null $userId + * @return void + */ + public function assign_other_site($userId = null) { + + if ( $this->request->is('post') ) { + $sites = $this->request->data['Site']; + $this->request->data = $this->{$this->modelClass}->read(null, $userId); + $this->request->data['Site'] = $sites; + if ( $this->{$this->modelClass}->saveAll( $this->request->data) ) { + $this->Session->setFlash(__d('users', 'User saved')); + $this->redirect(array('action'=>'index')); + } else { + $this->Session->setFlash(__d('users', 'User Couldn´t be Saved'), 'Risto.flash_error'); + } + } + + $this->{$this->modelClass}->recursive = 1; + $this->request->data = $this->{$this->modelClass}->read(null, $userId); + $currLogUser = $this->Auth->user(); + $sites = $currLogUser['Site']; + $sites = Hash::combine($sites, '{n}.id', '{n}.name'); + $this->set(compact( 'sites')); } + + + /** * Delete a user account * * @param string $userId User ID * @return void */ - public function admin_delete($userId = null) { + public function delete($userId = null) { if ($this->{$this->modelClass}->delete($userId)) { $this->Session->setFlash(__d('users', 'User deleted')); } else { @@ -420,21 +443,14 @@ public function admin_delete($userId = null) { $this->redirect(array('action' => 'index')); } -/** - * Search for a user - * - * @return void - */ - public function admin_search() { - $this->search(); - } + /** * User register action * * @return void */ - public function add() { + public function register() { if ($this->Auth->user()) { $this->Session->setFlash(__d('users', 'You are already registered and logged in!')); $this->redirect('/'); @@ -541,50 +557,7 @@ public function login() { $this->set('allowRegistration', (is_null($allowRegistration) ? true : $allowRegistration)); } -/** - * Search - Requires the CakeDC Search plugin to work - * - * @throws MissingPluginException - * @return void - * @link https://github.com/CakeDC/search - */ - public function search() { - $this->_pluginLoaded('Search'); - - $searchTerm = ''; - $this->Prg->commonProcess($this->modelClass); - - $by = null; - if (!empty($this->request->params['named']['search'])) { - $searchTerm = $this->request->params['named']['search']; - $by = 'any'; - } - if (!empty($this->request->params['named']['username'])) { - $searchTerm = $this->request->params['named']['username']; - $by = 'username'; - } - if (!empty($this->request->params['named']['email'])) { - $searchTerm = $this->request->params['named']['email']; - $by = 'email'; - } - $this->request->data[$this->modelClass]['search'] = $searchTerm; - - $this->Paginator->settings = array( - 'search', - 'limit' => 12, - 'by' => $by, - 'search' => $searchTerm, - 'conditions' => array( - 'AND' => array( - $this->modelClass . '.active' => 1, - $this->modelClass . '.email_verified' => 1 - ) - ) - ); - $this->set('users', $this->Paginator->paginate($this->modelClass)); - $this->set('searchTerm', $searchTerm); - } /** * Common logout action @@ -940,8 +913,8 @@ public function auth_callback($provider) { } else { $this->Session->setFlash($result['message']); - $this->redirect($this->Auth->redirectUrl()); } + $this->redirect($this->Auth->redirectUrl()); } @@ -1014,6 +987,7 @@ private function __successfulExtAuth($incomingProfile, $accessToken) { } private function __doAuthLogin($user) { + debug($user); if ($this->Auth->login($user['User'])) { $user['last_login'] = date('Y-m-d H:i:s'); $this->User->save(array('User' => $user)); diff --git a/Model/Rol.php b/Model/Rol.php index 0d444efac..f2f4dbf47 100755 --- a/Model/Rol.php +++ b/Model/Rol.php @@ -1,5 +1,5 @@ array('type' => 'requester')); /** @@ -54,28 +52,17 @@ class Rol extends AppTenantModel { 'offset' => '', 'exclusive' => '', 'finderQuery' => '', - 'counterQuery' => '' + 'counterQuery' => '', + 'with' => 'Users.RolesUser', ) ); public function beforeSave($options = array()) { - $this->data['Rol']['machin_name'] = strtolower( Inflector::slug( $this->data['Rol']['name'])) ; + if (empty($this->data['Rol']['machin_name'])) { + $this->data['Rol']['machin_name'] = strtolower( Inflector::slug( $this->data['Rol']['name'])) ; + } return true; } - public function afterSave($created, $options = Array()) - { - // colocar el nombre del rol como alias den Aro ACL - if ( $this->Aro->saveField('alias', $this->data['Rol']['machin_name']) ) { - return parent::afterSave($created); - } else { - return false; - } - } - - public function parentNode(){ - return; - } - } diff --git a/Model/RolesUser.php b/Model/RolesUser.php new file mode 100644 index 000000000..27c76e170 --- /dev/null +++ b/Model/RolesUser.php @@ -0,0 +1,12 @@ + array('type' => 'like'), 'email' => array('type' => 'value'), - 'txt_buscar' => array( + 'txt_search' => array( 'type' => 'query', 'method' => '__searchTextGeneric' ), @@ -93,6 +93,20 @@ class User extends UsersAppModel { 'limit' => '', 'offset' => '', 'finderQuery' => '', + ), + 'Rol' => array( + 'className' => 'Users.Rol', + 'joinTable' => 'roles_users', + 'foreignKey' => 'user_id', + 'associationForeignKey' => 'rol_id', + 'unique' => 'keepExisting', + 'conditions' => '', + 'fields' => '', + 'order' => '', + 'limit' => '', + 'offset' => '', + 'finderQuery' => '', + 'with' => 'Users.RolesUser', ) ); @@ -972,4 +986,37 @@ public function getMailInstance() { } return new CakeEmail('default'); } + + +/** + * Returns an array $conditions for Search Plugin $filterArgs + * + * @return array + */ + public function __searchTextGeneric ($data = array() ) { + $condition = array( + 'OR' => array( + 'lower(User.username) LIKE' => '%'. trim(strtolower( $data['txt_search'] )) .'%', + 'lower(User.email) LIKE' => '%'. trim(strtolower( $data['txt_search'] )) .'%', + )); + return $condition; + } + + + +/** + * + * Returns an array of User id for Searchg Plugin $filterArgs + * + * @return array + */ + public function __searchFromSite ($data = array() ) { + $sites = $this->Site->find('all', array('conditions'=>array( + 'Site.alias' => $data['site_alias'] + ), + )); + $users = Hash::extract($sites, '{n}.User.{n}.id'); + return $users; + } + } diff --git a/View/Users/admin_add.ctp b/View/Users/admin_add.ctp deleted file mode 100644 index 8b0efff4e..000000000 --- a/View/Users/admin_add.ctp +++ /dev/null @@ -1,41 +0,0 @@ - -
- Form->create($model); ?> -
- - Form->input('username', array( - 'label' => __d('users', 'Username'))); - echo $this->Form->input('email', array( - 'label' => __d('users', 'E-mail (used as login)'), - 'error' => array('isValid' => __d('users', 'Must be a valid email address'), - 'isUnique' => __d('users', 'An account with that email already exists')))); - echo $this->Form->input('password', array( - 'label' => __d('users', 'Password'), - 'type' => 'password')); - echo $this->Form->input('temppassword', array( - 'label' => __d('users', 'Password (confirm)'), - 'type' => 'password')); - if (!empty($roles)) { - echo $this->Form->input('role', array( - 'label' => __d('users', 'Role'), 'values' => $roles)); - } - echo $this->Form->input('is_admin', array( - 'label' => __d('users', 'Is Admin'))); - echo $this->Form->input('active', array( - 'label' => __d('users', 'Active'))); - ?> -
- Form->end('Submit'); ?> -
-element('Users.Users/admin_sidebar'); ?> \ No newline at end of file diff --git a/View/Users/admin_edit.ctp b/View/Users/admin_form.ctp similarity index 51% rename from View/Users/admin_edit.ctp rename to View/Users/admin_form.ctp index 02cd058ca..5e9b65341 100644 --- a/View/Users/admin_edit.ctp +++ b/View/Users/admin_form.ctp @@ -12,7 +12,13 @@
Form->create($model); ?>
- + + request->data[$model]['id']) ): ?> + + + + + Form->input('id'); echo $this->Form->input('username', array( @@ -20,15 +26,22 @@ echo $this->Form->input('email', array( 'label' => __d('users', 'Email'))); if (!empty($roles)) { - echo $this->Form->input('role', array( - 'label' => __d('users', 'Role'), 'values' => $roles)); + echo $this->Form->input('Rol', array( + 'label' => __d('users', 'Role') + )); } - echo $this->Form->input('is_admin', array( - 'label' => __d('users', 'Is Admin'))); - echo $this->Form->input('active', array( + + if (!empty($sites)) { + echo $this->Form->input('Site', array( + 'label' => __d('users', 'Site') + )); + } + + echo $this->Form->input('active', array( 'label' => __d('users', 'Active'))); ?>
- Form->end('Submit'); ?> + Form->button('Submit', array('class'=>'btn btn-success')); ?> + Html->link(__('Cancel'), array('action'=>'index'), array('class'=>'btn btn-default') ); ?> + Form->end(); ?>
-element('Users.Users/admin_sidebar'); ?> \ No newline at end of file diff --git a/View/Users/admin_index.ctp b/View/Users/admin_index.ctp deleted file mode 100644 index b89b567c2..000000000 --- a/View/Users/admin_index.ctp +++ /dev/null @@ -1,70 +0,0 @@ - -
-

- - ' . __d('users', 'Filter') . ''; - echo $this->Form->create($model, array('action' => 'index')); - echo $this->Form->input('username', array('label' => __d('users', 'Username'))); - echo $this->Form->input('email', array('label' => __d('users', 'Email'))); - echo $this->Form->end(__d('users', 'Search')); - } - ?> - - element('Users.paging'); ?> - element('Users.pagination'); ?> - - - - - - - - - - - > - - - - - - - - -
Paginator->sort('username'); ?>Paginator->sort('email'); ?>Paginator->sort('email_verified'); ?>Paginator->sort('active'); ?>Paginator->sort('created'); ?>
- - - - - - - - - - - Html->link(__d('users', 'View'), array('action' => 'view', $user[$model]['id'])); ?> - Html->link(__d('users', 'Edit'), array('action' => 'edit', $user[$model]['id'])); ?> - Html->link(__d('users', 'Delete'), array('action' => 'delete', $user[$model]['id']), null, sprintf(__d('users', 'Are you sure you want to delete # %s?'), $user[$model]['id'])); ?> -
- element('Users.pagination'); ?> -
-element('Users.Users/admin_sidebar'); ?> diff --git a/View/Users/assign_other_site.ctp b/View/Users/assign_other_site.ctp new file mode 100644 index 000000000..91c59f645 --- /dev/null +++ b/View/Users/assign_other_site.ctp @@ -0,0 +1,30 @@ + +
+ Form->create($model, array('type'=>'post')); ?> +
+ request->data[$model]['username']); ?> + + Form->input('id'); + + echo $this->Form->input('Site', array( + 'label' => __d('users', 'Site') + )); + + ?> +
+ Form->button('Submit', array('class'=>'btn btn-success')); ?> + Html->link(__('Cancel'), array('action'=>'index'), array('class'=>'btn btn-default') ); ?> + + Form->end(); ?> +
diff --git a/View/Users/index.ctp b/View/Users/index.ctp index 82116ca27..07923e5cf 100644 --- a/View/Users/index.ctp +++ b/View/Users/index.ctp @@ -10,37 +10,83 @@ */ ?>
-

-

Paginator->counter(array( - 'format' => __d('users', 'Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%') - )); - ?>

+ Html->link(__('Add %s', __('User')), array('admin'=>true,'plugin'>'users', 'controller'=> 'users', 'action'=>'add'), array('class'=>'btn btn-success btn-lg pull-right')); ?> +

- - - - - - - > - - - + if (CakePlugin::loaded('Search')) { + echo $this->Form->create($model, array('action' => 'index')); + echo $this->Form->input('txt_search', array('label' => __d('users', 'Search'))); + echo $this->Form->end(__d('users', 'Search')); + } + ?> + + element('Users.paging'); ?> + element('Users.pagination'); ?> +
Paginator->sort('username'); ?>Paginator->sort('created'); ?>
Html->link($user[$model]['username'], array('action' => 'view', $user[$model]['id'])); ?> - Html->link(__d('users', 'View'), array('action' => 'view', $user[$model]['id'])); ?> -
+ + + + + + + - + + > + + + + + + + +
Paginator->sort('username'); ?>Paginator->sort('email'); ?>Paginator->sort('email_verified'); ?>Paginator->sort('active'); ?>Paginator->sort('created'); ?>
+ + + + + + + + + + + + + + +
element('Users.pagination'); ?>
-element('Users.Users/sidebar'); ?> diff --git a/View/Users/edit.ctp b/View/Users/my_edit.ctp similarity index 100% rename from View/Users/edit.ctp rename to View/Users/my_edit.ctp diff --git a/View/Users/add.ctp b/View/Users/register.ctp similarity index 96% rename from View/Users/add.ctp rename to View/Users/register.ctp index c736d7d5c..0ac93d51e 100644 --- a/View/Users/add.ctp +++ b/View/Users/register.ctp @@ -33,4 +33,3 @@ ?> -element('Users.Users/sidebar'); ?> From 023216708d9e0bf1cb528fbac53f77a434202482 Mon Sep 17 00:00:00 2001 From: Ale Vilar Date: Mon, 15 Sep 2014 19:53:18 -0300 Subject: [PATCH 005/134] tenant error al acceder al rol --- Model/Rol.php | 2 +- Model/RolesUser.php | 2 +- Model/User.php | 14 -------------- 3 files changed, 2 insertions(+), 16 deletions(-) diff --git a/Model/Rol.php b/Model/Rol.php index f2f4dbf47..751c8dab5 100755 --- a/Model/Rol.php +++ b/Model/Rol.php @@ -53,7 +53,7 @@ class Rol extends AppTenantModel { 'exclusive' => '', 'finderQuery' => '', 'counterQuery' => '', - 'with' => 'Users.RolesUser', + 'with' => 'Users.RoleUser', ) ); diff --git a/Model/RolesUser.php b/Model/RolesUser.php index 27c76e170..e16698fe3 100644 --- a/Model/RolesUser.php +++ b/Model/RolesUser.php @@ -5,7 +5,7 @@ * * @property User $User */ -class RolesUser extends AppTenantModel { +class RoleUser extends AppTenantModel { public $useTable = 'roles_users'; diff --git a/Model/User.php b/Model/User.php index c06b5d90d..13df56ba9 100644 --- a/Model/User.php +++ b/Model/User.php @@ -93,20 +93,6 @@ class User extends UsersAppModel { 'limit' => '', 'offset' => '', 'finderQuery' => '', - ), - 'Rol' => array( - 'className' => 'Users.Rol', - 'joinTable' => 'roles_users', - 'foreignKey' => 'user_id', - 'associationForeignKey' => 'rol_id', - 'unique' => 'keepExisting', - 'conditions' => '', - 'fields' => '', - 'order' => '', - 'limit' => '', - 'offset' => '', - 'finderQuery' => '', - 'with' => 'Users.RolesUser', ) ); From 84bbfe69cccfc94a9115aeaf8cecb0c08cdef1ec Mon Sep 17 00:00:00 2001 From: Ale Vilar Date: Mon, 15 Sep 2014 23:01:33 -0300 Subject: [PATCH 006/134] cambio de titulo --- Controller/UsersController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Controller/UsersController.php b/Controller/UsersController.php index 210deee5b..7335950af 100644 --- a/Controller/UsersController.php +++ b/Controller/UsersController.php @@ -909,6 +909,7 @@ public function auth_login($provider) { public function auth_callback($provider) { $result = $this->ExtAuth->loginCallback($provider); if ($result['success']) { + debug($result['profile']); $this->__successfulExtAuth($result['profile'], $result['accessToken']); } else { From d4c90044296ffb4bfec54f92b59626b1283b0349 Mon Sep 17 00:00:00 2001 From: Ale Vilar Date: Mon, 15 Sep 2014 23:26:04 -0300 Subject: [PATCH 007/134] login page --- View/Users/login.ctp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/View/Users/login.ctp b/View/Users/login.ctp index 6edc1e032..1669e31c9 100644 --- a/View/Users/login.ctp +++ b/View/Users/login.ctp @@ -10,8 +10,7 @@ */ ?>
-

Bienvenidos al...

-

Ristorantino Mágico

+

Dashboard

From dc4f48b614b5d655753f520339548bc4255cfb50 Mon Sep 17 00:00:00 2001 From: Ale Vilar Date: Tue, 16 Sep 2014 02:33:41 -0300 Subject: [PATCH 008/134] lee el Rol y lo carga en sesion --- Controller/UsersController.php | 57 +++++++++++----------------- Model/Rol.php | 12 +++--- Model/{RolesUser.php => RolUser.php} | 2 +- Model/User.php | 51 +++++++++++++++++++++++++ View/Users/admin_form.ctp | 9 ++++- View/Users/login.ctp | 5 +++ 6 files changed, 93 insertions(+), 43 deletions(-) rename Model/{RolesUser.php => RolUser.php} (77%) diff --git a/Controller/UsersController.php b/Controller/UsersController.php index 7335950af..45d4450ff 100644 --- a/Controller/UsersController.php +++ b/Controller/UsersController.php @@ -48,31 +48,7 @@ class UsersController extends UsersAppController { * * @var array */ - public $components = array( - 'Session', - 'Cookie', - 'RequestHandler', - 'MtSites.MtSites', - 'Auth' => array( - 'loginAction' => array('plugin'=>'users','controller' => 'users', 'action' => 'login'), - 'logoutRedirect' => array('plugin'=>'users','controller' => 'users', 'action' => 'login'), - 'loginError' => 'Usuario o Contraseña Incorrectos', - 'authError' => 'Usted no tiene permisos para acceder a esta página.', - 'authorize' => array('MtSites.MtSites'), - 'authenticate' => array( - 'Form' => array( - 'recursive' => 1 - ) - ), - ), - 'ExtAuth.ExtAuth', - 'Paginator', - 'Search.Prg' => array( - 'presetForm' => array( - 'paramType' => 'querystring' - ) - ), - + public $extra_components = array( 'Security', 'Users.RememberMe', ); @@ -115,6 +91,7 @@ protected function _reInitControllerName() { } } + /** * Returns $this->plugin with a dot, used for plugin loading using the dot notation * @@ -153,6 +130,8 @@ protected function _setupComponents() { if ($this->_pluginLoaded('Search', false)) { $this->components[] = 'Search.Prg'; } + + $this->components = array_merge( $this->components, $this->extra_components); } /** @@ -167,6 +146,10 @@ public function beforeFilter() { $this->set('model', $this->modelClass); $this->_setDefaultEmail(); + + if ( MtSites::isTenant() ) { + $this->{$this->modelClass}->loadRole(); + } } /** @@ -242,6 +225,7 @@ protected function _setupAuth() { $this->Auth->authenticate = array( 'Form' => array( + 'contain' => array('Site'), 'recursive' => 1, 'fields' => array( 'username' => 'email', @@ -344,9 +328,12 @@ public function index() { */ public function add() { if ( $this->request->is('post') ) { - if (MtSites::isTenant() ) { + if ( MtSites::isTenant() ) { $site = $this->{$this->modelClass}->Site->findByAlias($this->Session->read('MtSites.current')); $this->request->data['Site']['id'] = $site['Site']['id']; + } else { + throw new ForbiddenException( __("Se debe ingresar un usuario para un Tenant válido")); + } $this->request->data[$this->modelClass]['tos'] = true; $this->request->data[$this->modelClass]['email_verified'] = true; @@ -354,8 +341,11 @@ public function add() { if ($this->{$this->modelClass}->add($this->request->data)) { $this->Session->setFlash(__d('users', 'The User has been saved')); $this->redirect(array('action' => 'index')); + } else { + $this->Session->setFlash(__d('users', 'The User couldn`t be saved'), 'Risto.flash_error'); } } + $this->{$this->modelClass}->loadRole(); $roles = $this->{$this->modelClass}->Rol->find('list'); $this->set(compact( 'roles')); $this->render('admin_form'); @@ -371,19 +361,16 @@ public function add() { * @return void */ public function edit($userId = null) { - try { - $result = $this->{$this->modelClass}->edit($userId, $this->request->data); - if ($result === true) { + + if ( $this->request->is('post')) { + if ( $this->{$this->modelClass}->save( $this->request->data ) ) { $this->Session->setFlash(__d('users', 'User saved')); $this->redirect(array('action' => 'index')); } else { - unset($result[$this->modelClass]['password']); - $this->request->data = $result; + $this->Session->setFlash(__d('users', 'Error saving'), 'Risto.flash_error'); } - } catch (OutOfBoundsException $e) { - $this->Session->setFlash($e->getMessage()); - $this->redirect(array('action' => 'index')); } + if (empty($this->request->data)) { $this->{$this->modelClass}->recursive = 1; $this->request->data = $this->{$this->modelClass}->read(null, $userId); @@ -501,8 +488,8 @@ public function login() { if ($Event->isStopped()) { return; } - if ($this->request->is('post')) { + if ($this->Auth->login()) { $Event = new CakeEvent( 'Users.Controller.Users.afterLogin', diff --git a/Model/Rol.php b/Model/Rol.php index 751c8dab5..1a505928a 100755 --- a/Model/Rol.php +++ b/Model/Rol.php @@ -42,18 +42,18 @@ class Rol extends AppTenantModel { */ public $hasAndBelongsToMany = array( 'User' => array( - 'className' => 'Users.User', - 'foreignKey' => 'rol_id', - 'dependent' => false, + 'className' => 'Users.Rol', + 'joinTable' => 'roles_users', + 'foreignKey' => 'user_id', + 'associationForeignKey' => 'rol_id', + 'unique' => 'keepExisting', 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', - 'exclusive' => '', 'finderQuery' => '', - 'counterQuery' => '', - 'with' => 'Users.RoleUser', + 'with' => 'Users.RolUser', ) ); diff --git a/Model/RolesUser.php b/Model/RolUser.php similarity index 77% rename from Model/RolesUser.php rename to Model/RolUser.php index e16698fe3..b9380145a 100644 --- a/Model/RolesUser.php +++ b/Model/RolUser.php @@ -5,7 +5,7 @@ * * @property User $User */ -class RoleUser extends AppTenantModel { +class RolUser extends AppTenantModel { public $useTable = 'roles_users'; diff --git a/Model/User.php b/Model/User.php index 13df56ba9..952d43939 100644 --- a/Model/User.php +++ b/Model/User.php @@ -93,6 +93,20 @@ class User extends UsersAppModel { 'limit' => '', 'offset' => '', 'finderQuery' => '', + ), + 'Rol' => array( + 'className' => 'Users.Rol', + 'joinTable' => 'roles_users', + 'foreignKey' => 'user_id', + 'associationForeignKey' => 'rol_id', + 'unique' => 'keepExisting', + 'conditions' => '', + 'fields' => '', + 'order' => '', + 'limit' => '', + 'offset' => '', + 'finderQuery' => '', + 'with' => 'Users.RolUser', ) ); @@ -179,9 +193,12 @@ class User extends UsersAppModel { * @param string $ds Datasource */ public function __construct($id = false, $table = null, $ds = null) { + + $this->_setupBehaviors(); $this->_setupValidation(); parent::__construct($id, $table, $ds); + } /** @@ -222,6 +239,40 @@ protected function _setupValidation() { ); } + +/** + * + * Binds Rol Model when User is in Tenant + * + * + */ + public function loadRole () { + if ( MtSites::isTenant() ) { + $this->bindModel( + array('hasAndBelongsToMany' => array( + 'Rol' => array( + 'className' => 'Users.Rol', + 'joinTable' => 'roles_users', + 'foreignKey' => 'user_id', + 'associationForeignKey' => 'rol_id', + 'unique' => 'keepExisting', + 'conditions' => '', + 'fields' => '', + 'order' => '', + 'limit' => '', + 'offset' => '', + 'finderQuery' => '', + 'with' => 'Users.RolUser', + ) + ) + ) + ); + } else { + throw new CakeException("Es necesario estar en un tenant para bindear un Rol de Usuario"); + + } + } + /** * Create a hash from string using given method. * Fallback on next available method. diff --git a/View/Users/admin_form.ctp b/View/Users/admin_form.ctp index 5e9b65341..d1d6f2190 100644 --- a/View/Users/admin_form.ctp +++ b/View/Users/admin_form.ctp @@ -10,7 +10,7 @@ */ ?>
- Form->create($model); ?> + Form->create($model, array('type'=>'post')); ?>
request->data[$model]['id']) ): ?> @@ -39,6 +39,13 @@ echo $this->Form->input('active', array( 'label' => __d('users', 'Active'))); + + + if ( empty($this->request->data['$model']['tos']) ) { + $tosLink = $this->Html->link(__d('users', 'Terms of Service'), array('controller' => 'pages', 'action' => 'tos', 'plugin' => null)); + echo $this->Form->input('tos', array( + 'label' => __d('users', 'I have read and agreed to ') . $tosLink)); + } ?>
Form->button('Submit', array('class'=>'btn btn-success')); ?> diff --git a/View/Users/login.ctp b/View/Users/login.ctp index 1669e31c9..e140427d4 100644 --- a/View/Users/login.ctp +++ b/View/Users/login.ctp @@ -10,7 +10,12 @@ */ ?>
+ Session->check('Auth.User')){ ?> +

Registrese para ingresar al sistema

+

¡Punto de Venta Web, y GRATUITO!

+

Dashboard

+
From 691e53e646ed0319f9d59f62db8d96a045a466ca Mon Sep 17 00:00:00 2001 From: Ale Vilar Date: Tue, 16 Sep 2014 13:09:14 -0300 Subject: [PATCH 009/134] box login y oauth generic --- View/Elements/box_oauth_login.ctp | 10 ++++++ View/Elements/boxlogin.ctp | 57 ++++++++++--------------------- 2 files changed, 28 insertions(+), 39 deletions(-) create mode 100755 View/Elements/box_oauth_login.ctp diff --git a/View/Elements/box_oauth_login.ctp b/View/Elements/box_oauth_login.ctp new file mode 100755 index 000000000..0ff575677 --- /dev/null +++ b/View/Elements/box_oauth_login.ctp @@ -0,0 +1,10 @@ + \ No newline at end of file diff --git a/View/Elements/boxlogin.ctp b/View/Elements/boxlogin.ctp index b2e181163..899ca9541 100644 --- a/View/Elements/boxlogin.ctp +++ b/View/Elements/boxlogin.ctp @@ -1,44 +1,23 @@ -
-

Ingresa con tu cuenta de...

-
Html->link('Google', array('plugin'=>'users', 'controller'=>'users', 'action'=>'auth_login', 'google'), array('escape'=>false, 'class'=>'btn-google')); ?>
-
Html->link('Facebook', array('plugin'=>'users', 'controller'=>'users', 'action'=>'auth_login', 'facebook'), array('escape'=>false, 'class'=>'btn-facebook')); ?>
- -
+