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