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-08-05 00:11:41 +00:00
|
|
|
namespace Pterodactyl\Services\Users;
|
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
|
|
|
|
2017-08-27 20:10:51 +00:00
|
|
|
class UserCreationService
|
2017-06-14 04:25:37 +00:00
|
|
|
{
|
2017-07-01 20:29:49 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Foundation\Application
|
|
|
|
*/
|
|
|
|
protected $app;
|
|
|
|
|
2017-06-14 04:25:37 +00:00
|
|
|
/**
|
2017-08-05 00:11:41 +00:00
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
2017-06-14 04:25:37 +00:00
|
|
|
*/
|
2017-08-05 00:11:41 +00:00
|
|
|
protected $connection;
|
2017-06-14 04:25:37 +00:00
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
|
|
|
/**
|
2017-08-05 00:11:41 +00:00
|
|
|
* CreationService constructor.
|
2017-06-14 04:25:37 +00:00
|
|
|
*
|
2017-08-05 00:11:41 +00:00
|
|
|
* @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
|
2017-06-14 04:25:37 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2017-07-01 20:29:49 +00:00
|
|
|
Application $application,
|
|
|
|
ChannelManager $notification,
|
2017-08-05 00:11:41 +00:00
|
|
|
ConnectionInterface $connection,
|
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-08-05 00:11:41 +00:00
|
|
|
$this->connection = $connection;
|
2017-06-14 04:25:37 +00:00
|
|
|
$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-08-22 03:10:48 +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
|
|
|
*/
|
2017-08-05 00:11:41 +00:00
|
|
|
public function handle(array $data)
|
2017-06-14 04:25:37 +00:00
|
|
|
{
|
|
|
|
if (array_key_exists('password', $data) && ! empty($data['password'])) {
|
|
|
|
$data['password'] = $this->hasher->make($data['password']);
|
|
|
|
}
|
|
|
|
|
2017-08-05 00:11:41 +00:00
|
|
|
$this->connection->beginTransaction();
|
2017-07-01 20:29:49 +00:00
|
|
|
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-08-05 00:11:41 +00:00
|
|
|
$this->connection->commit();
|
2017-06-14 04:25:37 +00:00
|
|
|
|
2017-08-05 00:11:41 +00:00
|
|
|
// @todo fire event, handle notification there
|
2017-07-01 20:29:49 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|