Skip to content

Commit 2235481

Browse files
committed
More service structure testing and configuration
Tests aren't working as well as I had hoped, so a lot are commented out while I wait to hear back on this bug causing them to fail.
1 parent ce2b244 commit 2235481

File tree

18 files changed

+754
-400
lines changed

18 files changed

+754
-400
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/*
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Exceptions\Model;
26+
27+
use Illuminate\Contracts\Validation\Validator;
28+
use Illuminate\Validation\ValidationException;
29+
use Illuminate\Contracts\Support\MessageProvider;
30+
31+
class DataValidationException extends ValidationException implements MessageProvider
32+
{
33+
/**
34+
* DataValidationException constructor.
35+
*
36+
* @param \Illuminate\Contracts\Validation\Validator $validator
37+
*/
38+
public function __construct(Validator $validator)
39+
{
40+
parent::__construct($validator);
41+
}
42+
43+
/**
44+
* @return \Illuminate\Support\MessageBag
45+
*/
46+
public function getMessageBag()
47+
{
48+
return $this->validator->errors();
49+
}
50+
}

app/Http/Controllers/Admin/UserController.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,20 @@ class UserController extends Controller
4747
/**
4848
* @var \Pterodactyl\Models\User
4949
*/
50-
protected $userModel;
50+
protected $model;
5151

5252
/**
5353
* UserController constructor.
5454
*
5555
* @param \Prologue\Alerts\AlertsMessageBag $alert
5656
* @param \Pterodactyl\Services\UserService $service
57-
* @param \Pterodactyl\Models\User $userModel
57+
* @param \Pterodactyl\Models\User $model
5858
*/
59-
public function __construct(AlertsMessageBag $alert, UserService $service, User $userModel)
59+
public function __construct(AlertsMessageBag $alert, UserService $service, User $model)
6060
{
6161
$this->alert = $alert;
6262
$this->service = $service;
63-
$this->userModel = $userModel;
63+
$this->model = $model;
6464
}
6565

6666
/**
@@ -71,7 +71,7 @@ public function __construct(AlertsMessageBag $alert, UserService $service, User
7171
*/
7272
public function index(Request $request)
7373
{
74-
$users = $this->userModel->withCount('servers', 'subuserOf');
74+
$users = $this->model->newQuery()->withCount('servers', 'subuserOf');
7575

7676
if (! is_null($request->input('query'))) {
7777
$users->search($request->input('query'));
@@ -108,13 +108,19 @@ public function view(User $user)
108108
/**
109109
* Delete a user from the system.
110110
*
111+
* @param \Illuminate\Http\Request $request
111112
* @param \Pterodactyl\Models\User $user
112113
* @return \Illuminate\Http\RedirectResponse
113114
*
114115
* @throws \Exception
116+
* @throws \Pterodactyl\Exceptions\DisplayException
115117
*/
116-
public function delete(User $user)
118+
public function delete(Request $request, User $user)
117119
{
120+
if ($request->user()->id === $user->id) {
121+
throw new DisplayException('Cannot delete your own account.');
122+
}
123+
118124
try {
119125
$this->service->delete($user->id);
120126

@@ -146,9 +152,11 @@ public function store(UserFormRequest $request)
146152
/**
147153
* Update a user on the system.
148154
*
149-
* @param \Pterodactyl\Http\Requests\Admin\UserFormRequest $request
150-
* @param \Pterodactyl\Models\User $user
155+
* @param \Pterodactyl\Http\Requests\Admin\UserFormRequest $request
156+
* @param \Pterodactyl\Models\User $user
151157
* @return \Illuminate\Http\RedirectResponse
158+
*
159+
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
152160
*/
153161
public function update(UserFormRequest $request, User $user)
154162
{
@@ -166,7 +174,7 @@ public function update(UserFormRequest $request, User $user)
166174
*/
167175
public function json(Request $request)
168176
{
169-
return $this->userModel->search($request->input('q'))->all([
177+
return $this->model->search($request->input('q'))->all([
170178
'id', 'email', 'username', 'name_first', 'name_last',
171179
])->transform(function ($item) {
172180
$item->md5 = md5(strtolower($item->email));

app/Http/Requests/Admin/LocationRequest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ class LocationRequest extends AdminFormRequest
3535
*/
3636
public function rules()
3737
{
38-
return app()->make(Location::class)->getRules();
38+
if ($this->method() === 'PATCH') {
39+
return Location::getUpdateRulesForId($this->location->id);
40+
}
41+
42+
return Location::getCreateRules();
3943
}
4044
}

app/Http/Requests/Admin/UserFormRequest.php

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,46 +25,19 @@
2525
namespace Pterodactyl\Http\Requests\Admin;
2626

2727
use Pterodactyl\Models\User;
28-
use Pterodactyl\Contracts\Repositories\UserInterface;
2928

3029
class UserFormRequest extends AdminFormRequest
3130
{
32-
/**
33-
* {@inheritdoc}
34-
*/
35-
public function repository()
36-
{
37-
return UserInterface::class;
38-
}
39-
4031
/**
4132
* {@inheritdoc}
4233
*/
4334
public function rules()
4435
{
4536
if ($this->method() === 'PATCH') {
46-
return [
47-
'email' => 'required|email|unique:users,email,' . $this->user->id,
48-
'username' => 'required|alpha_dash|between:1,255|unique:users,username, ' . $this->user->id . '|' . User::USERNAME_RULES,
49-
'name_first' => 'required|string|between:1,255',
50-
'name_last' => 'required|string|between:1,255',
51-
'password' => 'sometimes|nullable|' . User::PASSWORD_RULES,
52-
'root_admin' => 'required|boolean',
53-
// 'language' => 'sometimes|required|string|min:1|max:5',
54-
// 'use_totp' => 'sometimes|required|boolean',
55-
// 'totp_secret' => 'sometimes|required|size:16',
56-
];
37+
return User::getUpdateRulesForId($this->user->id);
5738
}
5839

59-
return [
60-
'email' => 'required|email|unique:users,email',
61-
'username' => 'required|alpha_dash|between:1,255|unique:users,username|' . User::USERNAME_RULES,
62-
'name_first' => 'required|string|between:1,255',
63-
'name_last' => 'required|string|between:1,255',
64-
'password' => 'sometimes|nullable|' . User::PASSWORD_RULES,
65-
'root_admin' => 'required|boolean',
66-
'external_id' => 'sometimes|nullable|numeric|unique:users,external_id',
67-
];
40+
return User::getCreateRules();
6841
}
6942

7043
public function normalize()

app/Models/Location.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424

2525
namespace Pterodactyl\Models;
2626

27-
use Watson\Validating\ValidatingTrait;
27+
use Sofa\Eloquence\Eloquence;
28+
use Sofa\Eloquence\Validable;
2829
use Illuminate\Database\Eloquent\Model;
30+
use Sofa\Eloquence\Contracts\Validable as ValidableContract;
2931

30-
class Location extends Model
32+
class Location extends Model implements ValidableContract
3133
{
32-
use ValidatingTrait;
34+
use Eloquence, Validable;
3335

3436
/**
3537
* The table associated with the model.
@@ -50,9 +52,19 @@ class Location extends Model
5052
*
5153
* @var array
5254
*/
53-
protected $rules = [
54-
'short' => 'required|string|between:1,60|unique:locations,short',
55-
'long' => 'required|string|between:1,255',
55+
protected static $applicationRules = [
56+
'short' => 'required',
57+
'long' => 'required',
58+
];
59+
60+
/**
61+
* Rules ensuring that the raw data stored in the database meets expectations.
62+
*
63+
* @var array
64+
*/
65+
protected static $dataIntegrityRules = [
66+
'short' => 'string|between:1,60|unique:locations,short',
67+
'long' => 'string|between:1,255',
5668
];
5769

5870
/**

app/Models/User.php

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,29 @@
2626

2727
use Hash;
2828
use Google2FA;
29+
use Sofa\Eloquence\Eloquence;
30+
use Sofa\Eloquence\Validable;
2931
use Illuminate\Auth\Authenticatable;
3032
use Illuminate\Database\Eloquent\Model;
3133
use Illuminate\Notifications\Notifiable;
3234
use Pterodactyl\Exceptions\DisplayException;
33-
use Nicolaslopezj\Searchable\SearchableTrait;
3435
use Illuminate\Auth\Passwords\CanResetPassword;
3536
use Illuminate\Foundation\Auth\Access\Authorizable;
37+
use Sofa\Eloquence\Contracts\Validable as ValidableContract;
3638
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
3739
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
3840
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
3941
use Pterodactyl\Notifications\SendPasswordReset as ResetPasswordNotification;
4042

41-
class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract
43+
class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract, ValidableContract
4244
{
43-
use Authenticatable, Authorizable, CanResetPassword, Notifiable, SearchableTrait;
45+
use Authenticatable, Authorizable, CanResetPassword, Eloquence, Notifiable, Validable;
4446

4547
/**
4648
* The rules for user passwords.
4749
*
4850
* @var string
51+
* @deprecated
4952
*/
5053
const PASSWORD_RULES = 'regex:((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})';
5154

@@ -101,16 +104,53 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac
101104
* @var array
102105
*/
103106
protected $searchable = [
104-
'columns' => [
105-
'email' => 10,
106-
'username' => 9,
107-
'name_first' => 6,
108-
'name_last' => 6,
109-
'uuid' => 1,
110-
],
107+
'email' => 10,
108+
'username' => 9,
109+
'name_first' => 6,
110+
'name_last' => 6,
111+
'uuid' => 1,
112+
];
113+
114+
/**
115+
* Default values for specific fields in the database.
116+
*
117+
* @var array
118+
*/
119+
protected $attributes = [
120+
'root_admin' => false,
121+
'language' => 'en',
122+
'use_totp' => false,
123+
'totp_secret' => null,
124+
];
125+
126+
/**
127+
* Rules verifying that the data passed in forms is valid and meets application logic rules.
128+
* @var array
129+
*/
130+
protected static $applicationRules = [
131+
'email' => 'required|email',
132+
'username' => 'required|alpha_dash',
133+
'name_first' => 'required|string',
134+
'name_last' => 'required|string',
135+
'password' => 'sometimes|regex:((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,})',
111136
];
112137

113-
protected $query;
138+
/**
139+
* Rules verifying that the data being stored matches the expectations of the database.
140+
*
141+
* @var array
142+
*/
143+
protected static $dataIntegrityRules = [
144+
'email' => 'unique:users,email',
145+
'username' => 'between:1,255|unique:users,username',
146+
'name_first' => 'between:1,255',
147+
'name_last' => 'between:1,255',
148+
'password' => 'nullable|string',
149+
'root_admin' => 'boolean',
150+
'language' => 'string|between:2,5',
151+
'use_totp' => 'boolean',
152+
'totp_secret' => 'nullable|string',
153+
];
114154

115155
/**
116156
* Enables or disables TOTP on an account if the token is valid.
@@ -209,7 +249,7 @@ public function serverAccessArray()
209249
* Change the access level for a given call to `access()` on the user.
210250
*
211251
* @param string $level can be all, admin, subuser, owner
212-
* @return void
252+
* @return $this
213253
*/
214254
public function setAccessLevel($level = 'all')
215255
{
@@ -226,7 +266,7 @@ public function setAccessLevel($level = 'all')
226266
* Note: does not account for user admin status.
227267
*
228268
* @param array $load
229-
* @return \Illuiminate\Database\Eloquent\Builder
269+
* @return \Pterodactyl\Models\Server
230270
*/
231271
public function access(...$load)
232272
{

0 commit comments

Comments
 (0)