2017-08-05 00:11:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Users;
|
|
|
|
|
2017-12-03 20:00:47 +00:00
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
use Illuminate\Support\Collection;
|
2017-08-05 00:11:41 +00:00
|
|
|
use Illuminate\Contracts\Hashing\Hasher;
|
2017-12-03 20:00:47 +00:00
|
|
|
use Pterodactyl\Traits\Services\HasUserLevels;
|
2017-08-05 00:11:41 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
2017-12-03 20:00:47 +00:00
|
|
|
use Pterodactyl\Services\DaemonKeys\RevokeMultipleDaemonKeysService;
|
2017-08-05 00:11:41 +00:00
|
|
|
|
2017-08-27 20:10:51 +00:00
|
|
|
class UserUpdateService
|
2017-08-05 00:11:41 +00:00
|
|
|
{
|
2017-12-03 20:00:47 +00:00
|
|
|
use HasUserLevels;
|
|
|
|
|
2017-08-05 00:11:41 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Hashing\Hasher
|
|
|
|
*/
|
2017-12-03 20:00:47 +00:00
|
|
|
private $hasher;
|
2017-08-05 00:11:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
|
|
|
|
*/
|
2017-12-03 20:00:47 +00:00
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\DaemonKeys\RevokeMultipleDaemonKeysService
|
|
|
|
*/
|
|
|
|
private $revocationService;
|
2017-08-05 00:11:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* UpdateService constructor.
|
|
|
|
*
|
2017-12-03 20:00:47 +00:00
|
|
|
* @param \Illuminate\Contracts\Hashing\Hasher $hasher
|
|
|
|
* @param \Pterodactyl\Services\DaemonKeys\RevokeMultipleDaemonKeysService $revocationService
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $repository
|
2017-08-05 00:11:41 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
Hasher $hasher,
|
2017-12-03 20:00:47 +00:00
|
|
|
RevokeMultipleDaemonKeysService $revocationService,
|
2017-08-05 00:11:41 +00:00
|
|
|
UserRepositoryInterface $repository
|
|
|
|
) {
|
|
|
|
$this->hasher = $hasher;
|
|
|
|
$this->repository = $repository;
|
2017-12-03 20:00:47 +00:00
|
|
|
$this->revocationService = $revocationService;
|
2017-08-05 00:11:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-03 20:00:47 +00:00
|
|
|
* Update the user model instance. If the user has been removed as an administrator
|
|
|
|
* revoke all of the authentication tokens that have beenn assigned to their account.
|
2017-08-05 00:11:41 +00:00
|
|
|
*
|
2017-12-03 20:00:47 +00:00
|
|
|
* @param \Pterodactyl\Models\User $user
|
|
|
|
* @param array $data
|
|
|
|
* @return \Illuminate\Support\Collection
|
2017-08-05 00:11:41 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
2017-08-31 02:11:14 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
2017-08-05 00:11:41 +00:00
|
|
|
*/
|
2017-12-03 20:00:47 +00:00
|
|
|
public function handle(User $user, array $data): Collection
|
2017-08-05 00:11:41 +00:00
|
|
|
{
|
2018-02-08 03:13:40 +00:00
|
|
|
if (! empty(array_get($data, 'password'))) {
|
2017-08-05 00:11:41 +00:00
|
|
|
$data['password'] = $this->hasher->make($data['password']);
|
2018-02-08 03:13:40 +00:00
|
|
|
} else {
|
|
|
|
unset($data['password']);
|
2017-08-05 00:11:41 +00:00
|
|
|
}
|
|
|
|
|
2017-12-03 20:00:47 +00:00
|
|
|
if ($this->isUserLevel(User::USER_LEVEL_ADMIN)) {
|
|
|
|
if (array_get($data, 'root_admin', 0) == 0 && $user->root_admin) {
|
|
|
|
$this->revocationService->handle($user, array_get($data, 'ignore_connection_error', false));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unset($data['root_admin']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return collect([
|
|
|
|
'model' => $this->repository->update($user->id, $data),
|
|
|
|
'exceptions' => $this->revocationService->getExceptions(),
|
|
|
|
]);
|
2017-08-05 00:11:41 +00:00
|
|
|
}
|
|
|
|
}
|