1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Form\Element; |
4
|
|
|
|
5
|
|
|
class Password extends NamedFormElement |
6
|
|
|
{ |
7
|
|
|
public function __construct($path, $label = null) |
8
|
|
|
{ |
9
|
|
|
parent::__construct($path, $label); |
10
|
|
|
|
11
|
|
|
$this->setHtmlAttributes([ |
12
|
|
|
'class' => 'form-control', |
13
|
|
|
'type' => 'password', |
14
|
|
|
]); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
protected $allowEmpty = false; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $view = 'form.element.password'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param \Illuminate\Http\Request $request |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
public function save(\Illuminate\Http\Request $request) |
33
|
|
|
{ |
34
|
|
|
$value = $this->getValueFromModel(); |
35
|
|
|
|
36
|
|
|
if (! $this->isAllowedEmptyValue() && $this->getModel()->exists && empty($value)) { |
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
parent::save($request); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Checks if value exists only inside request instance. Otherwise it'll return null, because password hash |
45
|
|
|
* should not be returned from model and rendered inside forms. |
46
|
|
|
* |
47
|
|
|
* @return array|mixed|null|string |
48
|
|
|
*/ |
49
|
|
|
public function getValueFromModel() |
50
|
|
|
{ |
51
|
|
|
if (($value = $this->getValueFromRequest(request())) !== null) { |
|
|
|
|
52
|
|
|
return $value; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this->getDefaultValue(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
public function getValidationRules() |
62
|
|
|
{ |
63
|
|
|
$data = parent::getValidationRules(); |
64
|
|
|
|
65
|
|
|
if (! $this->isAllowedEmptyValue() && $this->getModel()->exists) { |
66
|
|
|
foreach ($data as $field => $rules) { |
67
|
|
|
foreach ($rules as $i => $rule) { |
68
|
|
|
if ($rule == 'required') { |
69
|
|
|
unset($data[$field][$i]); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $data; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function isAllowedEmptyValue() |
82
|
|
|
{ |
83
|
|
|
return $this->allowEmpty; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
|
|
public function allowEmptyValue() |
90
|
|
|
{ |
91
|
|
|
$this->allowEmpty = true; |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
|
|
public function hashWithBcrypt() |
100
|
|
|
{ |
101
|
|
|
return $this->mutateValue(function ($value) { |
102
|
|
|
return bcrypt($value); |
103
|
|
|
}); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
|
|
public function hashWithMD5() |
110
|
|
|
{ |
111
|
|
|
return $this->mutateValue(function ($value) { |
112
|
|
|
return md5($value); |
113
|
|
|
}); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
public function hashWithSHA1() |
120
|
|
|
{ |
121
|
|
|
return $this->mutateValue(function ($value) { |
122
|
|
|
return sha1($value); |
123
|
|
|
}); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|