dolibarr 25.0.0-alpha
modGeneratePassStandard.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
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 <https://www.gnu.org/licenses/>.
17 * or see https://www.gnu.org/
18 */
19
26require_once DOL_DOCUMENT_ROOT.'/core/modules/security/generate/modules_genpassword.php';
27
28
33{
37 public $position = 50;
38
42 public $id;
43
44 public $picto = 'fa-shield-alt';
45
54 public function __construct($db, $conf, $langs, $user)
55 {
56 $this->id = "standard";
57 $this->length = '12';
58 $this->length2 = 12;
59
60 $this->db = $db;
61 $this->conf = $conf;
62 $this->langs = $langs;
63 $this->user = $user;
64 }
65
71 public function getDescription()
72 {
73 global $langs;
74 return $langs->trans("PasswordGenerationStandard", $this->length);
75 }
76
82 public function getExample()
83 {
84 return $this->getNewGeneratedPassword();
85 }
86
92 public function getNewGeneratedPassword()
93 {
94 // start with a blank password
95 $password = "";
96
97 // define possible characters
98 $possible = "0123456789qwertyuiopasdfghjklzxcvbnmASDFGHJKLZXCVBNMQWERTYUIOP";
99
100 // set up a counter
101 $i = 0;
102
103 // add random characters to $password until $length is reached
104 while ($i < $this->length) {
105 // pick a random character from the possible ones
106 $char = substr($possible, random_int(0, dol_strlen($possible) - 1), 1); // Cryptographic random
107
108 if (substr_count($password, $char) <= 6) { // we don't want this character if it's already 5 times in the password
109 $password .= $char;
110 $i++;
111 }
112 }
113
114 // done!
115 return $password;
116 }
117
125 public function validatePassword($password)
126 {
127 global $langs;
128
129 dol_syslog("modGeneratePassStandard::validatePassword");
130
131 if (dol_strlen($password) < $this->length2) {
132 $langs->load("other");
133 $this->error = $langs->trans("YourPasswordMustHaveAtLeastXChars", $this->length2);
134 return 0;
135 }
136
137 return 1;
138 }
139}
Parent class for password rules/management modules.
Class to generate a password according to a dolibarr standard rule (12 random chars)
validatePassword($password)
Validate a password This function is called by User->setPassword() and internally to validate that th...
getDescription()
Return description of module.
getExample()
Return an example of password generated by this module.
__construct($db, $conf, $langs, $user)
Constructor.
dol_strlen($string, $stringencoding='UTF-8', $binarycount=0)
Make a strlen call.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
conf($dolibarr_main_document_root, $realpathconf=null)
Load conf file (file must exists)
Definition inc.php:431
$conf db user
Active Directory does not allow anonymous connections.
Definition repair.php:166