1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\PasswordSaver\Model\Fields; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ORM\DataObject; |
6
|
|
|
use SilverStripe\ORM\FieldType\DBVarchar; |
7
|
|
|
use Sunnysideup\PasswordSaver\Form\ClientSidePasswordField; |
8
|
|
|
use Sunnysideup\UUDI\Api\HashCreator; |
|
|
|
|
9
|
|
|
|
10
|
|
|
class DBClientSidePassword extends DBVarchar |
11
|
|
|
{ |
12
|
|
|
protected $fieldLength = 7; |
13
|
|
|
|
14
|
|
|
private static $casting = [ |
15
|
|
|
'FormGet' => 'HTMLText', |
16
|
|
|
'FormSet' => 'HTMLText', |
17
|
|
|
]; |
18
|
|
|
|
19
|
|
|
public function __construct($name = null, $size = 7, $options = []) |
20
|
|
|
{ |
21
|
|
|
if ((int) $size) { |
22
|
|
|
$this->fieldLength = (int) $size; |
23
|
|
|
} |
24
|
|
|
parent::__construct($name, $size, $options); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public static function get_unique_value(?int $size = 7): string |
28
|
|
|
{ |
29
|
|
|
if (! $size) { |
|
|
|
|
30
|
|
|
$size = 7; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return HashCreator::generate_hash($size); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Remove ugly parts of a url to make it nice. |
38
|
|
|
*/ |
39
|
|
|
public function Nice() |
40
|
|
|
{ |
41
|
|
|
return $this->value; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Scaffold the ExternalURLField for this ExternalURL. |
46
|
|
|
* |
47
|
|
|
* @param null|mixed $title |
48
|
|
|
* @param null|mixed $params |
49
|
|
|
*/ |
50
|
|
|
public function scaffoldFormField($title = null, $params = null) |
51
|
|
|
{ |
52
|
|
|
return new ClientSidePasswordField($this->name, $title); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function forTemplate() |
56
|
|
|
{ |
57
|
|
|
if ($this->value) { |
58
|
|
|
return $this->Nice(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return '(no code available)'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Saves this field to the given data object. |
66
|
|
|
* |
67
|
|
|
* @param DataObject $dataObject |
68
|
|
|
*/ |
69
|
|
|
public function saveInto($dataObject) |
70
|
|
|
{ |
71
|
|
|
$fieldName = $this->name; |
72
|
|
|
if (empty($fieldName)) { |
73
|
|
|
throw new \BadMethodCallException( |
74
|
|
|
"DBField::saveInto() Called on a nameless '" . static::class . "' object" |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
if (! $dataObject->{$fieldName}) { |
78
|
|
|
$dataObject->{$fieldName} = self::get_unique_value($this->fieldLength); |
79
|
|
|
} |
80
|
|
|
$length = strlen($dataObject->{$fieldName}); |
81
|
|
|
if ($length < $this->fieldLength) { |
82
|
|
|
$dataObject->{$fieldName} .= self::get_unique_value($this->fieldLength - $length); |
83
|
|
|
} |
84
|
|
|
if ($length > $this->fieldLength) { |
85
|
|
|
$dataObject->{$fieldName} = substr($dataObject->{$fieldName}, 0, $this->fieldLength); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function FormGet(string $link, ?string $name = 'get'): string |
90
|
|
|
{ |
91
|
|
|
return $this->formInner($link, $name); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function FormSet(string $link, ?string $name = 'set'): string |
95
|
|
|
{ |
96
|
|
|
return $this->formInner($link, $name); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function formInner(string $link, string $name): string |
100
|
|
|
{ |
101
|
|
|
return ' |
102
|
|
|
<form method="post" action="' . $link . '" class="password-getter-setter-form" formtarget="_blank" target="_blank" rel="noreferrer noopener"> |
103
|
|
|
<input type="hidden" value="' . $this->value . '" name="Code" /> |
104
|
|
|
<button type="submit" />' . $name . '</button> |
105
|
|
|
</form> |
106
|
|
|
'; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths