Skip to content

Commit

Permalink
small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy132 committed Aug 2, 2024
1 parent ebd63d8 commit 409c90a
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __invoke(ClientApiRequest $request, Server $server): array
// We could do this with a query and a lot of joins, but that gets pretty
// painful so for now we'll execute a simpler query.
$subusers = $server->subusers()->pluck('user_id')->merge([$server->owner_id]);
$rootAdmins = Role::findOrCreate(Role::ROOT_ADMIN)->users()->pluck('id');
$rootAdmins = Role::getRootAdmin()->users()->pluck('id');

$builder->select('activity_logs.*')
->leftJoin('users', function (JoinClause $join) {
Expand Down
5 changes: 5 additions & 0 deletions app/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ public function isRootAdmin(): bool
{
return $this->name === self::ROOT_ADMIN;
}

public static function getRootAdmin(): self
{
return self::findOrCreate(self::ROOT_ADMIN);

Check failure on line 29 in app/Models/Role.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method App\Models\Role::getRootAdmin() should return App\Models\Role but returns Spatie\Permission\Contracts\Role.

Check failure on line 29 in app/Models/Role.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method App\Models\Role::getRootAdmin() should return App\Models\Role but returns Spatie\Permission\Contracts\Role.

Check failure on line 29 in app/Models/Role.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method App\Models\Role::getRootAdmin() should return App\Models\Role but returns Spatie\Permission\Contracts\Role.
}
}
2 changes: 1 addition & 1 deletion app/Services/Users/UserCreationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function handle(array $data): User
]));

if (array_key_exists('root_admin', $data) && $data['root_admin']) {
$user->syncRoles(Role::findOrCreate(Role::ROOT_ADMIN));
$user->syncRoles(Role::getRootAdmin());
}

if (isset($generateResetToken)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function up(): void
{
$adminUsers = User::whereRootAdmin(1)->get();
foreach ($adminUsers as $adminUser) {
$adminUser->syncRoles(Role::findOrCreate(Role::ROOT_ADMIN));
$adminUser->syncRoles(Role::getRootAdmin());
}

Schema::table('users', function (Blueprint $table) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected function createNewDefaultApiKey(User $user, array $permissions = []):
protected function createApiUser(): User
{
$user = User::factory()->create();
$user->syncRoles(Role::findOrCreate(Role::ROOT_ADMIN));
$user->syncRoles(Role::getRootAdmin());

return $user;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Integration/Api/Client/ClientControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testServersAreFilteredUsingNameAndUuidInformation(): void
{
/** @var \App\Models\User[] $users */
$users = User::factory()->times(2)->create();
$users[0]->syncRoles(Role::findOrCreate(Role::ROOT_ADMIN));
$users[0]->syncRoles(Role::getRootAdmin());

/** @var \App\Models\Server[] $servers */
$servers = [
Expand Down Expand Up @@ -226,7 +226,7 @@ public function testOnlyAdminLevelServersAreReturned(): void
{
/** @var \App\Models\User[] $users */
$users = User::factory()->times(4)->create();
$users[0]->syncRoles(Role::findOrCreate(Role::ROOT_ADMIN));
$users[0]->syncRoles(Role::getRootAdmin());

$servers = [
$this->createServerModel(['user_id' => $users[0]->id]),
Expand Down Expand Up @@ -261,7 +261,7 @@ public function testAllServersAreReturnedToAdmin(): void
{
/** @var \App\Models\User[] $users */
$users = User::factory()->times(4)->create();
$users[0]->syncRoles(Role::findOrCreate(Role::ROOT_ADMIN));
$users[0]->syncRoles(Role::getRootAdmin());

$servers = [
$this->createServerModel(['user_id' => $users[0]->id]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public function testUserPermissionsAreReturnedCorrectly(): void
->assertOk()
->assertJsonPath('permissions', [Permission::ACTION_FILE_READ, Permission::ACTION_FILE_SFTP]);

$user->syncRoles(Role::findOrCreate(Role::ROOT_ADMIN));
$user->syncRoles(Role::getRootAdmin());

$this->postJson('/api/remote/sftp/auth', $data)
->assertOk()
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Http/Middleware/AdminAuthenticateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AdminAuthenticateTest extends MiddlewareTestCase
public function testAdminsAreAuthenticated(): void
{
$user = User::factory()->create();
$user->syncRoles(Role::findOrCreate(Role::ROOT_ADMIN));
$user->syncRoles(Role::getRootAdmin());

$this->request->shouldReceive('user')->withNoArgs()->twice()->andReturn($user);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function testNonAdminUser(): void
public function testAdminUser(): void
{
$user = $this->generateRequestUserModel();
$user->syncRoles(Role::findOrCreate(Role::ROOT_ADMIN));
$user->syncRoles(Role::getRootAdmin());

$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
}
Expand Down

0 comments on commit 409c90a

Please sign in to comment.