Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pajcho/6121291 to your computer and use it in GitHub Desktop.
Save pajcho/6121291 to your computer and use it in GitHub Desktop.
Sentry 2 multiple user types in FuelPHP framework

Get multiple Sentry instances in FuelPHP framework

I followed this gist (https://gist.github.com/leabdalla/5999421) to achieve the goal, but because Sentry 2 uses different approach to implement in FuelPHP than Laravel it needed some additional code. So here is a solution for that.

Note that this is not perfect solution, and it could break when we do composer update, because it depends on current Sentry version.

1. Create New Files

You will need to create this file structure:

/app
    /vendor
        /Cartalyst
            /Sentry
                /Facades
                    /FuelPHP
                        SentryAdmin.php
                    FacadeAdmin.php
                /Users
                    /Eloquent
                        Admin.php

######SentryAdmin.php

http://bin.fuelphp.com/~j4

This file is same as FuelPHP Facade except we pass new model, session and cookie names to use with admin auth

######FacadeAdmin.php

http://bin.fuelphp.com/~j5

Just extending Sentry Facade because we need new instance for handling Admin auth. Otherwise we won't be able to use both instances in same pass

######Admin.php

http://bin.fuelphp.com/~j6

Admin user model. Extends Sentry User model. In this example I have changed login attribute, but thats just because I needed it. Its not required

2. Edit Botstrap.php

After that we will need to load all that classes in botstrap.php file. Just add this:

Autoloader::add_classes(array(
    // Separate Sentry instance used for admin auth
	'Cartalyst\\Sentry\\Users\\Eloquent\\Admin'         => APPPATH . 'vendor/Cartalyst/Sentry/Users/Eloquent/Admin.php',
	'Cartalyst\\Sentry\\Facades\\FacadeAdmin'           => APPPATH . 'vendor/Cartalyst/Sentry/Facades/FacadeAdmin.php',
	'SentryAdmin'                                       => APPPATH . 'vendor/Cartalyst/Sentry/Facades/FuelPHP/SentryAdmin.php',
));

3. Alter Database

You will need to copy users and users_groups database tables and rename them to admins and admins_groups

Also you will have to rename column user_id in admins_groups to admin_id

4. Does it work?

After that you are all set and you can use \SentryAdmin the same as you would use \Sentry. To test it do this in your code:

print_r(\Sentry::getUserProvider());
print_r(\SentryAdmin::getUserProvider());

and you should get objects with different models. Something like this:

// User object
Cartalyst\Sentry\Users\Eloquent\Provider
Object (
    [model:protected] => Cartalyst\Sentry\Users\Eloquent\User
    [hasher:protected] => Cartalyst\Sentry\Hashing\NativeHasher
)

// Admin object
Cartalyst\Sentry\Users\Eloquent\Provider
Object (
    [model:protected] => Cartalyst\Sentry\Users\Eloquent\Admin
    [hasher:protected] => Cartalyst\Sentry\Hashing\NativeHasher
)

I hope this helps someone. If you have any questions feel free to comment and I'll try to help.

Thanks to Leandro Abdalla for his solution that realy helped me a lot. His solution for Laravel can be found here: https://gist.github.com/leabdalla/5999421

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment