Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Cartalyst/Sentry/Facades/CI/Sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Cartalyst\Sentry\Sentry as BaseSentry;
use Cartalyst\Sentry\Throttling\Eloquent\Provider as ThrottleProvider;
use Cartalyst\Sentry\Users\Eloquent\Provider as UserProvider;
use Cartalyst\Sentry\SessionHandlers\NativeSessionHandler;
use Illuminate\Database\Eloquent\Model as Eloquent;
use PDO;

Expand Down Expand Up @@ -89,8 +90,10 @@ public static function createSentry()
$userProvider = new UserProvider(new NativeHasher),
new GroupProvider,
new ThrottleProvider($userProvider),
new CISession($ci->session),
new CICookie($ci->input),
new NativeSessionHandler(
new CISession($ci->session),
new CICookie($ci->input)
),
$ci->input->ip_address()
);
}
Expand Down
7 changes: 5 additions & 2 deletions src/Cartalyst/Sentry/Facades/FuelPHP/Sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Cartalyst\Sentry\Facades\Facade;
use Cartalyst\Sentry\Groups\Eloquent\Provider as GroupProvider;
use Cartalyst\Sentry\Hashing\NativeHasher;
use Cartalyst\Sentry\SessionHandlers\NativeSessionHandler;
use Cartalyst\Sentry\Sessions\FuelPHPSession;
use Cartalyst\Sentry\Sentry as BaseSentry;
use Cartalyst\Sentry\Throttling\Eloquent\Provider as ThrottleProvider;
Expand Down Expand Up @@ -65,8 +66,10 @@ public static function createSentry()
$userProvider = new UserProvider(new NativeHasher),
new GroupProvider,
new ThrottleProvider($userProvider),
new FuelPHPSession(Session::instance()),
new FuelPHPCookie,
new NativeSessionHandler(
new FuelPHPSession(Session::instance()),
new FuelPHPCookie
),
Input::real_ip()
);
}
Expand Down
7 changes: 5 additions & 2 deletions src/Cartalyst/Sentry/Facades/Kohana/Sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Cartalyst\Sentry\Groups\Kohana\Provider as GroupProvider;
use Cartalyst\Sentry\Sessions\KohanaSession;
use Cartalyst\Sentry\Sentry as BaseSentry;
use Cartalyst\Sentry\SessionHandlers\NativeSessionHandler;
use Cartalyst\Sentry\Throttling\Kohana\Provider as ThrottleProvider;
use Cartalyst\Sentry\Users\Kohana\Provider as UserProvider;

Expand Down Expand Up @@ -67,8 +68,10 @@ public static function createSentry()
$userProvider = new UserProvider($hasher),
new GroupProvider,
new ThrottleProvider($userProvider),
new KohanaSession(\Session::instance($config['session_driver']), $config['session_key']),
new KohanaCookie($config['cookie_key']),
new NativeSessionHandler(
new KohanaSession(\Session::instance($config['session_driver']), $config['session_key']),
new KohanaCookie($config['cookie_key'])
),
\Request::$client_ip
);
}
Expand Down
7 changes: 5 additions & 2 deletions src/Cartalyst/Sentry/Facades/Native/Sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Cartalyst\Sentry\Hashing\NativeHasher;
use Cartalyst\Sentry\Sessions\NativeSession;
use Cartalyst\Sentry\Sessions\SessionInterface;
use Cartalyst\Sentry\SessionHandlers\NativeSessionHandler;
use Cartalyst\Sentry\Sentry as BaseSentry;
use Cartalyst\Sentry\Throttling\Eloquent\Provider as ThrottleProvider;
use Cartalyst\Sentry\Throttling\ProviderInterface as ThrottleProviderInterface;
Expand Down Expand Up @@ -63,8 +64,10 @@ public static function createSentry(
$userProvider,
$groupProvider ?: new GroupProvider,
$throttleProvider ?: new ThrottleProvider($userProvider),
$session ?: new NativeSession,
$cookie ?: new NativeCookie,
new NativeSessionHandler(
$session ?: new NativeSession,
$cookie ?: new NativeCookie
),
$ipAddress ?: static::guessIpAddress()
);
}
Expand Down
105 changes: 66 additions & 39 deletions src/Cartalyst/Sentry/Sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
use Cartalyst\Sentry\Hashing\NativeHasher;
use Cartalyst\Sentry\Sessions\NativeSession;
use Cartalyst\Sentry\Sessions\SessionInterface;
use Cartalyst\Sentry\SessionHandlers\SessionHandlerInterface;
use Cartalyst\Sentry\SessionHandlers\NativeSessionHandler;
use Cartalyst\Sentry\Throttling\Eloquent\Provider as ThrottleProvider;
use Cartalyst\Sentry\Throttling\ProviderInterface as ThrottleProviderInterface;
use Cartalyst\Sentry\Users\LoginRequiredException;
Expand All @@ -36,6 +38,9 @@
use Cartalyst\Sentry\Users\UserNotActivatedException;

class Sentry {
const SESSION_KEY_PERSIST_CODE = 'persistCode';
const SESSION_KEY_USER_ID = 'userId';
const SESSION_MASQUERADE_STACK = 'masqueradeStack';

/**
* The user that's been retrieved and is used
Expand All @@ -47,19 +52,6 @@ class Sentry {
*/
protected $user;

/**
* The session driver used by Sentry.
*
* @var \Cartalyst\Sentry\Sessions\SessionInterface
*/
protected $session;

/**
* The cookie driver used by Sentry.
*
* @var \Cartalyst\Sentry\Cookies\CookieInterface
*/
protected $cookie;

/**
* The user provider, used for retrieving
Expand Down Expand Up @@ -95,6 +87,14 @@ class Sentry {
*/
protected $ipAddress = '0.0.0.0';


/**
* The session handler class
*
* @var \Cartalyst\Sentry\SessionHandler\SessionHandlerInterface
*/
protected $session;

/**
* Create a new Sentry object.
*
Expand All @@ -110,17 +110,16 @@ public function __construct(
UserProviderInterface $userProvider = null,
GroupProviderInterface $groupProvider = null,
ThrottleProviderInterface $throttleProvider = null,
SessionInterface $session = null,
CookieInterface $cookie = null,
SessionHandlerInterface $sessionHandler = null,
$ipAddress = null
)
{
$this->userProvider = $userProvider ?: new UserProvider(new NativeHasher);
$this->groupProvider = $groupProvider ?: new GroupProvider;
$this->throttleProvider = $throttleProvider ?: new ThrottleProvider($this->userProvider);

$this->session = $session ?: new NativeSession;
$this->cookie = $cookie ?: new NativeCookie;

$this->session = $sessionHandler ?: new NativeSessionHandler();

if (isset($ipAddress))
{
Expand Down Expand Up @@ -245,21 +244,16 @@ public function check()
{
if (is_null($this->user))
{
// Check session first, follow by cookie
if ( ! $userArray = $this->session->get() and ! $userArray = $this->cookie->get())
{
return false;
}
$id = $this->session->get(self::SESSION_KEY_USER_ID);
$persistCode = $this->session->get(self::SESSION_KEY_PERSIST_CODE);

// Now check our user is an array with two elements,
// the username followed by the persist code
if ( ! is_array($userArray) or count($userArray) !== 2)

// If either user id or persist code is not set we are not logged in
if ($id == null || $persistCode == null)
{
return false;
}

list($id, $persistCode) = $userArray;

// Let's find our user
try
{
Expand Down Expand Up @@ -321,18 +315,15 @@ public function login(UserInterface $user, $remember = false)
throw new UserNotActivatedException("Cannot login user [$login] as they are not activated.");
}

$this->user = $user;

// Create an array of data to persist to the session and / or cookie
$toPersist = array($user->getId(), $user->getPersistCode());

$this->user = $user;

// Set sessions
$this->session->put($toPersist);
$this->session->set(self::SESSION_KEY_USER_ID, $user->getId());
$this->session->set(self::SESSION_KEY_PERSIST_CODE, $user->getPersistCode());

if ($remember)
{
$this->cookie->forever($toPersist);
}
$remember && $this->session->forever();

// The user model can attach any handlers
// to the "recordLogin" event.
Expand All @@ -358,8 +349,7 @@ public function logout()
{
$this->user = null;

$this->session->forget();
$this->cookie->forget();
$this->session->destroy();
}

/**
Expand Down Expand Up @@ -397,7 +387,7 @@ public function getUser()
*/
public function setSession(SessionInterface $session)
{
$this->session = $session;
$this->session->setSession($session);
}

/**
Expand All @@ -407,7 +397,7 @@ public function setSession(SessionInterface $session)
*/
public function getSession()
{
return $this->session;
return $this->session->getSession();
}

/**
Expand Down Expand Up @@ -713,6 +703,43 @@ public function findThrottlerByUserLogin($login, $ipAddress = null)
return $this->throttleProvider->findByUserLogin($login,$ipAddress);
}

/**
* Masquerades as another user
*
* @param \Cartalyst\Sentry\User\UserInterface $user User to masquerade as
* @return void
*/
public function masquerade(UserInterface $user)
{
if (!$this->check()) {
throw new LoginRequiredException();
}

$old[static::SESSION_KEY_USER_ID] = $this->session->get(static::SESSION_KEY_USER_ID);
$old[static::SESSION_KEY_PERSIST_CODE] = $this->session->get(static::SESSION_KEY_PERSIST_CODE);
$stack = $this->session->get(static::SESSION_MASQUERADE_STACK) ?: array();
array_push($stack, $old);
$this->session->set(static::SESSION_MASQUERADE_STACK, $stack);
$this->login($user);
}

/**
* LogOut of masquerade. Behaves like a normal logout of person is not masqueraded
*
* @return void
*/
public function masqueradedLogout() {
$stack = $this->session->get(static::SESSION_MASQUERADE_STACK);
if ($stack == null || !($oldUser = array_pop($stack))) {
return $this->LogOut();
}

$this->session->set(static::SESSION_KEY_USER_ID, $oldUser[static::SESSION_KEY_USER_ID]);
$this->session->set(static::SESSION_KEY_PERSIST_CODE, $oldUser[static::SESSION_KEY_PERSIST_CODE]);
$this->session->set(static::SESSION_MASQUERADE_STACK, $stack);
$this->user = $this->userProvider->findById($oldUser[static::SESSION_KEY_USER_ID]);
}

/**
* Handle dynamic method calls into the method.
*
Expand Down
7 changes: 5 additions & 2 deletions src/Cartalyst/Sentry/SentryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Cartalyst\Sentry\Hashing\Sha256Hasher;
use Cartalyst\Sentry\Hashing\WhirlpoolHasher;
use Cartalyst\Sentry\Sentry;
use Cartalyst\Sentry\SessionHandlers\NativeSessionHandler;
use Cartalyst\Sentry\Sessions\IlluminateSession;
use Cartalyst\Sentry\Throttling\Eloquent\Provider as ThrottleProvider;
use Cartalyst\Sentry\Users\Eloquent\Provider as UserProvider;
Expand Down Expand Up @@ -289,8 +290,10 @@ protected function registerSentry()
$app['sentry.user'],
$app['sentry.group'],
$app['sentry.throttle'],
$app['sentry.session'],
$app['sentry.cookie'],
new NativeSessionHandler(
$app['sentry.session'],
$app['sentry.cookie']
),
$app['request']->getClientIp()
);
});
Expand Down
Loading