1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* sysPass |
4
|
|
|
* |
5
|
|
|
* @author nuxsmin |
6
|
|
|
* @link https://syspass.org |
7
|
|
|
* @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org |
8
|
|
|
* |
9
|
|
|
* This file is part of sysPass. |
10
|
|
|
* |
11
|
|
|
* sysPass is free software: you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU General Public License as published by |
13
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
14
|
|
|
* (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* sysPass is distributed in the hope that it will be useful, |
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19
|
|
|
* GNU General Public License for more details. |
20
|
|
|
* |
21
|
|
|
* You should have received a copy of the GNU General Public License |
22
|
|
|
* along with sysPass. If not, see <http://www.gnu.org/licenses/>. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace SP\Services\Upgrade; |
26
|
|
|
|
27
|
|
|
use SP\Core\Acl\ActionsInterface; |
28
|
|
|
use SP\Core\Events\Event; |
29
|
|
|
use SP\Core\Events\EventMessage; |
30
|
|
|
use SP\DataModel\CustomFieldDefDataOld; |
31
|
|
|
use SP\DataModel\CustomFieldDefinitionData; |
32
|
|
|
use SP\Services\CustomField\CustomFieldDefService; |
33
|
|
|
use SP\Services\CustomField\CustomFieldTypeService; |
34
|
|
|
use SP\Services\Service; |
35
|
|
|
use SP\Storage\Database\Database; |
36
|
|
|
use SP\Storage\Database\QueryData; |
37
|
|
|
use SP\Util\Util; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Class UpgradeCustomField |
41
|
|
|
* |
42
|
|
|
* @package SP\Services\Upgrade |
43
|
|
|
*/ |
44
|
|
|
final class UpgradeCustomFieldDefinition extends Service |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* @var \SP\Storage\Database\Database |
48
|
|
|
*/ |
49
|
|
|
private $db; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* upgrade_300_18010101 |
53
|
|
|
* |
54
|
|
|
* @throws \Exception |
55
|
|
|
*/ |
56
|
|
|
public function upgrade_300_18010101() |
57
|
|
|
{ |
58
|
|
|
$this->eventDispatcher->notifyEvent('upgrade.customField.start', |
59
|
|
|
new Event($this, EventMessage::factory() |
60
|
|
|
->addDescription(__u('Custom fields update')) |
61
|
|
|
->addDescription(__FUNCTION__)) |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
try { |
65
|
|
|
$customFieldTypeService = $this->dic->get(CustomFieldTypeService::class); |
66
|
|
|
|
67
|
|
|
$customFieldType = []; |
68
|
|
|
|
69
|
|
|
foreach ($customFieldTypeService->getAll() as $customFieldTypeData) { |
70
|
|
|
$customFieldType[$customFieldTypeData->getName()] = $customFieldTypeData->getId(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->transactionAware(function () use ($customFieldType) { |
74
|
|
|
$customFieldDefService = $this->dic->get(CustomFieldDefService::class); |
75
|
|
|
|
76
|
|
|
$queryData = new QueryData(); |
77
|
|
|
$queryData->setQuery('SELECT id, moduleId, field FROM CustomFieldDefinition WHERE field IS NOT NULL'); |
78
|
|
|
|
79
|
|
|
foreach ($this->db->doSelect($queryData)->getDataAsArray() as $item) { |
80
|
|
|
/** @var CustomFieldDefDataOld $data */ |
81
|
|
|
$data = Util::unserialize(CustomFieldDefDataOld::class, $item->field, 'SP\DataModel\CustomFieldDefData'); |
82
|
|
|
|
83
|
|
|
$itemData = new CustomFieldDefinitionData(); |
84
|
|
|
$itemData->setId($item->id); |
85
|
|
|
$itemData->setModuleId($this->moduleMapper((int)$item->moduleId)); |
86
|
|
|
$itemData->setName($data->getName()); |
87
|
|
|
$itemData->setHelp($data->getHelp()); |
88
|
|
|
$itemData->setRequired($data->isRequired()); |
|
|
|
|
89
|
|
|
$itemData->setShowInList($data->isShowInItemsList()); |
|
|
|
|
90
|
|
|
$itemData->setTypeId($customFieldType[$this->typeMapper((int)$data->getType())]); |
91
|
|
|
|
92
|
|
|
$customFieldDefService->updateRaw($itemData); |
93
|
|
|
|
94
|
|
|
$this->eventDispatcher->notifyEvent('upgrade.customField.process', |
95
|
|
|
new Event($this, EventMessage::factory() |
96
|
|
|
->addDescription(__u('Field updated')) |
97
|
|
|
->addDetail(__u('Field'), $data->getName())) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
}); |
101
|
|
|
} catch (\Exception $e) { |
102
|
|
|
processException($e); |
103
|
|
|
|
104
|
|
|
$this->eventDispatcher->notifyEvent('exception', new Event($e)); |
105
|
|
|
|
106
|
|
|
throw $e; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->eventDispatcher->notifyEvent('upgrade.customField.end', |
110
|
|
|
new Event($this, EventMessage::factory() |
111
|
|
|
->addDescription(__u('Custom fields update')) |
112
|
|
|
->addDescription(__FUNCTION__)) |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param int $moduleId |
118
|
|
|
* |
119
|
|
|
* @return int |
120
|
|
|
*/ |
121
|
|
|
private function moduleMapper(int $moduleId) |
122
|
|
|
{ |
123
|
|
|
switch ($moduleId) { |
124
|
|
|
case 10: |
125
|
|
|
return ActionsInterface::ACCOUNT; |
126
|
|
|
case 61: |
127
|
|
|
return ActionsInterface::CATEGORY; |
128
|
|
|
case 62: |
129
|
|
|
return ActionsInterface::CLIENT; |
130
|
|
|
case 71: |
131
|
|
|
return ActionsInterface::USER; |
132
|
|
|
case 72: |
133
|
|
|
return ActionsInterface::GROUP; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $moduleId; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param int $typeId |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
private function typeMapper(int $typeId) |
144
|
|
|
{ |
145
|
|
|
$types = [ |
146
|
|
|
1 => 'text', |
147
|
|
|
2 => 'password', |
148
|
|
|
3 => 'date', |
149
|
|
|
4 => 'number', |
150
|
|
|
5 => 'email', |
151
|
|
|
6 => 'tel', |
152
|
|
|
7 => 'url', |
153
|
|
|
8 => 'color', |
154
|
|
|
9 => 'text', |
155
|
|
|
10 => 'textarea' |
156
|
|
|
]; |
157
|
|
|
|
158
|
|
|
return isset($types[$typeId]) ? $types[$typeId] : $types[1]; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* upgrade_300_18072901 |
163
|
|
|
* |
164
|
|
|
* @throws \Exception |
165
|
|
|
*/ |
166
|
|
|
public function upgrade_300_18072901() |
167
|
|
|
{ |
168
|
|
|
$this->eventDispatcher->notifyEvent('upgrade.customField.start', |
169
|
|
|
new Event($this, EventMessage::factory() |
170
|
|
|
->addDescription(__u('Custom fields update')) |
171
|
|
|
->addDescription(__FUNCTION__)) |
172
|
|
|
); |
173
|
|
|
|
174
|
|
|
try { |
175
|
|
|
$this->transactionAware(function () { |
176
|
|
|
$customFieldDefService = $this->dic->get(CustomFieldDefService::class); |
177
|
|
|
|
178
|
|
|
foreach ($customFieldDefService->getAllBasic() as $item) { |
179
|
|
|
|
180
|
|
|
$itemData = clone $item; |
181
|
|
|
$itemData->setModuleId($this->moduleMapper((int)$item->getModuleId())); |
182
|
|
|
|
183
|
|
|
$customFieldDefService->updateRaw($itemData); |
184
|
|
|
|
185
|
|
|
$this->eventDispatcher->notifyEvent('upgrade.customField.process', |
186
|
|
|
new Event($this, EventMessage::factory() |
187
|
|
|
->addDescription(__u('Field updated')) |
188
|
|
|
->addDetail(__u('Field'), $item->getName())) |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
}); |
192
|
|
|
} catch (\Exception $e) { |
193
|
|
|
processException($e); |
194
|
|
|
|
195
|
|
|
$this->eventDispatcher->notifyEvent('exception', new Event($e)); |
196
|
|
|
|
197
|
|
|
throw $e; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$this->eventDispatcher->notifyEvent('upgrade.customField.end', |
201
|
|
|
new Event($this, EventMessage::factory() |
202
|
|
|
->addDescription(__u('Custom fields update')) |
203
|
|
|
->addDescription(__FUNCTION__)) |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
protected function initialize() |
208
|
|
|
{ |
209
|
|
|
$this->db = $this->dic->get(Database::class); |
210
|
|
|
} |
211
|
|
|
} |