1

I want any user who isn't the master user to be redirected to a separate page at /profiles.

I've achieved this by editing the admin sprinkle, adding this simple statement to the pageDashboard function in AdminController.php:

    if(!$currentUser->isMaster()){
        header("Location: /profiles");
        exit;
    }

I want to move this to my own sprinkle, but I'm not clear on how best to do this. Would I create my own controller that extends AdminController, and just replace the function? Or is there a neater way of doing it? What I have now works but obviously isn't future-proof as this file will be overwritten in future updates.

Jonny Perl
  • 133
  • 6

1 Answers1

3

You can change where the users are redirected after login using the determineRedirectOnLogin service. See : https://learn.userfrosting.com/services/default-services#determineredirectonlogin. In your sprinkle ServicesProvider, simply overwrite the default service with something similar:

$container['determineRedirectOnLogin'] = function ($c) {
    return function ($response) use ($c)
    {
        if (!$c->currentUser->isMaster()) {
            return $response->withHeader('UF-Redirect', '/dashboard');
        } else {
            return $response->withHeader('UF-Redirect', '/profiles');            
        }
    };
};

You can then use the permission system to remove access to the dashboard for the non root users if you wish so.

Side note, like you pointed out, you shouldn't edit any core sprinkles and move that code to your own sprinkle.

Louis Charette
  • 1,325
  • 1
  • 10
  • 34
  • Thanks. It turns out I'd already attempted this method but had messed up both the namespace and the name of the file itself (I had called mine `[sprinklename]ServicesProvider.php` instead of just `ServicesProvider.php`) before going on to take the more blunt approach described in my question. Once I fixed the filename and namespace everything was fine. – Jonny Perl Jul 01 '17 at 23:16
  • The ServicesProvider name convention changed from 4.0.x to 4.1.x, maybe that's why – Louis Charette Jul 01 '17 at 23:52
  • Aha - yes, that threw me a bit on a few other things! – Jonny Perl Jul 02 '17 at 20:08