modUser.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2005-2009 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \defgroup user Module user management
  21. * \brief Module pour gerer les utilisateurs
  22. * \file htdocs/core/modules/modUser.class.php
  23. * \ingroup user
  24. * \brief Description and activation file for the module users
  25. */
  26. include_once DOL_DOCUMENT_ROOT.'/core/modules/DolibarrModules.class.php';
  27. /**
  28. * Class to describe and enable module User
  29. */
  30. class modUser extends DolibarrModules
  31. {
  32. /**
  33. * Constructor. Define names, constants, directories, boxes, permissions
  34. *
  35. * @param DoliDB $db Database handler
  36. */
  37. public function __construct($db)
  38. {
  39. global $conf;
  40. $this->db = $db;
  41. $this->numero = 0;
  42. $this->family = "hr"; // Family for module (or "base" if core module)
  43. $this->module_position = '05';
  44. // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
  45. $this->name = preg_replace('/^mod/i', '', get_class($this));
  46. $this->description = "Management of users and groups of users (mandatory)";
  47. // Possible values for version are: 'development', 'experimental', 'dolibarr' or version
  48. $this->version = 'dolibarr';
  49. $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
  50. $this->picto = 'group';
  51. // Data directories to create when module is enabled
  52. $this->dirs = array("/users/temp");
  53. // Config pages
  54. $this->config_page_url = array("user.php");
  55. // Dependencies
  56. $this->hidden = false; // A condition to hide module
  57. $this->depends = array(); // List of module class names as string that must be enabled if this module is enabled
  58. $this->requiredby = array(); // List of module ids to disable if this one is disabled
  59. $this->conflictwith = array(); // List of module class names as string this module is in conflict with
  60. $this->phpmin = array(5, 6); // Minimum version of PHP required by module
  61. $this->langfiles = array("main", "users", "companies", "members", "salaries", "hrm");
  62. $this->always_enabled = true; // Can't be disabled
  63. // Constants
  64. $this->const = array();
  65. // Boxes
  66. $this->boxes = array(
  67. 0=>array('file'=>'box_lastlogin.php', 'enabledbydefaulton'=>'Home'),
  68. 1=>array('file'=>'box_birthdays.php', 'enabledbydefaulton'=>'Home'),
  69. 2=>array('file'=>'box_dolibarr_state_board.php', 'enabledbydefaulton'=>'Home')
  70. );
  71. // Permissions
  72. $this->rights = array();
  73. $this->rights_class = 'user';
  74. $this->rights_admin_allowed = 1; // Admin is always granted of permission (even when module is disabled)
  75. $r = 0;
  76. $r++;
  77. $this->rights[$r][0] = 251;
  78. $this->rights[$r][1] = 'Read information of other users, groups and permissions';
  79. $this->rights[$r][2] = 'r';
  80. $this->rights[$r][3] = 0;
  81. $this->rights[$r][4] = 'user';
  82. $this->rights[$r][5] = 'lire';
  83. $r++;
  84. $this->rights[$r][0] = 252;
  85. $this->rights[$r][1] = 'Read permissions of other users';
  86. $this->rights[$r][2] = 'r';
  87. $this->rights[$r][3] = 0;
  88. $this->rights[$r][4] = 'user_advance'; // Visible if option MAIN_USE_ADVANCED_PERMS is on
  89. $this->rights[$r][5] = 'readperms';
  90. $r++;
  91. $this->rights[$r][0] = 253;
  92. $this->rights[$r][1] = 'Create/modify internal and external users, groups and permissions';
  93. $this->rights[$r][2] = 'w';
  94. $this->rights[$r][3] = 0;
  95. $this->rights[$r][4] = 'user';
  96. $this->rights[$r][5] = 'creer';
  97. $r++;
  98. $this->rights[$r][0] = 254;
  99. $this->rights[$r][1] = 'Create/modify external users only';
  100. $this->rights[$r][2] = 'w';
  101. $this->rights[$r][3] = 0;
  102. $this->rights[$r][4] = 'user_advance'; // Visible if option MAIN_USE_ADVANCED_PERMS is on
  103. $this->rights[$r][5] = 'write';
  104. $r++;
  105. $this->rights[$r][0] = 255;
  106. $this->rights[$r][1] = 'Modify the password of other users';
  107. $this->rights[$r][2] = 'w';
  108. $this->rights[$r][3] = 0;
  109. $this->rights[$r][4] = 'user';
  110. $this->rights[$r][5] = 'password';
  111. $r++;
  112. $this->rights[$r][0] = 256;
  113. $this->rights[$r][1] = 'Delete or disable other users';
  114. $this->rights[$r][2] = 'd';
  115. $this->rights[$r][3] = 0;
  116. $this->rights[$r][4] = 'user';
  117. $this->rights[$r][5] = 'supprimer';
  118. $r++;
  119. $this->rights[$r][0] = 341;
  120. $this->rights[$r][1] = 'Read its own permissions';
  121. $this->rights[$r][2] = 'r';
  122. $this->rights[$r][3] = 0;
  123. $this->rights[$r][4] = 'self_advance'; // Visible if option MAIN_USE_ADVANCED_PERMS is on
  124. $this->rights[$r][5] = 'readperms';
  125. $r++;
  126. $this->rights[$r][0] = 342;
  127. $this->rights[$r][1] = 'Create/modify of its own user';
  128. $this->rights[$r][2] = 'w';
  129. $this->rights[$r][3] = 0;
  130. $this->rights[$r][4] = 'self';
  131. $this->rights[$r][5] = 'creer';
  132. $r++;
  133. $this->rights[$r][0] = 343;
  134. $this->rights[$r][1] = 'Modify its own password';
  135. $this->rights[$r][2] = 'w';
  136. $this->rights[$r][3] = 0;
  137. $this->rights[$r][4] = 'self';
  138. $this->rights[$r][5] = 'password';
  139. $r++;
  140. $this->rights[$r][0] = 344;
  141. $this->rights[$r][1] = 'Modify its own permissions';
  142. $this->rights[$r][2] = 'w';
  143. $this->rights[$r][3] = 0;
  144. $this->rights[$r][4] = 'self_advance'; // Visible if option MAIN_USE_ADVANCED_PERMS is on
  145. $this->rights[$r][5] = 'writeperms';
  146. $r++;
  147. $this->rights[$r][0] = 351;
  148. $this->rights[$r][1] = 'Read groups';
  149. $this->rights[$r][2] = 'r';
  150. $this->rights[$r][3] = 0;
  151. $this->rights[$r][4] = 'group_advance'; // Visible if option MAIN_USE_ADVANCED_PERMS is on
  152. $this->rights[$r][5] = 'read';
  153. $r++;
  154. $this->rights[$r][0] = 352;
  155. $this->rights[$r][1] = 'Read permissions of groups';
  156. $this->rights[$r][2] = 'r';
  157. $this->rights[$r][3] = 0;
  158. $this->rights[$r][4] = 'group_advance'; // Visible if option MAIN_USE_ADVANCED_PERMS is on
  159. $this->rights[$r][5] = 'readperms';
  160. $r++;
  161. $this->rights[$r][0] = 353;
  162. $this->rights[$r][1] = 'Create/modify groups and permissions';
  163. $this->rights[$r][2] = 'w';
  164. $this->rights[$r][3] = 0;
  165. $this->rights[$r][4] = 'group_advance'; // Visible if option MAIN_USE_ADVANCED_PERMS is on
  166. $this->rights[$r][5] = 'write';
  167. $r++;
  168. $this->rights[$r][0] = 354;
  169. $this->rights[$r][1] = 'Delete groups';
  170. $this->rights[$r][2] = 'd';
  171. $this->rights[$r][3] = 0;
  172. $this->rights[$r][4] = 'group_advance'; // Visible if option MAIN_USE_ADVANCED_PERMS is on
  173. $this->rights[$r][5] = 'delete';
  174. $r++;
  175. $this->rights[$r][0] = 358;
  176. $this->rights[$r][1] = 'Export all users';
  177. $this->rights[$r][2] = 'r';
  178. $this->rights[$r][3] = 0;
  179. $this->rights[$r][4] = 'user';
  180. $this->rights[$r][5] = 'export';
  181. // Menus
  182. $this->menu = 1; // This module add menu entries. They are coded into menu manager.
  183. // Exports
  184. $r = 0;
  185. $r++;
  186. $this->export_code[$r] = $this->rights_class.'_'.$r;
  187. $this->export_label[$r] = 'List of users and attributes';
  188. $this->export_permission[$r] = array(array("user", "user", "export"));
  189. $this->export_fields_array[$r] = array(
  190. 'u.rowid'=>"Id", 'u.login'=>"Login", 'u.lastname'=>"Lastname", 'u.firstname'=>"Firstname", 'u.employee'=>"Employee", 'u.job'=>"PostOrFunction", 'u.gender'=>"Gender",
  191. 'u.accountancy_code'=>"UserAccountancyCode",
  192. 'u.address'=>"Address", 'u.zip'=>"Zip", 'u.town'=>"Town",
  193. 'u.office_phone'=>'Phone', 'u.user_mobile'=>"Mobile", 'u.office_fax'=>'Fax',
  194. 'u.email'=>"Email", 'u.note'=>"Note", 'u.signature'=>'Signature',
  195. 'u.fk_user'=>'HierarchicalResponsible', 'u.thm'=>'THM', 'u.tjm'=>'TJM', 'u.weeklyhours'=>'WeeklyHours',
  196. 'u.dateemployment'=>'DateEmploymentStart', 'u.dateemploymentend'=>'DateEmploymentEnd', 'u.salary'=>'Salary', 'u.color'=>'Color', 'u.api_key'=>'ApiKey',
  197. 'u.birth'=>'DateOfBirth',
  198. 'u.datec'=>"DateCreation", 'u.tms'=>"DateLastModification",
  199. 'u.admin'=>"Administrator", 'u.statut'=>'Status', 'u.datelastlogin'=>'LastConnexion', 'u.datepreviouslogin'=>'PreviousConnexion',
  200. 'u.fk_socpeople'=>"IdContact", 'u.fk_soc'=>"IdCompany",
  201. 'u.fk_member'=>"MemberId",
  202. "a.firstname"=>"MemberFirstname",
  203. "a.lastname"=>"MemberLastname",
  204. 'g.nom'=>"Group"
  205. );
  206. $this->export_TypeFields_array[$r] = array(
  207. 'u.rowid'=>'Numeric', 'u.login'=>"Text", 'u.lastname'=>"Text", 'u.firstname'=>"Text", 'u.employee'=>'Boolean', 'u.job'=>'Text',
  208. 'u.accountancy_code'=>'Text',
  209. 'u.address'=>"Text", 'u.zip'=>"Text", 'u.town'=>"Text",
  210. 'u.office_phone'=>'Text', 'u.user_mobile'=>'Text', 'u.office_fax'=>'Text',
  211. 'u.email'=>'Text', 'u.datec'=>"Date", 'u.tms'=>"Date", 'u.admin'=>"Boolean", 'u.statut'=>'Status', 'u.note'=>"Text", 'u.signature'=>"Text", 'u.datelastlogin'=>'Date',
  212. 'u.fk_user'=>"FormSelect:select_dolusers",
  213. 'u.birth'=>'Date',
  214. 'u.datepreviouslogin'=>'Date',
  215. 'u.fk_socpeople'=>'FormSelect:selectcontacts',
  216. 'u.fk_soc'=>"FormSelect:select_company",
  217. 'u.tjm'=>"Numeric", 'u.thm'=>"Numeric", 'u.fk_member'=>"Numeric",
  218. 'u.weeklyhours'=>"Numeric",
  219. 'u.dateemployment'=>"Date", 'u.dateemploymentend'=>"Date", 'u.salary'=>"Numeric",
  220. 'u.color'=>'Text', 'u.api_key'=>'Text',
  221. 'a.firstname'=>'Text',
  222. 'a.lastname'=>'Text',
  223. 'g.nom'=>"Text"
  224. );
  225. $this->export_entities_array[$r] = array(
  226. 'u.rowid'=>"user", 'u.login'=>"user", 'u.lastname'=>"user", 'u.firstname'=>"user", 'u.employee'=>'user', 'u.job'=>'user', 'u.gender'=>'user',
  227. 'u.accountancy_code'=>'user',
  228. 'u.address'=>"user", 'u.zip'=>"user", 'u.town'=>"user",
  229. 'u.office_phone'=>'user', 'u.user_mobile'=>'user', 'u.office_fax'=>'user',
  230. 'u.email'=>'user', 'u.note'=>"user", 'u.signature'=>'user',
  231. 'u.fk_user'=>'user', 'u.thm'=>'user', 'u.tjm'=>'user', 'u.weeklyhours'=>'user',
  232. 'u.dateemployment'=>'user', 'u.dateemploymentend'=>'user', 'u.salary'=>'user', 'u.color'=>'user', 'u.api_key'=>'user',
  233. 'u.birth'=>'user',
  234. 'u.datec'=>"user", 'u.tms'=>"user",
  235. 'u.admin'=>"user", 'u.statut'=>'user', 'u.datelastlogin'=>'user', 'u.datepreviouslogin'=>'user',
  236. 'u.fk_socpeople'=>"contact", 'u.fk_soc'=>"company", 'u.fk_member'=>"member",
  237. 'a.firstname'=>"member", 'a.lastname'=>"member",
  238. 'g.nom'=>"Group"
  239. );
  240. $keyforselect = 'user';
  241. $keyforelement = 'user';
  242. $keyforaliasextra = 'extra';
  243. include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
  244. if (empty($conf->adherent->enabled)) {
  245. unset($this->export_fields_array[$r]['u.fk_member']);
  246. unset($this->export_entities_array[$r]['u.fk_member']);
  247. }
  248. $this->export_sql_start[$r] = 'SELECT DISTINCT ';
  249. $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'user as u';
  250. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'user_extrafields as extra ON u.rowid = extra.fk_object';
  251. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'usergroup_user as ug ON u.rowid = ug.fk_user';
  252. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'usergroup as g ON ug.fk_usergroup = g.rowid';
  253. $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'adherent as a ON u.fk_member = a.rowid';
  254. $this->export_sql_end[$r] .= ' WHERE u.entity IN ('.getEntity('user').')';
  255. // Imports
  256. $r = 0;
  257. // Import list of users attributes
  258. $r++;
  259. $this->import_code[$r] = $this->rights_class.'_'.$r;
  260. $this->import_label[$r] = 'ImportDataset_user_1';
  261. $this->import_icon[$r] = 'user';
  262. $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
  263. $this->import_tables_array[$r] = array('u'=>MAIN_DB_PREFIX.'user', 'extra'=>MAIN_DB_PREFIX.'user_extrafields'); // List of tables to insert into (insert done in same order)
  264. $this->import_fields_array[$r] = array(
  265. 'u.login'=>"Login*", 'u.lastname'=>"Name*", 'u.firstname'=>"Firstname", 'u.employee'=>"Employee*", 'u.job'=>"PostOrFunction", 'u.gender'=>"Gender",
  266. 'u.accountancy_code'=>"UserAccountancyCode",
  267. 'u.pass_crypted'=>"Password", 'u.admin'=>"Administrator", 'u.fk_soc'=>"Company*", 'u.address'=>"Address", 'u.zip'=>"Zip", 'u.town'=>"Town",
  268. 'u.fk_state'=>"StateId", 'u.fk_country'=>"CountryCode",
  269. 'u.office_phone'=>"Phone", 'u.user_mobile'=>"Mobile", 'u.office_fax'=>"Fax",
  270. 'u.email'=>"Email", 'u.note'=>"Note", 'u.signature'=>'Signature',
  271. 'u.fk_user'=>'HierarchicalResponsible', 'u.thm'=>'THM', 'u.tjm'=>'TJM', 'u.weeklyhours'=>'WeeklyHours',
  272. 'u.dateemployment'=>'DateEmploymentStart', 'u.dateemploymentend'=>'DateEmploymentEnd', 'u.salary'=>'Salary', 'u.color'=>'Color', 'u.api_key'=>'ApiKey',
  273. 'u.birth'=>'DateOfBirth',
  274. 'u.datec'=>"DateCreation",
  275. 'u.statut'=>'Status'
  276. );
  277. // Add extra fields
  278. $sql = "SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE type <> 'separate' AND elementtype = 'user' AND entity IN (0,".$conf->entity.")";
  279. $resql = $this->db->query($sql);
  280. if ($resql) { // This can fail when class is used on old database (during migration for example)
  281. while ($obj = $this->db->fetch_object($resql)) {
  282. $fieldname = 'extra.'.$obj->name;
  283. $fieldlabel = ucfirst($obj->label);
  284. $this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
  285. }
  286. }
  287. // End add extra fields
  288. $this->import_fieldshidden_array[$r] = array('u.fk_user_creat'=>'user->id', 'extra.fk_object'=>'lastrowid-'.MAIN_DB_PREFIX.'user'); // aliastable.field => ('user->id' or 'lastrowid-'.tableparent)
  289. $this->import_convertvalue_array[$r] = array(
  290. 'u.fk_state'=>array('rule'=>'fetchidfromcodeid', 'classfile'=>'/core/class/cstate.class.php', 'class'=>'Cstate', 'method'=>'fetch', 'dict'=>'DictionaryState'),
  291. 'u.fk_country'=>array('rule'=>'fetchidfromcodeid', 'classfile'=>'/core/class/ccountry.class.php', 'class'=>'Ccountry', 'method'=>'fetch', 'dict'=>'DictionaryCountry'),
  292. 'u.salary'=>array('rule'=>'numeric')
  293. );
  294. //$this->import_convertvalue_array[$r]=array('s.fk_soc'=>array('rule'=>'lastrowid',table='t');
  295. $this->import_regex_array[$r] = array(
  296. 'u.employee'=>'^[0|1]',
  297. 'u.datec'=>'^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]( [0-9][0-9]:[0-9][0-9]:[0-9][0-9])?$',
  298. 'u.dateemployment'=>'^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$',
  299. 'u.birth'=>'^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$'
  300. );
  301. $this->import_examplevalues_array[$r] = array(
  302. 'u.lastname'=>"Doe", 'u.firstname'=>'John', 'u.login'=>'jdoe', 'u.employee'=>'0 or 1', 'u.job'=>'CTO', 'u.gender'=>'man or woman',
  303. 'u.pass_crypted'=>'Encrypted password',
  304. 'u.fk_soc'=>'0 (internal user) or company name (external user)', 'u.datec'=>dol_print_date(dol_now(), '%Y-%m-%d'), 'u.address'=>"61 jump street",
  305. 'u.zip'=>"123456", 'u.town'=>"Big town", 'u.fk_country'=>'US, FR, DE...', 'u.office_phone'=>"0101010101", 'u.office_fax'=>"0101010102",
  306. 'u.email'=>"test@mycompany.com", 'u.salary'=>"10000", 'u.note'=>"This is an example of note for record", 'u.datec'=>"2015-01-01 or 2015-01-01 12:30:00",
  307. 'u.statut'=>"0 (closed) or 1 (active)",
  308. );
  309. $this->import_updatekeys_array[$r] = array('u.lastname'=>'Lastname', 'u.firstname'=>'Firstname', 'u.login'=>'Login');
  310. }
  311. /**
  312. * Function called when module is enabled.
  313. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  314. * It also creates data directories
  315. *
  316. * @param string $options Options when enabling module ('', 'noboxes')
  317. * @return int 1 if OK, 0 if KO
  318. */
  319. public function init($options = '')
  320. {
  321. global $conf;
  322. // Permissions
  323. $this->remove($options);
  324. $sql = array();
  325. return $this->_init($sql, $options);
  326. }
  327. }