Skip to content

Commit 7d62587

Browse files
committed
Simplify FormController context identification logic
1 parent 3532a05 commit 7d62587

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

modules/backend/behaviors/FormController.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,7 @@ public function __construct($controller)
122122
*/
123123
public function initForm($model, $context = null)
124124
{
125-
if ($context !== null) {
126-
$this->context = $context;
127-
}
128-
129-
$context = $this->formGetContext();
125+
$context = $this->context = $context ?? $this->formGetContext();
130126

131127
/*
132128
* Each page can supply a unique form definition, if desired
@@ -432,15 +428,13 @@ public function formGetModel()
432428
}
433429

434430
/**
435-
* Returns the active form context, either obtained from the postback
436-
* variable called `form_context` or detected from the configuration,
437-
* or routing parameters.
431+
* Returns the active form context detected from the configuration or routing parameters.
438432
*
439433
* @return string
440434
*/
441435
public function formGetContext()
442436
{
443-
return post('form_context', $this->context);
437+
return $this->context;
444438
}
445439

446440
/**

modules/backend/lang/en/lang.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@
160160
'updated_at' => 'Updated at',
161161
'deleted_at' => 'Deleted at',
162162
'show_deleted' => 'Show deleted',
163+
'self_escalation_denied' => 'You cannot modify your own role, permissions, or superuser status.',
164+
'superuser_grant_denied' => 'Only superusers can grant superuser status or modify other superuser accounts.',
165+
'manage_users_denied' => 'You do not have permission to manage other administrators.',
163166
'group' => [
164167
'name' => 'Group',
165168
'name_field' => 'Name',

modules/backend/models/User.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
use Mail;
44
use Event;
55
use Backend;
6-
use BackendAuth;
6+
use Backend\Facades\BackendAuth;
7+
use Illuminate\Support\Facades\Lang;
78
use Winter\Storm\Auth\Models\User as UserBase;
9+
use Winter\Storm\Exception\ApplicationException;
810

911
/**
1012
* Administrator user model
@@ -117,6 +119,36 @@ public function getAvatarThumb($size = 25, $options = null)
117119
'&d='. urlencode($default);
118120
}
119121

122+
/**
123+
* Before save event — enforce authorization rules to prevent privilege escalation.
124+
* @return void
125+
*/
126+
public function beforeSave()
127+
{
128+
$actor = BackendAuth::getUser();
129+
$isCurrentUser = $this->exists && $actor && $actor->getKey() === $this->getKey();
130+
131+
// No authenticated user (CLI, artisan, queue, seeders) — allow everything
132+
if (!$actor) {
133+
return;
134+
}
135+
136+
// Rule 1: Self-escalation — users cannot modify their own role, superuser status, or permissions
137+
if ($isCurrentUser && $this->isDirty(['role_id', 'is_superuser', 'permissions'])) {
138+
throw new ApplicationException(Lang::get('backend::lang.user.self_escalation_denied'));
139+
}
140+
141+
// Rule 2: Must have backend.manage_users to manage other users
142+
if (!$isCurrentUser && !$actor->hasAccess('backend.manage_users')) {
143+
throw new ApplicationException(Lang::get('backend::lang.user.manage_users_denied'));
144+
}
145+
146+
// Rule 3: Only superusers can grant superuser status or edit existing superusers
147+
if (!$actor->isSuperUser() && ($this->is_superuser || $this->getOriginal('is_superuser'))) {
148+
throw new ApplicationException(Lang::get('backend::lang.user.superuser_grant_denied'));
149+
}
150+
}
151+
120152
/**
121153
* After create event
122154
* @return void

0 commit comments

Comments
 (0)