Skip to content

Commit 9d10c2a

Browse files
committed
Support custom user id though API, closes pterodactyl#115
1 parent c347a67 commit 9d10c2a

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
88
### Added
99
* Added support for file copying through the file manager. [#127](https://github.com/Pterodactyl/Panel/issues/127)
1010
* Added support for creating new files and folders directly from the right-click dropdown menu.
11+
* Added support for setting custom `user_id` when using the API to create users.
1112

1213
### Changed
1314
* Support for sub-folders within the `getJavascript()` route for servers.
15+
* 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.
16+
* 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.
1417

1518
### Fixed
1619
* 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)

app/Http/Controllers/API/UserController.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ public function __construct()
6262
*/
6363
public function list(Request $request)
6464
{
65-
$users = Models\User::paginate(50);
66-
return $this->response->paginator($users, new UserTransformer);
65+
return Models\User::all()->toArray();
6766
}
6867

6968
/**
@@ -95,7 +94,12 @@ public function view(Request $request, $id)
9594
if (!$query->first()) {
9695
throw new NotFoundHttpException('No user by that ID was found.');
9796
}
98-
return $query->first();
97+
98+
$user = $query->first();
99+
$userArray = $user->toArray();
100+
$userArray['servers'] = Models\Server::select('id', 'uuid', 'node', 'suspended')->where('owner', $user->id)->get();
101+
102+
return $userArray;
99103
} catch (NotFoundHttpException $ex) {
100104
throw $ex;
101105
} catch (\Exception $ex) {
@@ -113,25 +117,18 @@ public function view(Request $request, $id)
113117
* @Request({
114118
* "email": "foo@example.com",
115119
* "password": "foopassword",
116-
* "admin": false
120+
* "admin": false,
121+
* "custom_id": 123
117122
* }, headers={"Authorization": "Bearer <token>"}),
118123
* @Response(201),
119-
* @Response(422, body={
120-
* "message": "A validation error occured.",
121-
* "errors": {
122-
* "email": {"The email field is required."},
123-
* "password": {"The password field is required."},
124-
* "admin": {"The admin field is required."}
125-
* },
126-
* "status_code": 422
127-
* })
124+
* @Response(422)
128125
* })
129126
*/
130127
public function create(Request $request)
131128
{
132129
try {
133130
$user = new UserRepository;
134-
$create = $user->create($request->input('email'), $request->input('password'), $request->input('admin'));
131+
$create = $user->create($request->input('email'), $request->input('password'), $request->input('admin'), $request->input('custom_id'));
135132
return $this->response->created(route('api.users.view', [
136133
'id' => $create
137134
]));

app/Repositories/UserRepository.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,22 @@ public function __construct()
5252
*
5353
* @param string $email
5454
* @param string|null $password An unhashed version of the user's password.
55+
* @param bool $admin Boolean value if user should be an admin or not.
56+
* @param int $token A custom user ID.
5557
* @return bool|integer
5658
*/
57-
public function create($email, $password = null, $admin = false)
59+
public function create($email, $password = null, $admin = false, $token = null)
5860
{
5961
$validator = Validator::make([
6062
'email' => $email,
6163
'password' => $password,
62-
'root_admin' => $admin
64+
'root_admin' => $admin,
65+
'custom_id' => $token,
6366
], [
6467
'email' => 'required|email|unique:users,email',
6568
'password' => 'nullable|regex:((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})',
66-
'root_admin' => 'required|boolean'
69+
'root_admin' => 'required|boolean',
70+
'custom_id' => 'nullable|unique:users,id',
6771
]);
6872

6973
// Run validator, throw catchable and displayable exception if it fails.
@@ -78,6 +82,11 @@ public function create($email, $password = null, $admin = false)
7882
$user = new Models\User;
7983
$uuid = new UuidService;
8084

85+
// Support for API Services
86+
if (!is_null($token)) {
87+
$user->id = $token;
88+
}
89+
8190
$user->uuid = $uuid->generate('users', 'uuid');
8291
$user->email = $email;
8392
$user->password = Hash::make((is_null($password)) ? str_random(30) : $password);

0 commit comments

Comments
 (0)