Skip to content
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

[Login] Add login suggestion and redirection for error 403 #9041

Merged
merged 3 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions modules/login/jsx/loginIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,11 @@ class Login extends Component {
})
.then((response) => {
if (response.ok) {
response.json().then((data) => {
// success - refresh page and user is logged in.
window.location.href = window.location.origin;
response.json().then(() => {
// Redirect if there is a "redirect" param, refresh the page otherwise
window.location.href = this.props.redirect !== null
? this.props.redirect
: window.location.origin;
});
} else {
response.json().then((data) => {
Expand Down Expand Up @@ -337,6 +339,7 @@ Login.propTypes = {
defaultRequestFirstName: PropTypes.string,
defaultRequestLastName: PropTypes.string,
defaultRequestEmail: PropTypes.string,
redirect: PropTypes.string,
};

window.addEventListener('load', () => {
Expand All @@ -353,6 +356,7 @@ window.addEventListener('load', () => {
defaultRequestFirstName={getParam('firstname', '')}
defaultRequestLastName={getParam('lastname', '')}
defaultRequestEmail={getParam('email', '')}
redirect={getParam('redirect', null)}
module={'login'}
/>
);
Expand Down
6 changes: 6 additions & 0 deletions modules/login/test/Login_Redirect_Test_Plan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Login Redirect Test Plan

1. As an anonymous user, go to a page that requires to be logged in (this requires to use the URL of the page).
2. When met with a 403 error, choose the option "Try logging in" and enter your user credentials.
3. You should be redirected to the previous page and have access to it if you have the permissions to do so.
4. The option "Try logging in" should not appeared for logged in users that access pages they do not have the permissions for.
9 changes: 8 additions & 1 deletion smarty/templates/403.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<div class="container">
<h2>403: Forbidden</h2>
<h3>{$message}</h3>
<div><a href="{$baseurl}">Go to main page</a> | <a onclick="window.history.back();" href="#">Go back one page</a></div>
<div>
<a href="{$baseurl}">Go to main page</a>
| <a onclick="window.history.back();" href="#">Go back one page</a>
{if $anonymous}
| <a href="/login?redirect={$url}">Try logging in</a>
{/if}
</div>
</div>
5 changes: 5 additions & 0 deletions src/Http/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public function __construct(
$lorisInstance = $request->getAttribute('loris');
$user = $request->getAttribute('user') ?? new \LORIS\AnonymousUser();

// Variables used to suggest the user to login and later redirect them if they
// are not authenticated in a 403.
$tpl_data['anonymous'] = $user instanceof \LORIS\AnonymousUser;
$tpl_data['url'] = urlencode($uri->__toString());

// Add a link to the issue tracker as long as a LORIS Instance object
// is present in the request.
if (! $user instanceof \LORIS\AnonymousUser
Expand Down