Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-17T04:10:42.210Z
Answers.Text: 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.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-07T20:59:31.030Z
Answers.LastActivityDate: 2023-08-07T20:59:31.030Z
Answers.UserDisplayName: Stef Verniers
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-07T17:22:01.787Z
Answers.Text: Meanwhile, I achieved by rewriting the base craft controller
Answers.UserDisplayName: Stef Verniers
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-07T19:17:38.123Z
Answers.Text: @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.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-07T19:17:41.703Z
Answers.Text: @StefVerniers Consider adding your solution as an answer; could be helpful to others in the future!
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-23T00:17:19.653Z
Answers.Text: Ah, apologies. I thought the 403 template appeared for CP URLs as well.
Answers.UserDisplayName: cherrykoda
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-07T14:57:51.613Z
Answers.LastActivityDate: 2023-08-07T14:57:51.613Z
Answers.UserDisplayName: cherrykoda
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-08-06T08:20:20.353Z
Comments.Text: not sure, but are you probably looking for this? https://craftcms.com/docs/4.x/config/general.html#postloginredirect
Comments.UserDisplayName: funkysoul
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-08-06T10:17:06.253Z
Comments.Text: 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
Comments.UserDisplayName: Stef Verniers
Tags: <craft4>
Title: Redirect users with no CP permissions automatically
ThreadText: # 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**: @username_0 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.  
  
  ---
  
  