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?


2 Answers 2


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.

  • Meanwhile, I achieved by rewriting the base craft controller Aug 7, 2023 at 17:22
  • 1
    @cherrykoda This is a good idea, but error templates are only rendered for front end (i.e non-CP) requests, so this wouldn't work unfortunately. Aug 7, 2023 at 19:17
  • @StefVerniers Consider adding your solution as an answer; could be helpful to others in the future! Aug 7, 2023 at 19:17
  • Ah, apologies. I thought the 403 template appeared for CP URLs as well.
    – cherrykoda
    Aug 23, 2023 at 0:17

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));

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

  • 1
    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
    Aug 17, 2023 at 4:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.