PageRenderTime 60ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/user/class/userdatabase.class.php

https://bitbucket.org/speedealing/speedealing
PHP | 588 lines | 377 code | 77 blank | 134 comment | 77 complexity | 35f1eed1d1819ad70c321bb7044ec2c7 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2012-2013 Regis Houssin <regis.houssin@capnetworks.com>
  3. * Copyright (C) 2012-2013 Herve Prot <herve.prot@symeos.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/user/class/usergroup.class.php
  20. * \brief File of class to manage user groups
  21. */
  22. require_once DOL_DOCUMENT_ROOT . '/core/class/nosqlDocument.class.php';
  23. require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php';
  24. require_once DOL_DOCUMENT_ROOT . '/core/db/couchdb/lib/couchAdmin.php';
  25. if (!empty($conf->ldap->enabled))
  26. require_once DOL_DOCUMENT_ROOT . '/core/class/ldap.class.php';
  27. /**
  28. * \class UserGroup
  29. * \brief Class to manage user groups
  30. */
  31. class UserDatabase extends nosqlDocument {
  32. public $element = 'usergroup';
  33. public $table_element = 'usergroup';
  34. protected $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  35. var $couchAdmin;
  36. var $couchdb;
  37. var $id; // Group id
  38. var $nom; // Name of group
  39. var $globalgroup; // Global group
  40. var $note; // Note on group
  41. var $datec; // Creation date of group
  42. var $datem; // Modification date of group
  43. var $members = array(); // Array of users
  44. var $membersRoles = array(); // Array of groups
  45. private $_tab_loaded = array(); // Array of cache of already loaded permissions
  46. /**
  47. * Constructor de la classe
  48. *
  49. * @param DoliDb $db Database handler
  50. */
  51. function __construct($db = '') {
  52. $this->db = $db;
  53. parent::__construct($db);
  54. $fk_extrafields = new ExtraFields($db);
  55. $this->fk_extrafields = $fk_extrafields->load("extrafields:" . get_class($this), true); // load and cache
  56. $this->couchAdmin = new couchAdmin($this->couchdb);
  57. return 0;
  58. }
  59. /**
  60. * Charge un objet group avec toutes ces caracteristiques (excpet ->members array)
  61. *
  62. * @param int $id name of the data to fetch
  63. * @return int <0 if KO, >0 if OK
  64. */
  65. function fetch($id) {
  66. global $conf, $langs;
  67. $this->couchdb->useDatabase($id);
  68. $this->values = $this->couchdb->getDatabaseInfos();
  69. $this->couchAdmin = new couchAdmin($this->couchdb);
  70. $members = $this->couchAdmin->getDatabaseReaderUsers();
  71. $membersRoles = $this->couchAdmin->getDatabaseReaderRoles();
  72. if (!empty($members)) {
  73. foreach ($members as $aRow) {
  74. try {
  75. $user = new User($this->db);
  76. $user->load("user:" . $aRow);
  77. } catch (Exception $e) {
  78. // User NOT FOUND
  79. $user->email = $aRow;
  80. $user->name = $aRow;
  81. $user->_id = "org.couchdb.user:" . $aRow;
  82. $user->Firstname = "Unknown";
  83. $user->Lastname = "Unknown";
  84. $user->Status = "DISABLE";
  85. }
  86. $this->members[] = clone $user;
  87. }
  88. }
  89. $group = new stdClass();
  90. if (!empty($membersRoles)) {
  91. foreach ($membersRoles as $aRow) {
  92. $group->id = $aRow;
  93. $this->membersRoles[] = clone $group;
  94. }
  95. }
  96. $membersAdmin = $this->couchAdmin->getDatabaseAdminUsers();
  97. //$membersRolesAdmin = $this->couchAdmin->getDatabaseAdminRoles();
  98. //$this->membersRoles = array_merge($this->membersRoles, $membersRolesAdmin);
  99. if (!empty($membersAdmin)) {
  100. foreach ($membersAdmin as $aRow) {
  101. try {
  102. $user = $this->couchAdmin->getUser($aRow);
  103. } catch (Exception $e) {
  104. // User NOT FOUND
  105. $user->email = $aRow;
  106. $user->name = $aRow;
  107. $user->_id = "org.couchdb.user:" . $aRow;
  108. $user->Firstname = "Unknown";
  109. $user->Lastname = "Unknown";
  110. $user->Status = "DISABLE";
  111. $user->admin = true;
  112. }
  113. $user->admin = true;
  114. $this->members[] = clone $user;
  115. }
  116. }
  117. /* foreach ($membersRolesAdmin as $aRow) {
  118. $group->Administrator = true;
  119. $group->id = $aRow;
  120. $this->membersRoles[] = clone $group;
  121. } */
  122. $this->id = $this->values->db_name;
  123. return 1;
  124. }
  125. /**
  126. * Return array of groups objects for a particular user
  127. *
  128. * @param int $userid User id to search
  129. * @return array Array of groups objects
  130. */
  131. function listGroupsForUser($userid) {
  132. global $conf, $user;
  133. $ret = array();
  134. $sql = "SELECT g.rowid, ug.entity as usergroup_entity";
  135. $sql.= " FROM " . MAIN_DB_PREFIX . "usergroup as g,";
  136. $sql.= " " . MAIN_DB_PREFIX . "usergroup_user as ug";
  137. $sql.= " WHERE ug.fk_usergroup = g.rowid";
  138. $sql.= " AND ug.fk_user = " . $userid;
  139. if (!empty($conf->multicompany->enabled) && $conf->entity == 1 && $user->admin && !$user->entity) {
  140. $sql.= " AND g.entity IS NOT NULL";
  141. } else {
  142. $sql.= " AND g.entity IN (0," . $conf->entity . ")";
  143. }
  144. $sql.= " ORDER BY g.nom";
  145. dol_syslog(get_class($this) . "::listGroupsForUser sql=" . $sql, LOG_DEBUG);
  146. $result = $this->db->query($sql);
  147. if ($result) {
  148. while ($obj = $this->db->fetch_object($result)) {
  149. $newgroup = new UserGroup($this->db);
  150. $newgroup->fetch($obj->rowid);
  151. $newgroup->usergroup_entity = $obj->usergroup_entity;
  152. $ret[] = $newgroup;
  153. }
  154. $this->db->free($result);
  155. return $ret;
  156. } else {
  157. $this->error = $this->db->lasterror();
  158. dol_syslog(get_class($this) . "::listGroupsForUser " . $this->error, LOG_ERR);
  159. return -1;
  160. }
  161. }
  162. /**
  163. * Return array of users id for group
  164. *
  165. * @return array of users
  166. */
  167. function listUsersForGroup() {
  168. global $conf, $user;
  169. $ret = array();
  170. $sql = "SELECT u.rowid, ug.entity as usergroup_entity";
  171. $sql.= " FROM " . MAIN_DB_PREFIX . "user as u,";
  172. $sql.= " " . MAIN_DB_PREFIX . "usergroup_user as ug";
  173. $sql.= " WHERE ug.fk_user = u.rowid";
  174. $sql.= " AND ug.fk_usergroup = " . $this->id;
  175. if (!empty($conf->multicompany->enabled) && $conf->entity == 1 && $user->admin && !$user->entity) {
  176. $sql.= " AND u.entity IS NOT NULL";
  177. } else {
  178. $sql.= " AND u.entity IN (0," . $conf->entity . ")";
  179. }
  180. dol_syslog(get_class($this) . "::listUsersForGroup sql=" . $sql, LOG_DEBUG);
  181. $result = $this->db->query($sql);
  182. if ($result) {
  183. while ($obj = $this->db->fetch_object($result)) {
  184. $newuser = new User($this->db);
  185. $newuser->fetch($obj->rowid);
  186. $newuser->usergroup_entity = $obj->usergroup_entity;
  187. $ret[] = $newuser;
  188. }
  189. $this->db->free($result);
  190. return $ret;
  191. } else {
  192. $this->error = $this->db->lasterror();
  193. dol_syslog(get_class($this) . "::listUsersForGroup " . $this->error, LOG_ERR);
  194. return -1;
  195. }
  196. }
  197. /**
  198. * Ajoute un droit a l'utilisateur
  199. *
  200. * @param int $rid id du droit a ajouter
  201. * @param string $allmodule Ajouter tous les droits du module allmodule
  202. * @param string $allperms Ajouter tous les droits du module allmodule, perms allperms
  203. * @return int > 0 if OK, < 0 if KO
  204. */
  205. function addrights($rid, $allmodule = '', $allperms = '') {
  206. global $conf;
  207. dol_syslog(get_class($this) . "::addrights $rid, $allmodule, $allperms");
  208. $err = 0;
  209. $whereforadd = '';
  210. $this->db->begin();
  211. if ($rid) {
  212. // Si on a demande ajout d'un droit en particulier, on recupere
  213. // les caracteristiques (module, perms et subperms) de ce droit.
  214. $sql = "SELECT module, perms, subperms";
  215. $sql.= " FROM " . MAIN_DB_PREFIX . "rights_def";
  216. $sql.= " WHERE id = '" . $rid . "'";
  217. $sql.= " AND entity = " . $conf->entity;
  218. $result = $this->db->query($sql);
  219. if ($result) {
  220. $obj = $this->db->fetch_object($result);
  221. $module = $obj->module;
  222. $perms = $obj->perms;
  223. $subperms = $obj->subperms;
  224. } else {
  225. $err++;
  226. dol_print_error($this->db);
  227. }
  228. // Where pour la liste des droits a ajouter
  229. $whereforadd = "id=" . $rid;
  230. // Ajout des droits induits
  231. if ($subperms)
  232. $whereforadd.=" OR (module='$module' AND perms='$perms' AND (subperms='lire' OR subperms='read'))";
  233. else if ($perms)
  234. $whereforadd.=" OR (module='$module' AND (perms='lire' OR perms='read') AND subperms IS NULL)";
  235. // Pour compatibilite, si lowid = 0, on est en mode ajout de tout
  236. // TODO A virer quand sera gere par l'appelant
  237. if (substr($rid, -1, 1) == 0)
  238. $whereforadd = "module='$module'";
  239. }
  240. else {
  241. // Where pour la liste des droits a ajouter
  242. if ($allmodule)
  243. $whereforadd = "module='$allmodule'";
  244. if ($allperms)
  245. $whereforadd = " AND perms='$allperms'";
  246. }
  247. // Ajout des droits de la liste whereforadd
  248. if ($whereforadd) {
  249. //print "$module-$perms-$subperms";
  250. $sql = "SELECT id";
  251. $sql.= " FROM " . MAIN_DB_PREFIX . "rights_def";
  252. $sql.= " WHERE $whereforadd";
  253. $sql.= " AND entity = " . $conf->entity;
  254. $result = $this->db->query($sql);
  255. if ($result) {
  256. $num = $this->db->num_rows($result);
  257. $i = 0;
  258. while ($i < $num) {
  259. $obj = $this->db->fetch_object($result);
  260. $nid = $obj->id;
  261. $sql = "DELETE FROM " . MAIN_DB_PREFIX . "usergroup_rights WHERE fk_usergroup = $this->id AND fk_id=" . $nid;
  262. if (!$this->db->query($sql))
  263. $err++;
  264. $sql = "INSERT INTO " . MAIN_DB_PREFIX . "usergroup_rights (fk_usergroup, fk_id) VALUES ($this->id, $nid)";
  265. if (!$this->db->query($sql))
  266. $err++;
  267. $i++;
  268. }
  269. }
  270. else {
  271. $err++;
  272. dol_print_error($this->db);
  273. }
  274. }
  275. if ($err) {
  276. $this->db->rollback();
  277. return -$err;
  278. } else {
  279. $this->db->commit();
  280. return 1;
  281. }
  282. }
  283. /**
  284. * Retire un droit a l'utilisateur
  285. *
  286. * @param int $rid id du droit a retirer
  287. * @param string $allmodule Retirer tous les droits du module allmodule
  288. * @param string $allperms Retirer tous les droits du module allmodule, perms allperms
  289. * @return int > 0 if OK, < 0 if OK
  290. */
  291. function delrights($rid, $allmodule = '', $allperms = '') {
  292. global $conf;
  293. $err = 0;
  294. $wherefordel = '';
  295. $this->db->begin();
  296. if ($rid) {
  297. // Si on a demande supression d'un droit en particulier, on recupere
  298. // les caracteristiques module, perms et subperms de ce droit.
  299. $sql = "SELECT module, perms, subperms";
  300. $sql.= " FROM " . MAIN_DB_PREFIX . "rights_def";
  301. $sql.= " WHERE id = '" . $rid . "'";
  302. $sql.= " AND entity = " . $conf->entity;
  303. $result = $this->db->query($sql);
  304. if ($result) {
  305. $obj = $this->db->fetch_object($result);
  306. $module = $obj->module;
  307. $perms = $obj->perms;
  308. $subperms = $obj->subperms;
  309. } else {
  310. $err++;
  311. dol_print_error($this->db);
  312. }
  313. // Where pour la liste des droits a supprimer
  314. $wherefordel = "id=" . $rid;
  315. // Suppression des droits induits
  316. if ($subperms == 'lire' || $subperms == 'read')
  317. $wherefordel.=" OR (module='$module' AND perms='$perms' AND subperms IS NOT NULL)";
  318. if ($perms == 'lire' || $perms == 'read')
  319. $wherefordel.=" OR (module='$module')";
  320. // Pour compatibilite, si lowid = 0, on est en mode suppression de tout
  321. // TODO A virer quand sera gere par l'appelant
  322. if (substr($rid, -1, 1) == 0)
  323. $wherefordel = "module='$module'";
  324. }
  325. else {
  326. // Where pour la liste des droits a supprimer
  327. if ($allmodule)
  328. $wherefordel = "module='$allmodule'";
  329. if ($allperms)
  330. $wherefordel = " AND perms='$allperms'";
  331. }
  332. // Suppression des droits de la liste wherefordel
  333. if ($wherefordel) {
  334. //print "$module-$perms-$subperms";
  335. $sql = "SELECT id";
  336. $sql.= " FROM " . MAIN_DB_PREFIX . "rights_def";
  337. $sql.= " WHERE $wherefordel";
  338. $sql.= " AND entity = " . $conf->entity;
  339. $result = $this->db->query($sql);
  340. if ($result) {
  341. $num = $this->db->num_rows($result);
  342. $i = 0;
  343. while ($i < $num) {
  344. $obj = $this->db->fetch_object($result);
  345. $nid = $obj->id;
  346. $sql = "DELETE FROM " . MAIN_DB_PREFIX . "usergroup_rights";
  347. $sql.= " WHERE fk_usergroup = $this->id AND fk_id=" . $nid;
  348. if (!$this->db->query($sql))
  349. $err++;
  350. $i++;
  351. }
  352. }
  353. else {
  354. $err++;
  355. dol_print_error($this->db);
  356. }
  357. }
  358. if ($err) {
  359. $this->db->rollback();
  360. return -$err;
  361. } else {
  362. $this->db->commit();
  363. return 1;
  364. }
  365. }
  366. /**
  367. * Charge dans l'objet group, la liste des permissions auquels le groupe a droit
  368. *
  369. * @param string $moduletag Name of module we want permissions ('' means all)
  370. * @return int <0 if KO, >0 if OK
  371. */
  372. function getrights($moduletag = '') {
  373. global $conf;
  374. if ($moduletag && isset($this->_tab_loaded[$moduletag]) && $this->_tab_loaded[$moduletag]) {
  375. // Le fichier de ce module est deja charge
  376. return;
  377. }
  378. if ($this->all_permissions_are_loaded) {
  379. // Si les permissions ont deja ete chargees, on quitte
  380. return;
  381. }
  382. /*
  383. * Recuperation des droits
  384. */
  385. $sql = "SELECT r.module, r.perms, r.subperms ";
  386. $sql.= " FROM " . MAIN_DB_PREFIX . "usergroup_rights as u, " . MAIN_DB_PREFIX . "rights_def as r";
  387. $sql.= " WHERE r.id = u.fk_id";
  388. $sql.= " AND r.entity = " . $conf->entity;
  389. $sql.= " AND u.fk_usergroup = " . $this->id;
  390. $sql.= " AND r.perms IS NOT NULL";
  391. if ($moduletag)
  392. $sql.= " AND r.module = '" . $this->db->escape($moduletag) . "'";
  393. dol_syslog(get_class($this) . '::getrights sql=' . $sql, LOG_DEBUG);
  394. $resql = $this->db->query($sql);
  395. if ($resql) {
  396. $num = $this->db->num_rows($resql);
  397. $i = 0;
  398. while ($i < $num) {
  399. $obj = $this->db->fetch_object($resql);
  400. $module = $obj->module;
  401. $perms = $obj->perms;
  402. $subperms = $obj->subperms;
  403. if ($perms) {
  404. if ($subperms) {
  405. $this->rights->$module->$perms->$subperms = 1;
  406. } else {
  407. $this->rights->$module->$perms = 1;
  408. }
  409. }
  410. $i++;
  411. }
  412. $this->db->free($resql);
  413. }
  414. if ($moduletag == '') {
  415. // Si module etait non defini, alors on a tout charge, on peut donc considerer
  416. // que les droits sont en cache (car tous charges) pour cet instance de group
  417. $this->all_permissions_are_loaded = 1;
  418. } else {
  419. // Si module defini, on le marque comme charge en cache
  420. $this->_tab_loaded[$moduletag] = 1;
  421. }
  422. return 1;
  423. }
  424. /**
  425. * Delete a database
  426. *
  427. * @return <0 if KO, > 0 if OK
  428. */
  429. function delete() {
  430. try {
  431. $this->couchdb->useDatabase($this->id);
  432. $this->couchdb->deleteDatabase();
  433. return 1;
  434. } catch (Exception $e) {
  435. dol_print_error('', $e->getMessage());
  436. return -1;
  437. }
  438. }
  439. /**
  440. * Compact a database
  441. *
  442. * @return <0 if KO, > 0 if OK
  443. */
  444. function compact() {
  445. try {
  446. $this->couchdb->useDatabase($this->id);
  447. $this->couchdb->compactDatabase();
  448. return 1;
  449. } catch (Exception $e) {
  450. dol_print_error('', $e->getMessage());
  451. return -1;
  452. }
  453. }
  454. /**
  455. * Compact views
  456. *
  457. * @return <0 if KO, > 0 if OK
  458. */
  459. function compactView() {
  460. try {
  461. $this->couchdb->useDatabase($this->id);
  462. $this->couchdb->compactAllViews();
  463. return 1;
  464. } catch (Exception $e) {
  465. dol_print_error('', $e->getMessage());
  466. return -1;
  467. }
  468. }
  469. /**
  470. * Purge deleted documents
  471. *
  472. * @return <0 if KO, > 0 if OK
  473. */
  474. function purgeDatabase() {
  475. try {
  476. $this->couchdb->useDatabase($this->id);
  477. $this->couchdb->purgeDatabase();
  478. return 1;
  479. } catch (Exception $e) {
  480. dol_print_error('', $e->getMessage());
  481. return -1;
  482. }
  483. }
  484. /**
  485. * Write data in memory to disk
  486. *
  487. * @return <0 if KO, > 0 if OK
  488. */
  489. function commit() {
  490. try {
  491. $this->couchdb->useDatabase($this->id);
  492. $this->couchdb->ensureFullCommit();
  493. return 1;
  494. } catch (Exception $e) {
  495. dol_print_error('', $e->getMessage());
  496. return -1;
  497. }
  498. }
  499. /**
  500. * Create a database
  501. *
  502. * @param int $notrigger 0=triggers enabled, 1=triggers disabled
  503. * @return int <0 if KO, >=0 if OK
  504. */
  505. function create() {
  506. $this->couchdb->useDatabase($this->id);
  507. $this->couchdb->createDatabase();
  508. return 1;
  509. }
  510. }
  511. ?>