mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-02 14:36:17 +00:00
2.x
This commit is contained in:
33
app/Actions/Projects/AddUser.php
Normal file
33
app/Actions/Projects/AddUser.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Projects;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class AddUser
|
||||
{
|
||||
public function add(Project $project, array $input): void
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = User::query()->findOrFail($input['user']);
|
||||
|
||||
$project->users()->detach($user);
|
||||
$project->users()->attach($user);
|
||||
}
|
||||
|
||||
public static function rules(Project $project): array
|
||||
{
|
||||
return [
|
||||
'user' => [
|
||||
'required',
|
||||
Rule::exists('users', 'id'),
|
||||
Rule::unique('user_project', 'user_id')->where(function (Builder $query) use ($project) {
|
||||
$query->where('project_id', $project->id);
|
||||
}),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
@ -27,15 +27,21 @@ public function create(User $user, array $input): Project
|
||||
return $project;
|
||||
}
|
||||
|
||||
private function validate(array $input): void
|
||||
public static function rules(): array
|
||||
{
|
||||
Validator::make($input, [
|
||||
return [
|
||||
'name' => [
|
||||
'required',
|
||||
'string',
|
||||
'max:255',
|
||||
'unique:projects,name',
|
||||
'lowercase:projects,name',
|
||||
],
|
||||
])->validate();
|
||||
];
|
||||
}
|
||||
|
||||
private function validate(array $input): void
|
||||
{
|
||||
Validator::make($input, self::rules())->validate();
|
||||
}
|
||||
}
|
||||
|
@ -23,15 +23,21 @@ public function update(Project $project, array $input): Project
|
||||
return $project;
|
||||
}
|
||||
|
||||
private function validate(Project $project, array $input): void
|
||||
public static function rules(Project $project): array
|
||||
{
|
||||
Validator::make($input, [
|
||||
return [
|
||||
'name' => [
|
||||
'required',
|
||||
'string',
|
||||
'max:255',
|
||||
Rule::unique('projects')->where('user_id', $project->user_id)->ignore($project->id),
|
||||
Rule::unique('projects', 'name')->ignore($project->id),
|
||||
'lowercase:projects,name',
|
||||
],
|
||||
])->validate();
|
||||
];
|
||||
}
|
||||
|
||||
private function validate(Project $project, array $input): void
|
||||
{
|
||||
Validator::make($input, self::rules($project))->validate();
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,6 @@
|
||||
use App\Enums\FirewallRuleStatus;
|
||||
use App\Enums\ServerProvider;
|
||||
use App\Enums\ServerStatus;
|
||||
use App\Exceptions\ServerProviderError;
|
||||
use App\Facades\Notifier;
|
||||
use App\Models\Server;
|
||||
use App\Models\User;
|
||||
@ -16,10 +15,8 @@
|
||||
use Illuminate\Support\Facades\Bus;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Throwable;
|
||||
|
||||
class CreateServer
|
||||
@ -29,8 +26,6 @@ class CreateServer
|
||||
*/
|
||||
public function create(User $creator, array $input): Server
|
||||
{
|
||||
$this->validateInputs($input);
|
||||
|
||||
$server = new Server([
|
||||
'project_id' => $creator->currentProject->id,
|
||||
'user_id' => $creator->id,
|
||||
@ -56,12 +51,8 @@ public function create(User $creator, array $input): Server
|
||||
$server->provider_id = $input['server_provider'];
|
||||
}
|
||||
|
||||
// validate type
|
||||
$this->validateType($server, $input);
|
||||
$server->type_data = $server->type()->data($input);
|
||||
|
||||
// validate provider
|
||||
$this->validateProvider($server, $input);
|
||||
$server->provider_data = $server->provider()->data($input);
|
||||
|
||||
// save
|
||||
@ -85,11 +76,6 @@ public function create(User $creator, array $input): Server
|
||||
} catch (Exception $e) {
|
||||
$server->provider()->delete();
|
||||
DB::rollBack();
|
||||
if ($e instanceof ServerProviderError) {
|
||||
throw ValidationException::withMessages([
|
||||
'provider' => __('Provider Error: ').$e->getMessage(),
|
||||
]);
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
@ -126,59 +112,82 @@ function () use ($server) {
|
||||
$bus->onConnection('ssh')->dispatch();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
private function validateInputs(array $input): void
|
||||
public static function rules(array $input): array
|
||||
{
|
||||
$rules = [
|
||||
'provider' => 'required|in:'.implode(',', config('core.server_providers')),
|
||||
'name' => 'required',
|
||||
'os' => 'required|in:'.implode(',', config('core.operating_systems')),
|
||||
'provider' => [
|
||||
'required',
|
||||
Rule::in(config('core.server_providers')),
|
||||
],
|
||||
'name' => [
|
||||
'required',
|
||||
],
|
||||
'os' => [
|
||||
'required',
|
||||
Rule::in(config('core.operating_systems')),
|
||||
],
|
||||
'type' => [
|
||||
'required',
|
||||
Rule::in(config('core.server_types')),
|
||||
],
|
||||
'server_provider' => [
|
||||
Rule::when(function () use ($input) {
|
||||
return $input['provider'] != ServerProvider::CUSTOM;
|
||||
}, [
|
||||
'required',
|
||||
'exists:server_providers,id,user_id,'.auth()->user()->id,
|
||||
]),
|
||||
],
|
||||
'ip' => [
|
||||
Rule::when(function () use ($input) {
|
||||
return $input['provider'] == ServerProvider::CUSTOM;
|
||||
}, [
|
||||
'required',
|
||||
new RestrictedIPAddressesRule,
|
||||
]),
|
||||
],
|
||||
'port' => [
|
||||
Rule::when(function () use ($input) {
|
||||
return $input['provider'] == ServerProvider::CUSTOM;
|
||||
}, [
|
||||
'required',
|
||||
'numeric',
|
||||
'min:1',
|
||||
'max:65535',
|
||||
]),
|
||||
],
|
||||
];
|
||||
|
||||
Validator::make($input, $rules)->validate();
|
||||
|
||||
if ($input['provider'] != 'custom') {
|
||||
$rules['server_provider'] = 'required|exists:server_providers,id,user_id,'.auth()->user()->id;
|
||||
}
|
||||
|
||||
if ($input['provider'] == 'custom') {
|
||||
$rules['ip'] = [
|
||||
'required',
|
||||
new RestrictedIPAddressesRule(),
|
||||
];
|
||||
$rules['port'] = [
|
||||
'required',
|
||||
'numeric',
|
||||
'min:1',
|
||||
'max:65535',
|
||||
];
|
||||
}
|
||||
|
||||
Validator::make($input, $rules)->validate();
|
||||
return array_merge($rules, self::typeRules($input), self::providerRules($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
private function validateType(Server $server, array $input): void
|
||||
private static function typeRules(array $input): array
|
||||
{
|
||||
Validator::make($input, $server->type()->createRules($input))
|
||||
->validate();
|
||||
if (! isset($input['type']) || ! in_array($input['type'], config('core.server_types'))) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$server = new Server(['type' => $input['type']]);
|
||||
|
||||
return $server->type()->createRules($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
private function validateProvider(Server $server, array $input): void
|
||||
private static function providerRules(array $input): array
|
||||
{
|
||||
Validator::make($input, $server->provider()->createRules($input))
|
||||
->validate();
|
||||
if (
|
||||
! isset($input['provider']) ||
|
||||
! in_array($input['provider'], config('core.server_providers')) ||
|
||||
$input['provider'] == ServerProvider::CUSTOM
|
||||
) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$server = new Server([
|
||||
'provider' => $input['provider'],
|
||||
'provider_id' => $input['server_provider'],
|
||||
]);
|
||||
|
||||
return $server->provider()->createRules($input);
|
||||
}
|
||||
|
||||
private function createFirewallRules(Server $server): void
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
use App\Models\Server;
|
||||
use App\ValidationRules\RestrictedIPAddressesRule;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class EditServer
|
||||
@ -14,8 +14,6 @@ class EditServer
|
||||
*/
|
||||
public function edit(Server $server, array $input): Server
|
||||
{
|
||||
$this->validate($input);
|
||||
|
||||
$checkConnection = false;
|
||||
if (isset($input['name'])) {
|
||||
$server->name = $input['name'];
|
||||
@ -41,15 +39,24 @@ public function edit(Server $server, array $input): Server
|
||||
return $server;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
protected function validate(array $input): void
|
||||
public static function rules(Server $server): array
|
||||
{
|
||||
Validator::make($input, [
|
||||
'ip' => [
|
||||
new RestrictedIPAddressesRule(),
|
||||
return [
|
||||
'name' => [
|
||||
'string',
|
||||
'max:255',
|
||||
Rule::unique('servers')->where('project_id', $server->project_id)->ignore($server->id),
|
||||
],
|
||||
])->validateWithBag('editServer');
|
||||
'ip' => [
|
||||
'string',
|
||||
new RestrictedIPAddressesRule,
|
||||
Rule::unique('servers')->where('project_id', $server->project_id)->ignore($server->id),
|
||||
],
|
||||
'port' => [
|
||||
'integer',
|
||||
'min:1',
|
||||
'max:65535',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,6 @@
|
||||
use App\Models\User;
|
||||
use App\ServerProviders\ServerProvider as ServerProviderContract;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
@ -17,11 +16,7 @@ class CreateServerProvider
|
||||
*/
|
||||
public function create(User $user, array $input): ServerProvider
|
||||
{
|
||||
$this->validateInput($input);
|
||||
|
||||
$provider = $this->getProvider($input['provider']);
|
||||
|
||||
$this->validateProvider($provider, $input);
|
||||
$provider = static::getProvider($input['provider']);
|
||||
|
||||
try {
|
||||
$provider->connect($input);
|
||||
@ -33,7 +28,7 @@ public function create(User $user, array $input): ServerProvider
|
||||
]);
|
||||
}
|
||||
|
||||
$serverProvider = new ServerProvider();
|
||||
$serverProvider = new ServerProvider;
|
||||
$serverProvider->user_id = $user->id;
|
||||
$serverProvider->profile = $input['name'];
|
||||
$serverProvider->provider = $input['provider'];
|
||||
@ -44,19 +39,16 @@ public function create(User $user, array $input): ServerProvider
|
||||
return $serverProvider;
|
||||
}
|
||||
|
||||
private function getProvider($name): ServerProviderContract
|
||||
private static function getProvider($name): ServerProviderContract
|
||||
{
|
||||
$providerClass = config('core.server_providers_class.'.$name);
|
||||
|
||||
return new $providerClass();
|
||||
return new $providerClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
private function validateInput(array $input): void
|
||||
public static function rules(): array
|
||||
{
|
||||
Validator::make($input, [
|
||||
return [
|
||||
'name' => [
|
||||
'required',
|
||||
],
|
||||
@ -65,14 +57,11 @@ private function validateInput(array $input): void
|
||||
Rule::in(config('core.server_providers')),
|
||||
Rule::notIn('custom'),
|
||||
],
|
||||
])->validate();
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
private function validateProvider(ServerProviderContract $provider, array $input): void
|
||||
public static function providerRules(array $input): array
|
||||
{
|
||||
Validator::make($input, $provider->credentialValidationRules($input))->validate();
|
||||
return static::getProvider($input['provider'])->credentialValidationRules($input);
|
||||
}
|
||||
}
|
||||
|
@ -4,31 +4,23 @@
|
||||
|
||||
use App\Models\ServerProvider;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class EditServerProvider
|
||||
{
|
||||
public function edit(ServerProvider $serverProvider, User $user, array $input): void
|
||||
{
|
||||
$this->validate($input);
|
||||
|
||||
$serverProvider->profile = $input['name'];
|
||||
$serverProvider->project_id = isset($input['global']) && $input['global'] ? null : $user->current_project_id;
|
||||
|
||||
$serverProvider->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
private function validate(array $input): void
|
||||
public static function rules(): array
|
||||
{
|
||||
$rules = [
|
||||
return [
|
||||
'name' => [
|
||||
'required',
|
||||
],
|
||||
];
|
||||
Validator::make($input, $rules)->validate();
|
||||
}
|
||||
}
|
||||
|
@ -101,22 +101,26 @@ public function create(Server $server, array $input): Site
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
private function validateInputs(Server $server, array $input): void
|
||||
public static function rules(array $input): void
|
||||
{
|
||||
$rules = [
|
||||
'server_id' => [
|
||||
'required',
|
||||
'exists:servers,id',
|
||||
],
|
||||
'type' => [
|
||||
'required',
|
||||
Rule::in(config('core.site_types')),
|
||||
],
|
||||
'domain' => [
|
||||
'required',
|
||||
new DomainRule(),
|
||||
new DomainRule,
|
||||
Rule::unique('sites', 'domain')->where(function ($query) use ($server) {
|
||||
return $query->where('server_id', $server->id);
|
||||
}),
|
||||
],
|
||||
'aliases.*' => [
|
||||
new DomainRule(),
|
||||
new DomainRule,
|
||||
],
|
||||
];
|
||||
|
||||
|
@ -27,7 +27,12 @@ public function create(array $input): User
|
||||
|
||||
private function validate(array $input): void
|
||||
{
|
||||
Validator::make($input, [
|
||||
Validator::make($input, self::rules())->validate();
|
||||
}
|
||||
|
||||
public static function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'required|string|max:255',
|
||||
'email' => 'required|email|unique:users,email',
|
||||
'password' => 'required|string|min:8',
|
||||
@ -35,6 +40,6 @@ private function validate(array $input): void
|
||||
'required',
|
||||
Rule::in([UserRole::ADMIN, UserRole::USER]),
|
||||
],
|
||||
])->validate();
|
||||
];
|
||||
}
|
||||
}
|
||||
|
43
app/Actions/User/UpdateProjects.php
Normal file
43
app/Actions/User/UpdateProjects.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\User;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\User;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UpdateProjects
|
||||
{
|
||||
public function update(User $user, array $input): void
|
||||
{
|
||||
$this->validate($input);
|
||||
$user->projects()->sync($input['projects']);
|
||||
|
||||
if ($user->currentProject && !$user->projects->contains($user->currentProject)) {
|
||||
$user->current_project_id = null;
|
||||
$user->save();
|
||||
}
|
||||
|
||||
/** @var Project $firstProject */
|
||||
$firstProject = $user->projects->first();
|
||||
if (!$user->currentProject && $firstProject) {
|
||||
$user->current_project_id = $firstProject->id;
|
||||
$user->save();
|
||||
}
|
||||
}
|
||||
|
||||
private function validate(array $input): void
|
||||
{
|
||||
validator($input, self::rules())->validate();
|
||||
}
|
||||
|
||||
public static function rules(): array
|
||||
{
|
||||
return [
|
||||
'projects.*' => [
|
||||
'required',
|
||||
Rule::exists('projects', 'id'),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@
|
||||
|
||||
class UpdateUser
|
||||
{
|
||||
public function update(User $user, array $input): void
|
||||
public function update(User $user, array $input): User
|
||||
{
|
||||
$this->validate($user, $input);
|
||||
|
||||
@ -18,18 +18,29 @@ public function update(User $user, array $input): void
|
||||
$user->timezone = $input['timezone'];
|
||||
$user->role = $input['role'];
|
||||
|
||||
if (isset($input['password']) && $input['password'] !== null) {
|
||||
if (isset($input['password'])) {
|
||||
$user->password = bcrypt($input['password']);
|
||||
}
|
||||
|
||||
$user->save();
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
private function validate(User $user, array $input): void
|
||||
{
|
||||
Validator::make($input, [
|
||||
Validator::make($input, self::rules($user))->validate();
|
||||
}
|
||||
|
||||
public static function rules(User $user): array
|
||||
{
|
||||
return [
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'email' => ['required', 'email', 'max:255', Rule::unique('users')->ignore($user->id)],
|
||||
'email' => [
|
||||
'required',
|
||||
'email', 'max:255',
|
||||
Rule::unique('users', 'email')->ignore($user->id)
|
||||
],
|
||||
'timezone' => [
|
||||
'required',
|
||||
Rule::in(timezone_identifiers_list()),
|
||||
@ -37,12 +48,7 @@ private function validate(User $user, array $input): void
|
||||
'role' => [
|
||||
'required',
|
||||
Rule::in([UserRole::ADMIN, UserRole::USER]),
|
||||
function ($attribute, $value, $fail) use ($user) {
|
||||
if ($user->is(auth()->user()) && $value !== $user->role) {
|
||||
$fail('You cannot change your own role');
|
||||
}
|
||||
},
|
||||
],
|
||||
])->validate();
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -3,23 +3,22 @@
|
||||
namespace App\Actions\User;
|
||||
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class UpdateUserPassword
|
||||
{
|
||||
/**
|
||||
* Validate and update the user's password.
|
||||
*/
|
||||
public function update($user, array $input): void
|
||||
{
|
||||
Validator::make($input, [
|
||||
'current_password' => ['required', 'string', 'current-password'],
|
||||
'password' => ['required', 'string'],
|
||||
'password_confirmation' => ['required', 'same:password'],
|
||||
])->validate();
|
||||
|
||||
$user->forceFill([
|
||||
'password' => Hash::make($input['password']),
|
||||
])->save();
|
||||
}
|
||||
|
||||
public static function rules(): array
|
||||
{
|
||||
return [
|
||||
'current_password' => ['required', 'string', 'current-password'],
|
||||
'password' => ['required', 'string'],
|
||||
'password_confirmation' => ['required', 'same:password'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -3,28 +3,12 @@
|
||||
namespace App\Actions\User;
|
||||
|
||||
use App\Models\User;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UpdateUserProfileInformation
|
||||
{
|
||||
/**
|
||||
* Validate and update the given user's profile information.
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function update(User $user, array $input): void
|
||||
{
|
||||
Validator::make($input, [
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'email' => ['required', 'email', 'max:255', Rule::unique('users')->ignore($user->id)],
|
||||
'timezone' => [
|
||||
'required',
|
||||
Rule::in(timezone_identifiers_list()),
|
||||
],
|
||||
])->validateWithBag('updateProfileInformation');
|
||||
|
||||
if ($input['email'] !== $user->email) {
|
||||
$this->updateVerifiedUser($user, $input);
|
||||
} else {
|
||||
@ -36,6 +20,18 @@ public function update(User $user, array $input): void
|
||||
}
|
||||
}
|
||||
|
||||
public static function rules(User $user): array
|
||||
{
|
||||
return [
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'email' => ['required', 'email', 'max:255', Rule::unique('users')->ignore($user->id)],
|
||||
'timezone' => [
|
||||
'required',
|
||||
Rule::in(timezone_identifiers_list()),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the given verified user's profile information.
|
||||
*/
|
||||
|
Reference in New Issue
Block a user