<p>I got it working by changing the function that causes the exception template to trigger.</p>
<pre><code>    public function requirePermission(string $permissionName): void
    {
        if (!Craft::$app-&gt;getUser()-&gt;checkPermission($permissionName)) {
            // throw new ForbiddenHttpException('User is not authorized to perform this action.');
            $default = Craft::t('app','You are not allowed to visit this page');
            Craft::$app-&gt;getSession()-&gt;setFlash('error', Craft::t('app', $default));
            $this-&gt;goHome();
        } 
    }
</code></pre>
<p>By adding a craft sessions flash message and redirect to the home url, I've achieved what I want.</p>

CC BY-SA 4.0
2023-08-17T04:10:42.210Z
For the record, it sounds like you edited a core Craft file, which is a bad idea for all sorts of reasons so I'd highly discourage this.
Brad Bell
CC BY-SA 4.0
2023-08-07T20:59:31.030Z
2023-08-07T20:59:31.030Z
Stef Verniers
<p>You could create a 403 template (403.twig) that contains content that prompts the user to go login or return to the homepage. Not exactly what you asked for, but its an easy built-in solution to the UX issue.</p>

CC BY-SA 4.0
2023-08-07T17:22:01.787Z
Meanwhile, I achieved by rewriting the base craft controller
Stef Verniers
CC BY-SA 4.0
2023-08-07T19:17:38.123Z
@cherrykoda This is a good idea, but [error templates](https://craftcms.com/docs/4.x/routing.html#error-templates) are only rendered for front end (i.e non-CP) requests, so this wouldn't work unfortunately.
Mats Mikkel Rummelhoff
CC BY-SA 4.0
2023-08-07T19:17:41.703Z
@StefVerniers Consider adding your solution as an answer; could be helpful to others in the future!
Mats Mikkel Rummelhoff
CC BY-SA 4.0
2023-08-23T00:17:19.653Z
Ah, apologies. I thought the 403 template appeared for CP URLs as well.
cherrykoda
CC BY-SA 4.0
2023-08-07T14:57:51.613Z
2023-08-07T14:57:51.613Z
cherrykoda
<p>I have some user groups defined with their specific rules.
A normal user or visitor is of course not allowed to access the admin dashboard.
When I'm logged in as a user with no permissions to access the dashboard, I retrieve an error page from yii which says I'm not authorized to visit this page, what I want.</p>
<p>But instead of seeing this page, I want to be redirected immediately to the homepage with a flash message.</p>
<p>Where can I program this? I'm not sure if this is twig related or module related..</p>
<p>Anyone that can help me out?</p>

CC BY-SA 4.0
2023-08-06T08:20:20.353Z
not sure, but are you probably looking for this? https://craftcms.com/docs/4.x/config/general.html#postloginredirect
funkysoul
CC BY-SA 4.0
2023-08-06T10:17:06.253Z
Not exactly.. when you would type the url for the admin panel, it should redirect you if you don't have the permissions so that is not a postloginredirect
Stef Verniers
<craft4>
Redirect users with no CP permissions automatically
# Redirect users with no CP permissions automatically

(Asked by: username_0 on 2023-08-05)

I have some user groups defined with their specific rules.
A normal user or visitor is of course not allowed to access the admin dashboard.
When I'm logged in as a user with no permissions to access the dashboard, I retrieve an error page from yii which says I'm not authorized to visit this page, what I want.

But instead of seeing this page, I want to be redirected immediately to the homepage with a flash message.

Where can I program this? I'm not sure if this is twig related or module related..

Anyone that can help me out?

**Tags:** craft4

> **Comments:**
> **username_1**: not sure, but are you probably looking for this? https://craftcms.com/docs/4.x/config/general.html#postloginredirect  
> **username_0**: Not exactly.. when you would type the url for the admin panel, it should redirect you if you don't have the permissions so that is not a postloginredirect  

---

## 2 answers

### Answer by username_0 on 2023-08-07. Score: -1 | Accepted answer

I got it working by changing the function that causes the exception template to trigger.
    public function requirePermission(string $permissionName): void
    {
        if (!Craft::$app->getUser()->checkPermission($permissionName)) {
            // throw new ForbiddenHttpException('User is not authorized to perform this action.');
            $default = Craft::t('app','You are not allowed to visit this page');
            Craft::$app->getSession()->setFlash('error', Craft::t('app', $default));
            $this->goHome();
        } 
    }

By adding a craft sessions flash message and redirect to the home url, I've achieved what I want.

> **Comments:**
> **username_2**: For the record, it sounds like you edited a core Craft file, which is a bad idea for all sorts of reasons so I'd highly discourage this.  

---

### Answer by username_3 on 2023-08-07. Score: 1

You could create a 403 template (403.twig) that contains content that prompts the user to go login or return to the homepage. Not exactly what you asked for, but its an easy built-in solution to the UX issue.

> **Comments:**
> **username_0**: Meanwhile, I achieved by rewriting the base craft controller  
> **username_4**: @username_3 This is a good idea, but [error templates](https://craftcms.com/docs/4.x/routing.html#error-templates) are only rendered for front end (i.e non-CP) requests, so this wouldn't work unfortunately.  
> **username_4**: @StefVerniers Consider adding your solution as an answer; could be helpful to others in the future!  
> **username_3**: Ah, apologies. I thought the 403 template appeared for CP URLs as well.  

---

