2023-01-21 06:08:34 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Helpers;
|
|
|
|
|
2023-05-17 12:56:55 -04:00
|
|
|
use BookStack\Permissions\PermissionsRepo;
|
|
|
|
use BookStack\Users\Models\Role;
|
|
|
|
use BookStack\Users\Models\User;
|
2023-01-21 06:08:34 -05:00
|
|
|
|
|
|
|
class UserRoleProvider
|
|
|
|
{
|
|
|
|
protected ?User $admin = null;
|
|
|
|
protected ?User $editor = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a typical "Admin" user.
|
|
|
|
*/
|
|
|
|
public function admin(): User
|
|
|
|
{
|
|
|
|
if (is_null($this->admin)) {
|
|
|
|
$adminRole = Role::getSystemRole('admin');
|
2023-02-26 05:50:14 -05:00
|
|
|
$this->admin = $adminRole->users()->first();
|
2023-01-21 06:08:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->admin;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a typical "Editor" user.
|
|
|
|
*/
|
|
|
|
public function editor(): User
|
|
|
|
{
|
|
|
|
if ($this->editor === null) {
|
|
|
|
$editorRole = Role::getRole('editor');
|
|
|
|
$this->editor = $editorRole->users->first();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->editor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a typical "Viewer" user.
|
|
|
|
*/
|
|
|
|
public function viewer(array $attributes = []): User
|
|
|
|
{
|
|
|
|
$user = Role::getRole('viewer')->users()->first();
|
|
|
|
if (!empty($attributes)) {
|
|
|
|
$user->forceFill($attributes)->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2023-08-16 11:02:00 -04:00
|
|
|
/**
|
|
|
|
* Get the system "guest" user.
|
|
|
|
*/
|
|
|
|
public function guest(): User
|
|
|
|
{
|
2023-08-17 09:59:28 -04:00
|
|
|
return User::getDefault();
|
2023-08-16 11:02:00 -04:00
|
|
|
}
|
|
|
|
|
2023-01-21 06:08:34 -05:00
|
|
|
/**
|
|
|
|
* Create a new fresh user without any relations.
|
|
|
|
*/
|
|
|
|
public function newUser(array $attrs = []): User
|
|
|
|
{
|
|
|
|
return User::factory()->create($attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new fresh user, with the given attrs, that has assigned a fresh role
|
|
|
|
* that has the given role permissions.
|
|
|
|
* Intended as a helper to create a blank slate baseline user and role.
|
|
|
|
* @return array{0: User, 1: Role}
|
|
|
|
*/
|
|
|
|
public function newUserWithRole(array $userAttrs = [], array $rolePermissions = []): array
|
|
|
|
{
|
|
|
|
$user = $this->newUser($userAttrs);
|
|
|
|
$role = $this->attachNewRole($user, $rolePermissions);
|
|
|
|
|
|
|
|
return [$user, $role];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attach a new role, with the given role permissions, to the given user
|
|
|
|
* and return that role.
|
|
|
|
*/
|
|
|
|
public function attachNewRole(User $user, array $rolePermissions = []): Role
|
|
|
|
{
|
|
|
|
$role = $this->createRole($rolePermissions);
|
|
|
|
$user->attachRole($role);
|
|
|
|
return $role;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new basic role with the given role permissions.
|
|
|
|
*/
|
|
|
|
public function createRole(array $rolePermissions = []): Role
|
|
|
|
{
|
|
|
|
$permissionRepo = app(PermissionsRepo::class);
|
|
|
|
$roleData = Role::factory()->make()->toArray();
|
2023-02-18 13:36:34 -05:00
|
|
|
$roleData['permissions'] = $rolePermissions;
|
2023-01-21 06:08:34 -05:00
|
|
|
|
|
|
|
return $permissionRepo->saveNewRole($roleData);
|
|
|
|
}
|
|
|
|
}
|