2017-06-14 04:25:37 +00:00
|
|
|
<?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;
|
|
|
|
|
2017-06-18 01:52:32 +00:00
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
use Illuminate\Database\Connection;
|
2017-06-14 04:25:37 +00:00
|
|
|
use Illuminate\Contracts\Hashing\Hasher;
|
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
|
|
|
use Pterodactyl\Notifications\AccountCreated;
|
2017-06-25 00:49:09 +00:00
|
|
|
use Pterodactyl\Exceptions\Model\DataValidationException;
|
|
|
|
use Pterodactyl\Services\Helpers\TemporaryPasswordService;
|
2017-06-14 04:25:37 +00:00
|
|
|
|
|
|
|
class UserService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Database\Connection
|
|
|
|
*/
|
|
|
|
protected $database;
|
|
|
|
|
|
|
|
/**
|
2017-06-25 00:49:09 +00:00
|
|
|
* @var \Illuminate\Contracts\Hashing\Hasher
|
2017-06-14 04:25:37 +00:00
|
|
|
*/
|
2017-06-25 00:49:09 +00:00
|
|
|
protected $hasher;
|
2017-06-14 04:25:37 +00:00
|
|
|
|
|
|
|
/**
|
2017-06-25 00:49:09 +00:00
|
|
|
* @var \Pterodactyl\Services\Helpers\TemporaryPasswordService
|
2017-06-14 04:25:37 +00:00
|
|
|
*/
|
2017-06-25 00:49:09 +00:00
|
|
|
protected $passwordService;
|
2017-06-14 04:25:37 +00:00
|
|
|
|
|
|
|
/**
|
2017-06-25 00:49:09 +00:00
|
|
|
* @var \Pterodactyl\Models\User
|
2017-06-14 04:25:37 +00:00
|
|
|
*/
|
2017-06-25 00:49:09 +00:00
|
|
|
protected $model;
|
2017-06-14 04:25:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* UserService constructor.
|
|
|
|
*
|
2017-06-25 00:49:09 +00:00
|
|
|
* @param \Illuminate\Database\Connection $database
|
|
|
|
* @param \Illuminate\Contracts\Hashing\Hasher $hasher
|
|
|
|
* @param \Pterodactyl\Services\Helpers\TemporaryPasswordService $passwordService
|
|
|
|
* @param \Pterodactyl\Models\User $model
|
2017-06-14 04:25:37 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
Connection $database,
|
|
|
|
Hasher $hasher,
|
2017-06-25 00:49:09 +00:00
|
|
|
TemporaryPasswordService $passwordService,
|
|
|
|
User $model
|
2017-06-14 04:25:37 +00:00
|
|
|
) {
|
|
|
|
$this->database = $database;
|
|
|
|
$this->hasher = $hasher;
|
2017-06-25 00:49:09 +00:00
|
|
|
$this->passwordService = $passwordService;
|
|
|
|
$this->model = $model;
|
2017-06-14 04:25:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new user on the system.
|
|
|
|
*
|
2017-06-25 00:49:09 +00:00
|
|
|
* @param array $data
|
2017-06-14 04:25:37 +00:00
|
|
|
* @return \Pterodactyl\Models\User
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
2017-06-25 00:49:09 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
2017-06-14 04:25:37 +00:00
|
|
|
*/
|
|
|
|
public function create(array $data)
|
|
|
|
{
|
|
|
|
if (array_key_exists('password', $data) && ! empty($data['password'])) {
|
|
|
|
$data['password'] = $this->hasher->make($data['password']);
|
|
|
|
}
|
|
|
|
|
2017-06-25 00:49:09 +00:00
|
|
|
$user = $this->model->newInstance($data);
|
2017-06-14 04:25:37 +00:00
|
|
|
|
|
|
|
// Persist the data
|
|
|
|
$token = $this->database->transaction(function () use ($user) {
|
|
|
|
if (empty($user->password)) {
|
2017-06-25 00:49:09 +00:00
|
|
|
$user->password = $this->hasher->make(str_random(30));
|
|
|
|
$token = $this->passwordService->generateReset($user->email);
|
2017-06-14 04:25:37 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 00:49:09 +00:00
|
|
|
if (! $user->save()) {
|
|
|
|
throw new DataValidationException($user->getValidator());
|
|
|
|
}
|
2017-06-14 04:25:37 +00:00
|
|
|
|
|
|
|
return $token ?? null;
|
|
|
|
});
|
|
|
|
|
|
|
|
$user->notify(new AccountCreated([
|
|
|
|
'name' => $user->name_first,
|
|
|
|
'username' => $user->username,
|
|
|
|
'token' => $token,
|
|
|
|
]));
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the user model.
|
|
|
|
*
|
2017-06-25 00:49:09 +00:00
|
|
|
* @param int|\Pterodactyl\Models\User $user
|
2017-06-14 04:25:37 +00:00
|
|
|
* @param array $data
|
|
|
|
* @return \Pterodactyl\Models\User
|
2017-06-25 00:49:09 +00:00
|
|
|
*
|
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
2017-06-14 04:25:37 +00:00
|
|
|
*/
|
2017-06-25 00:49:09 +00:00
|
|
|
public function update($user, array $data)
|
2017-06-14 04:25:37 +00:00
|
|
|
{
|
2017-06-25 00:49:09 +00:00
|
|
|
if (! $user instanceof User) {
|
|
|
|
$user = $this->model->findOrFail($user);
|
|
|
|
}
|
|
|
|
|
2017-06-14 04:25:37 +00:00
|
|
|
if (isset($data['password'])) {
|
|
|
|
$data['password'] = $this->hasher->make($data['password']);
|
|
|
|
}
|
|
|
|
|
2017-06-25 00:49:09 +00:00
|
|
|
$user->fill($data);
|
|
|
|
|
|
|
|
if (! $user->save()) {
|
|
|
|
throw new DataValidationException($user->getValidator());
|
|
|
|
}
|
2017-06-14 04:25:37 +00:00
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-25 00:49:09 +00:00
|
|
|
* @param int|\Pterodactyl\Models\User $user
|
2017-06-14 04:25:37 +00:00
|
|
|
* @return bool|null
|
2017-06-25 00:49:09 +00:00
|
|
|
*
|
2017-06-14 04:25:37 +00:00
|
|
|
* @throws \Exception
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
2017-06-25 00:49:09 +00:00
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
2017-06-14 04:25:37 +00:00
|
|
|
*/
|
2017-06-25 00:49:09 +00:00
|
|
|
public function delete($user)
|
2017-06-14 04:25:37 +00:00
|
|
|
{
|
2017-06-25 00:49:09 +00:00
|
|
|
if (! $user instanceof User) {
|
|
|
|
$user = $this->model->findOrFail($user);
|
2017-06-14 04:25:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($user->servers()->count() > 0) {
|
|
|
|
throw new DisplayException('Cannot delete an account that has active servers attached to it.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $user->delete();
|
|
|
|
}
|
|
|
|
}
|