|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* balloon |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copryright (c) 2012-2018 gyselroth GmbH (https://gyselroth.com) |
|
9
|
|
|
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Balloon\App\Cli\Console; |
|
13
|
|
|
|
|
14
|
|
|
use Balloon\Server; |
|
15
|
|
|
use GetOpt\GetOpt; |
|
16
|
|
|
use MongoDB\BSON\Binary; |
|
17
|
|
|
use MongoDB\BSON\ObjectId; |
|
18
|
|
|
use Psr\Log\LoggerInterface; |
|
19
|
|
|
|
|
20
|
|
|
class User |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Getopt. |
|
24
|
|
|
* |
|
25
|
|
|
* @var GetOpt |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $getopt; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Logger. |
|
31
|
|
|
* |
|
32
|
|
|
* @var LoggerInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $logger; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Server. |
|
38
|
|
|
* |
|
39
|
|
|
* @var Server |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $server; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Constructor. |
|
45
|
|
|
* |
|
46
|
|
|
* @param Server $server |
|
47
|
|
|
* @param LoggerInterface $logger |
|
48
|
|
|
* @param GetOpt $getopt |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(Server $server, LoggerInterface $logger, GetOpt $getopt) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->server = $server; |
|
53
|
|
|
$this->logger = $logger; |
|
54
|
|
|
$this->getopt = $getopt; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get help. |
|
59
|
|
|
*/ |
|
60
|
|
|
public function help(): User |
|
61
|
|
|
{ |
|
62
|
|
|
echo "add\n"; |
|
63
|
|
|
echo "Add a new user\n\n"; |
|
64
|
|
|
|
|
65
|
|
|
echo "edit\n"; |
|
66
|
|
|
echo "Edit a user\n\n"; |
|
67
|
|
|
echo $this->getopt->getHelpText(); |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/* |
|
73
|
|
|
* Get operands |
|
74
|
|
|
* |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
|
|
public static function getOperands(): array |
|
78
|
|
|
{ |
|
79
|
|
|
return [ |
|
80
|
|
|
\GetOpt\Operand::create('action', \GetOpt\Operand::REQUIRED), |
|
81
|
|
|
\GetOpt\Operand::create('id', \GetOpt\Operand::OPTIONAL), |
|
82
|
|
|
]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get user options. |
|
87
|
|
|
* |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
|
|
public static function getOptions(): array |
|
91
|
|
|
{ |
|
92
|
|
|
return [ |
|
93
|
|
|
\GetOpt\Option::create('u', 'username', GetOpt::REQUIRED_ARGUMENT) |
|
94
|
|
|
->setDescription('Specify the username [REQUIRED]'), |
|
95
|
|
|
\GetOpt\Option::create('p', 'password', GetOpt::REQUIRED_ARGUMENT) |
|
96
|
|
|
->setDescription('Specify a password'), |
|
97
|
|
|
\GetOpt\Option::create('a', 'admin', GetOpt::NO_ARGUMENT) |
|
98
|
|
|
->setDescription('Admin account flag'), |
|
99
|
|
|
\GetOpt\Option::create('A', 'avatar', GetOpt::REQUIRED_ARGUMENT) |
|
100
|
|
|
->setDescription('Set an avatar image (Path/URL to JPEG image)'), |
|
101
|
|
|
\GetOpt\Option::create('m', 'mail', GetOpt::REQUIRED_ARGUMENT) |
|
102
|
|
|
->setDescription('Mail address'), |
|
103
|
|
|
\GetOpt\Option::create('s', 'softquota', GetOpt::REQUIRED_ARGUMENT) |
|
104
|
|
|
->setDescription('Softquota in bytes'), |
|
105
|
|
|
\GetOpt\Option::create('H', 'hardquota', GetOpt::REQUIRED_ARGUMENT) |
|
106
|
|
|
->setDescription('Hardquota in bytes'), |
|
107
|
|
|
\GetOpt\Option::create('n', 'namespace', GetOpt::REQUIRED_ARGUMENT) |
|
108
|
|
|
->setDescription('A namespace'), |
|
109
|
|
|
\GetOpt\Option::create('L', 'locale', GetOpt::REQUIRED_ARGUMENT) |
|
110
|
|
|
->setDescription('A Locale (Example: en_US)'), |
|
111
|
|
|
]; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Start. |
|
116
|
|
|
* |
|
117
|
|
|
* @return bool |
|
118
|
|
|
*/ |
|
119
|
|
View Code Duplication |
public function add(): bool |
|
|
|
|
|
|
120
|
|
|
{ |
|
121
|
|
|
$options = $this->parseParams(); |
|
122
|
|
|
$result = $this->server->addUser($this->getopt->getOption('username'), $options); |
|
123
|
|
|
|
|
124
|
|
|
$this->logger->info('new user ['.$result.'] created', [ |
|
125
|
|
|
'category' => get_class($this), |
|
126
|
|
|
]); |
|
127
|
|
|
|
|
128
|
|
|
return true; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Start. |
|
133
|
|
|
* |
|
134
|
|
|
* @return bool |
|
135
|
|
|
*/ |
|
136
|
|
|
public function edit(): bool |
|
137
|
|
|
{ |
|
138
|
|
|
$id = new ObjectId($this->getopt->getOperand('id')); |
|
139
|
|
|
$options = $this->parseParams(); |
|
140
|
|
|
|
|
141
|
|
|
$user = $this->server->getUserById($id); |
|
142
|
|
|
|
|
143
|
|
|
$this->logger->info('update user ['.$user->getId().']', [ |
|
144
|
|
|
'category' => get_class($this), |
|
145
|
|
|
]); |
|
146
|
|
|
|
|
147
|
|
|
if ($this->getopt->getOption('username') !== null) { |
|
148
|
|
|
$options['username'] = $this->getopt->getOption('username'); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$user->setAttributes($options); |
|
152
|
|
|
|
|
153
|
|
|
return true; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Parse params. |
|
158
|
|
|
* |
|
159
|
|
|
* @return arrray |
|
160
|
|
|
*/ |
|
161
|
|
|
protected function parseParams(): array |
|
162
|
|
|
{ |
|
163
|
|
|
$map = [ |
|
164
|
|
|
'mail' => 'mail', |
|
165
|
|
|
'softquota' => 'soft_quota', |
|
166
|
|
|
'hardquota' => 'hard_quota', |
|
167
|
|
|
'namespace' => 'namespace', |
|
168
|
|
|
'password' => 'password', |
|
169
|
|
|
'locale' => 'locale', |
|
170
|
|
|
'avatar' => 'avatar', |
|
171
|
|
|
'admin' => 'admin', |
|
172
|
|
|
]; |
|
173
|
|
|
|
|
174
|
|
|
$options = array_intersect_key($this->getopt->getOptions(), $map); |
|
175
|
|
|
ksort($options); |
|
176
|
|
|
$map = array_intersect_key($map, $options); |
|
177
|
|
|
ksort($map); |
|
178
|
|
|
$options = array_combine(array_values($map), $options); |
|
179
|
|
|
|
|
180
|
|
|
if (isset($options['avatar'])) { |
|
181
|
|
|
$options['avatar'] = new Binary(file_get_contents($options['avatar']), Binary::TYPE_GENERIC); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
return $options; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.