]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Core/AbstractPassword.php
Improve coding standards
[galette.git] / galette / lib / Galette / Core / AbstractPassword.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Copyright © 2003-2024 The Galette Team
7 *
8 * This file is part of Galette (https://galette.eu).
9 *
10 * Galette is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * Galette is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 namespace Galette\Core;
25
26 use Analog\Analog;
27 use Galette\Entity\Adherent;
28
29 /**
30 * Abstract password
31 *
32 * @author Frédéric Jacquot <gna@logeek.com>
33 * @author Georges Khaznadar (password encryption, images) <georges@unknow.org>
34 * @author Johan Cwiklinski <johan@x-tnd.be>
35 */
36
37 abstract class AbstractPassword
38 {
39 /** Default password size */
40 public const DEFAULT_SIZE = 8;
41
42 protected string $chars = 'abcdefghjkmnpqrstuvwxyz0123456789';
43 protected ?string $hash = null;
44 protected string $new_password;
45
46 /**
47 * Generates a random password based on default salt
48 *
49 * @param int|null $size Password size (optional)
50 *
51 * @return string random password
52 */
53 public function makeRandomPassword(int $size = null): string
54 {
55 $size = $size ?? static::DEFAULT_SIZE;
56 $pass = '';
57 $i = 0;
58 while ($i <= $size - 1) {
59 $num = mt_rand(0, strlen($this->chars) - 1) % strlen($this->chars);
60 $pass .= substr($this->chars, $num, 1);
61 $i++;
62 }
63 return $pass;
64 }
65
66 /**
67 * Generates a new password for specified member
68 *
69 * @param int $id_adh Member identifier
70 *
71 * @return boolean
72 */
73 abstract public function generateNewPassword(int $id_adh): bool;
74
75 /**
76 * Remove expired passwords queries (older than 24 hours)
77 *
78 * @return boolean
79 */
80 abstract protected function cleanExpired(): bool;
81
82 /**
83 * Retrieve new password for sending it to the user
84 *
85 * @return string the new password
86 */
87 public function getNewPassword(): string
88 {
89 return $this->new_password;
90 }
91
92 /**
93 * Retrieve new hash
94 *
95 * @return string hash
96 */
97 public function getHash(): string
98 {
99 return $this->hash;
100 }
101
102 /**
103 * Set password
104 *
105 * @param string $password Password
106 *
107 * @return self
108 */
109 protected function setPassword(string $password): self
110 {
111 $this->new_password = $password;
112 return $this;
113 }
114
115 /**
116 * Set hash
117 *
118 * @param string $hash Hash
119 *
120 * @return self
121 */
122 protected function setHash(string $hash): self
123 {
124 $this->hash = $hash;
125 return $this;
126 }
127 }