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.
|
|
|
|
*/
|
|
|
|
|
2017-07-09 17:29:18 +00:00
|
|
|
namespace Pterodactyl\Services;
|
2017-06-14 04:25:37 +00:00
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
use Illuminate\Foundation\Application;
|
2017-06-14 04:25:37 +00:00
|
|
|
use Illuminate\Contracts\Hashing\Hasher;
|
2017-07-01 20:29:49 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
|
|
use Illuminate\Notifications\ChannelManager;
|
2017-06-14 04:25:37 +00:00
|
|
|
use Pterodactyl\Notifications\AccountCreated;
|
2017-06-25 00:49:09 +00:00
|
|
|
use Pterodactyl\Services\Helpers\TemporaryPasswordService;
|
2017-07-01 20:29:49 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
2017-06-14 04:25:37 +00:00
|
|
|
|
|
|
|
class UserService
|
|
|
|
{
|
2017-07-01 20:29:49 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Foundation\Application
|
|
|
|
*/
|
|
|
|
protected $app;
|
|
|
|
|
2017-06-14 04:25:37 +00:00
|
|
|
/**
|
|
|
|
* @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-07-01 20:29:49 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Notifications\ChannelManager
|
|
|
|
*/
|
|
|
|
protected $notification;
|
|
|
|
|
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-07-01 20:29:49 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
|
2017-06-14 04:25:37 +00:00
|
|
|
*/
|
2017-07-01 20:29:49 +00:00
|
|
|
protected $repository;
|
2017-06-14 04:25:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* UserService constructor.
|
|
|
|
*
|
2017-07-01 20:29:49 +00:00
|
|
|
* @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
|
2017-06-14 04:25:37 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2017-07-01 20:29:49 +00:00
|
|
|
Application $application,
|
|
|
|
ChannelManager $notification,
|
|
|
|
ConnectionInterface $database,
|
2017-06-14 04:25:37 +00:00
|
|
|
Hasher $hasher,
|
2017-06-25 00:49:09 +00:00
|
|
|
TemporaryPasswordService $passwordService,
|
2017-07-01 20:29:49 +00:00
|
|
|
UserRepositoryInterface $repository
|
2017-06-14 04:25:37 +00:00
|
|
|
) {
|
2017-07-01 20:29:49 +00:00
|
|
|
$this->app = $application;
|
2017-06-14 04:25:37 +00:00
|
|
|
$this->database = $database;
|
|
|
|
$this->hasher = $hasher;
|
2017-07-01 20:29:49 +00:00
|
|
|
$this->notification = $notification;
|
2017-06-25 00:49:09 +00:00
|
|
|
$this->passwordService = $passwordService;
|
2017-07-01 20:29:49 +00:00
|
|
|
$this->repository = $repository;
|
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-07-01 20:29:49 +00:00
|
|
|
// Begin Transaction
|
|
|
|
$this->database->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);
|
2017-06-14 04:25:37 +00:00
|
|
|
|
|
|
|
// Persist the data
|
2017-07-01 20:29:49 +00:00
|
|
|
$this->database->commit();
|
|
|
|
|
|
|
|
$this->notification->send($user, $this->app->makeWith(AccountCreated::class, [
|
|
|
|
'user' => [
|
|
|
|
'name' => $user->name_first,
|
|
|
|
'username' => $user->username,
|
|
|
|
'token' => $token ?? null,
|
|
|
|
],
|
2017-06-14 04:25:37 +00:00
|
|
|
]));
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-01 20:29:49 +00:00
|
|
|
* Update the user model instance.
|
2017-06-14 04:25:37 +00:00
|
|
|
*
|
2017-07-01 20:29:49 +00:00
|
|
|
* @param int $id
|
|
|
|
* @param array $data
|
|
|
|
* @return mixed
|
2017-06-25 00:49:09 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
2017-06-14 04:25:37 +00:00
|
|
|
*/
|
2017-07-01 20:29:49 +00:00
|
|
|
public function update($id, array $data)
|
2017-06-14 04:25:37 +00:00
|
|
|
{
|
|
|
|
if (isset($data['password'])) {
|
|
|
|
$data['password'] = $this->hasher->make($data['password']);
|
|
|
|
}
|
|
|
|
|
2017-07-01 20:29:49 +00:00
|
|
|
$user = $this->repository->update($id, $data);
|
2017-06-14 04:25:37 +00:00
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
}
|