2017-06-14 04:25:37 +00:00
|
|
|
<?php
|
2017-09-26 02:43:01 +00:00
|
|
|
/**
|
2017-06-14 04:25:37 +00:00
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-06-14 04:25:37 +00:00
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|