From 5efee34378ee7710ac68afca95f0d0225cae992e Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 30 Dec 2017 20:25:04 -0600 Subject: [PATCH] close #840 --- CHANGELOG.md | 1 + app/Http/Controllers/Admin/UserController.php | 12 +++- app/Http/Requests/Admin/AdminFormRequest.php | 13 +--- app/Http/Requests/Admin/UserFormRequest.php | 8 ++- app/Models/User.php | 9 +-- app/Observers/UserObserver.php | 15 ----- app/Services/Components/UuidService.php | 63 ------------------- app/Services/Users/UserCreationService.php | 6 +- .../pterodactyl/admin/users/new.blade.php | 11 ++++ .../pterodactyl/admin/users/view.blade.php | 59 +++++++++-------- .../Users/UserCreationServiceTest.php | 49 ++++++++++++--- 11 files changed, 111 insertions(+), 135 deletions(-) delete mode 100644 app/Services/Components/UuidService.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c4563986..fd6c1de41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines. ### Fixed * `[beta.3]` — Fixes a bug with the default environment file that was causing an inability to perform a fresh install when running package discovery. * `[beta.3]` — Fixes an edge case caused by the Laravel 5.5 upgrade that would try to perform an in_array check aganist a null value. +* `[beta.3]` — Fixes a bug that would cause an error when attempting to create a new user on the Panel. ## v0.7.0-beta.3 (Derelict Dermodactylus) ### Fixed diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index ad5dead01..faa535b6a 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -9,6 +9,7 @@ use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Http\Controllers\Controller; use Illuminate\Contracts\Translation\Translator; use Pterodactyl\Services\Users\UserUpdateService; +use Pterodactyl\Traits\Helpers\AvailableLanguages; use Pterodactyl\Services\Users\UserCreationService; use Pterodactyl\Services\Users\UserDeletionService; use Pterodactyl\Http\Requests\Admin\UserFormRequest; @@ -16,6 +17,8 @@ use Pterodactyl\Contracts\Repository\UserRepositoryInterface; class UserController extends Controller { + use AvailableLanguages; + /** * @var \Prologue\Alerts\AlertsMessageBag */ @@ -92,7 +95,9 @@ class UserController extends Controller */ public function create() { - return view('admin.users.new'); + return view('admin.users.new', [ + 'languages' => $this->getAvailableLanguages(true), + ]); } /** @@ -103,7 +108,10 @@ class UserController extends Controller */ public function view(User $user) { - return view('admin.users.view', ['user' => $user]); + return view('admin.users.view', [ + 'user' => $user, + 'languages' => $this->getAvailableLanguages(true), + ]); } /** diff --git a/app/Http/Requests/Admin/AdminFormRequest.php b/app/Http/Requests/Admin/AdminFormRequest.php index 0e6293733..012d73364 100644 --- a/app/Http/Requests/Admin/AdminFormRequest.php +++ b/app/Http/Requests/Admin/AdminFormRequest.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Http\Requests\Admin; @@ -39,11 +32,11 @@ abstract class AdminFormRequest extends FormRequest * Return only the fields that we are interested in from the request. * This will include empty fields as a null value. * - * @param array $only + * @param array|null $only * @return array */ - public function normalize($only = []) + public function normalize(array $only = null) { - return $this->all(empty($only) ? array_keys($this->rules()) : $only); + return $this->only($only ?? array_keys($this->rules())); } } diff --git a/app/Http/Requests/Admin/UserFormRequest.php b/app/Http/Requests/Admin/UserFormRequest.php index 757be4341..ab760a7f1 100644 --- a/app/Http/Requests/Admin/UserFormRequest.php +++ b/app/Http/Requests/Admin/UserFormRequest.php @@ -22,12 +22,16 @@ class UserFormRequest extends AdminFormRequest return User::getCreateRules(); } - public function normalize($only = []) + /** + * @param array|null $only + * @return array + */ + public function normalize(array $only = null) { if ($this->method === 'PATCH') { return array_merge( $this->all(['password']), - $this->only(['email', 'username', 'name_first', 'name_last', 'root_admin', 'ignore_connection_error']) + $this->only(['email', 'username', 'name_first', 'name_last', 'root_admin', 'language', 'ignore_connection_error']) ); } diff --git a/app/Models/User.php b/app/Models/User.php index 39e4a0a03..02c7e7add 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Models; @@ -127,6 +120,8 @@ class User extends Model implements 'name_first' => 'required', 'name_last' => 'required', 'password' => 'sometimes', + 'language' => 'sometimes', + 'use_totp' => 'sometimes', ]; /** diff --git a/app/Observers/UserObserver.php b/app/Observers/UserObserver.php index 8537d061c..dd29d908b 100644 --- a/app/Observers/UserObserver.php +++ b/app/Observers/UserObserver.php @@ -1,27 +1,14 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Observers; use Pterodactyl\Events; use Pterodactyl\Models\User; -use Pterodactyl\Services\Components\UuidService; class UserObserver { protected $uuid; - public function __construct(UuidService $uuid) - { - $this->uuid = $uuid; - } - /** * Listen to the User creating event. * @@ -29,8 +16,6 @@ class UserObserver */ public function creating(User $user) { - $user->uuid = $this->uuid->generate('users', 'uuid'); - event(new Events\User\Creating($user)); } diff --git a/app/Services/Components/UuidService.php b/app/Services/Components/UuidService.php deleted file mode 100644 index 1e5c2a2bc..000000000 --- a/app/Services/Components/UuidService.php +++ /dev/null @@ -1,63 +0,0 @@ -. - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ - -namespace Pterodactyl\Services\Components; - -use DB; -use Uuid; - -class UuidService -{ - /** - * Generate a unique UUID validating against specified table and column. - * Defaults to `users.uuid`. - * - * @param string $table - * @param string $field - * @param int $type - * @return string - * @deprecated - */ - public function generate($table = 'users', $field = 'uuid', $type = 4) - { - $return = false; - do { - $uuid = Uuid::generate($type); - if (! DB::table($table)->where($field, $uuid)->exists()) { - $return = $uuid; - } - } while (! $return); - - return (string) $return; - } - - /** - * Generates a ShortUUID code which is 8 characters long and is used for identifying servers in the system. - * - * @param string $table - * @param string $field - * @param null|string $attachedUuid - * @return string - * @deprecated - */ - public function generateShort($table = 'servers', $field = 'uuidShort', $attachedUuid = null) - { - $return = false; - do { - $short = (is_null($attachedUuid)) ? substr(Uuid::generate(4), 0, 8) : substr($attachedUuid, 0, 8); - $attachedUuid = null; - - if (! DB::table($table)->where($field, $short)->exists()) { - $return = $short; - } - } while (! $return); - - return (string) $return; - } -} diff --git a/app/Services/Users/UserCreationService.php b/app/Services/Users/UserCreationService.php index 4a34b7a96..b267a18f5 100644 --- a/app/Services/Users/UserCreationService.php +++ b/app/Services/Users/UserCreationService.php @@ -9,6 +9,7 @@ namespace Pterodactyl\Services\Users; +use Ramsey\Uuid\Uuid; use Illuminate\Foundation\Application; use Illuminate\Contracts\Hashing\Hasher; use Illuminate\Database\ConnectionInterface; @@ -96,7 +97,10 @@ class UserCreationService $token = $this->passwordService->handle($data['email']); } - $user = $this->repository->create($data); + $user = $this->repository->create(array_merge($data, [ + 'uuid' => Uuid::uuid4()->toString(), + ])); + $this->connection->commit(); // @todo fire event, handle notification there diff --git a/resources/themes/pterodactyl/admin/users/new.blade.php b/resources/themes/pterodactyl/admin/users/new.blade.php index 3e1b82742..4f4da9bd2 100644 --- a/resources/themes/pterodactyl/admin/users/new.blade.php +++ b/resources/themes/pterodactyl/admin/users/new.blade.php @@ -51,6 +51,17 @@ +
+ +
+ +

The default language to use when rendering the Panel for this user.

+
+
+
+ +
+ +

The default language to use when rendering the Panel for this user.

+
+
-
-
-
-

Associated Servers

-
-
- - - - - - - - - - - - - Oh dear, this hasn't been fixed yet? + {{--
--}} + {{--
--}} + {{--
--}} + {{--

Associated Servers

--}} + {{--
--}} + {{--
--}} + {{--
IdentifierServer NameAccessNode
--}} + {{----}} + {{----}} + {{----}} + {{----}} + {{----}} + {{----}} + {{----}} + {{----}} + {{----}} + {{----}} + {{----}} {{--@foreach($user->setAccessLevel('subuser')->access()->get() as $server)--}} {{----}} {{----}} @@ -136,12 +146,11 @@ {{----}} {{----}} {{--@endforeach--}} - -
IdentifierServer NameAccessNode
@if($server->suspended === 0)Active@elseSuspended@endif
-
- -
-
+ {{----}} + {{----}} + {{----}} + {{----}} + {{----}}
diff --git a/tests/Unit/Services/Users/UserCreationServiceTest.php b/tests/Unit/Services/Users/UserCreationServiceTest.php index 9e83eca93..5650b0322 100644 --- a/tests/Unit/Services/Users/UserCreationServiceTest.php +++ b/tests/Unit/Services/Users/UserCreationServiceTest.php @@ -1,16 +1,10 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Tests\Unit\Services; use Mockery as m; use Tests\TestCase; +use Tests\Traits\MocksUuids; use Illuminate\Foundation\Application; use Illuminate\Contracts\Hashing\Hasher; use Illuminate\Database\ConnectionInterface; @@ -22,6 +16,8 @@ use Pterodactyl\Contracts\Repository\UserRepositoryInterface; class UserCreationServiceTest extends TestCase { + use MocksUuids; + /** * @var \Illuminate\Foundation\Application */ @@ -93,9 +89,10 @@ class UserCreationServiceTest extends TestCase $this->hasher->shouldReceive('make')->with('raw-password')->once()->andReturn('enc-password'); $this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); - $this->hasher->shouldNotReceive('make'); - $this->passwordService->shouldNotReceive('handle'); - $this->repository->shouldReceive('create')->with(['password' => 'enc-password'])->once()->andReturn($user); + $this->repository->shouldReceive('create')->with([ + 'password' => 'enc-password', + 'uuid' => $this->getKnownUuid(), + ])->once()->andReturn($user); $this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); $this->appMock->shouldReceive('makeWith')->with(AccountCreated::class, [ 'user' => [ @@ -116,6 +113,37 @@ class UserCreationServiceTest extends TestCase $this->assertEquals($user->name_first, 'FirstName'); } + /** + * Test that a UUID passed in the submission data is not used when + * creating the user. + */ + public function testUuidPassedInDataIsIgnored() + { + $user = (object) [ + 'name_first' => 'FirstName', + 'username' => 'user_name', + ]; + + $this->hasher->shouldReceive('make')->andReturn('enc-password'); + $this->database->shouldReceive('beginTransaction')->andReturnNull(); + $this->repository->shouldReceive('create')->with([ + 'password' => 'enc-password', + 'uuid' => $this->getKnownUuid(), + ])->once()->andReturn($user); + $this->database->shouldReceive('commit')->andReturnNull(); + $this->appMock->shouldReceive('makeWith')->andReturnNull(); + $this->notification->shouldReceive('send')->andReturnNull(); + + $response = $this->service->handle([ + 'password' => 'raw-password', + 'uuid' => 'test-uuid', + ]); + + $this->assertNotNull($response); + $this->assertEquals($user->username, $response->username); + $this->assertEquals($user->name_first, 'FirstName'); + } + /** * Test that a user is created with a random password when no password is provided. */ @@ -138,6 +166,7 @@ class UserCreationServiceTest extends TestCase $this->repository->shouldReceive('create')->with([ 'password' => 'created-enc-password', 'email' => 'user@example.com', + 'uuid' => $this->getKnownUuid(), ])->once()->andReturn($user); $this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();