Skip to content

Commit

Permalink
Luna user roles field
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed Dec 16, 2023
1 parent af48353 commit 249663d
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 69 deletions.
15 changes: 15 additions & 0 deletions src/Access/AccessService.php
Original file line number Diff line number Diff line change
Expand Up @@ -777,4 +777,19 @@ public function getBasicRoles(): array

return array_map([$this, 'wrapUserRole'], $roles);
}

/**
* @return array<string|\BackedEnum>
*/
public function getSelectableRoles(): array
{
return $this->app->config('access.selectable_roles') ?: [];
}

public function canSelectUserRoles(): bool
{
$roles = $this->getSelectableRoles();

return $roles !== [] && $this->check(static::ROLE_MODIFY_ACTION);
}
}
53 changes: 53 additions & 0 deletions src/Field/UserRoleListField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Lyrasoft\Luna\Field;

use Lyrasoft\Luna\Access\AccessService;
use Windwalker\DI\Attributes\Inject;
use Windwalker\Form\Field\ListField;
use Windwalker\Utilities\Enum\EnumTranslatableInterface;

use function Windwalker\value;

class UserRoleListField extends ListField
{
#[Inject]
protected AccessService $accessService;

protected function prepareOptions(): array
{
$roles = $this->accessService->getSelectableRoles();

$options = [];

$iAmSuperUser = $this->accessService->isSuperUser();

foreach ($roles as $value => $text) {
if ($text instanceof EnumTranslatableInterface || is_numeric($value)) {
$value = value($text);
$text = $this->accessService->wrapUserRole($value)?->getTitle() ?? $value;
}

if (!$iAmSuperUser && $this->accessService->isSuperUserRole($value)) {
continue;
}

$options[] = static::createOption($text, (string) $value);
}

return $options;
}

/**
* @return array
*/
protected function getAccessors(): array
{
return array_merge(
parent::getAccessors(),
[]
);
}
}
86 changes: 43 additions & 43 deletions src/LunaPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,49 @@ public function __construct(public ApplicationInterface $app)
{
}

public function getLoginName(): string
{
return $this->app->config('user.login_name') ?? 'username';
}

public function isAdmin(): bool
{
if ($this->app instanceof WebApplicationInterface) {
return $this->app->service(AppRequest::class)->getMatchedRoute()?->getExtraValue('namespace') === 'admin';
}

return false;
}

public function isFront(): bool
{
if ($this->app instanceof WebApplicationInterface) {
return $this->app->service(AppRequest::class)->getMatchedRoute()?->getExtraValue('namespace') === 'front';
}

return false;
}

public function bootBeforeRequest(Container $container): void
{
// Error
if (!$this->app->isDebug() && $this->app->getClient() === AppClient::WEB) {
$errorService = $container->get(ErrorService::class);

$errorService->addHandler(
$container->newInstance(
LunaErrorHandler::class,
[
'layout' => $this->app->config('luna.error.layout') ?? 'error',
'route' => $this->app->config('luna.error.route') ?? 'front::home',
]
),
'default'
);
$errorService->register();
}
}

public function register(Container $container): void
{
$container->share(static::class, $this);
Expand Down Expand Up @@ -130,49 +173,6 @@ public function register(Container $container): void
);
}

public function getLoginName(): string
{
return $this->app->config('user.login_name') ?? 'username';
}

public function isAdmin(): bool
{
if ($this->app instanceof WebApplicationInterface) {
return $this->app->service(AppRequest::class)->getMatchedRoute()?->getExtraValue('namespace') === 'admin';
}

return false;
}

public function isFront(): bool
{
if ($this->app instanceof WebApplicationInterface) {
return $this->app->service(AppRequest::class)->getMatchedRoute()?->getExtraValue('namespace') === 'front';
}

return false;
}

public function bootBeforeRequest(Container $container): void
{
// Error
if (!$this->app->isDebug() && $this->app->getClient() === AppClient::WEB) {
$errorService = $container->get(ErrorService::class);

$errorService->addHandler(
$container->newInstance(
LunaErrorHandler::class,
[
'layout' => $this->app->config('luna.error.layout') ?? 'error',
'route' => $this->app->config('luna.error.route') ?? 'front::home',
]
),
'default'
);
$errorService->register();
}
}

protected function registerFaker(Container $container)
{
if ($container->has(FakerService::class)) {
Expand Down
29 changes: 3 additions & 26 deletions src/Module/Admin/User/Form/EditForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Lyrasoft\Luna\Access\AccessService;
use Lyrasoft\Luna\Auth\SRP\SRPService;
use Lyrasoft\Luna\Field\UserRoleListField;
use Lyrasoft\Luna\LunaPackage;
use Unicorn\Field\CalendarField;
use Unicorn\Field\SingleImageDragField;
Expand All @@ -14,14 +15,10 @@
use Windwalker\Filter\Rule\EmailAddress;
use Windwalker\Form\Field\EmailField;
use Windwalker\Form\Field\HiddenField;
use Windwalker\Form\Field\ListField;
use Windwalker\Form\Field\PasswordField;
use Windwalker\Form\Field\TextField;
use Windwalker\Form\FieldDefinitionInterface;
use Windwalker\Form\Form;
use Windwalker\Utilities\Enum\EnumTranslatableInterface;

use function Windwalker\value;

/**
* The EditForm class.
Expand Down Expand Up @@ -105,29 +102,9 @@ function (Form $form) {
->width(400)
->height(400);

$roles = $this->luna->app->config('access.selectable_roles') ?: [];

if ($roles && $this->accessService->check(AccessService::ROLE_MODIFY_ACTION)) {
$iAmSuperUser = $this->accessService->isSuperUser();

$form->add('roles', ListField::class)
if ($this->accessService->canSelectUserRoles()) {
$form->add('roles', UserRoleListField::class)
->label($this->trans('luna.user.field.roles'))
// ->required(true)
->registerOptions(
$roles,
function (ListField $field, mixed $text, mixed $value) use ($iAmSuperUser) {
if ($text instanceof EnumTranslatableInterface || is_numeric($value)) {
$value = value($text);
$text = $this->accessService->wrapUserRole($value)?->getTitle() ?? $value;
}

if (!$iAmSuperUser && $this->accessService->isSuperUserRole($value)) {
return;
}

$field->option($text, (string) $value);
}
)
->multiple(true)
->addClass('has-tom-select');
}
Expand Down

0 comments on commit 249663d

Please sign in to comment.