modUser.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 <http://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 Fichier de description et activation du module Utilisateur
  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. 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 = '10';
  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 = "Gestion des utilisateurs (requis)";
  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,4); // Minimum version of PHP required by module
  61. $this->langfiles = array("main","users","companies","members",'salaries');
  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. );
  69. // Permissions
  70. $this->rights = array();
  71. $this->rights_class = 'user';
  72. $this->rights_admin_allowed = 1; // Admin is always granted of permission (even when module is disabled)
  73. $r=0;
  74. $r++;
  75. $this->rights[$r][0] = 251;
  76. $this->rights[$r][1] = 'Consulter les autres utilisateurs';
  77. $this->rights[$r][2] = 'r';
  78. $this->rights[$r][3] = 0;
  79. $this->rights[$r][4] = 'user';
  80. $this->rights[$r][5] = 'lire';
  81. $r++;
  82. $this->rights[$r][0] = 252;
  83. $this->rights[$r][1] = 'Consulter les permissions des autres utilisateurs';
  84. $this->rights[$r][2] = 'r';
  85. $this->rights[$r][3] = 0;
  86. $this->rights[$r][4] = 'user_advance';
  87. $this->rights[$r][5] = 'readperms';
  88. $r++;
  89. $this->rights[$r][0] = 253;
  90. $this->rights[$r][1] = 'Creer/modifier utilisateurs internes et externes';
  91. $this->rights[$r][2] = 'w';
  92. $this->rights[$r][3] = 0;
  93. $this->rights[$r][4] = 'user';
  94. $this->rights[$r][5] = 'creer';
  95. $r++;
  96. $this->rights[$r][0] = 254;
  97. $this->rights[$r][1] = 'Creer/modifier utilisateurs externes seulement';
  98. $this->rights[$r][2] = 'w';
  99. $this->rights[$r][3] = 0;
  100. $this->rights[$r][4] = 'user_advance';
  101. $this->rights[$r][5] = 'write';
  102. $r++;
  103. $this->rights[$r][0] = 255;
  104. $this->rights[$r][1] = 'Modifier le mot de passe des autres utilisateurs';
  105. $this->rights[$r][2] = 'w';
  106. $this->rights[$r][3] = 0;
  107. $this->rights[$r][4] = 'user';
  108. $this->rights[$r][5] = 'password';
  109. $r++;
  110. $this->rights[$r][0] = 256;
  111. $this->rights[$r][1] = 'Supprimer ou desactiver les autres utilisateurs';
  112. $this->rights[$r][2] = 'd';
  113. $this->rights[$r][3] = 0;
  114. $this->rights[$r][4] = 'user';
  115. $this->rights[$r][5] = 'supprimer';
  116. $r++;
  117. $this->rights[$r][0] = 341;
  118. $this->rights[$r][1] = 'Consulter ses propres permissions';
  119. $this->rights[$r][2] = 'r';
  120. $this->rights[$r][3] = 0;
  121. $this->rights[$r][4] = 'self_advance'; // Visible if option MAIN_USE_ADVANCED_PERMS is on
  122. $this->rights[$r][5] = 'readperms';
  123. $r++;
  124. $this->rights[$r][0] = 342;
  125. $this->rights[$r][1] = 'Creer/modifier ses propres infos utilisateur';
  126. $this->rights[$r][2] = 'w';
  127. $this->rights[$r][3] = 0;
  128. $this->rights[$r][4] = 'self';
  129. $this->rights[$r][5] = 'creer';
  130. $r++;
  131. $this->rights[$r][0] = 343;
  132. $this->rights[$r][1] = 'Modifier son propre mot de passe';
  133. $this->rights[$r][2] = 'w';
  134. $this->rights[$r][3] = 0;
  135. $this->rights[$r][4] = 'self';
  136. $this->rights[$r][5] = 'password';
  137. $r++;
  138. $this->rights[$r][0] = 344;
  139. $this->rights[$r][1] = 'Modifier ses propres permissions';
  140. $this->rights[$r][2] = 'w';
  141. $this->rights[$r][3] = 0;
  142. $this->rights[$r][4] = 'self_advance'; // Visible if option MAIN_USE_ADVANCED_PERMS is on
  143. $this->rights[$r][5] = 'writeperms';
  144. $r++;
  145. $this->rights[$r][0] = 351;
  146. $this->rights[$r][1] = 'Consulter les groupes';
  147. $this->rights[$r][2] = 'r';
  148. $this->rights[$r][3] = 0;
  149. $this->rights[$r][4] = 'group_advance'; // Visible if option MAIN_USE_ADVANCED_PERMS is on
  150. $this->rights[$r][5] = 'read';
  151. $r++;
  152. $this->rights[$r][0] = 352;
  153. $this->rights[$r][1] = 'Consulter les permissions des groupes';
  154. $this->rights[$r][2] = 'r';
  155. $this->rights[$r][3] = 0;
  156. $this->rights[$r][4] = 'group_advance'; // Visible if option MAIN_USE_ADVANCED_PERMS is on
  157. $this->rights[$r][5] = 'readperms';
  158. $r++;
  159. $this->rights[$r][0] = 353;
  160. $this->rights[$r][1] = 'Creer/modifier les groupes et leurs permissions';
  161. $this->rights[$r][2] = 'w';
  162. $this->rights[$r][3] = 0;
  163. $this->rights[$r][4] = 'group_advance'; // Visible if option MAIN_USE_ADVANCED_PERMS is on
  164. $this->rights[$r][5] = 'write';
  165. $r++;
  166. $this->rights[$r][0] = 354;
  167. $this->rights[$r][1] = 'Supprimer ou desactiver les groupes';
  168. $this->rights[$r][2] = 'd';
  169. $this->rights[$r][3] = 0;
  170. $this->rights[$r][4] = 'group_advance'; // Visible if option MAIN_USE_ADVANCED_PERMS is on
  171. $this->rights[$r][5] = 'delete';
  172. $r++;
  173. $this->rights[$r][0] = 358;
  174. $this->rights[$r][1] = 'Exporter les utilisateurs';
  175. $this->rights[$r][2] = 'r';
  176. $this->rights[$r][3] = 0;
  177. $this->rights[$r][4] = 'user';
  178. $this->rights[$r][5] = 'export';
  179. // Menus
  180. $this->menu = 1; // This module add menu entries. They are coded into menu manager.
  181. // Exports
  182. $r=0;
  183. $r++;
  184. $this->export_code[$r]=$this->rights_class.'_'.$r;
  185. $this->export_label[$r]='List of users and attributes';
  186. $this->export_permission[$r]=array(array("user","user","export"));
  187. $this->export_fields_array[$r]=array(
  188. 'u.rowid'=>"Id",'u.login'=>"Login",'u.lastname'=>"Lastname",'u.firstname'=>"Firstname",'u.employee'=>"Employee",'u.job'=>"PostOrFunction",'u.gender'=>"Gender",
  189. 'u.accountancy_code'=>"UserAccountancyCode",
  190. 'u.address'=>"Address",'u.zip'=>"Zip",'u.town'=>"Town",
  191. 'u.office_phone'=>'Phone','u.user_mobile'=>"Mobile",'u.office_fax'=>'Fax',
  192. 'u.email'=>"Email",'u.note'=>"Note",'u.signature'=>'Signature',
  193. 'u.fk_user'=>'Supervisor','u.thm'=>'THM','u.tjm'=>'TJM','u.weeklyhours'=>'WeeklyHours',
  194. 'u.dateemployment'=>'DateEmployment','u.salary'=>'Salary','u.color'=>'Color','u.api_key'=>'ApiKey',
  195. 'u.birth'=>'BirthdayDate',
  196. 'u.datec'=>"DateCreation",'u.tms'=>"DateLastModification",
  197. 'u.admin'=>"Administrator",'u.statut'=>'Status','u.datelastlogin'=>'LastConnexion','u.datepreviouslogin'=>'PreviousConnexion',
  198. 'u.fk_socpeople'=>"IdContact",'u.fk_soc'=>"IdCompany",'u.fk_member'=>"MemberId"
  199. );
  200. $this->export_TypeFields_array[$r]=array(
  201. 'u.login'=>"Text",'u.lastname'=>"Text",'u.firstname'=>"Text",'u.employee'=>'Boolean','u.job'=>'Text',
  202. 'u.accountancy_code'=>'Text',
  203. 'u.address'=>"Text",'u.zip'=>"Text",'u.town'=>"Text",
  204. 'u.office_phone'=>'Text','u.user_mobile'=>'Text','u.office_fax'=>'Text',
  205. 'u.email'=>'Text','u.datec'=>"Date",'u.tms'=>"Date",'u.admin'=>"Boolean",'u.statut'=>'Status','u.note'=>"Text",'u.datelastlogin'=>'Date',
  206. 'u.fk_user'=>"List:user:login",
  207. 'u.birth'=>'Date',
  208. 'u.datepreviouslogin'=>'Date','u.fk_soc'=>"List:societe:nom:rowid",'u.fk_member'=>"List:adherent:firstname"
  209. );
  210. $this->export_entities_array[$r]=array(
  211. 'u.rowid'=>"user",'u.login'=>"user",'u.lastname'=>"user",'u.firstname'=>"user",'u.employee'=>'user','u.job'=>'user','u.gender'=>'user',
  212. 'u.accountancy_code'=>'user',
  213. 'u.address'=>"user",'u.zip'=>"user",'u.town'=>"user",
  214. 'u.office_phone'=>'user','u.user_mobile'=>'user','u.office_fax'=>'user',
  215. 'u.email'=>'user','u.note'=>"user",'u.signature'=>'user',
  216. 'u.fk_user'=>'user','u.thm'=>'user','u.tjm'=>'user','u.weeklyhours'=>'user',
  217. 'u.dateemployment'=>'user','u.salary'=>'user','u.color'=>'user','u.api_key'=>'user',
  218. 'u.birth'=>'user',
  219. 'u.datec'=>"user",'u.tms'=>"user",
  220. 'u.admin'=>"user",'u.statut'=>'user','u.datelastlogin'=>'user','u.datepreviouslogin'=>'user',
  221. 'u.fk_socpeople'=>"contact",'u.fk_soc'=>"company",'u.fk_member'=>"member"
  222. );
  223. if (empty($conf->adherent->enabled))
  224. {
  225. unset($this->export_fields_array[$r]['u.fk_member']);
  226. unset($this->export_entities_array[$r]['u.fk_member']);
  227. }
  228. $this->export_sql_start[$r]='SELECT DISTINCT ';
  229. $this->export_sql_end[$r] =' FROM '.MAIN_DB_PREFIX.'user as u';
  230. $this->export_sql_end[$r] .=' WHERE u.entity IN ('.getEntity('user').')';
  231. // Imports
  232. $r=0;
  233. // Import list of users attributes
  234. $r++;
  235. $this->import_code[$r]=$this->rights_class.'_'.$r;
  236. $this->import_label[$r]='ImportDataset_user_1';
  237. $this->import_icon[$r]='user';
  238. $this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon
  239. $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)
  240. $this->import_fields_array[$r]=array(
  241. 'u.login'=>"Login*",'u.lastname'=>"Name*",'u.firstname'=>"Firstname",'u.employee'=>"Employee*",'u.job'=>"PostOrFunction",'u.gender'=>"Gender",
  242. 'u.accountancy_code'=>"UserAccountancyCode",
  243. 'u.pass_crypted'=>"Password",'u.admin'=>"Administrator",'u.fk_soc'=>"Company*",'u.address'=>"Address",'u.zip'=>"Zip",'u.town'=>"Town",
  244. 'u.fk_state'=>"StateId",'u.fk_country'=>"CountryCode",
  245. 'u.office_phone'=>"Phone",'u.user_mobile'=>"Mobile",'u.office_fax'=>"Fax",
  246. 'u.email'=>"Email",'u.note'=>"Note",'u.signature'=>'Signature',
  247. 'u.fk_user'=>'Supervisor','u.thm'=>'THM','u.tjm'=>'TJM','u.weeklyhours'=>'WeeklyHours',
  248. 'u.dateemployment'=>'DateEmployment','u.salary'=>'Salary','u.color'=>'Color','u.api_key'=>'ApiKey',
  249. 'u.birth'=>'BirthdayDate',
  250. 'u.datec'=>"DateCreation",
  251. 'u.statut'=>'Status'
  252. );
  253. // Add extra fields
  254. $sql="SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'user' AND entity IN (0,".$conf->entity.")";
  255. $resql=$this->db->query($sql);
  256. if ($resql) // This can fail when class is used on old database (during migration for example)
  257. {
  258. while ($obj=$this->db->fetch_object($resql))
  259. {
  260. $fieldname='extra.'.$obj->name;
  261. $fieldlabel=ucfirst($obj->label);
  262. $this->import_fields_array[$r][$fieldname]=$fieldlabel.($obj->fieldrequired?'*':'');
  263. }
  264. }
  265. // End add extra fields
  266. $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)
  267. $this->import_convertvalue_array[$r]=array(
  268. 'u.fk_state'=>array('rule'=>'fetchidfromcodeid','classfile'=>'/core/class/cstate.class.php','class'=>'Cstate','method'=>'fetch','dict'=>'DictionaryState'),
  269. 'u.fk_country'=>array('rule'=>'fetchidfromcodeid','classfile'=>'/core/class/ccountry.class.php','class'=>'Ccountry','method'=>'fetch','dict'=>'DictionaryCountry'),
  270. 'u.salary'=>array('rule'=>'numeric')
  271. );
  272. //$this->import_convertvalue_array[$r]=array('s.fk_soc'=>array('rule'=>'lastrowid',table='t');
  273. $this->import_regex_array[$r]=array(
  274. 'u.employee'=>'^[0|1]',
  275. '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])?$',
  276. 'u.dateemployment'=>'^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$',
  277. 'u.birth'=>'^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$'
  278. );
  279. $this->import_examplevalues_array[$r]=array(
  280. 'u.lastname'=>"Doe",'u.firstname'=>'John','u.login'=>'jdoe','u.employee'=>'0 or 1',
  281. '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",
  282. 'u.zip'=>"123456",'u.town'=>"Big town",'u.fk_country'=>'US, FR, DE...','u.office_phone'=>"0101010101",'u.office_fax'=>"0101010102",
  283. '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",
  284. 'u.statut'=>"0 (closed) or 1 (active)",
  285. );
  286. $this->import_updatekeys_array[$r]=array('u.lastname'=>'Lastname','u.firstname'=>'Firstname','u.login'=>'Login');
  287. }
  288. /**
  289. * Function called when module is enabled.
  290. * The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  291. * It also creates data directories
  292. *
  293. * @param string $options Options when enabling module ('', 'noboxes')
  294. * @return int 1 if OK, 0 if KO
  295. */
  296. function init($options='')
  297. {
  298. global $conf;
  299. // Permissions
  300. $this->remove($options);
  301. $sql = array();
  302. return $this->_init($sql,$options);
  303. }
  304. }