1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of FlexPHP. |
4
|
|
|
* |
5
|
|
|
* (c) Freddie Gar <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace FlexPHP\Database\Factories\User; |
11
|
|
|
|
12
|
|
|
final class SQLSrvUserFactory extends AbstractUserFactory |
13
|
|
|
{ |
14
|
|
|
public const MAPPING_PERMISSION = [ |
15
|
|
|
'ALL PRIVILEGES' => 'ALL', |
16
|
|
|
'CREATE' => 'CREATE', |
17
|
|
|
'DROP' => 'CONTROL', |
18
|
|
|
'DELETE' => 'DELETE', |
19
|
|
|
'INSERT' => 'INSERT', |
20
|
|
|
'SELECT' => 'SELECT', |
21
|
|
|
'UPDATE' => 'UPDATE', |
22
|
|
|
'GRANT OPTION' => 'GRANT', |
23
|
|
|
]; |
24
|
|
|
|
25
|
4 |
|
public function asCreate(): string |
26
|
|
|
{ |
27
|
4 |
|
return \sprintf(<<<T |
28
|
4 |
|
CREATE LOGIN %1\$s WITH PASSWORD = '%2\$s'; |
29
|
|
|
GO |
30
|
|
|
CREATE USER %1\$s FOR LOGIN %1\$s; |
31
|
|
|
GO |
32
|
|
|
T |
33
|
4 |
|
, $this->name, $this->password); |
34
|
|
|
} |
35
|
|
|
|
36
|
2 |
|
public function asDrop(): string |
37
|
|
|
{ |
38
|
2 |
|
return \sprintf(<<<T |
39
|
2 |
|
DROP USER %s; |
40
|
|
|
GO |
41
|
|
|
T |
42
|
2 |
|
, $this->name); |
43
|
|
|
} |
44
|
|
|
|
45
|
27 |
|
public function asPrivileges(): string |
46
|
|
|
{ |
47
|
27 |
|
$privileges = []; |
48
|
|
|
|
49
|
27 |
|
foreach ($this->permissions as $permission) { |
50
|
27 |
|
$perm = $this->getPermission($permission['permission']); |
51
|
27 |
|
$database = $this->getDatabase($permission['database']); |
52
|
27 |
|
$table = $this->getTable($permission['table']); |
53
|
27 |
|
$scope = $this->getScope($database, $table); |
54
|
|
|
|
55
|
27 |
|
$privileges[] = empty($scope) |
56
|
9 |
|
? $this->getTemplate($perm) |
57
|
18 |
|
: $this->getTemplateScope($perm, $scope); |
58
|
|
|
} |
59
|
|
|
|
60
|
27 |
|
return \implode("\n", $privileges); |
61
|
|
|
} |
62
|
|
|
|
63
|
27 |
|
private function getPermission(string $permission): string |
64
|
|
|
{ |
65
|
27 |
|
return $this::MAPPING_PERMISSION[$permission] ?? ''; |
66
|
|
|
} |
67
|
|
|
|
68
|
27 |
|
private function getDatabase(string $database): string |
69
|
|
|
{ |
70
|
27 |
|
if (empty($database) || $database === '*') { |
71
|
9 |
|
return ''; |
72
|
|
|
} |
73
|
|
|
|
74
|
18 |
|
return $database; |
75
|
|
|
} |
76
|
|
|
|
77
|
27 |
|
private function getTable(string $table): string |
78
|
|
|
{ |
79
|
27 |
|
if (empty($table) || $table === '*') { |
80
|
17 |
|
return ''; |
81
|
|
|
} |
82
|
|
|
|
83
|
10 |
|
return $table; |
84
|
|
|
} |
85
|
|
|
|
86
|
27 |
|
private function getScope(string $database, string $table): string |
87
|
|
|
{ |
88
|
27 |
|
$scope = ''; |
89
|
|
|
|
90
|
27 |
|
if (!empty($database)) { |
91
|
18 |
|
$scope .= $database; |
92
|
|
|
} |
93
|
|
|
|
94
|
27 |
|
if (!empty($table)) { |
95
|
10 |
|
$scope .= '.' . $table; |
96
|
|
|
} |
97
|
|
|
|
98
|
27 |
|
if (!empty($scope)) { |
99
|
18 |
|
$scope = 'ON ' . $scope; |
100
|
|
|
} |
101
|
|
|
|
102
|
27 |
|
return $scope; |
103
|
|
|
} |
104
|
|
|
|
105
|
9 |
|
private function getTemplate(string $permission): string |
106
|
|
|
{ |
107
|
9 |
|
return \sprintf(<<<T |
108
|
9 |
|
GRANT %s TO %s; |
109
|
|
|
GO |
110
|
|
|
T |
111
|
9 |
|
, $permission, $this->name); |
112
|
|
|
} |
113
|
|
|
|
114
|
18 |
|
private function getTemplateScope(string $permission, string $scope): string |
115
|
|
|
{ |
116
|
18 |
|
return \sprintf(<<<T |
117
|
18 |
|
GRANT %s %s TO %s; |
118
|
|
|
GO |
119
|
|
|
T |
120
|
18 |
|
, $permission, $scope, $this->name); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|