Skip to content

Instantly share code, notes, and snippets.

@umanda
Last active November 19, 2019 20:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save umanda/c4be8da584715cbb901d to your computer and use it in GitHub Desktop.
Save umanda/c4be8da584715cbb901d to your computer and use it in GitHub Desktop.
Multiple user type for Laravel 4.2 and Sentry 2.

Recently I wanted to have a system with multiple user loggings with Laravel and Sentry 2. I was searched over the internet and I found a nice solution from leabdalla.

https://gist.github.com/leabdalla/5999421

Unfortunately this solution is working only Laravel 4.0 and Sentry 2. I have noted leabdalla post his GITS in 2013 and meantime laravel and Sentry has some updates. Therefor leabdalla’s code not working with latest Laravel and Sentry releases.

I have fixed that issue and my changes are compatible with Latest Laravel releases. As well as this might be not work with future changes in laravel as well as sentry

<?php namespace Jayolab\Authentication\Models;
use Illuminate\Database\Eloquent\Model;
use Cartalyst\Sentry\Groups\GroupInterface;
use Cartalyst\Sentry\Hashing\HasherInterface;
use Cartalyst\Sentry\Users\LoginRequiredException;
use Cartalyst\Sentry\Users\PasswordRequiredException;
use Cartalyst\Sentry\Users\UserAlreadyActivatedException;
use Cartalyst\Sentry\Users\UserExistsException;
use Cartalyst\Sentry\Users\UserInterface;
class Admin extends \Cartalyst\Sentry\Users\Eloquent\User implements UserInterface{
//class Admin extends Model implements UserInterface{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'admins';
public function groups(){
return $this->belongsToMany('Cartalyst\Sentry\Groups\Eloquent\Group', 'admins_groups');
}
protected static $groupModel = 'Cartalyst\Sentry\Groups\Eloquent\Group';
}
<?php
class AdminAuthController extends BaseController {
public function index(){
print "<pre>";
print_r(Sentry::getUserProvider());
print "<br>";
print_r(SentryAdmin::getUserProvider());
print "<br>";
print_r(SentryAdmin::findAllUsers());
print "<br>";
print_r(Sentry::findAllUsers());
print_r(Sentry::getUserProvider());
print_r(SentryAdmin::getUserProvider());
}
}
<?php
/**
* Part of the Sentry package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file. It is also available at
* the following URL: http://www.opensource.org/licenses/BSD-3-Clause
*
* @package Sentry
* @version 2.0.0
* @author Cartalyst LLC
* @license BSD License (3-clause)
* @copyright (c) 2011 - 2013, Cartalyst LLC
* @link http://cartalyst.com
*/
return array(
'admin'=>array(
'driver' => 'eloquent',
'hasher' => 'native',
'cookie' => array(
'key' => 'cartalyst_sentry',
),
'groups' => array(
'model' => 'Cartalyst\Sentry\Groups\Eloquent\Group',
),
'users' => array(
'model' => 'Jayolab\Authentication\Models\Admin',
'login_attribute' => 'email',
),
'user_groups_pivot_table' => 'admins_groups',
'throttling' => array(
'enabled' => true,
'model' => 'Cartalyst\Sentry\Throttling\Eloquent\Throttle',
'attempt_limit' => 5,
'suspension_time' => 15,
),
),
/*
|--------------------------------------------------------------------------
| Default Authentication Driver
|--------------------------------------------------------------------------
|
| This option controls the authentication driver that will be utilized.
| This drivers manages the retrieval and authentication of the users
| attempting to get access to protected areas of your application.
|
| Supported: "eloquent" (more coming soon).
|
*/
'driver' => 'eloquent',
/*
|--------------------------------------------------------------------------
| Default Hasher
|--------------------------------------------------------------------------
|
| This option allows you to specify the default hasher used by Sentry
|
| Supported: "native", "bcrypt", "sha256", "whirlpool"
|
*/
'hasher' => 'native',
/*
|--------------------------------------------------------------------------
| Cookie
|--------------------------------------------------------------------------
|
| Configuration specific to the cookie component of Sentry.
|
*/
'cookie' => array(
/*
|--------------------------------------------------------------------------
| Default Cookie Key
|--------------------------------------------------------------------------
|
| This option allows you to specify the default cookie key used by Sentry.
|
| Supported: string
|
*/
'key' => 'cartalyst_sentry',
),
/*
|--------------------------------------------------------------------------
| Groups
|--------------------------------------------------------------------------
|
| Configuration specific to the group management component of Sentry.
|
*/
'groups' => array(
/*
|--------------------------------------------------------------------------
| Model
|--------------------------------------------------------------------------
|
| When using the "eloquent" driver, we need to know which
| Eloquent models should be used throughout Sentry.
|
*/
'model' => 'Cartalyst\Sentry\Groups\Eloquent\Group',
),
/*
|--------------------------------------------------------------------------
| Users
|--------------------------------------------------------------------------
|
| Configuration specific to the user management component of Sentry.
|
*/
'users' => array(
/*
|--------------------------------------------------------------------------
| Model
|--------------------------------------------------------------------------
|
| When using the "eloquent" driver, we need to know which
| Eloquent models should be used throughout Sentry.
|
*/
'model' => 'Cartalyst\Sentry\Users\Eloquent\User',
/*
|--------------------------------------------------------------------------
| Login Attribute
|--------------------------------------------------------------------------
|
| If you're using the "eloquent" driver and extending the base Eloquent
| model, we allow you to globally override the login attribute without
| even subclassing the model, simply by specifying the attribute below.
|
*/
'login_attribute' => 'email',
),
/*
|--------------------------------------------------------------------------
| User Groups Pivot Table
|--------------------------------------------------------------------------
|
| When using the "eloquent" driver, you can specify the table name
| for the user groups pivot table.
|
| Default: users_groups
|
*/
'user_groups_pivot_table' => 'users_groups',
/*
|--------------------------------------------------------------------------
| Throttling
|--------------------------------------------------------------------------
|
| Throttling is an optional security feature for authentication, which
| enables limiting of login attempts and the suspension & banning of users.
|
*/
'throttling' => array(
/*
|--------------------------------------------------------------------------
| Throttling
|--------------------------------------------------------------------------
|
| Enable throttling or not. Throttling is where users are only allowed a
| certain number of login attempts before they are suspended. Suspension
| must be removed before a new login attempt is allowed.
|
*/
'enabled' => true,
/*
|--------------------------------------------------------------------------
| Model
|--------------------------------------------------------------------------
|
| When using the "eloquent" driver, we need to know which
| Eloquent models should be used throughout Sentry.
|
*/
'model' => 'Cartalyst\Sentry\Throttling\Eloquent\Throttle',
/*
|--------------------------------------------------------------------------
| Attempts Limit
|--------------------------------------------------------------------------
|
| When using the "eloquent" driver and extending the base Eloquent model,
| you have the option to globally set the login attempts.
|
| Supported: int
|
*/
'attempt_limit' => 5,
/*
|--------------------------------------------------------------------------
| Suspension Time
|--------------------------------------------------------------------------
|
| When using the "eloquent" driver and extending the base Eloquent model,
| you have the option to globally set the suspension time, in minutes.
|
| Supported: int
|
*/
'suspension_time' => 15,
),
);
<?php namespace Jayolab\Authentication\Sentry;
use Illuminate\Support\Facades\Facade;
class SentryAdmin extends Facade {
protected static function getFacadeAccessor() {
return 'sentry.admin';
}
}
<?php namespace Jayolab\Authentication\Sentry;
use Cartalyst\Sentry\Cookies\IlluminateCookie;
use Cartalyst\Sentry\Groups\Eloquent\Provider as GroupProvider;
use Cartalyst\Sentry\Hashing\BcryptHasher;
use Cartalyst\Sentry\Hashing\NativeHasher;
use Cartalyst\Sentry\Hashing\Sha256Hasher;
use Cartalyst\Sentry\Hashing\WhirlpoolHasher;
use Cartalyst\Sentry\Sentry;
use Cartalyst\Sentry\Sessions\IlluminateSession;
use Cartalyst\Sentry\Throttling\Eloquent\Provider as ThrottleProvider;
use Cartalyst\Sentry\Users\Eloquent\Provider as UserProvider;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Config;
class SentryAdminServiceProvider extends ServiceProvider {
/**
* Boot the service provider.
*
* @return void
*/
public function boot()
{
$this->package('cartalyst/sentry', 'cartalyst/sentry');
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->registerHasher();
$this->registerUserProvider();
$this->registerGroupProvider();
$this->registerThrottleProvider();
$this->registerSession();
$this->registerCookie();
$this->registerSentry();
}
/**
* Register the hasher used by Sentry.
*
* @return void
*/
protected function registerHasher()
{
$this->app['sentry.admin.hasher'] = $this->app->share(function($app)
{
$hasher = $app['config']['cartalyst/sentry::admin.hasher'];
//print_r($app['config']['cartalyst/sentry::hasher']);
//print_r($app['config']['cartalyst/sentry::admin.hasher']);
//print_r($app['config']['cartalyst/config.sentry::hasher']);
switch ($hasher)
{
case 'native':
return new NativeHasher;
break;
case 'bcrypt':
return new BcryptHasher;
break;
case 'sha256':
return new Sha256Hasher;
break;
case 'whirlpool':
return new WhirlpoolHasher;
break;
}
throw new \InvalidArgumentException("Invalid hasher [$hasher] chosen for Sentry.");
});
}
/**
* Register the user provider used by Sentry.
*
* @return void
*/
protected function registerUserProvider()
{
$this->app['sentry.admin.user'] = $this->app->share(function($app)
{
$model = $app['config']['cartalyst/sentry::admin.users.model'];
// We will never be accessing a user in Sentry without accessing
// the user provider first. So, we can lazily set up our user
// model's login attribute here. If you are manually using the
// attribute outside of Sentry, you will need to ensure you are
// overriding at runtime.
if (method_exists($model, 'setLoginAttributeName'))
{
$loginAttribute = $app['config']['cartalyst/sentry::admin.users.login_attribute'];
forward_static_call_array(
array($model, 'setLoginAttributeName'),
array($loginAttribute)
);
}
// Define the Group model to use for relationships.
if (method_exists($model, 'setGroupModel'))
{
$groupModel = $app['config']['cartalyst/sentry::admin.groups.model'];
forward_static_call_array(
array($model, 'setGroupModel'),
array($groupModel)
);
}
// Define the user group pivot table name to use for relationships.
if (method_exists($model, 'setUserGroupsPivot'))
{
$pivotTable = $app['config']['cartalyst/sentry::admin.user_groups_pivot_table'];
forward_static_call_array(
array($model, 'setUserGroupsPivot'),
array($pivotTable)
);
}
return new UserProvider($app['sentry.admin.hasher'], $model);
});
}
/**
* Register the group provider used by Sentry.
*
* @return void
*/
protected function registerGroupProvider()
{
$this->app['sentry.admin.group'] = $this->app->share(function($app)
{
$model = $app['config']['cartalyst/sentry::admin.groups.model'];
// Define the User model to use for relationships.
if (method_exists($model, 'setUserModel'))
{
$userModel = $app['config']['cartalyst/sentry::admin.users.model'];
forward_static_call_array(
array($model, 'setUserModel'),
array($userModel)
);
}
// Define the user group pivot table name to use for relationships.
if (method_exists($model, 'setUserGroupsPivot'))
{
$pivotTable = $app['config']['cartalyst/sentry::admin.user_groups_pivot_table'];
forward_static_call_array(
array($model, 'setUserGroupsPivot'),
array($pivotTable)
);
}
return new GroupProvider($model);
});
}
/**
* Register the throttle provider used by Sentry.
*
* @return void
*/
protected function registerThrottleProvider()
{
$this->app['sentry.admin.throttle'] = $this->app->share(function($app)
{
$model = $app['config']['cartalyst/sentry::admin.throttling.model'];
$throttleProvider = new ThrottleProvider($app['sentry.admin.user'], $model);
if ($app['config']['cartalyst/sentry::admin.throttling.enabled'] === false)
{
$throttleProvider->disable();
}
if (method_exists($model, 'setAttemptLimit'))
{
$attemptLimit = $app['config']['cartalyst/sentry::admin.throttling.attempt_limit'];
forward_static_call_array(
array($model, 'setAttemptLimit'),
array($attemptLimit)
);
}
if (method_exists($model, 'setSuspensionTime'))
{
$suspensionTime = $app['config']['cartalyst/sentry::admin.throttling.suspension_time'];
forward_static_call_array(
array($model, 'setSuspensionTime'),
array($suspensionTime)
);
}
// Define the User model to use for relationships.
if (method_exists($model, 'setUserModel'))
{
$userModel = $app['config']['cartalyst/sentry::admin.users.model'];
forward_static_call_array(
array($model, 'setUserModel'),
array($userModel)
);
}
return $throttleProvider;
});
}
/**
* Register the session driver used by Sentry.
*
* @return void
*/
protected function registerSession()
{
$this->app['sentry.admin.session'] = $this->app->share(function($app)
{
$key = $app['config']['cartalyst/sentry::admin.cookie.key'];
return new IlluminateSession($app['session.store'], $key);
});
}
/**
* Register the cookie driver used by Sentry.
*
* @return void
*/
protected function registerCookie()
{
$this->app['sentry.admin.cookie'] = $this->app->share(function($app)
{
$key = $app['config']['cartalyst/sentry::admin.cookie.key'];
/**
* We'll default to using the 'request' strategy, but switch to
* 'jar' if the Laravel version in use is 4.0.*
*/
$strategy = 'request';
if (preg_match('/^4\.0\.\d*$/D', $app::VERSION))
{
$strategy = 'jar';
}
return new IlluminateCookie($app['request'], $app['cookie'], $key, $strategy);
});
}
/**
* Takes all the components of Sentry and glues them
* together to create Sentry.
*
* @return void
*/
protected function registerSentry()
{
$this->app['sentry.admin'] = $this->app->share(function($app)
{
return new Sentry(
$app['sentry.admin.user'],
$app['sentry.admin.group'],
$app['sentry.admin.throttle'],
$app['sentry.admin.session'],
$app['sentry.admin.cookie'],
$app['request']->getClientIp()
);
});
}
}
@nico0689
Copy link

Hi, thanks so much for those snnipes but I having some issue when I'm trying to implement this way.

Issue: "Class sentry.admin does not exist". Do you know what I'm doing wrong? Thanks and regards!

@umanda
Copy link
Author

umanda commented Jun 13, 2015

Hi nico0689

Sorry for the late reply. Please check your namespaces. I pretty sure issue is there.

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