-
-
Notifications
You must be signed in to change notification settings - Fork 578
[6.x] Elevated Sessions #11688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[6.x] Elevated Sessions #11688
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
480f357
Elevated Sessions
duncanmcclean 5752f1c
Require elevated session before changing another user's password
duncanmcclean d2bb314
Add ability for actions to require elevated sessions
duncanmcclean 6f050e2
Require elevated session before copying password reset link
duncanmcclean 094a928
Require elevated session when updating permissions on a role
duncanmcclean 7912a02
wip
duncanmcclean dc08871
Improve how we enforce elevated sessions on the backend
duncanmcclean d351866
Update tests
duncanmcclean 9d2f48c
Include user ID in session key
duncanmcclean 552556c
Rename session key
duncanmcclean b4be28d
Pint
duncanmcclean 6d8ecad
Wordsmithing.
duncanmcclean d501c9b
Merge remote-tracking branch 'origin/master' into elevated-sessions
duncanmcclean de6fcda
Wire up elevated session modal
duncanmcclean 8b33826
Catch promise returned by `this.requireElevatedSession`
duncanmcclean 526d61c
Merge remote-tracking branch 'origin/master' into elevated-sessions
duncanmcclean 2cb53ba
Prettier
duncanmcclean 3cdd919
Make sure we're dealing with a Statamic user instance
duncanmcclean df56f84
nitpick
jasonvarga 468298d
Don't require passing request in
jasonvarga 8b3ef5b
nitpick
jasonvarga 5608ddb
Only require elevated session if you're updating someone elses passwo…
jasonvarga ed62949
Use expiry instead of diff ...
jasonvarga 9b77e29
Use macros to clean up and colocate logic
jasonvarga 95a0c4b
Middleware tweaks and non-json path ...
jasonvarga b9eaf43
group tests so they can be run together
jasonvarga fbf7d21
Store the current timestamp in the session and calculate expiry when …
jasonvarga 91e5def
Including user ids in session key is not necessary.
jasonvarga 6f00df2
Nitpick
jasonvarga 31b59e3
Require elevated session for impersonation
jasonvarga 982926c
We now have a better dedicated exception. Test should be performing j…
jasonvarga 9ad7c76
Show toast if elevated session was not completed when saving role
jasonvarga 15aae20
Add a convenience method
jasonvarga 5b34928
Move away from mixins so its usable in composition api
jasonvarga 0642163
Expose
jasonvarga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import axios from 'axios'; | ||
|
||
export async function requireElevatedSession() { | ||
const response = await axios.get(cp_url('elevated-session')); | ||
|
||
if (response.data.elevated) return; | ||
|
||
return new Promise((resolve, reject) => { | ||
const component = Statamic.$components.append('elevated-session-modal', { | ||
props: {}, | ||
}); | ||
|
||
component.on('closed', (shouldResolve) => { | ||
shouldResolve ? resolve() : reject(); | ||
component.destroy(); | ||
}); | ||
}); | ||
} | ||
|
||
export async function requireElevatedSessionIf(condition) { | ||
return condition ? requireElevatedSession() : Promise.resolve(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<template> | ||
<modal name="elevated-session" height="auto" :width="500" @closed="modalClosed" v-slot="{ close }" click-to-close> | ||
<div class="-max-h-screen-px"> | ||
<div | ||
class="flex items-center justify-between rounded-t-lg border-b bg-gray-200 px-5 py-3 text-lg font-semibold dark:border-dark-900 dark:bg-dark-550" | ||
> | ||
{{ __('Confirm Your Password') }} | ||
</div> | ||
|
||
<div class="publish-fields p-2"> | ||
<div class="form-group w-full"> | ||
<label v-text="__('messages.elevated_session_enter_password')" /> | ||
<small class="help-block text-red-500" v-if="errors.password" v-text="errors.password[0]" /> | ||
<div class="flex items-center"> | ||
<input | ||
type="password" | ||
v-model="password" | ||
ref="password" | ||
class="input-text" | ||
tabindex="1" | ||
autofocus | ||
@keydown.enter.prevent="submit" | ||
/> | ||
<button @click="submit(close)" class="btn-primary ltr:ml-2 rtl:mr-2" v-text="__('Confirm')" /> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</modal> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
data() { | ||
return { | ||
password: null, | ||
errors: [], | ||
shouldResolve: false, | ||
}; | ||
}, | ||
|
||
methods: { | ||
submit(close) { | ||
this.$axios | ||
.post(cp_url('elevated-session'), { password: this.password }) | ||
.then((response) => { | ||
this.shouldResolve = true; | ||
close(); | ||
}) | ||
.catch((error) => { | ||
this.errors = error.response.data.errors; | ||
if (error.response.status === 422) { | ||
this.$refs.password.focus(); | ||
} | ||
}); | ||
}, | ||
|
||
modalClosed() { | ||
this.$emit('closed', this.shouldResolve); | ||
}, | ||
}, | ||
}; | ||
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
@php | ||
use function Statamic\trans as __; | ||
@endphp | ||
|
||
@extends('statamic::outside') | ||
|
||
@section('content') | ||
@include('statamic::partials.outside-logo') | ||
|
||
<div class="relative mx-auto flex max-w-xs items-center justify-center rounded shadow-lg"> | ||
<div class="outside-shadow absolute inset-0"></div> | ||
<div class="card auth-card"> | ||
<div class="mb-4 pb-4 text-center"> | ||
<h1 class="mb-4 text-lg text-gray-800 dark:text-white/80">{{ __('Confirm Your Password') }}</h1> | ||
<p class="text-sm text-gray dark:text-dark-175"> | ||
{{ __('statamic::messages.elevated_session_enter_password') }} | ||
</p> | ||
</div> | ||
|
||
@if (session('status')) | ||
<div class="alert alert-success mb-6"> | ||
{{ session('status') }} | ||
</div> | ||
@endif | ||
|
||
<form method="POST" action="{{ cp_route('elevated-session.confirm') }}"> | ||
@csrf | ||
|
||
<div class="mb-8"> | ||
<label for="password" class="mb-2">{{ __('Password') }}</label> | ||
<input id="password" type="password" class="input-text" name="password" /> | ||
|
||
@error('password') | ||
<div class="mt-2 text-xs text-red-500">{{ $message }}</div> | ||
@enderror | ||
</div> | ||
|
||
<button type="submit" class="btn-primary"> | ||
{{ __('Submit') }} | ||
</button> | ||
</form> | ||
</div> | ||
</div> | ||
@endsection |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Statamic\Exceptions; | ||
|
||
use Illuminate\Http\Request; | ||
|
||
class ElevatedSessionAuthorizationException extends \Exception | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct(__('Requires an elevated session.')); | ||
} | ||
|
||
public function render(Request $request) | ||
{ | ||
return $request->wantsJson() | ||
? response()->json(['message' => $this->getMessage()], 403) | ||
: redirect()->setIntendedUrl($request->fullUrl())->to('/cp/auth/confirm-password'); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/Http/Controllers/CP/Auth/ElevatedSessionController.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Statamic\Http\Controllers\CP\Auth; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Hash; | ||
use Illuminate\Validation\ValidationException; | ||
use Statamic\Facades\User; | ||
|
||
class ElevatedSessionController | ||
{ | ||
public function status(Request $request) | ||
{ | ||
return [ | ||
'elevated' => $request->hasElevatedSession(), | ||
'expiry' => $request->getElevatedSessionExpiry(), | ||
]; | ||
} | ||
|
||
public function showForm() | ||
{ | ||
return view('statamic::auth.confirm-password'); | ||
} | ||
|
||
public function confirm(Request $request) | ||
{ | ||
$user = User::current(); | ||
|
||
$validated = $request->validate([ | ||
'password' => 'required', | ||
]); | ||
|
||
if (! Hash::check($validated['password'], $user->password())) { | ||
throw ValidationException::withMessages([ | ||
'password' => [__('statamic::validation.current_password')], | ||
]); | ||
} | ||
|
||
session()->elevate(); | ||
|
||
return $request->wantsJson() | ||
? $this->status($request) | ||
: redirect()->intended(cp_route('index'))->with('success', __('Password confirmed')); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.