1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pterodactyl - Panel |
4
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <[email protected]> |
5
|
|
|
* Some Modifications (c) 2015 Dylan Seidt <[email protected]>. |
6
|
|
|
* |
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
* in the Software without restriction, including without limitation the rights |
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
* furnished to do so, subject to the following conditions: |
13
|
|
|
* |
14
|
|
|
* The above copyright notice and this permission notice shall be included in all |
15
|
|
|
* copies or substantial portions of the Software. |
16
|
|
|
* |
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23
|
|
|
* SOFTWARE. |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace Pterodactyl\Repositories; |
27
|
|
|
|
28
|
|
|
use DB; |
29
|
|
|
use Auth; |
30
|
|
|
use Hash; |
31
|
|
|
use Carbon; |
32
|
|
|
use Settings; |
33
|
|
|
use Validator; |
34
|
|
|
use Pterodactyl\Models; |
35
|
|
|
use Pterodactyl\Services\UuidService; |
36
|
|
|
use Pterodactyl\Exceptions\DisplayException; |
37
|
|
|
use Pterodactyl\Notifications\AccountCreated; |
38
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException; |
39
|
|
|
|
40
|
|
|
class UserRepository |
41
|
|
|
{ |
42
|
|
|
public function __construct() |
43
|
|
|
{ |
44
|
|
|
// |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Creates a user on the panel. Returns the created user's ID. |
49
|
|
|
* |
50
|
|
|
* @param string $email |
|
|
|
|
51
|
|
|
* @param string|null $password An unhashed version of the user's password. |
|
|
|
|
52
|
|
|
* @param bool $admin Boolean value if user should be an admin or not. |
|
|
|
|
53
|
|
|
* @param int $token A custom user ID. |
|
|
|
|
54
|
|
|
* @return bool|int |
55
|
|
|
*/ |
56
|
|
|
public function create(array $data) |
57
|
|
|
{ |
58
|
|
|
$validator = Validator::make($data, [ |
59
|
|
|
'email' => 'required|email|unique:users,email', |
60
|
|
|
'username' => 'required|string|between:1,255|unique:users,username|' . Models\User::USERNAME_RULES, |
61
|
|
|
'name_first' => 'required|string|between:1,255', |
62
|
|
|
'name_last' => 'required|string|between:1,255', |
63
|
|
|
'password' => 'sometimes|nullable|' . Models\User::PASSWORD_RULES, |
64
|
|
|
'root_admin' => 'required|boolean', |
65
|
|
|
'custom_id' => 'sometimes|nullable|unique:users,id', |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
// Run validator, throw catchable and displayable exception if it fails. |
69
|
|
|
// Exception includes a JSON result of failed validation rules. |
70
|
|
|
if ($validator->fails()) { |
71
|
|
|
throw new DisplayValidationException($validator->errors()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
DB::beginTransaction(); |
75
|
|
|
|
76
|
|
|
try { |
77
|
|
|
$user = new Models\User; |
78
|
|
|
$uuid = new UuidService; |
79
|
|
|
|
80
|
|
|
// Support for API Services |
81
|
|
|
if (isset($data['custom_id']) && ! is_null($data['custom_id'])) { |
82
|
|
|
$user->id = $token; |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// UUIDs are not mass-fillable. |
86
|
|
|
$user->uuid = $uuid->generate('users', 'uuid'); |
|
|
|
|
87
|
|
|
|
88
|
|
|
$user->fill([ |
89
|
|
|
'email' => $data['email'], |
90
|
|
|
'username' => $data['username'], |
91
|
|
|
'name_first' => $data['name_first'], |
92
|
|
|
'name_last' => $data['name_last'], |
93
|
|
|
'password' => Hash::make((empty($data['password'])) ? str_random(30) : $data['password']), |
94
|
|
|
'root_admin' => $data['root_admin'], |
95
|
|
|
'language' => Settings::get('default_language', 'en'), |
96
|
|
|
]); |
97
|
|
|
$user->save(); |
98
|
|
|
|
99
|
|
|
// Setup a Password Reset to use when they set a password. |
100
|
|
|
// Only used if no password is provided. |
101
|
|
|
if (empty($data['password'])) { |
102
|
|
|
$token = str_random(32); |
103
|
|
|
DB::table('password_resets')->insert([ |
104
|
|
|
'email' => $user->email, |
|
|
|
|
105
|
|
|
'token' => $token, |
106
|
|
|
'created_at' => Carbon::now()->toDateTimeString(), |
107
|
|
|
]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
DB::commit(); |
111
|
|
|
|
112
|
|
|
return $user; |
113
|
|
|
} catch (\Exception $ex) { |
114
|
|
|
DB::rollBack(); |
115
|
|
|
throw $ex; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Updates a user on the panel. |
121
|
|
|
* |
122
|
|
|
* @param int $id |
123
|
|
|
* @param array $data An array of columns and their associated values to update for the user. |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
public function update($id, array $data) |
127
|
|
|
{ |
128
|
|
|
$user = Models\User::findOrFail($id); |
129
|
|
|
|
130
|
|
|
$validator = Validator::make($data, [ |
131
|
|
|
'email' => 'sometimes|required|email|unique:users,email,' . $id, |
132
|
|
|
'username' => 'sometimes|required|string|between:1,255|unique:users,username,' . $user->id . '|' . Models\User::USERNAME_RULES, |
133
|
|
|
'name_first' => 'sometimes|required|string|between:1,255', |
134
|
|
|
'name_last' => 'sometimes|required|string|between:1,255', |
135
|
|
|
'password' => 'sometimes|nullable|' . Models\User::PASSWORD_RULES, |
136
|
|
|
'root_admin' => 'sometimes|required|boolean', |
137
|
|
|
'language' => 'sometimes|required|string|min:1|max:5', |
138
|
|
|
'use_totp' => 'sometimes|required|boolean', |
139
|
|
|
'totp_secret' => 'sometimes|required|size:16', |
140
|
|
|
]); |
141
|
|
|
|
142
|
|
|
// Run validator, throw catchable and displayable exception if it fails. |
143
|
|
|
// Exception includes a JSON result of failed validation rules. |
144
|
|
|
if ($validator->fails()) { |
145
|
|
|
throw new DisplayValidationException($validator->errors()); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// The password and root_admin fields are not mass assignable. |
149
|
|
|
if (! empty($data['password'])) { |
150
|
|
|
$data['password'] = Hash::make($data['password']); |
151
|
|
|
} else { |
152
|
|
|
unset($data['password']); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$user->fill($data); |
156
|
|
|
|
157
|
|
|
return $user->save(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Deletes a user on the panel, returns the number of records deleted. |
162
|
|
|
* |
163
|
|
|
* @param int $id |
164
|
|
|
* @return int |
165
|
|
|
*/ |
166
|
|
|
public function delete($id) |
167
|
|
|
{ |
168
|
|
|
if (Models\Server::where('owner_id', $id)->count() > 0) { |
169
|
|
|
throw new DisplayException('Cannot delete a user with active servers attached to thier account.'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// @TODO: this should probably be checked outside of this method because we won't always have Auth::user() |
173
|
|
|
if (! is_null(Auth::user()) && Auth::user()->id === $id) { |
174
|
|
|
throw new DisplayException('Cannot delete your own account.'); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
DB::beginTransaction(); |
178
|
|
|
|
179
|
|
|
try { |
180
|
|
|
foreach(Models\Subuser::with('permissions')->where('user_id', $id)->get() as &$subuser) { |
|
|
|
|
181
|
|
|
foreach($subuser->permissions as &$permission) { |
182
|
|
|
$permission->delete(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$subuser->delete(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
Models\User::destroy($id); |
189
|
|
|
DB::commit(); |
190
|
|
|
|
191
|
|
|
return true; |
192
|
|
|
} catch (\Exception $ex) { |
193
|
|
|
DB::rollBack(); |
194
|
|
|
throw $ex; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.