Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Access denied page
  • Hi everyone ... I wonder if you can define access denied page ... like a 404 page.

    Hola a todos ...quisiera saber si es posible definir una página de acceso denegado ...como si fuera una página 404.
  • Access denied is usually application specific, so it's functionality is not build into the Framework. But you can easily add it, similar to how 404 is handled. Create a HttpAccessDeniedException class, and always load that so it's available everywhere. You can then throw this exception when you encounter an access denied situation. You can use the parameter to pass a message to the exception if needed. Then in your public/index.php, catch the exception and handle it, either by launching a request for a 403 controller, or simply create a response object using a view.
  • Hi ...this is my solution!!! I created a new class in fuel/apps/classes/lib/httpaccessdeniedexception.php and and always load that so it's available everywhere ...this is a code.
    <?php
    
    class Lib_HttpAccessDeniedException extends FuelException {
    
        public function handle() {
            $response = new \Response(\View::forge('403'), 403);
            \Event::shutdown();
            $response->send(true);
        }
        
        public function __construct($message, $code = 0) {
            
            parent::__construct($message, $code);
        }
        
        public function __toString() {
            return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
        }
    
        public static function _init() {
            
        }
    
        public static function show_403() {
            logger(\Fuel::L_WARNING, 'This method is deprecated.  Please use a HttpAccessDeniedException instead.', __METHOD__);
            $error = 'Access Denied';
            throw new Lib_HttpAccessDeniedException($error);
        }
    
    }
    

    modify a public/index.php file
    // Generate the request, execute it and send the output.
    try {
        $response = Request::forge()->execute()->response();
    } catch (HttpNotFoundException $e) {
        $route = array_key_exists('_404_', Router::$routes) ? Router::$routes['_404_']->translation : Config::get('routes._404_');
        if ($route) {
            $response = Request::forge($route)->execute()->response();
        } else {
            throw $e;
        }
    } catch (\Lib_HttpAccessDeniedException $e) {
        $route = array_key_exists('_403_', Router::$routes) ? Router::$routes['_403_']->translation : Config::get('routes._403_');
        if ($route) {
            $response = Request::forge($route)->execute()->response();
        } else {
            throw $e;
        }
    }
    

    modify a config/router.php and add this.
    '_403_' => 'welcome/403', // The main 403 route
    

    how to use?
    if(!Auth::has_access("$class_name.$action") && Auth::check()) {
       Lib_HttpAccessDeniedException::show_403();
    }
    
  • I would say
    if (!Auth::has_access("$class_name.$action") && Auth::check())
    {
        throw new Lib_HttpAccessDeniedException();
    }
    
    instead of calling the deprecated method? And I don't think that Auth::check() is needed. If not logged in, has_access() will return false anyway.
  • when running Lib_HttpAccessDeniedException :: show_403 (); is almost the same as Request :: show_404 (); Auth :: check () is necessary because when I run the view admin / login should be allow the entry.
  • Request::show_404() is deprecated and will be removed in v1.2, which is due in the next couple of weeks.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion