PageRenderTime 61ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/Controller/UsersController.php

https://github.com/Wargo/reddevil
PHP | 838 lines | 589 code | 91 blank | 158 comment | 131 complexity | 196fa880e6a9a4dda47fcaaec49f54a0 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. class UsersController extends AppController {
  3. /**
  4. * name property
  5. *
  6. * @var string 'Users'
  7. * @access public
  8. */
  9. public $name = 'Users';
  10. /**
  11. * postActions property
  12. *
  13. * @var array
  14. * @access public
  15. */
  16. public $postActions = array(
  17. 'admin_delete',
  18. 'admin_sudo',
  19. );
  20. var $paginate = array(
  21. 'limit'=>25,
  22. 'order'=>array('User.created'=>'DESC')
  23. );
  24. /**
  25. * beforeFilter method -
  26. *
  27. * Set the black hole to prevent white-screen-of-death symptoms for invalid form submissions.
  28. *
  29. * @access public
  30. * @return void
  31. */
  32. public function beforeFilter() {
  33. parent::beforeFilter();
  34. if(in_array($this->params['action'], array('edit', 'admin_edit', 'register', 'reset_password', 'admin_index', 'register'))) {
  35. $this->Security->validatePost = false;
  36. }
  37. $this->set('authFields', $this->Auth->fields);
  38. $this->Auth->allow(
  39. 'confirm',
  40. 'forgotten_password',
  41. 'index',
  42. 'login',
  43. 'logout',
  44. 'profile',
  45. 'register',
  46. 'register_popup',
  47. 'reset_password',
  48. 'switch_language'
  49. );
  50. $this->Auth->autoRedirect = false;
  51. if (isset($this->Security)) {
  52. $this->Security->blackHoleCallback = '_blackHole';
  53. }
  54. }
  55. /**
  56. * beforeRender method
  57. *
  58. * @return void
  59. * @access public
  60. */
  61. public function beforeRender() {
  62. if (!empty($this->request->data['User'])) {
  63. unset($this->request->data['User'][$this->Auth->fields['password']]);
  64. unset($this->request->data['User']['confirm']);
  65. unset($this->request->data['User']['current_password']);
  66. }
  67. return parent::beforeRender();
  68. }
  69. function admin_index() {
  70. /*
  71. * 'alomojó' convendría mover esta decisión al beforeRender de esta clase
  72. */
  73. if (!$this->Session->read('dir_comercial')) {
  74. $this->redirect('/');
  75. }
  76. if(!isset($this->params['named']['group'])){
  77. if (empty($this->request->data)) {
  78. $this->redirect(array('group' => 'api'));
  79. } else {
  80. $this->request->data['User']['group'] = 'api';
  81. }
  82. }
  83. switch($this->params['named']['group']){
  84. case 'api': $this->categoria_admin=MENU_ADMIN_APIS;
  85. $this->User->bindModel(array('hasOne'=>array('Api'=>array('className'=>'Api','foreignKey'=>'id'))));
  86. break;
  87. case 'callcenter': $this->categoria_admin=MENU_ADMIN_PROPIETARIOS;
  88. break;
  89. case 'delegado': $this->categoria_admin=MENU_ADMIN_DELEGADOS;
  90. break;
  91. case 'admin':
  92. if ($this->Session->read('nivel')<1) {
  93. $this->redirect(array('group' => 'api'));
  94. }
  95. default:
  96. $this->categoria_admin=MENU_ADMIN_ADMINISTRADORES;
  97. break;
  98. }
  99. $conditions = $this->_parseSearch();
  100. if (isset($this->params['named']['homologado']) && $this->params['named']['homologado'] != '') {
  101. $this->loadModel('Api');
  102. $_conditions = array('homologado' => $this->params['named']['homologado']);
  103. $user_ids = array_keys($this->Api->find('list', array('conditions' => $_conditions)));
  104. $conditions['User.id'] = $user_ids;
  105. $this->request->data['User']['homologado'] = $this->params['named']['homologado'];
  106. }
  107. $users = $this->paginate($conditions);
  108. $this->set(compact('users'));
  109. }
  110. function admin_view($id = null) {
  111. if (!$id) {
  112. $this->_message(__('Invalid user'), array('action' => 'index'), null, true);
  113. }
  114. $this->set('user', $this->User->read(null, $id));
  115. }
  116. function admin_editar($id = null) {
  117. if (!$this->Session->read('dir_comercial')) {
  118. $this->redirect('/');
  119. }
  120. if (!$id && !empty($this->request->data['User']['id'])) {
  121. $id = $this->request->data['User']['id'];
  122. }
  123. $this->_acceso($id);
  124. if (!empty($this->request->data)) {
  125. $welcomeMail=false;
  126. if ($id) {
  127. $this->User->id = $id;
  128. } else {
  129. if($this->request->data['User']['group']=='admin' && ($this->Session->read('nivel')<1)){
  130. $this->request->data['User']['group'] = 'api';
  131. }
  132. $this->User->create();
  133. if($this->request->data['User']['group']=='api'){
  134. $welcomeMail=true;
  135. }
  136. }
  137. if($welcomeMail){
  138. $this->User->actAs=array('MiUsers.UserAccount' => array(
  139. 'sendEmails' => array(
  140. 'welcome' => array('subject' => __('Tu cuenta de comercializador ha sido registrada')),
  141. 'accountChange' => false
  142. )
  143. ));
  144. }
  145. if ($this->User->save($this->request->data)) {
  146. if (!$id) {
  147. $id = $this->User->id;
  148. $registro_api['Api']['id']=$id;
  149. $registro_api['Api']['nombre']=$this->request->data['User']['first_name'].' '.$this->request->data['User']['last_name'];
  150. $this->loadModel('Api');
  151. $this->Api->create();
  152. $this->Api->save($registro_api);
  153. }
  154. if (empty($id)) {
  155. $id = $this->User->id;
  156. }
  157. if ($this->request->data['User']['group'] == 'admin') {
  158. $controller = 'administradores';
  159. } else if ($this->request->data['User']['group'] == 'admin') {
  160. $controller = 'administradores';
  161. } else {
  162. $controller = Inflector::pluralize($this->request->data['User']['group']);
  163. }
  164. $this->_message(__('Usuario guardado'), array('controller' => $controller, 'action' => 'editar', $id ), null, true);
  165. } else {
  166. $this->_message(__('No se pudo guardar el usuario. Por favor, vuelve a intentarlo.'), false, null, true);
  167. }
  168. }
  169. if (empty($this->request->data) && $id) {
  170. $this->request->data = $this->User->read(null, $id);
  171. }
  172. $this->set(compact('id'));
  173. $this->_setSelects();
  174. }
  175. function admin_delete($id = null) {
  176. if (!$id) {
  177. $this->_message(__('Usuario incorrecto'), array('action' => 'index'), null, true);
  178. }
  179. if ($this->User->delete($id)) {
  180. $this->_message(__('Usuario eliminado'), array('action' => 'index'), null, true);
  181. }
  182. $this->_message(__('No se pudo eliminar el usuario'), array('action' => 'index'), null, true);
  183. }
  184. function admin_cambiar_contrasena($id = null) {
  185. if (!$id) {
  186. $this->_message(__('Usuario incorrecto', true), array('action'=>'index'), null, true);
  187. }
  188. $user = $this->User->read(null, $id);
  189. if (!empty($this->request->data)) {
  190. if ($user) {
  191. list($return, $message) = $this->User->changePassword($this->request->data, $user);
  192. $this->_message($message, false, null, true);
  193. } else {
  194. $this->_message(__('Usuario incorrecto'), false, null, true);
  195. }
  196. }
  197. $this->set(compact('id', 'user'));
  198. }
  199. function propietario_cambiar_contrasena($id = null) {
  200. if (!$id || ($id != $this->Auth->user('id'))) {
  201. $this->_message(__('Invalid id for user'), false, null, true);
  202. }
  203. $user = $this->User->read(null, $id);
  204. if (!empty($this->request->data)) {
  205. if ($user) {
  206. list($return, $message) = $this->User->changePassword($this->request->data, $user);
  207. $this->_message($message, $this->referer(), null, true);
  208. } else {
  209. $this->_message(__('Usuario incorrecto'), false, null, true);
  210. }
  211. }
  212. $this->set(compact('id', 'user'));
  213. }
  214. function api_cambiar_contrasena($id = null) {
  215. $this->propietario_cambiar_contrasena($id);
  216. }
  217. function delegado_cambiar_contrasena($id = null) {
  218. $this->propietario_cambiar_contrasena($id);
  219. }
  220. /*
  221. * Funciones para modificar los datos del propio usuario logueado
  222. *
  223. */
  224. public function admin_mi_cuenta() {
  225. $this->request->data = array();
  226. $this->request->data['User'] = $this->Auth->user();
  227. $this->loadModel('Administrador');
  228. $this->request->data = array_merge($this->request->data, $this->Administrador->read(null, $this->Auth->user('id')));
  229. }
  230. public function admin_modificar() {
  231. $this->_modificar();
  232. }
  233. protected function _modificar() {
  234. if (!empty($this->request->data)) {
  235. $this->User->id = $this->Auth->user('id');
  236. if ($this->User->save($this->request->data)) {
  237. $this->Session->write('Auth', $this->User->read(null, $this->Auth->user('id')));
  238. $this->_message(__('Usuario guardado'), false, null , false);
  239. } else {
  240. $this->_message(__('Error al guardar el usuario'), false, null, true);
  241. }
  242. }
  243. $this->redirect($this->referer());
  244. }
  245. /**
  246. * change_password method
  247. *
  248. * Used for changing the password of a logged in user
  249. *
  250. * @return void
  251. * @access public
  252. */
  253. public function change_password() {
  254. if ($this->request->data) {
  255. list($return, $message) = $this->User->changePassword($this->request->data, $this->Auth->user());
  256. if ($message) {
  257. $this->_message($message, false, null, true);
  258. }
  259. if ($return) {
  260. return $this->redirect('/');
  261. }
  262. }
  263. }
  264. /**
  265. * delete_account method
  266. *
  267. * Desactiva la cuenta del propio usuario
  268. *
  269. * @return void
  270. * @access public
  271. */
  272. public function delete_account() {
  273. if (!empty($this->request->data)) {
  274. $password = $this->User->field('password', array('id' => $this->Auth->user('id')));
  275. if (empty($this->request->data['User']['confirm_delete'])) {
  276. $this->_message(__('Debes confirmar que deseas desactivar la cuenta'), false, null, true);
  277. } elseif ($password != $this->request->data['User']['password']) {
  278. $this->_message(__('La contraseña introducida es incorrecta'), false, null, true);
  279. } else {
  280. $this->User->Behaviors->detach('UserAccount');
  281. $this->User->id = $this->Auth->user('id');
  282. if ($this->User->save(array('active' => 0))) {
  283. $this->SwissArmy->loadComponent('Cookie');
  284. $this->Cookie->delete('User');
  285. $this->Session->destroy();
  286. $this->_message(__('Tu cuenta ha sido desactivada'));
  287. } else {
  288. $this->_message(__('Se produjo un error al desactivar la cuenta'), false, null, true);
  289. }
  290. }
  291. }
  292. }
  293. /**
  294. * confirm method
  295. *
  296. * @param mixed $token
  297. * @return void
  298. * @access public
  299. */
  300. public function confirm($token = null, $id = null) {
  301. /*
  302. $this->set('token', $token);
  303. $fields = $this->User->accountFields();
  304. $this->set('fields', $fields);
  305. if (!$this->request->data) {
  306. return;
  307. }
  308. */
  309. $email = $this->User->field('email', array('id' => $id));
  310. $this->request->data['User']['email'] = $email;
  311. $this->request->data['User']['token'] = $token;
  312. list($return, $message) = $this->User->confirmAccount($this->request->data);
  313. if ($message) {
  314. $this->_message($message, false, null, true);
  315. }
  316. if ($return) {
  317. ClassRegistry::init('User')->create_relations($email, $id);
  318. $this->Session->write('Auth.redirect', '/'); // Prevent auth from sending you back here
  319. return $this->redirect('/');
  320. }
  321. }
  322. /**
  323. * edit method
  324. *
  325. * @return void
  326. * @access public
  327. */
  328. public function edit() {
  329. if ($this->request->data) {
  330. $user_id = $this->Auth->user('id');
  331. $this->request->data['Profile']['user_id'] = $user_id;
  332. $error = ($this->request->data['Profile']['id'] != ClassRegistry::init('Profile')->field('id', compact('user_id')));
  333. $this->loadModel('Profile');
  334. if (!$error && $this->Profile->save($this->request->data)) {
  335. $this->_message(__('Perfil actualizado'), false, null, true);
  336. return $this->_back();
  337. } else {
  338. $this->_message(__('Errores en el formulario'), false, null, true);
  339. }
  340. } else {
  341. $user_id = $this->Auth->user('id');
  342. $conditions = array('user_id' => $user_id);
  343. $this->request->data = ClassRegistry::init('Profile')->find('first', compact('conditions'));
  344. }
  345. $this->_setSelects();
  346. }
  347. /**
  348. * forgotten_password method
  349. *
  350. * Send the user an email with a confirmation link/token in it. Use the $email (which could be an email or a username)
  351. * to find the users id. Don't send another email if there is one that is pending
  352. *
  353. * @access public
  354. * @return void
  355. */
  356. public function forgotten_password($email = false) {
  357. $this->layout = 'admin_login';
  358. if ($this->request->data) {
  359. $email = $this->request->data['User']['email'];
  360. if (!$email) {
  361. $this->_message(__('Falta el email'), false, null, true);
  362. return;
  363. }
  364. list($return, $message) = $this->User->forgottenPassword($this->request->data['User']['email']);
  365. if ($message) {
  366. $this->_message($message, false, null, true);
  367. }
  368. if ($return) {
  369. $this->redirect(array('action' => 'reset_password'));
  370. }
  371. }
  372. }
  373. /**
  374. * index method
  375. *
  376. * @return void
  377. * @access public
  378. */
  379. public function index() {
  380. return $this->redirect('/', 301);
  381. }
  382. /**
  383. * login method
  384. *
  385. * Only run if there is no user
  386. *
  387. * @access public
  388. * @return void
  389. */
  390. public function login() {
  391. if (!empty($this->request->data['User']['slug'])) {
  392. $redirect = array('controller' => 'videos', 'action' => 'view', $this->request->data['User']['slug']);
  393. } else {
  394. $redirect = array('controller' => 'videos', 'action' => 'home');
  395. }
  396. if ($this->request->is('post')) {
  397. if (!$this->Auth->login()) {
  398. //Mirar si el usuario está en la tabla de la BD de NATS
  399. $this->loadModel('NatsMember');
  400. $this->NatsMember->importMembers();
  401. }
  402. if ($this->Auth->login()) {
  403. $this->User->id = $this->Auth->user('id');
  404. $this->loadModel('NatsMember');
  405. if (!$this->NatsMember->checkActive($this->Auth->user())) {
  406. //@TODO Quitar caducidad
  407. $this->Session->write('Auth.User.caducidad', 0);
  408. $this->User->save(array('caducidad' => 0));
  409. } else {
  410. if ($caducidad = $this->NatsMember->updateSubscription($this->Auth->user())) {
  411. $this->Session->write('Auth.User.caducidad', $caducidad);
  412. }
  413. }
  414. $user_group=$this->Auth->user('group');
  415. if (!empty($this->request->data['User']['remember_me'])) {
  416. $token = $this->User->token(null, array('length' => 100, 'fields' => array(
  417. $this->Auth->fields['username'], $this->Auth->fields['password']
  418. )));
  419. $this->SwissArmy->loadComponent('Cookie');
  420. $this->Cookie->write('User.id', $this->User->id, true, '+2 weeks');
  421. $this->Cookie->write('User.token', $token, true, '+2 weeks');
  422. }
  423. $display = $this->User->display();
  424. $this->_message(__('Bienvenido de nuevo %1$s.', $display), false, null);
  425. if ($this->RequestHandler->isAjax() && !empty($this->params['refresh'])) {
  426. return $this->redirect($redirect);
  427. }
  428. return $this->redirect($redirect);
  429. } else {
  430. $this->Session->setFlash(__('Email o contraseña incorrectos'));
  431. }
  432. } elseif ($this->Auth->user('id')) {
  433. $user_group=$this->Auth->user('group');
  434. return $this->redirect($redirect);
  435. }
  436. //return $this->redirect(array('controller' => 'users', 'action' => 'register'));
  437. if ($this->Session->check('NatsCode')) {
  438. $NatsCode = $this->Session->read('NatsCode');
  439. } else {
  440. $NatsCode = Configure::read('NatsCode');
  441. }
  442. return $this->redirect('http://tour.reddevilx.com/signup/signup.php?nats='.$NatsCode.'&step=2');
  443. /* if (Configure::read()) {
  444. $this->Session->setFlash('Debug only message: Save some tedium - check remember me.');
  445. }*/
  446. }
  447. /**
  448. * logout method
  449. *
  450. * Delete the users cookie (if any), log them out, and send them a parting flash meassage. If no user is logged in just
  451. * send them back to where they came from (no reference to the session refer).
  452. *
  453. * @access public
  454. * @return void
  455. */
  456. public function logout() {
  457. $this->_logout();
  458. if ($this->Auth->user()) {
  459. $this->_message(__('¡Hasta pronto!'), false, null, true);
  460. }
  461. //$this->redirect($this->Auth->logout());
  462. $this->redirect('/');
  463. }
  464. protected function _logout() {
  465. $this->Session->destroy();
  466. if ($this->Auth->user()) {
  467. $this->SwissArmy->loadComponent('Cookie');
  468. $this->Cookie->delete('User');
  469. $this->Cookie->delete();
  470. }
  471. }
  472. /**
  473. * profile method
  474. *
  475. * @param mixed $username
  476. * @access public
  477. * @return void
  478. */
  479. public function profile() {
  480. if (!$this->Auth->user('id')) {
  481. $this->redirect('/');
  482. }
  483. $user = $this->User->findByid($this->Auth->user('id'));
  484. $this->set(compact('user'));
  485. $this->render('/Elements/Users/payment');
  486. }
  487. /**
  488. * register method
  489. *
  490. * @access public
  491. * @return void
  492. */
  493. public function register($slug = null) {
  494. $override = false;
  495. if ($this->request->data) {
  496. $this->_logout();
  497. if (!empty($this->request->data['User']['slug'])) {
  498. $slug = $this->request->data['User']['slug'];
  499. $loginRedirect = array('controller' => 'videos', 'action' => 'view_video', $slug);
  500. } else {
  501. $loginRedirect = array('controller' => 'videos', 'action' => 'home');
  502. }
  503. $this->request->data['User']['group'] = 'user';
  504. $this->request->data['User']['active'] = 1;
  505. if (!empty($this->request->data['User']['email'])) {
  506. if (empty($this->request->data['User']['option'])) {
  507. list($return, $message) = $this->User->register($this->request->data);
  508. $this->Auth->login();
  509. if ($return) {
  510. $this->redirect($loginRedirect);
  511. }
  512. } else {
  513. if ($this->Session->check('NatsCode')) {
  514. $NatsCode = $this->Session->read('NatsCode');
  515. } else {
  516. $NatsCode = Configure::read('NatsCode');
  517. }
  518. $this->redirect('http://tour.reddevilx.com/signup/signup.php?nats='.$NatsCode.'&step=2');
  519. }
  520. }
  521. }
  522. $this->set('slug', $slug);
  523. $this->set('passwordPolicy', $this->User->passwordPolicy());
  524. if ($this->request->isAjax()) {
  525. $this->render('/Elements/Users/register');
  526. }
  527. }
  528. public function register_popup($slug = null) {
  529. if ($slug) {
  530. $this->loadModel('Video');
  531. extract($this->Video->findBySlug($slug));
  532. $id = $Video['id'];
  533. } else {
  534. $id = null;
  535. }
  536. $this->set(compact('slug', 'id'));
  537. $this->render('/Elements/payment_popup');
  538. }
  539. public function renew() {
  540. if (!$this->Auth->user('id')) {
  541. $this->redirect('/');
  542. }
  543. if (!empty($this->request->data)) {
  544. $this->Session->write('payment', $this->request->data['User']['payment']);
  545. $this->redirect(array('action' => 'payment'));
  546. }
  547. }
  548. public function logout_payment() {
  549. if ($this->Session->check('NatsCode')) {
  550. $NatsCode = $this->Session->read('NatsCode');
  551. } else {
  552. $NatsCode = Configure::read('NatsCode');
  553. }
  554. $this->_logout();
  555. $this->redirect('http://tour.reddevilx.com/signup/signup.php?nats='.$NatsCode.'&step=2');
  556. }
  557. public function payment($video_id = false) {
  558. die;
  559. //@TODO Hacer aquí el pago con tarjeta
  560. if (!$this->Auth->user('id')) {
  561. $this->redirect('/');
  562. }
  563. $payment = $this->Session->read('payment');
  564. $tCaducidad = strtotime($this->User->field('caducidad', array('id' => $this->Auth->user('id'))));
  565. $now = time();
  566. if ($tCaducidad < $now) {
  567. $tCaducidad = $now;
  568. }
  569. switch ($payment) {
  570. case 'day': $caducidad = strftime('%Y-%m-%d %H:%M:%S', strtotime('+1 day', $tCaducidad)); break;
  571. case 'month': $caducidad = strftime('%Y-%m-%d %H:%M:%S', strtotime('+1 month', $tCaducidad)); break;
  572. case '3month': $caducidad = strftime('%Y-%m-%d %H:%M:%S', strtotime('+3 months', $tCaducidad)); break;
  573. case 'year': $caducidad = strftime('%Y-%m-%d %H:%M:%S', strtotime('+1 year', $tCaducidad)); break;
  574. }
  575. $this->User->id = $this->Auth->user('id');
  576. $this->User->save(compact('caducidad'));
  577. if ($video_id) {
  578. $this->loadModel('Video');
  579. $video_id = $this->Video->field('slug', array('id' => $video_id));
  580. }
  581. $this->set(compact('video_id'));
  582. }
  583. /**
  584. * reset_password method
  585. *
  586. * Used to set a new password after requesting a reset via the forgotten password method
  587. *
  588. * @param string $token
  589. * @access public
  590. * @return void
  591. */
  592. public function reset_password($token = null) {
  593. $this->set('token', $token);
  594. $loggedInUser = $this->User->id = $this->Auth->user('id');
  595. if ($loggedInUser) {
  596. $this->redirect(array('action' => 'change_password'));
  597. }
  598. $this->set('fields', $this->User->Behaviors->UserAccount->settings['User']['fields']);
  599. if (!$this->request->data) {
  600. return $this->render('confirm');
  601. }
  602. list($return, $message) = $this->User->resetPassword($this->request->data);
  603. if ($message) {
  604. $this->_message($message, false, null, true);
  605. }
  606. if ($return) {
  607. $this->Session->write('Auth.redirect', '/'); // Prevent auth from sending you back here
  608. return $this->redirect(array('action' => 'login'));
  609. }
  610. $view = 'confirm';
  611. if ($this->request->data) {
  612. if (empty($this->User->validationErrors[$this->Auth->fields['username']]) &&
  613. empty($this->User->validationErrors['token'])) {
  614. $view = 'reset_password';
  615. }
  616. }
  617. $this->render($view);
  618. }
  619. /**
  620. * postLogin method
  621. *
  622. * Called automatically when a user logs in normally, or by cookie
  623. *
  624. * @param array $userData array()
  625. * @param mixed $mode 'form' or 'cookie'
  626. * @return void
  627. * @access public
  628. */
  629. public function postLogin($userData = array(), $mode = null) {
  630. static $run;
  631. if($run) {
  632. return;
  633. }
  634. $run = true;
  635. $this->User->id = $id = $this->Auth->user('id');
  636. $display = $this->User->display();
  637. /* ... */
  638. }
  639. /**
  640. * isAuthorized method
  641. *
  642. * Allow logged in users to edit their profile and change their password
  643. *
  644. * @return bool
  645. * @access public
  646. */
  647. public function isAuthorized() {
  648. if (in_array($this->action, array('edit', 'change_password'))) {
  649. return true;
  650. }
  651. return parent::isAuthorized();
  652. }
  653. /**
  654. * setSelects method
  655. *
  656. * @param bool $restrictToData true
  657. * @return void
  658. * @access protected
  659. */
  660. protected function _setSelects($restrictToData = true) {
  661. $groups = $this->User->groups;
  662. if ($this->Session->read('nivel') < 1) {
  663. unset($groups['admin']);
  664. }
  665. $this->set(compact('groups'));
  666. }
  667. function admin_desactivar($id=0){
  668. $volver=$this->referer();
  669. if($id!=0):
  670. $this->User->id=$id;
  671. if($this->User->exists()){
  672. $this->User->saveField('active',0);
  673. $this->_message('Registro desactivado: %s', $volver,$id);
  674. }
  675. else{
  676. $this->_message('ERROR: el registro %s no existe', $volver,$id,true);
  677. }
  678. else:
  679. if(!empty($this->request->data['seleccionado'])){
  680. $afectados='';
  681. foreach($this->request->data['seleccionado'] as $seleccionado_id=>$seleccionado){
  682. if($seleccionado==1){
  683. $this->User->id=$seleccionado_id;
  684. if($this->User->exists()){
  685. $this->User->saveField('active',0);
  686. $afectados.=' '.$seleccionado_id;
  687. }
  688. }
  689. }
  690. if(empty($afectados)){
  691. $this->_message('ERROR: no se ha desactivado ningun registro', $volver,null,true);
  692. }
  693. else{
  694. $this->_message('Registros desactivados:%s', $volver,$afectados);
  695. }
  696. }
  697. else{
  698. $this->_message('ERROR: el registro 0 no existe', $volver,null,true);
  699. }
  700. endif;
  701. }
  702. function admin_activar($id=0){
  703. $volver=$this->referer();
  704. if($id!=0):
  705. $this->User->id=$id;
  706. if($this->User->exists()){
  707. $this->User->saveField('active',1);
  708. $this->_message('Registro activado: %s', $volver,$id);
  709. }
  710. else{
  711. $this->_message('ERROR: el registro %s no existe', $volver,$id,true);
  712. }
  713. else:
  714. if(!empty($this->request->data['seleccionado'])){
  715. $afectados='';
  716. foreach($this->request->data['seleccionado'] as $seleccionado_id=>$seleccionado){
  717. if($seleccionado==1){
  718. $this->User->id=$seleccionado_id;
  719. if($this->User->exists()){
  720. $this->User->saveField('active',1);
  721. $afectados.=' '.$seleccionado_id;
  722. }
  723. }
  724. }
  725. if(empty($afectados)){
  726. $this->_message('ERROR: no se ha activado ningun registro', $volver,null,true);
  727. }
  728. else{
  729. $this->_message('Registros activados:%s', $volver,$afectados);
  730. }
  731. }
  732. else{
  733. $this->_message('ERROR: el registro 0 no existe', $volver,null,true);
  734. }
  735. endif;
  736. }
  737. protected function _acceso($id) {
  738. if (empty($id)) {
  739. return true;
  740. }
  741. $grupo = $this->User->field('group', compact('id'));
  742. if ($grupo == 'admin') {
  743. return $this->Session->read('nivel') > 0;
  744. }
  745. return true;
  746. }
  747. }