[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;
}