Option to send user to login when accessing a restricted page

Created on 17 November 2023, 12 months ago
Updated 27 November 2023, 12 months ago

Problem/Motivation

The only options for a restricted page are a 403 or a 404. I want to send a user to the login screen so that after they login, they are sent back to the page and can access it (if they have the proper role).

Steps to reproduce

1. Restrict a page to "Visitor"
2. Open page in incognito window
3. User is given an error instead of being sent to the login screen.

Proposed resolution

Add an option to redirect to the login screen if a restricted page is hit and the user is not logged in.

✨ Feature request
Status

Needs review

Version

3.0

Component

User interface

Created by

πŸ‡ΊπŸ‡ΈUnited States nessthehero

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @nessthehero
  • Status changed to Needs review 12 months ago
  • πŸ‡ΊπŸ‡ΈUnited States nessthehero

    I've created a merge request.

  • πŸ‡¨πŸ‡·Costa Rica Royden_CH

    I've tested the merge request and it works fine.

  • Status changed to RTBC 12 months ago
  • πŸ‡¨πŸ‡·Costa Rica Royden_CH
  • πŸ‡ΊπŸ‡ΈUnited States nessthehero

    I am attaching a patch as well in case anyone wants to use this immediately in a project.

  • πŸ‡¨πŸ‡·Costa Rica Royden_CH

    I am having an issue with this patch/MR. The first time works as expected, the site redirects me to the login. But when I try again by deleting the login path and leave only the destination (example: /en/user/login?destination=[destination-path] -> /[destination-path]) the site shows me the node, without redirecting to the login page.

  • Status changed to Needs review 12 months ago
  • πŸ‡¨πŸ‡·Costa Rica Royden_CH
  • πŸ‡ΊπŸ‡ΈUnited States nessthehero

    I am not able to reproduce that behavior.

    Here are my testing steps:

    1. Create a new node that is restricted to "Visitor", and select the option "User must log in".
    2. Visit the node in an incognito window. I am redirected to the login screen.
    3. Do not log in. Instead, modify the url to be the url of the restricted page. Hit enter to navigate.
    4. I am redirected back to the login screen again.

    If you are logged in already, then it won't redirect you to the login screen or destroy your session. Perhaps what needs done is to show a 403 if you are logged in and try to hit the restricted page.

  • πŸ‡ΊπŸ‡ΈUnited States nessthehero

    Noticed two things while digging into this:

    1. The role is never passed into getRecords. It's always either '' or 'anonymous'.
    2. If I modify getAccessContentStatus to pass the user's role, it then looks for records that match the UID _and_ Role. I'm not setting the specific user id for my restrictions so this is definitely odd.

    I pushed up a change that improves the check for records using roles, and if the user is logged in and hits a page that is set to redirect to the login (and their role is restricted), they get a 403.

    Also adding in a new patch.

Production build 0.71.5 2024