Update user service to be more separated

This commit is contained in:
Dane Everitt 2017-08-04 19:11:41 -05:00
parent 8daec38622
commit 275c01bc37
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
12 changed files with 473 additions and 1361 deletions

View file

@ -22,7 +22,7 @@
* SOFTWARE.
*/
namespace Pterodactyl\Services;
namespace Pterodactyl\Services\Users;
use Illuminate\Foundation\Application;
use Illuminate\Contracts\Hashing\Hasher;
@ -32,7 +32,7 @@ use Pterodactyl\Notifications\AccountCreated;
use Pterodactyl\Services\Helpers\TemporaryPasswordService;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
class UserService
class CreationService
{
/**
* @var \Illuminate\Foundation\Application
@ -40,9 +40,9 @@ class UserService
protected $app;
/**
* @var \Illuminate\Database\Connection
* @var \Illuminate\Database\ConnectionInterface
*/
protected $database;
protected $connection;
/**
* @var \Illuminate\Contracts\Hashing\Hasher
@ -65,25 +65,25 @@ class UserService
protected $repository;
/**
* UserService constructor.
* CreationService constructor.
*
* @param \Illuminate\Foundation\Application $application
* @param \Illuminate\Notifications\ChannelManager $notification
* @param \Illuminate\Database\ConnectionInterface $database
* @param \Illuminate\Contracts\Hashing\Hasher $hasher
* @param \Pterodactyl\Services\Helpers\TemporaryPasswordService $passwordService
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $repository
* @param \Illuminate\Foundation\Application $application
* @param \Illuminate\Notifications\ChannelManager $notification
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Illuminate\Contracts\Hashing\Hasher $hasher
* @param \Pterodactyl\Services\Helpers\TemporaryPasswordService $passwordService
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $repository
*/
public function __construct(
Application $application,
ChannelManager $notification,
ConnectionInterface $database,
ConnectionInterface $connection,
Hasher $hasher,
TemporaryPasswordService $passwordService,
UserRepositoryInterface $repository
) {
$this->app = $application;
$this->database = $database;
$this->connection = $connection;
$this->hasher = $hasher;
$this->notification = $notification;
$this->passwordService = $passwordService;
@ -99,25 +99,22 @@ class UserService
* @throws \Exception
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
public function create(array $data)
public function handle(array $data)
{
if (array_key_exists('password', $data) && ! empty($data['password'])) {
$data['password'] = $this->hasher->make($data['password']);
}
// Begin Transaction
$this->database->beginTransaction();
$this->connection->beginTransaction();
if (! isset($data['password']) || empty($data['password'])) {
$data['password'] = $this->hasher->make(str_random(30));
$token = $this->passwordService->generateReset($data['email']);
}
$user = $this->repository->create($data);
$this->connection->commit();
// Persist the data
$this->database->commit();
// @todo fire event, handle notification there
$this->notification->send($user, $this->app->makeWith(AccountCreated::class, [
'user' => [
'name' => $user->name_first,
@ -128,24 +125,4 @@ class UserService
return $user;
}
/**
* Update the user model instance.
*
* @param int $id
* @param array $data
* @return mixed
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
public function update($id, array $data)
{
if (isset($data['password'])) {
$data['password'] = $this->hasher->make($data['password']);
}
$user = $this->repository->update($id, $data);
return $user;
}
}

View file

@ -0,0 +1,88 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Services\Users;
use Illuminate\Contracts\Translation\Translator;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Models\User;
class DeletionService
{
/**
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
*/
protected $repository;
/**
* @var \Illuminate\Contracts\Translation\Translator
*/
protected $translator;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
*/
protected $serverRepository;
/**
* DeletionService constructor.
*
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $serverRepository
* @param \Illuminate\Contracts\Translation\Translator $translator
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $repository
*/
public function __construct(
ServerRepositoryInterface $serverRepository,
Translator $translator,
UserRepositoryInterface $repository
) {
$this->repository = $repository;
$this->translator = $translator;
$this->serverRepository = $serverRepository;
}
/**
* Delete a user from the panel only if they have no servers attached to their account.
*
* @param int|\Pterodactyl\Models\User $user
* @return bool|null
*
* @throws \Pterodactyl\Exceptions\DisplayException
*/
public function handle($user)
{
if (! $user instanceof User) {
$user = $this->repository->find($user);
}
$servers = $this->serverRepository->findWhere([['owner_id', '=', $user->id]]);
if (count($servers) > 0) {
throw new DisplayException($this->translator->trans('admin/user.exceptions.user_has_servers'));
}
return $this->repository->delete($user->id);
}
}

View file

@ -0,0 +1,75 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Services\Users;
use Illuminate\Contracts\Hashing\Hasher;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
class UpdateService
{
/**
* @var \Illuminate\Contracts\Hashing\Hasher
*/
protected $hasher;
/**
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
*/
protected $repository;
/**
* UpdateService constructor.
*
* @param \Illuminate\Contracts\Hashing\Hasher $hasher
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $repository
*/
public function __construct(
Hasher $hasher,
UserRepositoryInterface $repository
) {
$this->hasher = $hasher;
$this->repository = $repository;
}
/**
* Update the user model instance.
*
* @param int $id
* @param array $data
* @return mixed
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
public function handle($id, array $data)
{
if (isset($data['password'])) {
$data['password'] = $this->hasher->make($data['password']);
}
$user = $this->repository->update($id, $data);
return $user;
}
}