Skip to content

Commit 542d1f8

Browse files
committed
Add new location and user management via CLI
1 parent a498bbc commit 542d1f8

File tree

10 files changed

+395
-33
lines changed

10 files changed

+395
-33
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
/*
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Console\Commands\Location;
26+
27+
use Illuminate\Console\Command;
28+
use Pterodactyl\Services\Locations\LocationDeletionService;
29+
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
30+
31+
class DeleteLocationCommand extends Command
32+
{
33+
/**
34+
* @var \Pterodactyl\Services\Locations\LocationDeletionService
35+
*/
36+
protected $deletionService;
37+
38+
/**
39+
* @var \Illuminate\Support\Collection
40+
*/
41+
protected $locations;
42+
43+
/**
44+
* @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface
45+
*/
46+
protected $repository;
47+
48+
/**
49+
* @var string
50+
*/
51+
protected $signature = 'p:location:delete
52+
{--minimal : Passing this flag will hide the list of current locations.}
53+
{--short= : The short code of the location to delete.}';
54+
55+
/**
56+
* DeleteLocationCommand constructor.
57+
*
58+
* @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $repository
59+
* @param \Pterodactyl\Services\Locations\LocationDeletionService $deletionService
60+
*/
61+
public function __construct(
62+
LocationDeletionService $deletionService,
63+
LocationRepositoryInterface $repository
64+
) {
65+
parent::__construct();
66+
67+
$this->deletionService = $deletionService;
68+
$this->repository = $repository;
69+
}
70+
71+
/**
72+
* Respond to the command request.
73+
*
74+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
75+
* @throws \Pterodactyl\Exceptions\Service\Location\HasActiveNodesException
76+
*/
77+
public function handle()
78+
{
79+
$this->locations = $this->locations ?? $this->repository->getAllWithNodes();
80+
$short = $this->option('short') ?? $this->anticipate(
81+
trans('command/messages.location.ask_short'), $this->locations->pluck('short')->toArray()
82+
);
83+
84+
$location = $this->locations->where('short', $short)->first();
85+
if (is_null($location)) {
86+
$this->error(trans('command/messages.location.no_location_found'));
87+
if (is_null($this->option('short')) && ! $this->option('no-interaction')) {
88+
$this->handle();
89+
}
90+
91+
return;
92+
}
93+
94+
$this->line(trans('command/messages.location.deleted'));
95+
$this->deletionService->handle($location->id);
96+
}
97+
}

app/Console/Commands/AddLocation.php renamed to app/Console/Commands/Location/MakeLocationCommand.php

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,51 +22,56 @@
2222
* SOFTWARE.
2323
*/
2424

25-
namespace Pterodactyl\Console\Commands;
25+
namespace Pterodactyl\Console\Commands\Location;
2626

2727
use Illuminate\Console\Command;
28+
use Pterodactyl\Services\Locations\LocationCreationService;
2829

29-
class AddLocation extends Command
30+
class MakeLocationCommand extends Command
3031
{
31-
protected $data = [];
32+
/**
33+
* @var \Pterodactyl\Services\Locations\LocationCreationService
34+
*/
35+
protected $creationService;
3236

3337
/**
34-
* The name and signature of the console command.
35-
*
3638
* @var string
3739
*/
38-
protected $signature = 'pterodactyl:location
39-
{--short= : The shortcode name of this location (ex. us1).}
40-
{--long= : A longer description of this location.}';
40+
protected $signature = 'p:location:make
41+
{--short= : The shortcode name of this location (ex. us1).}
42+
{--long= : A longer description of this location.}';
4143

4244
/**
43-
* The console command description.
44-
*
4545
* @var string
4646
*/
4747
protected $description = 'Creates a new location on the system via the CLI.';
4848

4949
/**
5050
* Create a new command instance.
51+
*
52+
* @param \Pterodactyl\Services\Locations\LocationCreationService $creationService
5153
*/
52-
public function __construct()
54+
public function __construct(LocationCreationService $creationService)
5355
{
5456
parent::__construct();
57+
58+
$this->creationService = $creationService;
5559
}
5660

5761
/**
58-
* Execute the console command.
62+
* Handle the command execution process.
5963
*
60-
* @return mixed
64+
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
6165
*/
6266
public function handle()
6367
{
64-
$this->data['short'] = (is_null($this->option('short'))) ? $this->ask('Location Short Code') : $this->option('short');
65-
$this->data['long'] = (is_null($this->option('long'))) ? $this->ask('Location Description') : $this->option('long');
66-
67-
$repo = new LocationRepository;
68-
$id = $repo->create($this->data);
68+
$short = $this->option('short') ?? $this->ask(trans('command/messages.location.ask_short'));
69+
$long = $this->option('long') ?? $this->ask(trans('command/messages.location.ask_long'));
6970

70-
$this->info('Location ' . $this->data['short'] . ' created with ID: ' . $id);
71+
$location = $this->creationService->handle(compact('short', 'long'));
72+
$this->line(trans('command/messages.location.created', [
73+
'name' => $location->short,
74+
'id' => $location->id,
75+
]));
7176
}
7277
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
/*
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Console\Commands\User;
26+
27+
use Webmozart\Assert\Assert;
28+
use Illuminate\Console\Command;
29+
use Pterodactyl\Services\Users\UserDeletionService;
30+
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
31+
32+
class DeleteUserCommand extends Command
33+
{
34+
/**
35+
* @var \Pterodactyl\Services\Users\UserDeletionService
36+
*/
37+
protected $deletionService;
38+
39+
/**
40+
* @var string
41+
*/
42+
protected $description = 'Deletes a user from the Panel if no servers are attached to their account.';
43+
44+
/**
45+
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
46+
*/
47+
protected $repository;
48+
49+
/**
50+
* @var string
51+
*/
52+
protected $signature = 'p:user:delete {--user=}';
53+
54+
public function __construct(
55+
UserDeletionService $deletionService,
56+
UserRepositoryInterface $repository
57+
) {
58+
parent::__construct();
59+
60+
$this->deletionService = $deletionService;
61+
$this->repository = $repository;
62+
}
63+
64+
/**
65+
* @return bool
66+
* @throws \Pterodactyl\Exceptions\DisplayException
67+
*/
68+
public function handle()
69+
{
70+
$search = $this->option('user') ?? $this->ask(trans('command/messages.user.search_users'));
71+
Assert::notEmpty($search, 'Search term must be a non-null value, received %s.');
72+
73+
$results = $this->repository->search($search)->all();
74+
if (count($results) < 1) {
75+
$this->error(trans('command/messages.user.no_users_found'));
76+
if (! $this->option('no-interaction')) {
77+
return $this->handle();
78+
}
79+
80+
return false;
81+
}
82+
83+
if (! $this->option('no-interaction')) {
84+
$tableValues = [];
85+
foreach ($results as $user) {
86+
$tableValues[] = [$user->id, $user->email, $user->name];
87+
}
88+
89+
$this->table(['User ID', 'Email', 'Name'], $tableValues);
90+
if (! $deleteUser = $this->ask(trans('command/messages.user.select_search_user'))) {
91+
return $this->handle();
92+
}
93+
} else {
94+
if (count($results) > 1) {
95+
$this->error(trans('command/messages.user.multiple_found'));
96+
97+
return false;
98+
}
99+
100+
$deleteUser = $results->first();
101+
}
102+
103+
if ($this->confirm(trans('command/messages.user.confirm_delete')) || $this->option('no-interaction')) {
104+
$this->deletionService->handle($deleteUser);
105+
$this->info(trans('command/messages.user.deleted'));
106+
}
107+
}
108+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/*
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Console\Commands\User;
26+
27+
use Illuminate\Console\Command;
28+
use Pterodactyl\Services\Users\UserCreationService;
29+
30+
class MakeUserCommand extends Command
31+
{
32+
/**
33+
* @var \Pterodactyl\Services\Users\UserCreationService
34+
*/
35+
protected $creationService;
36+
37+
protected $signature = 'p:user:make {--email=} {--username=} {--name-first=} {--name-last=} {--password=} {--no-password}';
38+
39+
/**
40+
* MakeUserCommand constructor.
41+
*
42+
* @param \Pterodactyl\Services\Users\UserCreationService $creationService
43+
*/
44+
public function __construct(UserCreationService $creationService)
45+
{
46+
parent::__construct();
47+
48+
$this->creationService = $creationService;
49+
}
50+
51+
/**
52+
* Handle command request to create a new user.
53+
*
54+
* @throws \Exception
55+
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
56+
*/
57+
public function handle()
58+
{
59+
$email = $this->option('email') ?? $this->ask(trans('command/messages.user.ask_email'));
60+
$username = $this->option('username') ?? $this->ask(trans('command/messages.user.ask_username'));
61+
$name_first = $this->option('name-first') ?? $this->ask(trans('command/messages.user.ask_name_first'));
62+
$name_last = $this->option('name-last') ?? $this->ask(trans('command/messages.user.ask_name_last'));
63+
64+
if (is_null($password = $this->option('password')) && ! $this->option('no-password')) {
65+
$this->warn(trans('command/messages.user.ask_password_help'));
66+
$this->line(trans('command/messages.user.ask_password_tip'));
67+
$password = $this->secret(trans('command/messages.user.ask_password'));
68+
}
69+
70+
$user = $this->creationService->handle(compact('email', 'username', 'name_first', 'name_last', 'password'));
71+
$this->table(['Field', 'Value'], [
72+
['UUID', $user->uuid],
73+
['Email', $user->email],
74+
['Username', $user->username],
75+
['Name', $user->name],
76+
]);
77+
}
78+
}

0 commit comments

Comments
 (0)