Advertisement
Guest User

Untitled

a guest
Dec 19th, 2016
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.56 KB | None | 0 0
  1. <?php
  2. App::uses('AppController', 'Controller');
  3. /**
  4. * Users Controller
  5. *
  6. * @property User $User
  7. * @property PaginatorComponent $Paginator
  8. * @property AuthComponent $Auth
  9. * @property SessionComponent $Session
  10. */
  11. class UsersController extends AppController {
  12. /**
  13. * Configuración de Componentes de Autorización, Sesión y el Plugin de AutoLogin
  14. */
  15. public $components = array('ConvertingCase');
  16. public $helpers = array("Time");
  17. /**
  18. * [$uses description]
  19. * @var array
  20. */
  21. public $uses = array('LoginViewModel');
  22. /**
  23. * [beforeFilter description]
  24. * @return [type] [description]
  25. */
  26. public function beforeFilter() {
  27. parent::beforeFilter();
  28. if ($this->request->params['action'] == 'start') {
  29. $this->Auth->allow();
  30. }
  31. }
  32. /**
  33. * [beforeRender description]
  34. * @return [type] [description]
  35. */
  36. public function beforeRender() {
  37. parent::beforeRender();
  38. if (in_array($this->request->params['action'], array('add', 'edit', 'addClient', 'editClient'))) {
  39. $this->set('validations', json_encode($this->User->validate));
  40. if ($this->User->validationErrors) {
  41. $this->set('validationErrors', $this->User->validationErrors);
  42. }
  43. }
  44. }
  45. /**
  46. * index method
  47. *
  48. * @return void
  49. */
  50. public function index() {
  51. $this->User->recursive = 0;
  52. $this->set('users', $this->User->find('all'));
  53. }
  54. /**
  55. * Método que realiza la validación del usuario
  56. * y el password antes de iniciar la sesión en la aplicación
  57. *
  58. * @return void
  59. */
  60. public function login() {
  61. //Si el Usuario se encuentra ya Logeado en el sistema
  62. if ($this->Session->check('Auth.User')) {
  63. //Redirecciona al index del sistema
  64. return $this->redirect(array('controller' => 'home', 'action' => 'index'));
  65. }
  66. //Si Uso el form para logearse (POST)
  67. if ($this->request->is('post')) {
  68. //Convertir a minusculas el nombre de usuario ya que no se hacen distinciones entre minusculas y mayusculas
  69. $this->request->data['User']['username'] = $this->ConvertingCase->convertingLowerCase($this->request->data['User']['username']);
  70. //Si el Usuario Colocó un nombre de usuario y contraseña correcta
  71. if ($this->Auth->login()) {
  72. $this->Cookie->write('resnik_remember_me_cookie', $this->request->data, true, '2 weeks');
  73. return $this->redirect(array('controller' => 'home', 'action' => 'index'));
  74. } else {
  75. //Verificar si el usuario está deshabilitado
  76. if ($this->User->checkUserPasswordAndStatus($this->request->data)) {
  77. $this->Session->setFlash(__('Usuario deshabilitado por favor consulte al administrador'), 'alert', array('class' => 'alert-danger'));
  78. } else {
  79. //Asigna a la variable session que va luego a la vista el mensaje de error
  80. $this->Session->setFlash(__('Nombre de Usuario o contraseña incorrectos'), 'alert', array('class' => 'alert-danger'));
  81. }
  82. }
  83. }
  84. $this->set('validations', json_encode($this->LoginViewModel->validate)); //Validaciones del Usuario
  85. //Se Define el Layout de Login que no es el mismo del deafult
  86. $this->layout = 'login';
  87. }
  88. /**
  89. * Método que cierra la sesión del usuario activo
  90. *
  91. * @return void
  92. */
  93. public function logout() {
  94. //delete Cookie
  95. $this->Cookie->delete('resnik_remember_me_cookie');
  96. //Redirecciona a la página de Login Inicial
  97. return $this->redirect($this->Auth->logout());
  98. }
  99. /**
  100. * view method
  101. *
  102. * @throws NotFoundException
  103. * @param string $id
  104. * @return void
  105. */
  106. public function view($id = null) {
  107. if (!$this->User->exists($id)) {
  108. throw new NotFoundException(__('Usuario inválido'));
  109. }
  110. $this->User->recursive = 0;
  111. $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
  112. $user = $this->User->find('first', $options);
  113. if ($user['User']['partner_id'] != null) {
  114. $options = array(
  115. 'conditions' => array(
  116. 'User.' . $this->User->primaryKey => $user['User']['partner_id']
  117. ),
  118. 'fields' => array(
  119. 'username'
  120. ));
  121. $partnerUser = $this->User->find('first', $options);
  122. $user['User']['PartnerUser'] = $partnerUser['User'];
  123. }
  124. $this->set('user', $user);
  125. }
  126. /**
  127. * add method
  128. *
  129. * @return void
  130. */
  131. public function add() {
  132. if ($this->request->is('post')) {
  133. $this->User->create();
  134. $this->request->data['User']['is_client'] = 0;
  135. $this->request->data['User']['status'] = 1;
  136. $this->request->data['User']['username'] = $this->ConvertingCase->convertingLowerCase($this->request->data['User']['username']);
  137. $this->request->data['User']['birthdate'] = $this->Date->completeDateDDMM($this->request->data['User']['birthdate']);
  138. $this->request->data['User']['partner_id'] = $this->User->checkPartner($this->request->data['User']['partner_id']);
  139. if ($this->request->data['User']['partner_id'] !== null &&
  140. $this->User->save($this->request->data) &&
  141. $this->User->savePartner($this->User->id, $this->request->data['User']['partner_id'])) {
  142. $this->Session->setFlash(__('El usuario ha sido registrado.'), 'alert', array('class' => 'alert-success'));
  143. return $this->redirect(array('action' => 'index'));
  144. } else {
  145. $this->Session->setFlash(__('Ha ocurrido un problema registrando al usuario, por favor intente de nuevo.'), 'alert', array('class' => 'alert-danger'));
  146. }
  147. }
  148. //unset($this->User->validate['password']);
  149. $partnerCondition = array(
  150. 'conditions' => array(
  151. 'NOT' => array(
  152. 'User.is_client' => true,
  153. ),
  154. 'User.partner_id' => null,
  155. ),
  156. 'fields' => array(
  157. 'User.id',
  158. 'User.username'
  159. )
  160. );
  161. $groupConditions = array(
  162. 'conditions' => array(
  163. 'NOT' => array(
  164. 'Group.alias' => array(
  165. 'cliente',
  166. 'gerenciacliente'
  167. )
  168. )
  169. )
  170. );
  171. $this->set('partners', $this->User->find('list', $partnerCondition));
  172. $this->set('groups', $this->User->Group->find('list', $groupConditions));
  173. $this->set('departments', $this->User->Department->find('list'));
  174. $this->set('positions', $this->User->Position->find('list'));
  175. }
  176. /**
  177. * [addClient description]
  178. */
  179. public function addClient() {
  180. if ($this->request->is('post')) {
  181. $this->User->create();
  182. $this->request->data['User']['is_client'] = 1;
  183. $this->request->data['User']['status'] = 1;
  184. $this->request->data['User']['username'] = $this->ConvertingCase->convertingLowerCase($this->request->data['User']['username']);
  185. $this->request->data['User']['birthdate'] = $this->Date->completeDateDDMM($this->request->data['User']['birthdate']);
  186. if ($this->User->save($this->request->data)) {
  187. $this->Session->setFlash(__('El cliente ha sido registrado.'), 'alert', array('class' => 'alert-success'));
  188. return $this->redirect(array('action' => 'index'));
  189. } else {
  190. $this->Session->setFlash(__('Ha ocurrido un problema registrando al cleinte, por favor intente de nuevo.'), 'alert', array('class' => 'alert-danger'));
  191. }
  192. }
  193. $groupOptions = array('conditions' => array(
  194. 'alias' => array('gerenciacliente', 'cliente')
  195. ));
  196. $this->set('groups', $this->User->Group->find('list', $groupOptions));
  197. $this->set('enterprises', $this->User->Enterprise->find('list'));
  198. $this->set('departments', $this->User->Department->find('list'));
  199. $this->set('positions', $this->User->Position->find('list'));
  200. }
  201. /**
  202. * edit method
  203. *
  204. * @throws NotFoundException
  205. * @param string $id
  206. * @return void
  207. */
  208. public function edit($id = null) {
  209. if (!$this->User->exists($id)) {
  210. throw new NotFoundException(__('Usuario inválido'));
  211. }
  212. if ($this->request->is(array('post', 'put'))) {
  213. $this->User->id = $id;
  214. $this->request->data['User']['status'] = isset($this->request->data['User']['status']) ? 1 : 0;
  215. $this->request->data['User']['birthdate'] = $this->Date->completeDateDDMM($this->request->data['User']['birthdate']);
  216. $this->request->data['User']['partner_id'] = $this->User->checkPartner($this->request->data['User']['partner_id']);
  217. if ($this->request->data['User']['password'] === '' && $this->request->data['User']['password_confirmation'] === '') {
  218. $this->User->modifyValidation();
  219. }
  220. if ($this->request->data['User']['partner_id'] !== null &&
  221. $this->User->save($this->request->data) &&
  222. $this->User->savePartner($this->User->id, $this->request->data['User']['partner_id'])) {
  223. $this->Session->setFlash(__('El usuario ha sido editado.'), 'alert', array('class' => 'alert-success'));
  224. return $this->redirect(array('action' => 'index'));
  225. } else {
  226. $this->set('groups', $this->User->Group->find('list'));
  227. $this->Session->setFlash(__('El usuario no ha podido ser salvado. Por favor, intente de nuevo.'), 'alert', array('class' => 'alert-danger'));
  228. }
  229. } else {
  230. $this->User->recursive = 0;
  231. $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
  232. $this->request->data = $this->User->find('first', $options);
  233. }
  234. $this->request->data['User']['birthdate'] = $this->Date->convertToDDMM($this->request->data['User']['birthdate']);
  235. $this->request->data['User']['password'] = ''; //Se limpia el campo del password por seguridad
  236. $partnerCondition = array(
  237. 'conditions' => array(
  238. 'NOT' => array(
  239. 'User.id' => $id,
  240. 'User.is_client' => true,
  241. ),
  242. 'OR' => array(
  243. 'User.partner_id IS NULL',
  244. 'User.partner_id' => $this->request->data['User']['id']
  245. )
  246. ),
  247. 'fields' => array(
  248. 'User.id',
  249. 'User.username'
  250. )
  251. );
  252. $this->set('partners', $this->User->find('list', $partnerCondition));
  253. $this->set('groups', $this->User->Group->find('list'));
  254. $this->set('departments', $this->User->Department->find('list'));
  255. $this->set('positions', $this->User->Position->find('list'));
  256. }
  257. /**
  258. * Editar Perfil
  259. * @return [type] [description]
  260. */
  261. public function editProfile($id = null) {
  262. if (!$this->User->exists($id)) {
  263. throw new NotFoundException(__('Usuario inválido'));
  264. }
  265. if ($this->request->is(array('post', 'put'))) {
  266. $this->User->id = $id;
  267. $this->request->data['User']['birthdate'] = $this->Date->completeDateDDMM($this->request->data['User']['birthdate']);
  268. if ($this->request->data['User']['password'] === '' && $this->request->data['User']['password_confirmation'] === '') {
  269. $this->User->modifyValidation();
  270. }
  271. $this->User->validator()->remove('language');
  272. $this->User->validator()->remove('group_id');
  273. if ($this->User->save($this->request->data)) {
  274. $this->Session->setFlash(__('Su perfil ha sido editado.'), 'alert', array('class' => 'alert-success'));
  275. return $this->redirect(array('controller'=> 'home', 'action' => 'index'));
  276. } else {
  277. debug($this->User->validationErrors);
  278. $this->Session->setFlash(__('El usuario no ha podido ser salvado. Por favor, intente de nuevo.'), 'alert', array('class' => 'alert-danger'));
  279. }
  280. } else {
  281. $this->User->recursive = 0;
  282. $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
  283. $this->request->data = $this->User->find('first', $options);
  284. }
  285. $this->request->data['User']['birthdate'] = $this->Date->convertToDDMM($this->request->data['User']['birthdate']);
  286. $this->request->data['User']['password'] = ''; //Se limpia el campo del password por seguridad
  287. }
  288. /**
  289. * edit method
  290. *
  291. * @throws NotFoundException
  292. * @param string $id
  293. * @return void
  294. */
  295. public function editClient($id = null) {
  296. if (!$this->User->exists($id)) {
  297. throw new NotFoundException(__('Usuario inválido'));
  298. }
  299. if ($this->request->is(array('post', 'put'))) {
  300. $this->User->id = $id;
  301. $this->request->data['User']['status'] = isset($this->request->data['User']['status']) ? 1 : 0;
  302. $this->request->data['User']['birthdate'] = $this->Date->completeDateDDMM($this->request->data['User']['birthdate']);
  303. if ($this->request->data['User']['password'] === '' && $this->request->data['User']['password_confirmation'] === '') {
  304. $this->User->modifyValidation();
  305. }
  306. if ($this->User->save($this->request->data)) {
  307. $this->Session->setFlash(__('El usuario ha sido editado.'), 'alert', array('class' => 'alert-success'));
  308. return $this->redirect(array('action' => 'index'));
  309. } else {
  310. $this->set('groups', $this->User->Group->find('list'));
  311. $this->Session->setFlash(__('El usuario no ha podido ser salvado. Por favor, intente de nuevo.'), 'alert', array('class' => 'alert-danger'));
  312. }
  313. } else {
  314. $this->User->recursive = 0;
  315. $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
  316. $this->request->data = $this->User->find('first', $options);
  317. }
  318. $this->request->data['User']['birthdate'] = $this->Date->convertToDDMM($this->request->data['User']['birthdate']);
  319. $this->request->data['User']['password'] = ''; //Se limpia el campo del password por seguridad
  320. $groupOptions = array('conditions' => array(
  321. 'alias' => array('gerenciacliente', 'cliente')
  322. ));
  323. $this->set('groups', $this->User->Group->find('list', $groupOptions));
  324. $this->set('departments', $this->User->Department->find('list'));
  325. $this->set('positions', $this->User->Position->find('list'));
  326. $this->set('enterprises', $this->User->Enterprise->find('list'));
  327. }
  328. /**
  329. * delete method
  330. *
  331. * @throws NotFoundException
  332. * @param string $id
  333. * @return void
  334. */
  335. public function delete() {
  336. $this->User->id = $this->request->data['User']['id'];
  337. if (!$this->User->exists()) {
  338. throw new NotFoundException(__('Usuario inválido'));
  339. }
  340. try {
  341. if ($this->request->is('post')) {
  342. if ($this->User->savePartner($this->User->id, '')) {
  343. $this->User->id = $this->request->data['User']['id'];
  344. if ($this->User->delete()) {
  345. $this->Session->setFlash(__('El usuario ha sido eliminado satisfactoriamente.'), 'alert', array('class' => 'alert-success'));
  346. return $this->redirect(array('action' => 'index'));
  347. }
  348. }
  349. $this->Session->setFlash(__('El usuario no pudo ser eliminado'), 'alert', array('class' => 'alert-danger'));
  350. return $this->redirect(array('action' => 'index'));
  351. }
  352. } catch(Exception $e) {
  353. $this->Session->setFlash(__('El usuario no pudo ser eliminado, revise no tener relaciones'), 'alert', array('class' => 'alert-danger'));
  354. return $this->redirect(array('action' => 'index'));
  355. }
  356. }
  357. /**
  358. * [start description]
  359. * @return [type] [description]
  360. */
  361. public function start() {
  362. if (!$this->Auth->loggedIn()) {
  363. $this->AclUtility->acoUpdate();
  364. if ($this->User->createAdminUser()) {
  365. //También inicial los Acos
  366. //Da todos los privilegios
  367. $this->Acl->allow($this->User->Group, 'controllers');
  368. $this->loadModel('Group');
  369. $this->Group->createDefaultGroups();
  370. $this->loadModel('Position');
  371. $this->Position->createDefaultPositions();
  372. $this->loadModel('Department');
  373. $this->Department->createDefaultDepartments();
  374. $this->Session->setFlash(__('El usuario administrador ha sido creado, username: administrador, pass: p4$$w0rd.'), 'alert', array('class' => 'alert-success'));
  375. } else {
  376. $this->Session->setFlash(__('El usuario administrador ya existe.'), 'alert', array('class' => 'alert-danger'));
  377. }
  378. }
  379. $this->layout = 'login';
  380. }
  381. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement