Skip to content

Commit

Permalink
Session start moved
Browse files Browse the repository at this point in the history
  • Loading branch information
sleeping-owl committed May 12, 2015
1 parent 7f3dab3 commit dcb47e9
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/SleepingOwl/Admin/AdminServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public function boot()
__DIR__.'/../../../public/' => public_path('packages/sleeping-owl/admin/'),
], 'assets');

app('SleepingOwl\Admin\Helpers\StartSession')->run();

Admin::instance()->router->registerRoutes();
$this->registerValidator();

Expand Down
95 changes: 95 additions & 0 deletions src/SleepingOwl/Admin/Helpers/StartSession.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php namespace SleepingOwl\Admin\Helpers;

use Illuminate\Contracts\Encryption\Encrypter;
use Illuminate\Http\Request;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Session\SessionManager;

class StartSession
{

protected $encrypter;
protected $request;
private $manager;

function __construct(Encrypter $encrypter, Request $request, SessionManager $manager)
{
$this->encrypter = $encrypter;
$this->request = clone $request;
$this->manager = $manager;
}

public function run()
{
$this->decrypt($this->request);
$session = $this->startSession($this->request);
$this->request->setSession($session);
}

protected function decrypt(Request $request)
{
foreach ($request->cookies as $key => $c)
{
try
{
$request->cookies->set($key, $this->decryptCookie($c));
}
catch (DecryptException $e)
{
$request->cookies->set($key, null);
}
}

return $request;
}

/**
* Decrypt the given cookie and return the value.
*
* @param string|array $cookie
* @return string|array
*/
protected function decryptCookie($cookie)
{
return is_array($cookie)
? $this->decryptArray($cookie)
: $this->encrypter->decrypt($cookie);
}

/**
* Decrypt an array based cookie.
*
* @param array $cookie
* @return array
*/
protected function decryptArray(array $cookie)
{
$decrypted = [];

foreach ($cookie as $key => $value)
{
$decrypted[$key] = $this->encrypter->decrypt($value);
}

return $decrypted;
}

protected function startSession(Request $request)
{
with($session = $this->getSession($request))->setRequestOnHandler($request);

$session->start();

return $session;
}

public function getSession(Request $request)
{
$session = $this->manager->driver();

$session->setId($request->cookies->get($session->getName()));

return $session;
}

}

0 comments on commit dcb47e9

Please sign in to comment.