[L6] Add support for custom model validation logic
This commit is contained in:
parent
5b4a65a60c
commit
c586157dc4
|
@ -38,19 +38,10 @@ class Allocation extends Validable
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $applicationRules = [
|
public static $validationRules = [
|
||||||
'node_id' => 'required',
|
'node_id' => 'required|exists:nodes,id',
|
||||||
'ip' => 'required',
|
'ip' => 'required|ip',
|
||||||
'port' => 'required',
|
'port' => 'required|numeric|between:1024,65553',
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected static $dataIntegrityRules = [
|
|
||||||
'node_id' => 'exists:nodes,id',
|
|
||||||
'ip' => 'ip',
|
|
||||||
'port' => 'numeric|between:1024,65553',
|
|
||||||
'ip_alias' => 'nullable|string',
|
'ip_alias' => 'nullable|string',
|
||||||
'server_id' => 'nullable|exists:servers,id',
|
'server_id' => 'nullable|exists:servers,id',
|
||||||
];
|
];
|
||||||
|
|
|
@ -74,30 +74,17 @@ class ApiKey extends Validable
|
||||||
*/
|
*/
|
||||||
protected $hidden = ['token'];
|
protected $hidden = ['token'];
|
||||||
|
|
||||||
/**
|
|
||||||
* Rules defining what fields must be passed when making a model.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected static $applicationRules = [
|
|
||||||
'identifier' => 'required',
|
|
||||||
'memo' => 'required',
|
|
||||||
'user_id' => 'required',
|
|
||||||
'token' => 'required',
|
|
||||||
'key_type' => 'present',
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rules to protect against invalid data entry to DB.
|
* Rules to protect against invalid data entry to DB.
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $dataIntegrityRules = [
|
public static $validationRules = [
|
||||||
'user_id' => 'exists:users,id',
|
'user_id' => 'required|exists:users,id',
|
||||||
'key_type' => 'integer|min:0|max:4',
|
'key_type' => 'present|integer|min:0|max:4',
|
||||||
'identifier' => 'string|size:16|unique:api_keys,identifier',
|
'identifier' => 'required|string|size:16|unique:api_keys,identifier',
|
||||||
'token' => 'string',
|
'token' => 'required|string',
|
||||||
'memo' => 'nullable|string|max:500',
|
'memo' => 'required|nullable|string|max:500',
|
||||||
'allowed_ips' => 'nullable|json',
|
'allowed_ips' => 'nullable|json',
|
||||||
'last_used_at' => 'nullable|date',
|
'last_used_at' => 'nullable|date',
|
||||||
'r_' . AdminAcl::RESOURCE_USERS => 'integer|min:0|max:3',
|
'r_' . AdminAcl::RESOURCE_USERS => 'integer|min:0|max:3',
|
||||||
|
|
|
@ -38,21 +38,11 @@ class DaemonKey extends Validable
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $applicationRules = [
|
public static $validationRules = [
|
||||||
'user_id' => 'required',
|
'user_id' => 'required|numeric|exists:users,id',
|
||||||
'server_id' => 'required',
|
'server_id' => 'required|numeric|exists:servers,id',
|
||||||
'secret' => 'required',
|
'secret' => 'required|string|min:20',
|
||||||
'expires_at' => 'required',
|
'expires_at' => 'required|date',
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected static $dataIntegrityRules = [
|
|
||||||
'user_id' => 'numeric|exists:users,id',
|
|
||||||
'server_id' => 'numeric|exists:servers,id',
|
|
||||||
'secret' => 'string|min:20',
|
|
||||||
'expires_at' => 'date',
|
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -43,19 +43,15 @@ class Database extends Validable
|
||||||
'database_host_id' => 'integer',
|
'database_host_id' => 'integer',
|
||||||
];
|
];
|
||||||
|
|
||||||
protected static $applicationRules = [
|
/**
|
||||||
'server_id' => 'required',
|
* @var array
|
||||||
'database_host_id' => 'required',
|
*/
|
||||||
'database' => 'required',
|
public static $validationRules = [
|
||||||
'remote' => 'required',
|
'server_id' => 'required|numeric|exists:servers,id',
|
||||||
];
|
'database_host_id' => 'required|exists:database_hosts,id',
|
||||||
|
'database' => 'required|string|alpha_dash|between:3,100',
|
||||||
protected static $dataIntegrityRules = [
|
|
||||||
'server_id' => 'numeric|exists:servers,id',
|
|
||||||
'database_host_id' => 'exists:database_hosts,id',
|
|
||||||
'database' => 'string|alpha_dash|between:3,100',
|
|
||||||
'username' => 'string|alpha_dash|between:3,100',
|
'username' => 'string|alpha_dash|between:3,100',
|
||||||
'remote' => 'string|regex:/^[0-9%.]{1,15}$/',
|
'remote' => 'required|string|regex:/^[0-9%.]{1,15}$/',
|
||||||
'password' => 'string',
|
'password' => 'string',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
|
@ -44,31 +44,18 @@ class DatabaseHost extends Validable
|
||||||
'node_id' => 'integer',
|
'node_id' => 'integer',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
|
||||||
* Application validation rules.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected static $applicationRules = [
|
|
||||||
'name' => 'required',
|
|
||||||
'host' => 'required',
|
|
||||||
'port' => 'required',
|
|
||||||
'username' => 'required',
|
|
||||||
'node_id' => 'sometimes',
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validation rules to assign to this model.
|
* Validation rules to assign to this model.
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $dataIntegrityRules = [
|
public static $validationRules = [
|
||||||
'name' => 'string|max:255',
|
'name' => 'required|string|max:255',
|
||||||
'host' => 'ip|unique:database_hosts,host',
|
'host' => 'required|ip|unique:database_hosts,host',
|
||||||
'port' => 'numeric|between:1,65535',
|
'port' => 'required|numeric|between:1,65535',
|
||||||
'username' => 'string|max:32',
|
'username' => 'required|string|max:32',
|
||||||
'password' => 'nullable|string',
|
'password' => 'nullable|string',
|
||||||
'node_id' => 'nullable|integer|exists:nodes,id',
|
'node_id' => 'sometimes|nullable|integer|exists:nodes,id',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -54,37 +54,19 @@ class Egg extends Validable
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $applicationRules = [
|
public static $validationRules = [
|
||||||
'nest_id' => 'required',
|
'nest_id' => 'required|bail|numeric|exists:nests,id',
|
||||||
'uuid' => 'required',
|
'uuid' => 'required|string|size:36',
|
||||||
'name' => 'required',
|
'name' => 'required|string|max:255',
|
||||||
'description' => 'required',
|
'description' => 'required|string',
|
||||||
'author' => 'required',
|
'author' => 'required|string|email',
|
||||||
'docker_image' => 'required',
|
'docker_image' => 'required|string|max:255',
|
||||||
'startup' => 'required',
|
'startup' => 'required|nullable|string',
|
||||||
'config_from' => 'sometimes',
|
'config_from' => 'sometimes|bail|nullable|numeric|exists:eggs,id',
|
||||||
'config_stop' => 'required_without:config_from',
|
'config_stop' => 'required_without:config_from|nullable|string|max:255',
|
||||||
'config_startup' => 'required_without:config_from',
|
'config_startup' => 'required_without:config_from|nullable|json',
|
||||||
'config_logs' => 'required_without:config_from',
|
'config_logs' => 'required_without:config_from|nullable|json',
|
||||||
'config_files' => 'required_without:config_from',
|
'config_files' => 'required_without:config_from|nullable|json',
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected static $dataIntegrityRules = [
|
|
||||||
'nest_id' => 'bail|numeric|exists:nests,id',
|
|
||||||
'uuid' => 'string|size:36',
|
|
||||||
'name' => 'string|max:255',
|
|
||||||
'description' => 'string',
|
|
||||||
'author' => 'string|email',
|
|
||||||
'docker_image' => 'string|max:255',
|
|
||||||
'startup' => 'nullable|string',
|
|
||||||
'config_from' => 'bail|nullable|numeric|exists:eggs,id',
|
|
||||||
'config_stop' => 'nullable|string|max:255',
|
|
||||||
'config_startup' => 'nullable|json',
|
|
||||||
'config_logs' => 'nullable|json',
|
|
||||||
'config_files' => 'nullable|json',
|
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -45,24 +45,15 @@ class EggVariable extends Validable
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $applicationRules = [
|
public static $validationRules = [
|
||||||
'name' => 'required',
|
|
||||||
'env_variable' => 'required',
|
|
||||||
'rules' => 'required',
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected static $dataIntegrityRules = [
|
|
||||||
'egg_id' => 'exists:eggs,id',
|
'egg_id' => 'exists:eggs,id',
|
||||||
'name' => 'string|between:1,255',
|
'name' => 'required|string|between:1,255',
|
||||||
'description' => 'string',
|
'description' => 'string',
|
||||||
'env_variable' => 'regex:/^[\w]{1,255}$/|notIn:' . self::RESERVED_ENV_NAMES,
|
'env_variable' => 'required|regex:/^[\w]{1,255}$/|notIn:' . self::RESERVED_ENV_NAMES,
|
||||||
'default_value' => 'string',
|
'default_value' => 'string',
|
||||||
'user_viewable' => 'boolean',
|
'user_viewable' => 'boolean',
|
||||||
'user_editable' => 'boolean',
|
'user_editable' => 'boolean',
|
||||||
'rules' => 'string',
|
'rules' => 'required|string',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -24,24 +24,14 @@ class Location extends Validable
|
||||||
*/
|
*/
|
||||||
protected $guarded = ['id', 'created_at', 'updated_at'];
|
protected $guarded = ['id', 'created_at', 'updated_at'];
|
||||||
|
|
||||||
/**
|
|
||||||
* Validation rules to apply to this model.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected static $applicationRules = [
|
|
||||||
'short' => 'required',
|
|
||||||
'long' => 'required',
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rules ensuring that the raw data stored in the database meets expectations.
|
* Rules ensuring that the raw data stored in the database meets expectations.
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $dataIntegrityRules = [
|
public static $validationRules = [
|
||||||
'short' => 'string|between:1,60|unique:locations,short',
|
'short' => 'required|string|between:1,60|unique:locations,short',
|
||||||
'long' => 'string|between:1,255',
|
'long' => 'required|string|between:1,255',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -30,19 +30,10 @@ class Nest extends Validable
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $applicationRules = [
|
public static $validationRules = [
|
||||||
'author' => 'required',
|
'author' => 'required|string|email',
|
||||||
'name' => 'required',
|
'name' => 'required|string|max:255',
|
||||||
'description' => 'sometimes',
|
'description' => 'sometimes|nullable|string',
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected static $dataIntegrityRules = [
|
|
||||||
'author' => 'string|email',
|
|
||||||
'name' => 'string|max:255',
|
|
||||||
'description' => 'nullable|string',
|
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -77,37 +77,21 @@ class Node extends Validable
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $applicationRules = [
|
public static $validationRules = [
|
||||||
'name' => 'required',
|
'name' => 'required|regex:/^([\w .-]{1,100})$/',
|
||||||
'location_id' => 'required',
|
|
||||||
'fqdn' => 'required',
|
|
||||||
'scheme' => 'required',
|
|
||||||
'memory' => 'required',
|
|
||||||
'memory_overallocate' => 'required',
|
|
||||||
'disk' => 'required',
|
|
||||||
'disk_overallocate' => 'required',
|
|
||||||
'daemonBase' => 'sometimes|required',
|
|
||||||
'daemonSFTP' => 'required',
|
|
||||||
'daemonListen' => 'required',
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected static $dataIntegrityRules = [
|
|
||||||
'name' => 'regex:/^([\w .-]{1,100})$/',
|
|
||||||
'description' => 'string',
|
'description' => 'string',
|
||||||
'location_id' => 'exists:locations,id',
|
'location_id' => 'required|exists:locations,id',
|
||||||
'public' => 'boolean',
|
'public' => 'boolean',
|
||||||
'fqdn' => 'string',
|
'fqdn' => 'required|string',
|
||||||
|
'scheme' => 'required',
|
||||||
'behind_proxy' => 'boolean',
|
'behind_proxy' => 'boolean',
|
||||||
'memory' => 'numeric|min:1',
|
'memory' => 'required|numeric|min:1',
|
||||||
'memory_overallocate' => 'numeric|min:-1',
|
'memory_overallocate' => 'required|numeric|min:-1',
|
||||||
'disk' => 'numeric|min:1',
|
'disk' => 'required|numeric|min:1',
|
||||||
'disk_overallocate' => 'numeric|min:-1',
|
'disk_overallocate' => 'required|numeric|min:-1',
|
||||||
'daemonBase' => 'regex:/^([\/][\d\w.\-\/]+)$/',
|
'daemonBase' => 'sometimes|required|regex:/^([\/][\d\w.\-\/]+)$/',
|
||||||
'daemonSFTP' => 'numeric|between:1,65535',
|
'daemonSFTP' => 'required|numeric|between:1,65535',
|
||||||
'daemonListen' => 'numeric|between:1,65535',
|
'daemonListen' => 'required|numeric|between:1,65535',
|
||||||
'maintenance_mode' => 'boolean',
|
'maintenance_mode' => 'boolean',
|
||||||
'upload_size' => 'int|between:1,1024',
|
'upload_size' => 'int|between:1,1024',
|
||||||
];
|
];
|
||||||
|
|
|
@ -33,27 +33,14 @@ class Pack extends Validable
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $applicationRules = [
|
public static $validationRules = [
|
||||||
'name' => 'required',
|
'name' => 'required|string',
|
||||||
'version' => 'required',
|
'version' => 'required|string',
|
||||||
'description' => 'sometimes',
|
'description' => 'sometimes|nullable|string',
|
||||||
'selectable' => 'sometimes|required',
|
'selectable' => 'sometimes|required|boolean',
|
||||||
'visible' => 'sometimes|required',
|
'visible' => 'sometimes|required|boolean',
|
||||||
'locked' => 'sometimes|required',
|
'locked' => 'sometimes|required|boolean',
|
||||||
'egg_id' => 'required',
|
'egg_id' => 'required|exists:eggs,id',
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected static $dataIntegrityRules = [
|
|
||||||
'name' => 'string',
|
|
||||||
'version' => 'string',
|
|
||||||
'description' => 'nullable|string',
|
|
||||||
'selectable' => 'boolean',
|
|
||||||
'visible' => 'boolean',
|
|
||||||
'locked' => 'boolean',
|
|
||||||
'egg_id' => 'exists:eggs,id',
|
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -43,17 +43,9 @@ class Permission extends Validable
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $applicationRules = [
|
public static $validationRules = [
|
||||||
'subuser_id' => 'required',
|
'subuser_id' => 'required|numeric|min:1',
|
||||||
'permission' => 'required',
|
'permission' => 'required|string',
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected static $dataIntegrityRules = [
|
|
||||||
'subuser_id' => 'numeric|min:1',
|
|
||||||
'permission' => 'string',
|
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -73,24 +73,13 @@ class Schedule extends Validable
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $applicationRules = [
|
public static $validationRules = [
|
||||||
'server_id' => 'required',
|
'server_id' => 'required|exists:servers,id',
|
||||||
'cron_day_of_week' => 'required',
|
|
||||||
'cron_day_of_month' => 'required',
|
|
||||||
'cron_hour' => 'required',
|
|
||||||
'cron_minute' => 'required',
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected static $dataIntegrityRules = [
|
|
||||||
'server_id' => 'exists:servers,id',
|
|
||||||
'name' => 'nullable|string|max:255',
|
'name' => 'nullable|string|max:255',
|
||||||
'cron_day_of_week' => 'string',
|
'cron_day_of_week' => 'required|string',
|
||||||
'cron_day_of_month' => 'string',
|
'cron_day_of_month' => 'required|string',
|
||||||
'cron_hour' => 'string',
|
'cron_hour' => 'required|string',
|
||||||
'cron_minute' => 'string',
|
'cron_minute' => 'required|string',
|
||||||
'is_active' => 'boolean',
|
'is_active' => 'boolean',
|
||||||
'is_processing' => 'boolean',
|
'is_processing' => 'boolean',
|
||||||
'last_run_at' => 'nullable|date',
|
'last_run_at' => 'nullable|date',
|
||||||
|
|
|
@ -95,53 +95,28 @@ class Server extends Validable
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $applicationRules = [
|
public static $validationRules = [
|
||||||
'external_id' => 'sometimes',
|
'external_id' => 'sometimes|nullable|string|between:1,191|unique:servers',
|
||||||
'owner_id' => 'required',
|
'owner_id' => 'required|integer|exists:users,id',
|
||||||
'name' => 'required',
|
'name' => 'required|string|min:1|max:255',
|
||||||
'memory' => 'required',
|
'node_id' => 'required|exists:nodes,id',
|
||||||
'swap' => 'required',
|
|
||||||
'io' => 'required',
|
|
||||||
'cpu' => 'required',
|
|
||||||
'oom_disabled' => 'sometimes',
|
|
||||||
'disk' => 'required',
|
|
||||||
'nest_id' => 'required',
|
|
||||||
'egg_id' => 'required',
|
|
||||||
'node_id' => 'required',
|
|
||||||
'allocation_id' => 'required',
|
|
||||||
'pack_id' => 'sometimes',
|
|
||||||
'skip_scripts' => 'sometimes',
|
|
||||||
'image' => 'required',
|
|
||||||
'startup' => 'required',
|
|
||||||
'database_limit' => 'present',
|
|
||||||
'allocation_limit' => 'sometimes',
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected static $dataIntegrityRules = [
|
|
||||||
'external_id' => 'nullable|string|between:1,191|unique:servers',
|
|
||||||
'owner_id' => 'integer|exists:users,id',
|
|
||||||
'name' => 'string|min:1|max:255',
|
|
||||||
'node_id' => 'exists:nodes,id',
|
|
||||||
'description' => 'string',
|
'description' => 'string',
|
||||||
'memory' => 'numeric|min:0',
|
'memory' => 'required|numeric|min:0',
|
||||||
'swap' => 'numeric|min:-1',
|
'swap' => 'required|numeric|min:-1',
|
||||||
'io' => 'numeric|between:10,1000',
|
'io' => 'required|numeric|between:10,1000',
|
||||||
'cpu' => 'numeric|min:0',
|
'cpu' => 'required|numeric|min:0',
|
||||||
'oom_disabled' => 'boolean',
|
'oom_disabled' => 'sometimes|boolean',
|
||||||
'disk' => 'numeric|min:0',
|
'disk' => 'required|numeric|min:0',
|
||||||
'allocation_id' => 'bail|unique:servers|exists:allocations,id',
|
'allocation_id' => 'required|bail|unique:servers|exists:allocations,id',
|
||||||
'nest_id' => 'exists:nests,id',
|
'nest_id' => 'required|exists:nests,id',
|
||||||
'egg_id' => 'exists:eggs,id',
|
'egg_id' => 'required|exists:eggs,id',
|
||||||
'pack_id' => 'nullable|numeric|min:0',
|
'pack_id' => 'sometimes|nullable|numeric|min:0',
|
||||||
'startup' => 'string',
|
'startup' => 'required|string',
|
||||||
'skip_scripts' => 'boolean',
|
'skip_scripts' => 'sometimes|boolean',
|
||||||
'image' => 'string|max:255',
|
'image' => 'required|string|max:255',
|
||||||
'installed' => 'in:0,1,2',
|
'installed' => 'in:0,1,2',
|
||||||
'database_limit' => 'nullable|integer|min:0',
|
'database_limit' => 'present|nullable|integer|min:0',
|
||||||
'allocation_limit' => 'nullable|integer|min:0',
|
'allocation_limit' => 'sometimes|nullable|integer|min:0',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -24,7 +24,7 @@ class Setting extends Validable
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $applicationRules = [
|
public static $validationRules = [
|
||||||
'key' => 'required|string|between:1,255',
|
'key' => 'required|string|between:1,255',
|
||||||
'value' => 'string',
|
'value' => 'string',
|
||||||
];
|
];
|
||||||
|
|
|
@ -41,17 +41,9 @@ class Subuser extends Validable
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $applicationRules = [
|
public static $validationRules = [
|
||||||
'user_id' => 'required',
|
'user_id' => 'required|numeric|exists:users,id',
|
||||||
'server_id' => 'required',
|
'server_id' => 'required|numeric|exists:servers,id',
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected static $dataIntegrityRules = [
|
|
||||||
'user_id' => 'numeric|exists:users,id',
|
|
||||||
'server_id' => 'numeric|exists:servers,id',
|
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -67,23 +67,12 @@ class Task extends Validable
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $applicationRules = [
|
public static $validationRules = [
|
||||||
'schedule_id' => 'required',
|
'schedule_id' => 'required|numeric|exists:schedules,id',
|
||||||
'sequence_id' => 'required',
|
'sequence_id' => 'required|numeric|min:1',
|
||||||
'action' => 'required',
|
'action' => 'required|string',
|
||||||
'payload' => 'required',
|
'payload' => 'required|string',
|
||||||
'time_offset' => 'required',
|
'time_offset' => 'required|numeric|between:0,900',
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected static $dataIntegrityRules = [
|
|
||||||
'schedule_id' => 'numeric|exists:schedules,id',
|
|
||||||
'sequence_id' => 'numeric|min:1',
|
|
||||||
'action' => 'string',
|
|
||||||
'payload' => 'string',
|
|
||||||
'time_offset' => 'numeric|between:0,900',
|
|
||||||
'is_queued' => 'boolean',
|
'is_queued' => 'boolean',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
|
@ -121,38 +121,21 @@ class User extends Validable implements
|
||||||
'totp_secret' => null,
|
'totp_secret' => null,
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
|
||||||
* Rules verifying that the data passed in forms is valid and meets application logic rules.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected static $applicationRules = [
|
|
||||||
'uuid' => 'required',
|
|
||||||
'email' => 'required',
|
|
||||||
'external_id' => 'sometimes',
|
|
||||||
'username' => 'required',
|
|
||||||
'name_first' => 'required',
|
|
||||||
'name_last' => 'required',
|
|
||||||
'password' => 'sometimes',
|
|
||||||
'language' => 'sometimes',
|
|
||||||
'use_totp' => 'sometimes',
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rules verifying that the data being stored matches the expectations of the database.
|
* Rules verifying that the data being stored matches the expectations of the database.
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $dataIntegrityRules = [
|
public static $validationRules = [
|
||||||
'uuid' => 'string|size:36|unique:users,uuid',
|
'uuid' => 'required|string|size:36|unique:users,uuid',
|
||||||
'email' => 'email|unique:users,email',
|
'email' => 'required|email|unique:users,email',
|
||||||
'external_id' => 'nullable|string|max:255|unique:users,external_id',
|
'external_id' => 'sometimes|nullable|string|max:255|unique:users,external_id',
|
||||||
'username' => 'between:1,255|unique:users,username',
|
'username' => 'required|between:1,255|unique:users,username',
|
||||||
'name_first' => 'string|between:1,255',
|
'name_first' => 'required|string|between:1,255',
|
||||||
'name_last' => 'string|between:1,255',
|
'name_last' => 'required|string|between:1,255',
|
||||||
'password' => 'nullable|string',
|
'password' => 'required|nullable|string',
|
||||||
'root_admin' => 'boolean',
|
'root_admin' => 'boolean',
|
||||||
'language' => 'string',
|
'language' => 'required|string',
|
||||||
'use_totp' => 'boolean',
|
'use_totp' => 'boolean',
|
||||||
'totp_secret' => 'nullable|string',
|
'totp_secret' => 'nullable|string',
|
||||||
];
|
];
|
||||||
|
|
|
@ -2,39 +2,139 @@
|
||||||
|
|
||||||
namespace Pterodactyl\Models;
|
namespace Pterodactyl\Models;
|
||||||
|
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
use Illuminate\Container\Container;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Contracts\Validation\Factory;
|
||||||
|
|
||||||
abstract class Validable extends Model
|
abstract class Validable extends Model
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var array
|
* Determines if the model should undergo data validation before it is saved
|
||||||
|
* to the database.
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
*/
|
*/
|
||||||
protected static $applicationRules = [];
|
protected $skipValidation = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The validator instance used by this model.
|
||||||
|
*
|
||||||
|
* @var \Illuminate\Validation\Validator
|
||||||
|
*/
|
||||||
|
protected $validator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Illuminate\Contracts\Validation\Factory
|
||||||
|
*/
|
||||||
|
protected static $validatorFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected static $dataIntegrityRules = [];
|
public static $validationRules = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Listen for the model saving event and fire off the validation
|
* Listen for the model saving event and fire off the validation
|
||||||
* function before it is saved.
|
* function before it is saved.
|
||||||
|
*
|
||||||
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
||||||
*/
|
*/
|
||||||
protected static function boot()
|
protected static function boot()
|
||||||
{
|
{
|
||||||
parent::boot();
|
parent::boot();
|
||||||
|
|
||||||
|
static::$validatorFactory = Container::getInstance()->make(Factory::class);
|
||||||
|
|
||||||
static::saving(function (Validable $model) {
|
static::saving(function (Validable $model) {
|
||||||
return $model->validate();
|
return $model->validate();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @todo implement custom logic once L6 is done
|
* Set the model to skip validation when saving.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function skipValidation()
|
||||||
|
{
|
||||||
|
$this->skipValidation = true;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the validator instance used by this model.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Validation\Validator|\Illuminate\Contracts\Validation\Validator
|
||||||
|
*/
|
||||||
|
public function getValidator()
|
||||||
|
{
|
||||||
|
$rules = $this->getKey() ? static::getRulesForUpdate($this) : static::getRules();
|
||||||
|
|
||||||
|
return $this->validator ?: $this->validator = static::$validatorFactory->make(
|
||||||
|
[], $rules, [], []
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public static function getRules()
|
||||||
|
{
|
||||||
|
return static::$validationRules;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \Illuminate\Database\Eloquent\Model|int|string $id
|
||||||
|
* @param string $primaryKey
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public static function getRulesForUpdate($id, string $primaryKey = 'id')
|
||||||
|
{
|
||||||
|
if ($id instanceof Model) {
|
||||||
|
list($primaryKey, $id) = [$id->getKeyName(), $id->getKey()];
|
||||||
|
}
|
||||||
|
|
||||||
|
$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')) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
list(, $args) = explode(':', $datum);
|
||||||
|
$args = explode(',', $args);
|
||||||
|
|
||||||
|
$datum = Rule::unique($args[0], $args[1] ?? $key)->ignore($id, $primaryKey)->__toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $rules;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines if the model is in a valid state or not.
|
||||||
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function validate()
|
public function validate()
|
||||||
{
|
{
|
||||||
return true;
|
if ($this->skipValidation) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->getValidator()->setData(
|
||||||
|
$this->getAttributes()
|
||||||
|
)->passes();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue