From 9d10c2a757ede3b52c8a8efdc570fbcdf2c310a1 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 6 Oct 2016 22:36:59 -0400 Subject: [PATCH] Support custom user id though API, closes #115 --- CHANGELOG.md | 3 +++ app/Http/Controllers/API/UserController.php | 25 +++++++++------------ app/Repositories/UserRepository.php | 15 ++++++++++--- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6267ed6b0..2172602ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,9 +8,12 @@ This project follows [Semantic Versioning](http://semver.org) guidelines. ### Added * Added support for file copying through the file manager. [#127](https://github.com/Pterodactyl/Panel/issues/127) * Added support for creating new files and folders directly from the right-click dropdown menu. +* Added support for setting custom `user_id` when using the API to create users. ### Changed * Support for sub-folders within the `getJavascript()` route for servers. +* API route for [`/api/users`](https://pterodactyl.readme.io/v0.5.0/reference#users) now returns a non-paginated result set, and is a single array. +* API route for [`/api/users/:id`](https://pterodactyl.readme.io/v0.5.0/reference#single-user) now returns a single array including an array of all servers the user is set as the owner of. ### Fixed * File manager would do multiple up-down-up-down loading actions if you escaped renaming a file. Fixed the binding issue. [#122](https://github.com/Pterodactyl/Panel/issues/122) diff --git a/app/Http/Controllers/API/UserController.php b/app/Http/Controllers/API/UserController.php index e32696333..abc8b7f73 100755 --- a/app/Http/Controllers/API/UserController.php +++ b/app/Http/Controllers/API/UserController.php @@ -62,8 +62,7 @@ class UserController extends BaseController */ public function list(Request $request) { - $users = Models\User::paginate(50); - return $this->response->paginator($users, new UserTransformer); + return Models\User::all()->toArray(); } /** @@ -95,7 +94,12 @@ class UserController extends BaseController if (!$query->first()) { throw new NotFoundHttpException('No user by that ID was found.'); } - return $query->first(); + + $user = $query->first(); + $userArray = $user->toArray(); + $userArray['servers'] = Models\Server::select('id', 'uuid', 'node', 'suspended')->where('owner', $user->id)->get(); + + return $userArray; } catch (NotFoundHttpException $ex) { throw $ex; } catch (\Exception $ex) { @@ -113,25 +117,18 @@ class UserController extends BaseController * @Request({ * "email": "foo@example.com", * "password": "foopassword", - * "admin": false + * "admin": false, + * "custom_id": 123 * }, headers={"Authorization": "Bearer "}), * @Response(201), - * @Response(422, body={ - * "message": "A validation error occured.", - * "errors": { - * "email": {"The email field is required."}, - * "password": {"The password field is required."}, - * "admin": {"The admin field is required."} - * }, - * "status_code": 422 - * }) + * @Response(422) * }) */ public function create(Request $request) { try { $user = new UserRepository; - $create = $user->create($request->input('email'), $request->input('password'), $request->input('admin')); + $create = $user->create($request->input('email'), $request->input('password'), $request->input('admin'), $request->input('custom_id')); return $this->response->created(route('api.users.view', [ 'id' => $create ])); diff --git a/app/Repositories/UserRepository.php b/app/Repositories/UserRepository.php index b9d943b08..f90bab484 100644 --- a/app/Repositories/UserRepository.php +++ b/app/Repositories/UserRepository.php @@ -52,18 +52,22 @@ class UserRepository * * @param string $email * @param string|null $password An unhashed version of the user's password. + * @param bool $admin Boolean value if user should be an admin or not. + * @param int $token A custom user ID. * @return bool|integer */ - public function create($email, $password = null, $admin = false) + public function create($email, $password = null, $admin = false, $token = null) { $validator = Validator::make([ 'email' => $email, 'password' => $password, - 'root_admin' => $admin + 'root_admin' => $admin, + 'custom_id' => $token, ], [ 'email' => 'required|email|unique:users,email', 'password' => 'nullable|regex:((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})', - 'root_admin' => 'required|boolean' + 'root_admin' => 'required|boolean', + 'custom_id' => 'nullable|unique:users,id', ]); // Run validator, throw catchable and displayable exception if it fails. @@ -78,6 +82,11 @@ class UserRepository $user = new Models\User; $uuid = new UuidService; + // Support for API Services + if (!is_null($token)) { + $user->id = $token; + } + $user->uuid = $uuid->generate('users', 'uuid'); $user->email = $email; $user->password = Hash::make((is_null($password)) ? str_random(30) : $password);