dolibarr 25.0.0-alpha
modUser.class.php
Go to the documentation of this file.
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-2024 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2026 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2026 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
31include_once DOL_DOCUMENT_ROOT.'/core/modules/DolibarrModules.class.php';
32
37{
43 public function __construct($db)
44 {
45 global $conf;
46
47 $this->db = $db;
48 $this->numero = 0;
49
50 $this->family = "hr"; // Family for module (or "base" if core module)
51 $this->module_position = '05';
52 // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
53 $this->name = preg_replace('/^mod/i', '', get_class($this));
54 $this->description = "Management of users and groups of users (mandatory)";
55
56 // Possible values for version are: 'development', 'experimental', 'dolibarr' or version
57 $this->version = 'dolibarr';
58
59 $this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
60 $this->picto = 'group';
61
62 // Data directories to create when module is enabled
63 $this->dirs = array("/users/temp");
64
65 // Config pages
66 $this->config_page_url = array("user.php");
67
68 // Dependencies
69 $this->hidden = false; // A condition to hide module
70 $this->depends = array(); // List of module class names as string that must be enabled if this module is enabled
71 $this->requiredby = array(); // List of module ids to disable if this one is disabled
72 $this->conflictwith = array(); // List of module class names as string this module is in conflict with
73 $this->langfiles = array("main", "users", "companies", "members", "salaries", "hrm");
74 $this->always_enabled = true; // Can't be disabled
75 //$this->auto_enabled = true; // always_enabled already set
76
77 // Constants
78 $this->const = array();
79
80 // Boxes
81 $this->boxes = [
82 ['file' => 'box_lastlogin.php', 'enabledbydefaulton' => 'Home'],
83 ['file' => 'box_birthdays.php', 'enabledbydefaulton' => 'Home'],
84 ['file' => 'box_dolibarr_state_board.php', 'enabledbydefaulton' => 'Home'],
85 ];
86
87 // Permissions
88 $this->rights = array();
89 $this->rights_class = 'user';
90 $this->rights_admin_allowed = 1; // Admin is always granted of permission (even when module is disabled)
91 $r = 0;
92
93 $r++;
94 $this->rights[$r][self::KEY_ID] = 251;
95 $this->rights[$r][self::KEY_LABEL] = 'Read information of other users, groups and permissions';
96 $this->rights[$r][self::KEY_DEFAULT] = 0;
97 $this->rights[$r][self::KEY_FIRST_LEVEL] = 'user';
98 $this->rights[$r][self::KEY_SECOND_LEVEL] = 'lire';
99
100 $r++;
101 $this->rights[$r][self::KEY_ID] = 252;
102 $this->rights[$r][self::KEY_LABEL] = 'Read permissions of other users';
103 $this->rights[$r][self::KEY_DEFAULT] = 0;
104 $this->rights[$r][self::KEY_FIRST_LEVEL] = 'user_advance'; // Visible if option MAIN_USE_ADVANCED_PERMS is on
105 $this->rights[$r][self::KEY_SECOND_LEVEL] = 'readperms';
106
107 $r++;
108 $this->rights[$r][self::KEY_ID] = 253;
109 $this->rights[$r][self::KEY_LABEL] = 'Create/modify internal and external users, groups and permissions';
110 $this->rights[$r][self::KEY_DEFAULT] = 0;
111 $this->rights[$r][self::KEY_FIRST_LEVEL] = 'user';
112 $this->rights[$r][self::KEY_SECOND_LEVEL] = 'creer';
113
114 $r++;
115 $this->rights[$r][self::KEY_ID] = 254;
116 $this->rights[$r][self::KEY_LABEL] = 'Create/modify external users only';
117 $this->rights[$r][self::KEY_DEFAULT] = 0;
118 $this->rights[$r][self::KEY_FIRST_LEVEL] = 'user_advance'; // Visible if option MAIN_USE_ADVANCED_PERMS is on
119 $this->rights[$r][self::KEY_SECOND_LEVEL] = 'write';
120
121 $r++;
122 $this->rights[$r][self::KEY_ID] = 255;
123 $this->rights[$r][self::KEY_LABEL] = 'Modify the password of other users';
124 $this->rights[$r][self::KEY_DEFAULT] = 0;
125 $this->rights[$r][self::KEY_FIRST_LEVEL] = 'user';
126 $this->rights[$r][self::KEY_SECOND_LEVEL] = 'password';
127
128 $r++;
129 $this->rights[$r][self::KEY_ID] = 256;
130 $this->rights[$r][self::KEY_LABEL] = 'Delete or disable other users';
131 $this->rights[$r][self::KEY_DEFAULT] = 0;
132 $this->rights[$r][self::KEY_FIRST_LEVEL] = 'user';
133 $this->rights[$r][self::KEY_SECOND_LEVEL] = 'supprimer';
134
135 $r++;
136 $this->rights[$r][self::KEY_ID] = 341;
137 $this->rights[$r][self::KEY_LABEL] = 'Read its own permissions';
138 $this->rights[$r][self::KEY_DEFAULT] = 0;
139 $this->rights[$r][self::KEY_FIRST_LEVEL] = 'self_advance'; // Visible if option MAIN_USE_ADVANCED_PERMS is on
140 $this->rights[$r][self::KEY_SECOND_LEVEL] = 'readperms';
141
142 $r++;
143 $this->rights[$r][self::KEY_ID] = 342;
144 $this->rights[$r][self::KEY_LABEL] = 'Create/modify of its own user';
145 $this->rights[$r][self::KEY_DEFAULT] = 0;
146 $this->rights[$r][self::KEY_FIRST_LEVEL] = 'self';
147 $this->rights[$r][self::KEY_SECOND_LEVEL] = 'creer';
148
149 $r++;
150 $this->rights[$r][self::KEY_ID] = 343;
151 $this->rights[$r][self::KEY_LABEL] = 'Modify its own password';
152 $this->rights[$r][self::KEY_DEFAULT] = 0;
153 $this->rights[$r][self::KEY_FIRST_LEVEL] = 'self';
154 $this->rights[$r][self::KEY_SECOND_LEVEL] = 'password';
155
156 $r++;
157 $this->rights[$r][self::KEY_ID] = 344;
158 $this->rights[$r][self::KEY_LABEL] = 'Modify its own permissions';
159 $this->rights[$r][self::KEY_DEFAULT] = 0;
160 $this->rights[$r][self::KEY_FIRST_LEVEL] = 'self_advance'; // Visible if option MAIN_USE_ADVANCED_PERMS is on
161 $this->rights[$r][self::KEY_SECOND_LEVEL] = 'writeperms';
162
163 $r++;
164 $this->rights[$r][self::KEY_ID] = 351;
165 $this->rights[$r][self::KEY_LABEL] = 'Read groups';
166 $this->rights[$r][self::KEY_DEFAULT] = 0;
167 $this->rights[$r][self::KEY_FIRST_LEVEL] = 'group_advance'; // Visible if option MAIN_USE_ADVANCED_PERMS is on
168 $this->rights[$r][self::KEY_SECOND_LEVEL] = 'read';
169
170 $r++;
171 $this->rights[$r][self::KEY_ID] = 352;
172 $this->rights[$r][self::KEY_LABEL] = 'Read permissions of groups';
173 $this->rights[$r][self::KEY_DEFAULT] = 0;
174 $this->rights[$r][self::KEY_FIRST_LEVEL] = 'group_advance'; // Visible if option MAIN_USE_ADVANCED_PERMS is on
175 $this->rights[$r][self::KEY_SECOND_LEVEL] = 'readperms';
176
177 $r++;
178 $this->rights[$r][self::KEY_ID] = 353;
179 $this->rights[$r][self::KEY_LABEL] = 'Create/modify groups and permissions';
180 $this->rights[$r][self::KEY_DEFAULT] = 0;
181 $this->rights[$r][self::KEY_FIRST_LEVEL] = 'group_advance'; // Visible if option MAIN_USE_ADVANCED_PERMS is on
182 $this->rights[$r][self::KEY_SECOND_LEVEL] = 'write';
183
184 $r++;
185 $this->rights[$r][self::KEY_ID] = 354;
186 $this->rights[$r][self::KEY_LABEL] = 'Delete groups';
187 $this->rights[$r][self::KEY_DEFAULT] = 0;
188 $this->rights[$r][self::KEY_FIRST_LEVEL] = 'group_advance'; // Visible if option MAIN_USE_ADVANCED_PERMS is on
189 $this->rights[$r][self::KEY_SECOND_LEVEL] = 'delete';
190
191 $r++;
192 $this->rights[$r][self::KEY_ID] = 358;
193 $this->rights[$r][self::KEY_LABEL] = 'Export all users';
194 $this->rights[$r][self::KEY_DEFAULT] = 0;
195 $this->rights[$r][self::KEY_FIRST_LEVEL] = 'user';
196 $this->rights[$r][self::KEY_SECOND_LEVEL] = 'export';
197
198
199 // Menus
200 $this->menu = 1; // This module add menu entries. They are coded into menu manager.
201
202
203 // Exports
204 $r = 0;
205
206 $r++;
207 $this->export_code[$r] = $this->rights_class.'_'.$r;
208 $this->export_label[$r] = 'List of users and attributes'; // Translation key (used only if key ExportDataset_user_1 not found)
209 $this->export_permission[$r] = array(array("user", "user", "export"));
210 $this->export_fields_array[$r] = array(
211 'u.rowid' => "Id", 'u.login' => "Login", 'u.lastname' => "Lastname", 'u.firstname' => "Firstname", 'u.employee' => "Employee", 'u.job' => "PostOrFunction", 'u.gender' => "Gender",
212 'u.accountancy_code' => "UserAccountancyCode",
213 'u.address' => "Address", 'u.zip' => "Zip", 'u.town' => "Town",
214 'u.office_phone' => 'Phone', 'u.user_mobile' => "Mobile", 'u.office_fax' => 'Fax',
215 'u.email' => "Email", 'u.note_public' => "NotePublic", 'u.note_private' => "NotePrivate", 'u.signature' => 'Signature',
216 'u.fk_user' => 'HierarchicalResponsible', 'u.thm' => 'THM', 'u.tjm' => 'TJM', 'u.weeklyhours' => 'WeeklyHours',
217 'u.dateemployment' => 'DateEmploymentStart', 'u.dateemploymentend' => 'DateEmploymentEnd', 'u.salary' => 'Salary', 'u.color' => 'Color', 'u.api_key' => 'ApiKey',
218 'u.birth' => 'DateOfBirth',
219 'u.datec' => "DateCreation", 'u.tms' => "DateLastModification",
220 'u.admin' => "Administrator", 'u.statut' => 'Status', 'u.datelastlogin' => 'LastConnexion', 'u.datepreviouslogin' => 'PreviousConnexion',
221 'u.fk_socpeople' => "IdContact", 'u.fk_soc' => "IdCompany",
222 'u.fk_member' => "MemberId",
223 "a.firstname" => "MemberFirstname",
224 "a.lastname" => "MemberLastname",
225 'g.nom' => "Group"
226 );
227 $this->export_TypeFields_array[$r] = array(
228 'u.rowid' => 'Numeric', 'u.login' => "Text", 'u.lastname' => "Text", 'u.firstname' => "Text", 'u.employee' => 'Boolean', 'u.job' => 'Text',
229 'u.accountancy_code' => 'Text',
230 'u.address' => "Text", 'u.zip' => "Text", 'u.town' => "Text",
231 'u.office_phone' => 'Text', 'u.user_mobile' => 'Text', 'u.office_fax' => 'Text',
232 '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',
233 'u.fk_user' => "FormSelect:select_dolusers",
234 'u.birth' => 'Date',
235 'u.datepreviouslogin' => 'Date',
236 'u.fk_socpeople' => 'FormSelect:selectcontacts',
237 'u.fk_soc' => "FormSelect:select_company",
238 'u.tjm' => "Numeric", 'u.thm' => "Numeric", 'u.fk_member' => "Numeric",
239 'u.weeklyhours' => "Numeric",
240 'u.dateemployment' => "Date", 'u.dateemploymentend' => "Date", 'u.salary' => "Numeric",
241 'u.color' => 'Text', 'u.api_key' => 'Text',
242 'a.firstname' => 'Text',
243 'a.lastname' => 'Text',
244 'g.nom' => "Text"
245 );
246 $this->export_entities_array[$r] = array(
247 'u.rowid' => "user", 'u.login' => "user", 'u.lastname' => "user", 'u.firstname' => "user", 'u.employee' => 'user', 'u.job' => 'user', 'u.gender' => 'user',
248 'u.accountancy_code' => 'user',
249 'u.address' => "user", 'u.zip' => "user", 'u.town' => "user",
250 'u.office_phone' => 'user', 'u.user_mobile' => 'user', 'u.office_fax' => 'user',
251 'u.email' => 'user', 'u.note_public' => "user", 'u.note_private' => "user", 'u.signature' => 'user',
252 'u.fk_user' => 'user', 'u.thm' => 'user', 'u.tjm' => 'user', 'u.weeklyhours' => 'user',
253 'u.dateemployment' => 'user', 'u.dateemploymentend' => 'user', 'u.salary' => 'user', 'u.color' => 'user', 'u.api_key' => 'user',
254 'u.birth' => 'user',
255 'u.datec' => "user", 'u.tms' => "user",
256 'u.admin' => "user", 'u.statut' => 'user', 'u.datelastlogin' => 'user', 'u.datepreviouslogin' => 'user',
257 'u.fk_socpeople' => "contact", 'u.fk_soc' => "company", 'u.fk_member' => "member",
258 'a.firstname' => "member", 'a.lastname' => "member",
259 'g.nom' => "Group"
260 );
261 $keyforselect = 'user';
262 $keyforelement = 'user';
263 $keyforaliasextra = 'extra';
264 include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
265 if (!isModEnabled('member')) {
266 unset($this->export_fields_array[$r]['u.fk_member']);
267 unset($this->export_entities_array[$r]['u.fk_member']);
268 }
269 $this->export_sql_start[$r] = 'SELECT DISTINCT ';
270 $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'user as u';
271 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'user_extrafields as extra ON u.rowid = extra.fk_object';
272 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'usergroup_user as ug ON u.rowid = ug.fk_user';
273 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'usergroup as g ON ug.fk_usergroup = g.rowid';
274 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'adherent as a ON u.fk_member = a.rowid';
275 $this->export_sql_end[$r] .= ' WHERE u.entity IN ('.getEntity('user').')';
276
277
278 $r++;
279 $this->export_code[$r] = $this->rights_class.'_'.$r;
280 $this->export_label[$r] = 'List of security events'; // Translation key (used only if key ExportDataset_user_2 not found)
281 $this->export_permission[$r] = array(array("user")); // Only admin
282 $this->export_fields_array[$r] = array(
283 'e.rowid' => "Id", 'e.type' => "Type",
284 'e.dateevent' => "Date",
285 'e.description' => 'Description',
286 'e.ip' => 'IPAddress', 'e.user_agent' => 'UserAgent',
287 'e.authentication_method' => 'AuthenticationMode',
288 'e.fk_user' => "UserID", 'u.login' => "Login",
289 );
290 $this->export_TypeFields_array[$r] = array(
291 'e.rowid' => 'Numeric', 'e.type' => "Text",
292 'e.dateevent' => "Date",
293 'e.description' => 'Text',
294 'e.ip' => 'Text', 'e.user_agent' => 'Text',
295 'e.authentication_method' => 'Text',
296 'e.fk_user' => "Numeric", 'u.login' => "Text",
297 );
298 $this->export_entities_array[$r] = array(
299 'e.rowid' => 'securityevent', 'e.type' => "securityevent",
300 'e.dateevent' => "securityevent",
301 'e.description' => 'securityevent',
302 'e.ip' => 'securityevent', 'e.user_agent' => 'securityevent',
303 'e.authentication_method' => 'securityevent',
304 'e.fk_user' => "user", 'u.login' => "user",
305 );
306 $this->export_sql_start[$r] = 'SELECT DISTINCT ';
307 $this->export_sql_end[$r] = ' FROM '.MAIN_DB_PREFIX.'events as e';
308 $this->export_sql_end[$r] .= ' LEFT JOIN '.MAIN_DB_PREFIX.'user as u ON e.fk_user = u.rowid';
309 $this->export_sql_end[$r] .= ' WHERE e.entity IN ('.getEntity('event').')';
310
311
312 // Imports
313 $r = 0;
314
315 // Import list of users attributes
316 $r++;
317 $this->import_code[$r] = $this->rights_class.'_'.$r;
318 $this->import_label[$r] = 'ImportDataset_user_1';
319 $this->import_icon[$r] = 'user';
320 $this->import_entities_array[$r] = array(); // We define here only fields that use another icon that the one defined into import_icon
321 $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)
322 $this->import_fields_array[$r] = array(
323 'u.login' => "Login*", 'u.lastname' => "Name*", 'u.firstname' => "Firstname", 'u.employee' => "Employee*", 'u.job' => "PostOrFunction", 'u.gender' => "Gender",
324 'u.accountancy_code' => "UserAccountancyCode",
325 'u.pass_crypted' => "Password", 'u.admin' => "Administrator", 'u.fk_soc' => "Company*", 'u.address' => "Address", 'u.zip' => "Zip", 'u.town' => "Town",
326 'u.fk_state' => "StateId", 'u.fk_country' => "CountryCode",
327 'u.office_phone' => "Phone", 'u.user_mobile' => "Mobile", 'u.office_fax' => "Fax",
328 'u.email' => "Email", 'u.note_public' => "NotePublic", 'u.note_private' => "NotePrivate", 'u.signature' => 'Signature',
329 'u.fk_user' => 'HierarchicalResponsible', 'u.thm' => 'THM', 'u.tjm' => 'TJM', 'u.weeklyhours' => 'WeeklyHours',
330 'u.dateemployment' => 'DateEmploymentStart', 'u.dateemploymentend' => 'DateEmploymentEnd', 'u.salary' => 'Salary', 'u.color' => 'Color', 'u.api_key' => 'ApiKey',
331 'u.birth' => 'DateOfBirth',
332 'u.datec' => "DateCreation",
333 'u.statut' => 'Status'
334 );
335 // Add extra fields
336 $sql = "SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE type <> 'separate' AND elementtype = 'user' AND entity IN (0,".((int) $conf->entity).")";
337 $resql = $this->db->query($sql);
338 if ($resql) { // This can fail when class is used on old database (during migration for example)
339 while ($obj = $this->db->fetch_object($resql)) {
340 $fieldname = 'extra.'.$obj->name;
341 $fieldlabel = ucfirst($obj->label);
342 $this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
343 }
344 }
345 // End add extra fields
346 $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)
347 $this->import_convertvalue_array[$r] = array(
348 'u.fk_state' => array('rule' => 'fetchidfromcodeid', 'classfile' => '/core/class/cstate.class.php', 'class' => 'Cstate', 'method' => 'fetch', 'dict' => 'DictionaryState'),
349 'u.fk_country' => array('rule' => 'fetchidfromcodeid', 'classfile' => '/core/class/ccountry.class.php', 'class' => 'Ccountry', 'method' => 'fetch', 'dict' => 'DictionaryCountry'),
350 'u.salary' => array('rule' => 'numeric')
351 );
352 //$this->import_convertvalue_array[$r]=array('s.fk_soc'=>array('rule'=>'lastrowid',table='t');
353 $this->import_regex_array[$r] = array(
354 'u.employee' => '^[0|1]',
355 '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])?$',
356 'u.dateemployment' => '^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$',
357 'u.birth' => '^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$'
358 );
359 $this->import_examplevalues_array[$r] = array(
360 'u.lastname' => "Doe", 'u.firstname' => 'John', 'u.login' => 'jdoe', 'u.employee' => '0 or 1', 'u.job' => 'CTO', 'u.gender' => 'man or woman',
361 'u.pass_crypted' => 'Encrypted password',
362 'u.fk_soc' => '0 (internal user) or company name (external user)', 'u.address' => "61 jump street",
363 'u.zip' => "123456", 'u.town' => "Big town", 'u.fk_country' => 'US, FR, DE...', 'u.office_phone' => "0101010101", 'u.office_fax' => "0101010102",
364 '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",
365 'u.statut' => "0 (closed) or 1 (active)",
366 );
367 $this->import_updatekeys_array[$r] = array('u.lastname' => 'Lastname', 'u.firstname' => 'Firstname', 'u.login' => 'Login');
368 }
369
370
379 public function init($options = '')
380 {
381 // Permissions
382 $this->remove($options);
383
384 $sql = array();
385
386 return $this->_init($sql, $options);
387 }
388}
Class DolibarrModules.
_init($array_sql, $options='')
Enables a module.
Class to describe and enable module User.
__construct($db)
Constructor.
init($options='')
Function called when module is enabled.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
isModEnabled($module)
Is Dolibarr module enabled.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:133