Files
panel-pelican-dev/app/Services/Subusers/SubuserCreationService.php

79 lines
2.6 KiB
PHP
Raw Permalink Normal View History

2017-08-23 21:34:11 -05:00
<?php
2024-03-12 22:39:16 -04:00
namespace App\Services\Subusers;
2017-08-23 21:34:11 -05:00
use App\Enums\SubuserPermission;
use App\Events\Server\SubUserAdded;
2025-09-24 13:34:19 +02:00
use App\Exceptions\Model\DataValidationException;
use App\Exceptions\Service\Subuser\ServerSubuserExistsException;
use App\Exceptions\Service\Subuser\UserIsServerOwnerException;
2024-03-12 22:39:16 -04:00
use App\Models\Server;
use App\Models\Subuser;
2025-09-24 13:34:19 +02:00
use App\Models\User;
2024-03-12 22:39:16 -04:00
use App\Services\Users\UserCreationService;
2025-09-24 13:34:19 +02:00
use Illuminate\Database\ConnectionInterface;
use Throwable;
2017-08-23 21:34:11 -05:00
class SubuserCreationService
{
2017-09-04 18:12:13 -05:00
/**
* SubuserCreationService constructor.
*/
2017-08-23 21:34:11 -05:00
public function __construct(
private ConnectionInterface $connection,
private UserCreationService $userCreationService,
) {}
2017-08-23 21:34:11 -05:00
/**
* Creates a new user on the system and assigns them access to the provided server.
* If the email address already belongs to a user on the system a new user will not
* be created.
*
* @param string[] $permissions
*
* @throws DataValidationException
* @throws ServerSubuserExistsException
* @throws UserIsServerOwnerException
* @throws Throwable
2017-08-23 21:34:11 -05:00
*/
public function handle(Server $server, string $email, array $permissions): Subuser
2017-08-23 21:34:11 -05:00
{
return $this->connection->transaction(function () use ($server, $email, $permissions) {
$user = User::withoutGlobalScopes()->where('email', $email)->first();
2024-03-16 19:10:31 -04:00
if (!$user) {
$user = $this->userCreationService->handle([
'email' => $email,
'root_admin' => false,
]);
} else {
if ($server->owner_id === $user->id) {
throw new UserIsServerOwnerException(trans('exceptions.subusers.user_is_owner'));
}
2024-03-16 19:10:31 -04:00
$subuserCount = $server->subusers()->where('user_id', $user->id)->count();
if ($subuserCount !== 0) {
throw new ServerSubuserExistsException(trans('exceptions.subusers.subuser_exists'));
}
2024-03-16 19:10:31 -04:00
}
$cleanedPermissions = collect($permissions)
->unique()
->filter(fn ($permission) => $permission === SubuserPermission::WebsocketConnect->value || user()?->can($permission, $server))
->sort()
->values()
->all();
$subuser = Subuser::withoutGlobalScopes()->updateOrCreate([
'user_id' => $user->id,
'server_id' => $server->id,
], [
'permissions' => $cleanedPermissions,
2017-09-04 18:12:13 -05:00
]);
Implement Webhooks (#548) * feat: First Webhook PoC draft * feat: Dispatch Webhooks PoC * fix: typo in webhook configuration scope * Update 2024_04_21_162552_create_webhooks_table.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * Update 2024_04_21_162552_create_webhooks_table.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * Update 2024_04_21_162544_create_webhook_configurations_table.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * Update 2024_04_21_162544_create_webhook_configurations_table.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * Update DispatchWebhooks.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * Update DispatchWebhooksJob.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * Update DispatchWebhookForConfiguration.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * Update DispatchWebhookForConfiguration.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * Update DispatchWebhookForConfiguration.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * Update DispatchWebhooksJob.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * Update DispatchWebhooksJob.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * Update DispatchWebhooksJob.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * chore: Implement Webhook Event Discovery * we got a test working for webhooks * WIP * Something is working! * More tests * clean up the tests now that they are passing * WIP * Don't use model specific events * WIP * WIP * WIP * WIP * WIP * Do it sync * Reset these * Don't need restored event type * Deleted some unused jobs * Find custom Events * Remove observers * Add custom event test * Run Pint * Add caching * Don't cache every single event * Fix tests * Run Pint * Phpstan fixes * Pint fix * Test fixes * Middleware unit test fix * Pint fixes * Remove index not working for older dbs * Use facade instead --------- Co-authored-by: Pascale Beier <mail@pascalebeier.de> Co-authored-by: Lance Pioch <lancepioch@gmail.com> Co-authored-by: Vehikl <go@vehikl.com>
2024-10-26 20:35:25 -04:00
event(new SubUserAdded($subuser));
Implement Webhooks (#548) * feat: First Webhook PoC draft * feat: Dispatch Webhooks PoC * fix: typo in webhook configuration scope * Update 2024_04_21_162552_create_webhooks_table.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * Update 2024_04_21_162552_create_webhooks_table.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * Update 2024_04_21_162544_create_webhook_configurations_table.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * Update 2024_04_21_162544_create_webhook_configurations_table.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * Update DispatchWebhooks.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * Update DispatchWebhooksJob.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * Update DispatchWebhookForConfiguration.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * Update DispatchWebhookForConfiguration.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * Update DispatchWebhookForConfiguration.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * Update DispatchWebhooksJob.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * Update DispatchWebhooksJob.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * Update DispatchWebhooksJob.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * chore: Implement Webhook Event Discovery * we got a test working for webhooks * WIP * Something is working! * More tests * clean up the tests now that they are passing * WIP * Don't use model specific events * WIP * WIP * WIP * WIP * WIP * Do it sync * Reset these * Don't need restored event type * Deleted some unused jobs * Find custom Events * Remove observers * Add custom event test * Run Pint * Add caching * Don't cache every single event * Fix tests * Run Pint * Phpstan fixes * Pint fix * Test fixes * Middleware unit test fix * Pint fixes * Remove index not working for older dbs * Use facade instead --------- Co-authored-by: Pascale Beier <mail@pascalebeier.de> Co-authored-by: Lance Pioch <lancepioch@gmail.com> Co-authored-by: Vehikl <go@vehikl.com>
2024-10-26 20:35:25 -04:00
return $subuser;
});
2017-08-23 21:34:11 -05:00
}
}