Skip to content
Snippets Groups Projects
Commit 1c8af755 authored by Yassine Doghri's avatar Yassine Doghri
Browse files

fix(users): remove required roles input when editing user + prevent owner's roles from being edited

fixes #239
parent 7512e2ed
No related branches found
No related tags found
No related merge requests found
...@@ -143,6 +143,17 @@ class UserController extends BaseController ...@@ -143,6 +143,17 @@ class UserController extends BaseController
$authorize = Services::authorization(); $authorize = Services::authorization();
$roles = $this->request->getPost('roles'); $roles = $this->request->getPost('roles');
if ($this->user->isOwner) {
return redirect()
->back()
->with('errors', [
lang('User.messages.editOwnerError', [
'username' => $this->user->username,
]),
]);
}
$authorize->setUserGroups($this->user->id, $roles ?? []); $authorize->setUserGroups($this->user->id, $roles ?? []);
// Success! // Success!
......
...@@ -45,6 +45,8 @@ return [ ...@@ -45,6 +45,8 @@ return [
'{username} will be prompted with a password reset upon next visit.', '{username} will be prompted with a password reset upon next visit.',
'banSuccess' => '{username} has been banned.', 'banSuccess' => '{username} has been banned.',
'unbanSuccess' => '{username} has been unbanned.', 'unbanSuccess' => '{username} has been unbanned.',
'editOwnerError' =>
'{username} is the instance owner, you cannot edit its roles.',
'banSuperAdminError' => 'banSuperAdminError' =>
'{username} is a superadmin, one does not simply ban a superadmin…', '{username} is a superadmin, one does not simply ban a superadmin…',
'deleteSuperAdminError' => 'deleteSuperAdminError' =>
......
...@@ -13,6 +13,7 @@ namespace Modules\Auth\Entities; ...@@ -13,6 +13,7 @@ namespace Modules\Auth\Entities;
use App\Entities\Podcast; use App\Entities\Podcast;
use App\Models\NotificationModel; use App\Models\NotificationModel;
use App\Models\PodcastModel; use App\Models\PodcastModel;
use App\Models\UserModel;
use Myth\Auth\Entities\User as MythAuthUser; use Myth\Auth\Entities\User as MythAuthUser;
use RuntimeException; use RuntimeException;
...@@ -31,6 +32,8 @@ use RuntimeException; ...@@ -31,6 +32,8 @@ use RuntimeException;
*/ */
class User extends MythAuthUser class User extends MythAuthUser
{ {
public bool $is_owner;
/** /**
* @var Podcast[]|null * @var Podcast[]|null
*/ */
...@@ -54,6 +57,17 @@ class User extends MythAuthUser ...@@ -54,6 +57,17 @@ class User extends MythAuthUser
'podcast_role' => '?string', 'podcast_role' => '?string',
]; ];
public function getIsOwner(): bool
{
$firstUser = (new UserModel())->first();
if (! $firstUser instanceof self) {
return false;
}
return $this->username === $firstUser->username;
}
/** /**
* Returns the podcasts the user is contributing to * Returns the podcasts the user is contributing to
* *
......
...@@ -23,7 +23,6 @@ ...@@ -23,7 +23,6 @@
id="roles" id="roles"
name="roles[]" name="roles[]"
label="<?= lang('User.form.roles') ?>" label="<?= lang('User.form.roles') ?>"
required="true"
options="<?= esc(json_encode($roleOptions)) ?>" options="<?= esc(json_encode($roleOptions)) ?>"
selected="<?= esc(json_encode($user->roles)) ?>" /> selected="<?= esc(json_encode($user->roles)) ?>" />
......
...@@ -30,10 +30,13 @@ ...@@ -30,10 +30,13 @@
[ [
'header' => lang('User.list.roles'), 'header' => lang('User.list.roles'),
'cell' => function ($user) { 'cell' => function ($user) {
return implode(',', $user->roles) . if ($user->isOwner) {
'<IconButton uri="' . route_to('user-edit', $user->id) . '" glyph="edit" variant="info">' . lang('User.edit_roles', [ return 'owner, ' . implode(',', $user->roles);
'username' => esc($user->username), }
]) . '</IconButton>';
return implode(',', $user->roles) . '<IconButton uri="' . route_to('user-edit', $user->id) . '" glyph="edit" variant="info">' . lang('User.edit_roles', [
'username' => esc($user->username),
]) . '</IconButton>';
}, },
], ],
[ [
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment