-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserPassword.php
More file actions
186 lines (148 loc) · 5.6 KB
/
Copy pathUserPassword.php
File metadata and controls
186 lines (148 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
declare(strict_types=1);
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <emondpph@gmail.com>
* License: https://github.com/quidphp/core/blob/master/LICENSE
*/
namespace Quid\Core\Col;
use Quid\Base;
use Quid\Core;
use Quid\Orm;
// userPassword
// class for the column which manages the password field for the user row
class UserPassword extends Core\ColAlias
{
// config
protected static array $config = [
'tag'=>'inputPassword',
'preValidate'=>['arrCount'=>2],
'required'=>true,
'search'=>false,
'export'=>false,
'visible'=>[
'session'=>'isPasswordVisible',
'row'=>'isUpdateable'],
'visibleGeneral'=>false,
'check'=>['kind'=>'char'],
'log'=>Core\Row\Log::class, // custom
'security'=>null // défini le niveau de sécurité du mot de passe utilisé, support pour loose
];
// onMakeAttr
// callback lors du set des attr
// permet de charger le niveau de sécurité du mot de passe
final protected function onAttr(array $return):array
{
$return['pattern'] = $return['pattern'] ?? null;
$return['validate'] = $return['validate'] ?? [];
$security = $return['security'] ?? null;
$originalValidate = 'passwordHash';
$validate = $originalValidate;
$originalPattern = 'password';
$pattern = $originalPattern;
if(is_string($security))
{
$security = ucfirst($security);
$validate .= $security;
$pattern .= $security;
}
if(!in_array($originalValidate,$return['validate'],true) && !in_array($validate,$return['validate'],true))
$return['validate'][] = $validate;
if(!in_array($return['pattern'],[$originalPattern,$pattern],true))
$return['pattern'] = $pattern;
return $return;
}
// preValidatePrepare
// prépare la valeur en provenance de post avant la prévalidation
final public function preValidatePrepare($value)
{
$return = null;
if(is_array($value))
{
$clean = Base\Arr::clean($value);
if(!empty($clean))
$return = $clean;
}
return $return;
}
// onSet
// logique onSet pour le champ password
// possible de fournir un mot de passe via un array ou une string
final protected function onSet($value,?Orm\Cell $cell,array $row,array $option)
{
$return = null;
if(is_array($value))
{
$table = $this->table();
$hashOption = $table->getAttr('crypt/passwordHash');
$security = $this->getSecurity();
$newPassword = Base\Arr::index(0,$value);
$newPasswordConfirm = Base\Arr::index(1,$value);
$oldPassword = Base\Arr::index(2,$value);
$oldPasswordHash = (!empty($cell))? $cell->get():null;
Base\Str::typecastNotNull($newPassword,$newPasswordConfirm,$oldPassword,$oldPasswordHash);
$validate = Base\Crypt::passwordValidate($newPassword,$newPasswordConfirm,$oldPassword,$oldPasswordHash,$security,$hashOption);
if(!empty($validate))
{
$rule = $this->rulePattern(true);
$neg = 'changePassword'.ucfirst($validate);
static::catchable(['log'=>false,'errorLog'=>false],$neg,$rule);
}
else
$return = Base\Crypt::passwordHash($newPassword,$hashOption);
}
elseif(is_string($value) && strlen($value))
$return = $value;
if(empty($return))
$return = (!empty($cell))? $cell->value():null;
return $return;
}
// onCommitted
// lors d'un changement de mot de passe réussi sur une mise à jour seulement
// utilisé pour ajouter de la communication et un log, et le callback onChangePassword pour le user
final protected function onCommitted(Orm\Cell $cell,bool $insert,array $option)
{
if($insert === false)
{
$option = Base\Arr::plus(['onChange'=>true],$option);
$row = $cell->row();
$pos = 'changePassword/success';
$log = $this->getAttr('log');
if(!empty($option['log']) && $option['log'] === true && !empty($log))
$log::logCloseDown('changePassword',['key'=>'changePassword','bool'=>true,'pos'=>$pos,'neg'=>null]);
if($option['onChange'] === true)
$row->callThis(fn() => $this->onChangePassword());
if(!empty($option['com']) && $option['com'] === true)
$cell->com($pos,'pos');
}
return $this;
}
// inputs
// retourne les inputs à utiliser pour le formulaire
final public function inputs(?array $attr=null,bool $required=false):array
{
$return = [];
$lang = $this->db()->lang();
$name = $this->name().'[]';
$pattern = $this->rulePattern();
foreach (['newPassword','newPasswordConfirm'] as $value)
{
$placeholder = $lang->text('changePassword/'.$value);
$id = $attr['id'] ?? null;
$array = ['name'=>$name,'id'=>$id,'placeholder'=>$placeholder,'data-required'=>$required,'data-pattern'=>$pattern];
$return[$value] = $array;
if(is_array($attr) && array_key_exists('id',$attr))
unset($attr['id']);
}
return $return;
}
// getSecurity
// retourne le niveau de sécurité du mot de passe
final public function getSecurity():?string
{
return $this->getAttr('security');
}
}
// init
UserPassword::__init();
?>