Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- App::uses('AppController', 'Controller');
- /**
- * Users Controller
- *
- * @property User $User
- * @property PaginatorComponent $Paginator
- * @property AuthComponent $Auth
- * @property SessionComponent $Session
- */
- class UsersController extends AppController {
- /**
- * Configuración de Componentes de Autorización, Sesión y el Plugin de AutoLogin
- */
- public $components = array('ConvertingCase');
- public $helpers = array("Time");
- /**
- * [$uses description]
- * @var array
- */
- public $uses = array('LoginViewModel');
- /**
- * [beforeFilter description]
- * @return [type] [description]
- */
- public function beforeFilter() {
- parent::beforeFilter();
- if ($this->request->params['action'] == 'start') {
- $this->Auth->allow();
- }
- }
- /**
- * [beforeRender description]
- * @return [type] [description]
- */
- public function beforeRender() {
- parent::beforeRender();
- if (in_array($this->request->params['action'], array('add', 'edit', 'addClient', 'editClient'))) {
- $this->set('validations', json_encode($this->User->validate));
- if ($this->User->validationErrors) {
- $this->set('validationErrors', $this->User->validationErrors);
- }
- }
- }
- /**
- * index method
- *
- * @return void
- */
- public function index() {
- $this->User->recursive = 0;
- $this->set('users', $this->User->find('all'));
- }
- /**
- * Método que realiza la validación del usuario
- * y el password antes de iniciar la sesión en la aplicación
- *
- * @return void
- */
- public function login() {
- //Si el Usuario se encuentra ya Logeado en el sistema
- if ($this->Session->check('Auth.User')) {
- //Redirecciona al index del sistema
- return $this->redirect(array('controller' => 'home', 'action' => 'index'));
- }
- //Si Uso el form para logearse (POST)
- if ($this->request->is('post')) {
- //Convertir a minusculas el nombre de usuario ya que no se hacen distinciones entre minusculas y mayusculas
- $this->request->data['User']['username'] = $this->ConvertingCase->convertingLowerCase($this->request->data['User']['username']);
- //Si el Usuario Colocó un nombre de usuario y contraseña correcta
- if ($this->Auth->login()) {
- $this->Cookie->write('resnik_remember_me_cookie', $this->request->data, true, '2 weeks');
- return $this->redirect(array('controller' => 'home', 'action' => 'index'));
- } else {
- //Verificar si el usuario está deshabilitado
- if ($this->User->checkUserPasswordAndStatus($this->request->data)) {
- $this->Session->setFlash(__('Usuario deshabilitado por favor consulte al administrador'), 'alert', array('class' => 'alert-danger'));
- } else {
- //Asigna a la variable session que va luego a la vista el mensaje de error
- $this->Session->setFlash(__('Nombre de Usuario o contraseña incorrectos'), 'alert', array('class' => 'alert-danger'));
- }
- }
- }
- $this->set('validations', json_encode($this->LoginViewModel->validate)); //Validaciones del Usuario
- //Se Define el Layout de Login que no es el mismo del deafult
- $this->layout = 'login';
- }
- /**
- * Método que cierra la sesión del usuario activo
- *
- * @return void
- */
- public function logout() {
- //delete Cookie
- $this->Cookie->delete('resnik_remember_me_cookie');
- //Redirecciona a la página de Login Inicial
- return $this->redirect($this->Auth->logout());
- }
- /**
- * view method
- *
- * @throws NotFoundException
- * @param string $id
- * @return void
- */
- public function view($id = null) {
- if (!$this->User->exists($id)) {
- throw new NotFoundException(__('Usuario inválido'));
- }
- $this->User->recursive = 0;
- $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
- $user = $this->User->find('first', $options);
- if ($user['User']['partner_id'] != null) {
- $options = array(
- 'conditions' => array(
- 'User.' . $this->User->primaryKey => $user['User']['partner_id']
- ),
- 'fields' => array(
- 'username'
- ));
- $partnerUser = $this->User->find('first', $options);
- $user['User']['PartnerUser'] = $partnerUser['User'];
- }
- $this->set('user', $user);
- }
- /**
- * add method
- *
- * @return void
- */
- public function add() {
- if ($this->request->is('post')) {
- $this->User->create();
- $this->request->data['User']['is_client'] = 0;
- $this->request->data['User']['status'] = 1;
- $this->request->data['User']['username'] = $this->ConvertingCase->convertingLowerCase($this->request->data['User']['username']);
- $this->request->data['User']['birthdate'] = $this->Date->completeDateDDMM($this->request->data['User']['birthdate']);
- $this->request->data['User']['partner_id'] = $this->User->checkPartner($this->request->data['User']['partner_id']);
- if ($this->request->data['User']['partner_id'] !== null &&
- $this->User->save($this->request->data) &&
- $this->User->savePartner($this->User->id, $this->request->data['User']['partner_id'])) {
- $this->Session->setFlash(__('El usuario ha sido registrado.'), 'alert', array('class' => 'alert-success'));
- return $this->redirect(array('action' => 'index'));
- } else {
- $this->Session->setFlash(__('Ha ocurrido un problema registrando al usuario, por favor intente de nuevo.'), 'alert', array('class' => 'alert-danger'));
- }
- }
- //unset($this->User->validate['password']);
- $partnerCondition = array(
- 'conditions' => array(
- 'NOT' => array(
- 'User.is_client' => true,
- ),
- 'User.partner_id' => null,
- ),
- 'fields' => array(
- 'User.id',
- 'User.username'
- )
- );
- $groupConditions = array(
- 'conditions' => array(
- 'NOT' => array(
- 'Group.alias' => array(
- 'cliente',
- 'gerenciacliente'
- )
- )
- )
- );
- $this->set('partners', $this->User->find('list', $partnerCondition));
- $this->set('groups', $this->User->Group->find('list', $groupConditions));
- $this->set('departments', $this->User->Department->find('list'));
- $this->set('positions', $this->User->Position->find('list'));
- }
- /**
- * [addClient description]
- */
- public function addClient() {
- if ($this->request->is('post')) {
- $this->User->create();
- $this->request->data['User']['is_client'] = 1;
- $this->request->data['User']['status'] = 1;
- $this->request->data['User']['username'] = $this->ConvertingCase->convertingLowerCase($this->request->data['User']['username']);
- $this->request->data['User']['birthdate'] = $this->Date->completeDateDDMM($this->request->data['User']['birthdate']);
- if ($this->User->save($this->request->data)) {
- $this->Session->setFlash(__('El cliente ha sido registrado.'), 'alert', array('class' => 'alert-success'));
- return $this->redirect(array('action' => 'index'));
- } else {
- $this->Session->setFlash(__('Ha ocurrido un problema registrando al cleinte, por favor intente de nuevo.'), 'alert', array('class' => 'alert-danger'));
- }
- }
- $groupOptions = array('conditions' => array(
- 'alias' => array('gerenciacliente', 'cliente')
- ));
- $this->set('groups', $this->User->Group->find('list', $groupOptions));
- $this->set('enterprises', $this->User->Enterprise->find('list'));
- $this->set('departments', $this->User->Department->find('list'));
- $this->set('positions', $this->User->Position->find('list'));
- }
- /**
- * edit method
- *
- * @throws NotFoundException
- * @param string $id
- * @return void
- */
- public function edit($id = null) {
- if (!$this->User->exists($id)) {
- throw new NotFoundException(__('Usuario inválido'));
- }
- if ($this->request->is(array('post', 'put'))) {
- $this->User->id = $id;
- $this->request->data['User']['status'] = isset($this->request->data['User']['status']) ? 1 : 0;
- $this->request->data['User']['birthdate'] = $this->Date->completeDateDDMM($this->request->data['User']['birthdate']);
- $this->request->data['User']['partner_id'] = $this->User->checkPartner($this->request->data['User']['partner_id']);
- if ($this->request->data['User']['password'] === '' && $this->request->data['User']['password_confirmation'] === '') {
- $this->User->modifyValidation();
- }
- if ($this->request->data['User']['partner_id'] !== null &&
- $this->User->save($this->request->data) &&
- $this->User->savePartner($this->User->id, $this->request->data['User']['partner_id'])) {
- $this->Session->setFlash(__('El usuario ha sido editado.'), 'alert', array('class' => 'alert-success'));
- return $this->redirect(array('action' => 'index'));
- } else {
- $this->set('groups', $this->User->Group->find('list'));
- $this->Session->setFlash(__('El usuario no ha podido ser salvado. Por favor, intente de nuevo.'), 'alert', array('class' => 'alert-danger'));
- }
- } else {
- $this->User->recursive = 0;
- $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
- $this->request->data = $this->User->find('first', $options);
- }
- $this->request->data['User']['birthdate'] = $this->Date->convertToDDMM($this->request->data['User']['birthdate']);
- $this->request->data['User']['password'] = ''; //Se limpia el campo del password por seguridad
- $partnerCondition = array(
- 'conditions' => array(
- 'NOT' => array(
- 'User.id' => $id,
- 'User.is_client' => true,
- ),
- 'OR' => array(
- 'User.partner_id IS NULL',
- 'User.partner_id' => $this->request->data['User']['id']
- )
- ),
- 'fields' => array(
- 'User.id',
- 'User.username'
- )
- );
- $this->set('partners', $this->User->find('list', $partnerCondition));
- $this->set('groups', $this->User->Group->find('list'));
- $this->set('departments', $this->User->Department->find('list'));
- $this->set('positions', $this->User->Position->find('list'));
- }
- /**
- * Editar Perfil
- * @return [type] [description]
- */
- public function editProfile($id = null) {
- if (!$this->User->exists($id)) {
- throw new NotFoundException(__('Usuario inválido'));
- }
- if ($this->request->is(array('post', 'put'))) {
- $this->User->id = $id;
- $this->request->data['User']['birthdate'] = $this->Date->completeDateDDMM($this->request->data['User']['birthdate']);
- if ($this->request->data['User']['password'] === '' && $this->request->data['User']['password_confirmation'] === '') {
- $this->User->modifyValidation();
- }
- $this->User->validator()->remove('language');
- $this->User->validator()->remove('group_id');
- if ($this->User->save($this->request->data)) {
- $this->Session->setFlash(__('Su perfil ha sido editado.'), 'alert', array('class' => 'alert-success'));
- return $this->redirect(array('controller'=> 'home', 'action' => 'index'));
- } else {
- debug($this->User->validationErrors);
- $this->Session->setFlash(__('El usuario no ha podido ser salvado. Por favor, intente de nuevo.'), 'alert', array('class' => 'alert-danger'));
- }
- } else {
- $this->User->recursive = 0;
- $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
- $this->request->data = $this->User->find('first', $options);
- }
- $this->request->data['User']['birthdate'] = $this->Date->convertToDDMM($this->request->data['User']['birthdate']);
- $this->request->data['User']['password'] = ''; //Se limpia el campo del password por seguridad
- }
- /**
- * edit method
- *
- * @throws NotFoundException
- * @param string $id
- * @return void
- */
- public function editClient($id = null) {
- if (!$this->User->exists($id)) {
- throw new NotFoundException(__('Usuario inválido'));
- }
- if ($this->request->is(array('post', 'put'))) {
- $this->User->id = $id;
- $this->request->data['User']['status'] = isset($this->request->data['User']['status']) ? 1 : 0;
- $this->request->data['User']['birthdate'] = $this->Date->completeDateDDMM($this->request->data['User']['birthdate']);
- if ($this->request->data['User']['password'] === '' && $this->request->data['User']['password_confirmation'] === '') {
- $this->User->modifyValidation();
- }
- if ($this->User->save($this->request->data)) {
- $this->Session->setFlash(__('El usuario ha sido editado.'), 'alert', array('class' => 'alert-success'));
- return $this->redirect(array('action' => 'index'));
- } else {
- $this->set('groups', $this->User->Group->find('list'));
- $this->Session->setFlash(__('El usuario no ha podido ser salvado. Por favor, intente de nuevo.'), 'alert', array('class' => 'alert-danger'));
- }
- } else {
- $this->User->recursive = 0;
- $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
- $this->request->data = $this->User->find('first', $options);
- }
- $this->request->data['User']['birthdate'] = $this->Date->convertToDDMM($this->request->data['User']['birthdate']);
- $this->request->data['User']['password'] = ''; //Se limpia el campo del password por seguridad
- $groupOptions = array('conditions' => array(
- 'alias' => array('gerenciacliente', 'cliente')
- ));
- $this->set('groups', $this->User->Group->find('list', $groupOptions));
- $this->set('departments', $this->User->Department->find('list'));
- $this->set('positions', $this->User->Position->find('list'));
- $this->set('enterprises', $this->User->Enterprise->find('list'));
- }
- /**
- * delete method
- *
- * @throws NotFoundException
- * @param string $id
- * @return void
- */
- public function delete() {
- $this->User->id = $this->request->data['User']['id'];
- if (!$this->User->exists()) {
- throw new NotFoundException(__('Usuario inválido'));
- }
- try {
- if ($this->request->is('post')) {
- if ($this->User->savePartner($this->User->id, '')) {
- $this->User->id = $this->request->data['User']['id'];
- if ($this->User->delete()) {
- $this->Session->setFlash(__('El usuario ha sido eliminado satisfactoriamente.'), 'alert', array('class' => 'alert-success'));
- return $this->redirect(array('action' => 'index'));
- }
- }
- $this->Session->setFlash(__('El usuario no pudo ser eliminado'), 'alert', array('class' => 'alert-danger'));
- return $this->redirect(array('action' => 'index'));
- }
- } catch(Exception $e) {
- $this->Session->setFlash(__('El usuario no pudo ser eliminado, revise no tener relaciones'), 'alert', array('class' => 'alert-danger'));
- return $this->redirect(array('action' => 'index'));
- }
- }
- /**
- * [start description]
- * @return [type] [description]
- */
- public function start() {
- if (!$this->Auth->loggedIn()) {
- $this->AclUtility->acoUpdate();
- if ($this->User->createAdminUser()) {
- //También inicial los Acos
- //Da todos los privilegios
- $this->Acl->allow($this->User->Group, 'controllers');
- $this->loadModel('Group');
- $this->Group->createDefaultGroups();
- $this->loadModel('Position');
- $this->Position->createDefaultPositions();
- $this->loadModel('Department');
- $this->Department->createDefaultDepartments();
- $this->Session->setFlash(__('El usuario administrador ha sido creado, username: administrador, pass: p4$$w0rd.'), 'alert', array('class' => 'alert-success'));
- } else {
- $this->Session->setFlash(__('El usuario administrador ya existe.'), 'alert', array('class' => 'alert-danger'));
- }
- }
- $this->layout = 'login';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement