1
1
mirror of https://github.com/pterodactyl/panel.git synced 2024-11-23 01:22:30 +01:00

[L6] Update even more areas of codebase using custom model validation logic

This commit is contained in:
Dane Everitt 2019-09-04 22:26:28 -07:00
parent c586157dc4
commit 0d6cf54314
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
23 changed files with 41 additions and 33 deletions

View File

@ -15,7 +15,7 @@ class StoreApplicationApiKeyRequest extends AdminFormRequest
*/
public function rules()
{
$modelRules = ApiKey::getCreateRules();
$modelRules = ApiKey::getRules();
return collect(AdminAcl::getResourceList())->mapWithKeys(function ($resource) use ($modelRules) {
return [AdminAcl::COLUMN_IDENTIFIER . $resource => $modelRules['r_' . $resource]];

View File

@ -12,10 +12,10 @@ class DatabaseHostFormRequest extends AdminFormRequest
public function rules()
{
if ($this->method() !== 'POST') {
return DatabaseHost::getUpdateRulesForId($this->route()->parameter('host'));
return DatabaseHost::getRulesForUpdate($this->route()->parameter('host'));
}
return DatabaseHost::getCreateRules();
return DatabaseHost::getRules();
}
/**

View File

@ -21,9 +21,9 @@ class LocationFormRequest extends AdminFormRequest
public function rules()
{
if ($this->method() === 'PATCH') {
return Location::getUpdateRulesForId($this->route()->parameter('location')->id);
return Location::getRulesForUpdate($this->route()->parameter('location')->id);
}
return Location::getCreateRules();
return Location::getRules();
}
}

View File

@ -20,10 +20,10 @@ class NodeFormRequest extends AdminFormRequest
public function rules()
{
if ($this->method() === 'PATCH') {
return Node::getUpdateRulesForId($this->route()->parameter('node')->id);
return Node::getRulesForUpdate($this->route()->parameter('node'));
}
return Node::getCreateRules();
return Node::getRules();
}
/**

View File

@ -20,10 +20,10 @@ class PackFormRequest extends AdminFormRequest
public function rules()
{
if ($this->method() === 'PATCH') {
return Pack::getUpdateRulesForId($this->route()->parameter('pack')->id);
return Pack::getRulesForUpdate($this->route()->parameter('pack')->id);
}
return Pack::getCreateRules();
return Pack::getRules();
}
/**

View File

@ -21,7 +21,7 @@ class ServerFormRequest extends AdminFormRequest
*/
public function rules()
{
$rules = Server::getCreateRules();
$rules = Server::getRules();
$rules['description'][] = 'nullable';
return $rules;

View File

@ -12,9 +12,9 @@ class UserFormRequest extends AdminFormRequest
*/
public function rules()
{
$rules = collect(User::getCreateRules());
$rules = collect(User::getRules());
if ($this->method() === 'PATCH') {
$rules = collect(User::getUpdateRulesForId($this->route()->parameter('user')->id))->merge([
$rules = collect(User::getRulesForUpdate($this->route()->parameter('user')))->merge([
'ignore_connection_error' => ['sometimes', 'nullable', 'boolean'],
]);
}

View File

@ -25,7 +25,7 @@ class StoreLocationRequest extends ApplicationApiRequest
*/
public function rules(): array
{
return collect(Location::getCreateRules())->only([
return collect(Location::getRules())->only([
'long',
'short',
])->toArray();

View File

@ -27,7 +27,7 @@ class UpdateLocationRequest extends StoreLocationRequest
{
$locationId = $this->route()->parameter('location')->id;
return collect(Location::getUpdateRulesForId($locationId))->only([
return collect(Location::getRulesForUpdate($locationId))->only([
'short',
'long',
])->toArray();

View File

@ -26,7 +26,7 @@ class StoreNodeRequest extends ApplicationApiRequest
*/
public function rules(array $rules = null): array
{
return collect($rules ?? Node::getCreateRules())->only([
return collect($rules ?? Node::getRules())->only([
'public',
'name',
'location_id',

View File

@ -17,6 +17,6 @@ class UpdateNodeRequest extends StoreNodeRequest
{
$nodeId = $this->getModel(Node::class)->id;
return parent::rules(Node::getUpdateRulesForId($nodeId));
return parent::rules(Node::getRulesForUpdate($nodeId));
}
}

View File

@ -28,7 +28,7 @@ class StoreServerRequest extends ApplicationApiRequest
*/
public function rules(): array
{
$rules = Server::getCreateRules();
$rules = Server::getRules();
return [
'external_id' => $rules['external_id'],

View File

@ -14,7 +14,7 @@ class UpdateServerBuildConfigurationRequest extends ServerWriteRequest
*/
public function rules(): array
{
$rules = Server::getUpdateRulesForId($this->getModel(Server::class)->id);
$rules = Server::getRulesForUpdate($this->getModel(Server::class));
return [
'allocation' => $rules['allocation_id'],

View File

@ -13,7 +13,7 @@ class UpdateServerDetailsRequest extends ServerWriteRequest
*/
public function rules(): array
{
$rules = Server::getUpdateRulesForId($this->getModel(Server::class)->id);
$rules = Server::getRulesForUpdate($this->getModel(Server::class));
return [
'external_id' => $rules['external_id'],

View File

@ -25,7 +25,7 @@ class UpdateServerStartupRequest extends ApplicationApiRequest
*/
public function rules(): array
{
$data = Server::getUpdateRulesForId($this->getModel(Server::class)->id);
$data = Server::getRulesForUpdate($this->getModel(Server::class));
return [
'startup' => $data['startup'],

View File

@ -26,7 +26,7 @@ class StoreUserRequest extends ApplicationApiRequest
*/
public function rules(array $rules = null): array
{
$rules = $rules ?? User::getCreateRules();
$rules = $rules ?? User::getRules();
$response = collect($rules)->only([
'external_id',

View File

@ -16,6 +16,6 @@ class UpdateUserRequest extends StoreUserRequest
{
$userId = $this->getModel(User::class)->id;
return parent::rules(User::getUpdateRulesForId($userId));
return parent::rules(User::getRulesForUpdate($userId));
}
}

View File

@ -32,7 +32,7 @@ class UpdateEmailRequest extends ClientApiRequest
*/
public function rules(): array
{
$rules = User::getUpdateRulesForId($this->user()->id);
$rules = User::getRulesForUpdate($this->user());
return ['email' => $rules['email']];
}

View File

@ -32,7 +32,7 @@ class UpdatePasswordRequest extends ClientApiRequest
*/
public function rules(): array
{
$rules = User::getUpdateRulesForId($this->user()->id);
$rules = User::getRulesForUpdate($this->user());
return ['password' => array_merge($rules['password'], ['confirmed'])];
}

View File

@ -40,7 +40,7 @@ class AccountDataFormRequest extends FrontendUserFormRequest
*/
public function rules()
{
$modelRules = User::getUpdateRulesForId($this->user()->id);
$modelRules = User::getRulesForUpdate($this->user());
switch ($this->input('do_action')) {
case 'email':

View File

@ -25,7 +25,7 @@ class ChangeServerNameRequest extends ServerFormRequest
public function rules()
{
return [
'name' => Server::getCreateRules()['name'],
'name' => Server::getRules()['name'],
];
}
}

View File

@ -144,8 +144,10 @@ class User extends Validable implements
* Implement language verification by overriding Eloquence's gather
* rules function.
*/
protected static function gatherRules()
public static function getRules()
{
$rules = self::getRules();
$rules['language'][] = new In(array_keys((new self)->getAvailableLanguages()));
$rules['username'][] = new Username;

View File

@ -79,14 +79,24 @@ abstract class Validable extends Model
}
/**
* Returns the rules associated with this model.
*
* @return array
*/
public static function getRules()
{
return static::$validationRules;
$rules = static::$validationRules;
foreach ($rules as $key => &$rule) {
$rule = is_array($rule) ? $rule : explode('|', $rule);
}
return $rules;
}
/**
* Returns the rules associated with the model, specifically for updating the given model
* rather than just creating it.
*
* @param \Illuminate\Database\Eloquent\Model|int|string $id
* @param string $primaryKey
* @return array
@ -98,17 +108,13 @@ abstract class Validable extends Model
}
$rules = static::getRules();
foreach ($rules as $key => &$rule) {
$rule = is_array($rule) ? $rule : explode('|', $rule);
}
foreach ($rules as $key => &$data) {
// For each rule in a given field, iterate over it and confirm if the rule
// is one for a unique field. If that is the case, append the ID of the current
// working model so we don't run into errors due to the way that field validation
// works.
foreach ($data as &$datum) {
if (! Str::startsWith($datum, 'unique')) {
if (! is_string($datum) || ! Str::startsWith($datum, 'unique')) {
continue;
}