usuario.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. <?php
  2. use oxusmedia\webApp\webApp;
  3. use oxusmedia\webApp\controller;
  4. use oxusmedia\webApp\grid;
  5. use oxusmedia\webApp\form;
  6. use oxusmedia\webApp\column;
  7. use oxusmedia\webApp\input;
  8. use oxusmedia\webApp\hidden;
  9. use oxusmedia\webApp\password;
  10. use oxusmedia\webApp\select;
  11. use oxusmedia\webApp\gridActionButton;
  12. use oxusmedia\webApp\notificacion;
  13. class usuario extends controller
  14. {
  15. public function index()
  16. {
  17. $this->webApp()->requireLoginRedir();
  18. $this->titulo = 'Usuarios';
  19. $grid = $this->configGrid();
  20. $this->render("index", array(
  21. 'grid' => $grid
  22. ));
  23. }
  24. private function configGrid() : grid
  25. {
  26. $grid = new grid('usuarios');
  27. $grid
  28. ->setJsonUrl($this->getMethodUrl('data'))
  29. ->setUniqueIdFields('id')
  30. ->setColModel(array(
  31. array(
  32. 'name' => $this->webApp()->getConfig('LOGIN_WITH_EMAIL') ? 'email' : 'usuario',
  33. 'width' => 200,
  34. 'format' => grid::FMT_STRING
  35. ),
  36. array(
  37. 'name' => 'nombre',
  38. 'width' => 200,
  39. 'format' => grid::FMT_STRING
  40. ),
  41. array(
  42. 'name' => 'role',
  43. 'width' => 150,
  44. 'format' => grid::FMT_SELECT,
  45. 'formatoptions' => array('value' => $this->getRoleDescription())
  46. ),
  47. array(
  48. 'name' => 'ultimoLogin',
  49. 'label' => 'Última sesión',
  50. 'format' => grid::FMT_DATETIME
  51. )
  52. ))
  53. ->setDefaultSortName('usuario')
  54. ->setDefaultSortOrder('asc')
  55. ->setActions(array(
  56. new gridActionButton(gridActionButton::ADD, $this->getMethodUrl('add')),
  57. new gridActionButton(gridActionButton::EDIT, $this->getMethodUrl('edit')),
  58. new gridActionButton(gridActionButton::MULTI_DELETE, $this->getMethodUrl('delete'))
  59. ));
  60. return $grid;
  61. }
  62. private function getRoleDescription($role = null)
  63. {
  64. $arr = array(
  65. webApp::ROLE_ADMIN => 'Administrador',
  66. webApp::ROLE_EDITOR => 'Editor',
  67. webApp::ROLE_USER => 'Usuario'
  68. );
  69. if ($role == null)
  70. return $arr;
  71. elseif (isset($arr[$role]))
  72. return $arr[$role];
  73. return false;
  74. }
  75. public function add()
  76. {
  77. $this->webApp()->requireLogin();
  78. $form = new form('usuarioForm', array(
  79. new column(array(
  80. ($this->webApp()->getConfig('LOGIN_WITH_EMAIL')
  81. ? new input('email', array(
  82. 'rules' => array(
  83. 'required' => true,
  84. 'email' => true
  85. )
  86. ))
  87. : new input('usuario', array(
  88. 'rules' => array(
  89. 'required' => true
  90. )
  91. ))
  92. ),
  93. new password('pass', array(
  94. 'label' => 'Contraseña',
  95. 'rules' => array(
  96. 'required' => true
  97. )
  98. )),
  99. new input('nombre', array(
  100. 'rules' => array(
  101. 'required' => true
  102. )
  103. )),
  104. new select('role', $this->getRoleDescription())
  105. ))
  106. ), array(
  107. 'action' => $this->getMethodUrl('add'),
  108. 'ajax' => true,
  109. 'gridId' => "usuarios"
  110. ));
  111. if (isset($_POST['usuarioForm'])) {
  112. $form->setAtributes($_POST['usuarioForm']);
  113. if ($form->validate()) {
  114. $param = $form->getAtributes();
  115. $param['pass'] = md5($param['pass']);
  116. $this->db()->insert('usuarios', $param);
  117. $this->returnJson(array(
  118. 'error' => 0
  119. ));
  120. }
  121. } else {
  122. echo $form->render();
  123. }
  124. }
  125. public function edit()
  126. {
  127. $this->webApp()->requireLogin();
  128. $usuario = $this->db()->queryRow('SELECT id, usuario, email, nombre, role FROM usuarios WHERE id = :id', array(
  129. 'id' => isset($_POST['usuario']['id']) ? $_POST['usuario']['id'] : $_POST['id']
  130. ));
  131. if ($usuario) {
  132. $form = new form('usuario', array(
  133. new column(array(
  134. new hidden('id'),
  135. ($this->webApp()->getConfig('LOGIN_WITH_EMAIL')
  136. ? new input('email', array(
  137. 'rules' => array(
  138. 'required' => true,
  139. 'email' => true
  140. )
  141. ))
  142. : new input('usuario', array(
  143. 'rules' => array(
  144. 'required' => true
  145. )
  146. ))
  147. ),
  148. new password('pass', array(
  149. 'label' => 'Contraseña',
  150. 'htmlOptions' => array(
  151. 'placeholder' => 'dejar vacío para no cambiar la contraseña'
  152. )
  153. )),
  154. new input('nombre', array(
  155. 'rules' => array(
  156. 'required' => true
  157. )
  158. )),
  159. new select('role', $this->getRoleDescription(), $usuario->usuario == 'admin' ? array('htmlOptions' => array('disabled' => 'disabled')) : null)
  160. ))
  161. ), array(
  162. 'action' => $this->getMethodUrl('edit'),
  163. 'ajax' => true,
  164. 'gridId' => "usuarios"
  165. ));
  166. if (isset($_POST['usuario'])) {
  167. $form->setAtributes($_POST['usuario']);
  168. if ($form->validate()) {
  169. $param = $form->getAtributes();
  170. if (!empty($param['pass']))
  171. $param['pass'] = md5($param['pass']);
  172. else
  173. unset($param['pass']);
  174. $this->db()->update('usuarios', $param,
  175. array(
  176. 'id' => $param['id']
  177. )
  178. );
  179. $this->returnJson(array(
  180. 'error' => 0
  181. ));
  182. }
  183. } else {
  184. $form->setAtributes($usuario);
  185. $this->render('edit', array(
  186. 'form' => $form,
  187. 'dispositivos' => $this->getDispositivos($usuario->id)
  188. ));
  189. }
  190. }
  191. }
  192. public function delete()
  193. {
  194. $this->webApp()->requireLogin();
  195. if (isset($_POST['id'])) {
  196. $db = $this->db();
  197. $usuario = $db->queryRow('SELECT * FROM usuarios WHERE id IN(:ids) AND usuario = :admin', array(
  198. 'ids' => implode(',', $_POST['id']),
  199. 'admin' => "admin"
  200. ));
  201. if (!$usuario) {
  202. $db->query('DELETE FROM usuarios_credenciales WHERE usuario_id IN(:ids)', array(
  203. 'ids' => implode(',', $_POST['id'])
  204. ));
  205. $db->query('DELETE FROM usuarios WHERE id IN(:ids)', array(
  206. 'ids' => implode(',', $_POST['id'])
  207. ));
  208. $this->returnJson(array(
  209. 'error' => 0
  210. ));
  211. } else {
  212. $this->returnJson(array(
  213. 'error' => 1,
  214. 'mensaje' => 'No se permite eliminar el usuario admin.'
  215. ));
  216. }
  217. }
  218. }
  219. public function data()
  220. {
  221. $this->webApp()->requireLogin();
  222. $grid = $this->configGrid();
  223. $grid->renderData($this->db(), "SELECT * FROM usuarios");
  224. }
  225. public function miperfil()
  226. {
  227. $this->webApp()->requireLoginRedir();
  228. $this->titulo = 'Mi perfil';
  229. $form = new form('usuario', array(
  230. new column(array(
  231. ($this->webApp()->getConfig('LOGIN_WITH_EMAIL')
  232. ? new input('email', array(
  233. 'rules' => array(
  234. 'required' => true,
  235. 'email' => true
  236. )
  237. ))
  238. : new input('usuario', array(
  239. 'rules' => array(
  240. 'required' => true
  241. )
  242. ))
  243. ),
  244. new password('pass', array(
  245. 'label' => 'Contraseña',
  246. 'htmlOptions' => array(
  247. 'placeholder' => 'dejar vacío para no cambiar la contraseña'
  248. )
  249. )),
  250. new input('nombre', array(
  251. 'rules' => array(
  252. 'required' => true
  253. )
  254. )),
  255. new select('theme', array(
  256. webApp::THEME_LIGHT => 'Claro',
  257. webApp::THEME_DARKLY => 'Oscuro'
  258. ), array(
  259. 'label' => 'Tema'
  260. ))
  261. ))
  262. ));
  263. if (isset($_POST['usuario'])) {
  264. $form->setAtributes($_POST['usuario']);
  265. if ($form->validate()) {
  266. $param = $form->getAtributes();
  267. if (!empty($param['pass']))
  268. $param['pass'] = md5($param['pass']);
  269. else
  270. unset($param['pass']);
  271. $this->db()->update('usuarios', $param,
  272. array(
  273. 'id' => $this->webApp()->getUsuarioId()
  274. )
  275. );
  276. $this->webApp()->setTheme($param['theme']);
  277. $this->notify('Sus datos se actualizaron correctamente', notificacion::SUCCESS);
  278. }
  279. } else {
  280. $usuario = $this->db()->queryRow('SELECT email, nombre, theme FROM usuarios WHERE id = :id', array(
  281. 'id' => $this->webApp()->getUsuarioId()
  282. ));
  283. $form->setAtributes($usuario);
  284. }
  285. $this->render("miperfil", array(
  286. 'form' => $form
  287. ));
  288. }
  289. public function dispositivos()
  290. {
  291. $this->webApp()->requireLoginRedir();
  292. $this->titulo = 'Mis dispositivos';
  293. if (isset($_GET['id'])) {
  294. $this->db()->query('DELETE FROM usuarios_credenciales WHERE id = :id AND usuario_id = :usuario_id', array(
  295. 'id' => $_GET['id'],
  296. 'usuario_id' => $this->webApp()->getUsuarioId()
  297. ));
  298. $this->notify('Dispositivo eliminado correctamente', notificacion::SUCCESS);
  299. $this->redirect($this->getMethodUrl('dispositivos'));
  300. } else {
  301. $this->addJs($this->webApp()->getUrlAssets() . 'webapp/js/jquery.blockUI.js');
  302. $this->render("dispositivos", array(
  303. 'dispositivos' => $this->getDispositivos($this->webApp()->getUsuarioId())
  304. ));
  305. }
  306. }
  307. private function getDispositivos($id)
  308. {
  309. return $this->db()->query('SELECT * FROM usuarios_credenciales WHERE usuario_id = :id', array(
  310. 'id' => $id
  311. ));
  312. }
  313. public function theme()
  314. {
  315. $this->webApp()->requireLoginRedir();
  316. if (isset($_GET['id'])) {
  317. if ($this->webApp()->setTheme($_GET['id'])) {
  318. $this->db()->update('usuarios',
  319. array(
  320. 'theme' => $_GET['id']
  321. ),
  322. array(
  323. 'id' => $this->webApp()->getUsuarioId()
  324. )
  325. );
  326. }
  327. $this->redirect($_SERVER['HTTP_REFERER']);
  328. }
  329. }
  330. }