]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Core/Password.php
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Password for galette. The original code was found
7 * in includes/functions.inc.php
11 * Copyright © 2003-2014 The Galette Team
13 * This file is part of Galette (http://galette.tuxfamily.org).
15 * Galette is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
20 * Galette is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
31 * @author Frédéric Jacquot <unknown@unknow.com>
32 * @author Georges Khaznadar (password encryption, images) <unknown@unknow.com>
33 * @author Johan Cwiklinski <johan@x-tnd.be>
34 * @copyright 2003-2014 The Galette Team
35 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
36 * @link http://galette.tuxfamily.org
37 * @since Available since 0.7dev - 2009-02-28
40 namespace Galette\Core
;
43 use Galette\Entity\Adherent
;
46 * Temporary password managment
51 * @author Frédéric Jacquot <unknown@unknow.com>
52 * @author Georges Khaznadar (password encryption, images) <unknown@unknow.com>
53 * @author Johan Cwiklinski <johan@x-tnd.be>
54 * @copyright 2009-2014 The Galette Team
55 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
56 * @link http://galette.tuxfamily.org
57 * @since Available since 0.7dev - 2011-06-16
60 class Password
extends AbstractPassword
62 public const TABLE
= 'tmppasswds';
63 public const PK
= Adherent
::PK
;
70 * @param Db $zdb Database instance:
71 * @param boolean $clean Whether we should clean expired passwords in database
73 public function __construct(Db
$zdb, $clean = true)
76 if ($clean === true) {
77 $this->cleanExpired();
82 * Remove all old password entries
84 * @param int $id_adh Member identifier
88 private function removeOldEntries($id_adh)
91 $delete = $this->zdb
->delete(self
::TABLE
);
92 $delete->where(self
::PK
. ' = ' . $id_adh);
94 $del = $this->zdb
->execute($delete);
97 'Temporary passwords for `' . $id_adh . '` has been removed.',
101 } catch (\Exception
$e) {
103 'An error has occurred removing old tmppasswords ' .
112 * Generates a new password for specified member
114 * @param int $id_adh Member identifier
118 public function generateNewPassword($id_adh)
120 //first of all, we'll remove all existant entries for specified id
121 $this->removeOldEntries($id_adh);
123 //second, generate a new password and store it in the database
124 $password = $this->makeRandomPassword();
125 $hash = password_hash($password, PASSWORD_BCRYPT
);
130 'tmp_passwd' => $hash,
131 'date_crea_tmp_passwd' => date('Y-m-d H:i:s')
134 $insert = $this->zdb
->insert(self
::TABLE
);
135 $insert->values($values);
137 $add = $this->zdb
->execute($insert);
140 'New passwords temporary set for `' . $id_adh . '`.',
143 $this->setPassword($password);
144 $this->setHash($hash);
149 } catch (\Exception
$e) {
151 "An error occurred trying to add temporary password entry. " .
160 * Remove expired passwords queries (older than 24 hours)
164 protected function cleanExpired()
166 $date = new \
DateTime();
167 $date->sub(new \
DateInterval('PT24H'));
170 $delete = $this->zdb
->delete(self
::TABLE
);
171 $delete->where
->lessThan(
172 'date_crea_tmp_passwd',
173 $date->format('Y-m-d H:i:s')
175 $del = $this->zdb
->execute($delete);
178 'Old Temporary passwords has been deleted.',
182 } catch (\Exception
$e) {
184 'An error occurred deleting expired temporary passwords. ' .
193 * Check if requested hash is valid
195 * @param string $hash the hash
197 * @return false if hash is not valid, member id otherwise
199 public function isHashValid($hash)
202 $select = $this->zdb
->select(self
::TABLE
);
205 )->where(array('tmp_passwd' => $hash));
207 $results = $this->zdb
->execute($select);
209 if ($results->count() > 0) {
210 $result = $results->current();
216 } catch (\Exception
$e) {
218 'An error occurred getting requested hash. ' . $e->getMessage(),
226 * Remove a hash that has been used (ie. once password has been updated)
228 * @param string $hash hash
232 public function removeHash($hash)
235 $delete = $this->zdb
->delete(self
::TABLE
);
237 array('tmp_passwd' => $hash)
240 $del = $this->zdb
->execute($delete);
243 'Used hash has been successfully remove',
248 } catch (\Exception
$e) {
250 'An error ocured attempting to delete used hash' .