Commit c24b6541 authored by Nicolas Widart's avatar Nicolas Widart

Decoupling the controller fron sentinel

parent 5c738fd3
...@@ -9,6 +9,8 @@ use Modules\Core\Http\Controllers\Admin\AdminBaseController; ...@@ -9,6 +9,8 @@ use Modules\Core\Http\Controllers\Admin\AdminBaseController;
use Modules\Core\Permissions\PermissionManager; use Modules\Core\Permissions\PermissionManager;
use Modules\User\Http\Requests\CreateUserRequest; use Modules\User\Http\Requests\CreateUserRequest;
use Modules\User\Http\Requests\UpdateUserRequest; use Modules\User\Http\Requests\UpdateUserRequest;
use Modules\User\Repositories\RoleRepository;
use Modules\User\Repositories\UserRepository;
class UserController extends AdminBaseController class UserController extends AdminBaseController
{ {
...@@ -16,25 +18,24 @@ class UserController extends AdminBaseController ...@@ -16,25 +18,24 @@ class UserController extends AdminBaseController
* @var PermissionManager * @var PermissionManager
*/ */
private $permissions; private $permissions;
/** /**
* @var \Modules\Session\Entities\User * @var UserRepository
*/ */
protected $users; private $user;
/** /**
* @var \Cartalyst\Sentinel\Roles\EloquentRole * @var RoleRepository
*/ */
protected $roles; private $role;
public function __construct(PermissionManager $permissions) public function __construct(PermissionManager $permissions, UserRepository $user, RoleRepository $role)
{ {
parent::__construct(); parent::__construct();
$this->beforeFilter('permissions'); $this->beforeFilter('permissions');
$this->users = Sentinel::getUserRepository();
$this->roles = Sentinel::getRoleRepository()->createModel();
$this->permissions = $permissions; $this->permissions = $permissions;
$this->user = $user;
$this->role = $role;
} }
/** /**
...@@ -44,7 +45,7 @@ class UserController extends AdminBaseController ...@@ -44,7 +45,7 @@ class UserController extends AdminBaseController
*/ */
public function index() public function index()
{ {
$users = $this->users->createModel()->all(); $users = $this->user->all();
return View::make('user::admin.users.index', compact('users')); return View::make('user::admin.users.index', compact('users'));
} }
...@@ -56,7 +57,7 @@ class UserController extends AdminBaseController ...@@ -56,7 +57,7 @@ class UserController extends AdminBaseController
*/ */
public function create() public function create()
{ {
$roles = $this->roles->all(); $roles = $this->role->all();
return View::make('user::admin.users.create', compact('roles')); return View::make('user::admin.users.create', compact('roles'));
} }
...@@ -70,26 +71,13 @@ class UserController extends AdminBaseController ...@@ -70,26 +71,13 @@ class UserController extends AdminBaseController
public function store(CreateUserRequest $request) public function store(CreateUserRequest $request)
{ {
$data = array_merge($request->all(), ['permissions' => $this->permissions->clean($request->permissions)]); $data = array_merge($request->all(), ['permissions' => $this->permissions->clean($request->permissions)]);
$user = $this->users->create($data);
$user->roles()->attach($request->roles);
$code = Activation::create($user); $this->user->createWithRoles($data, $request->roles);
Activation::complete($user, $code);
Flash::success('User created.'); Flash::success('User created.');
return Redirect::route('dashboard.user.index'); return Redirect::route('dashboard.user.index');
} }
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
}
/** /**
* Show the form for editing the specified resource. * Show the form for editing the specified resource.
* *
...@@ -98,11 +86,11 @@ class UserController extends AdminBaseController ...@@ -98,11 +86,11 @@ class UserController extends AdminBaseController
*/ */
public function edit($id) public function edit($id)
{ {
if (!$user = $this->users->createModel()->find($id)) { if (!$user = $this->user->find($id)) {
Flash::error('User not found'); Flash::error('User not found');
return Redirect::route('dashboard.user.index'); return Redirect::route('dashboard.user.index');
} }
$roles = $this->roles->all(); $roles = $this->role->all();
return View::make('user::admin.users.edit', compact('user', 'roles')); return View::make('user::admin.users.edit', compact('user', 'roles'));
} }
...@@ -116,11 +104,9 @@ class UserController extends AdminBaseController ...@@ -116,11 +104,9 @@ class UserController extends AdminBaseController
*/ */
public function update($id, UpdateUserRequest $request) public function update($id, UpdateUserRequest $request)
{ {
$user = $this->users->createModel()->find($id);
$data = array_merge($request->all(), ['permissions' => $this->permissions->clean($request->permissions)]); $data = array_merge($request->all(), ['permissions' => $this->permissions->clean($request->permissions)]);
$this->users->update($user, $data);
$user->roles()->sync($request->roles); $this->user->updateAndSyncRoles($id, $data, $request->roles);
Flash::success('User updated.'); Flash::success('User updated.');
return Redirect::route('dashboard.user.index'); return Redirect::route('dashboard.user.index');
...@@ -134,17 +120,9 @@ class UserController extends AdminBaseController ...@@ -134,17 +120,9 @@ class UserController extends AdminBaseController
*/ */
public function destroy($id) public function destroy($id)
{ {
if ($user = $this->users->createModel()->find($id)) $this->user->delete($id);
{
$user->delete();
Flash::success('User deleted');
return Redirect::to('users');
}
Flash::error('User not found');
Flash::success('User deleted');
return Redirect::to('users'); return Redirect::to('users');
} }
......
<?php namespace Modules\User\Providers;
use Illuminate\Routing\FilterServiceProvider as ServiceProvider;
class UserFiltersServiceProvider extends ServiceProvider
{
protected $filters = [
'permissions' => 'Modules\User\Http\Filters\PermissionFilter',
];
}
\ No newline at end of file
<?php namespace Modules\Users\Providers; <?php namespace Modules\User\Providers;
use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
class UsersServiceProvider extends ServiceProvider { class UsersServiceProvider extends ServiceProvider
{
/** /**
* Indicates if loading of the provider is deferred. * Indicates if loading of the provider is deferred.
* *
* @var bool * @var bool
*/ */
protected $defer = false; protected $defer = false;
/** /**
* Register the service provider. * The filters base class name.
* *
* @return void * @var array
*/ */
public function register() protected $filters = [
{ 'permissions' => 'PermissionFilter'
// ];
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->booted(function ($app) {
$this->registerFilters($app['router']);
$this->registerBindings();
});
}
/** /**
* Get the services provided by the provider. * Get the services provided by the provider.
* *
* @return array * @return array
*/ */
public function provides() public function provides()
{
return array();
}
/**
* Register the filters.
*
* @param Router $router
* @return void
*/
public function registerFilters(Router $router)
{
foreach ($this->filters as $name => $filter) {
$class = 'Modules\\User\\Http\\Filters\\' . $filter;
$router->filter($name, $class);
}
}
private function registerBindings()
{ {
return array(); $this->app->bind(
'Modules\User\Repositories\UserRepository',
'Modules\User\Repositories\Sentinel\SentinelUserRepository'
);
$this->app->bind(
'Modules\User\Repositories\RoleRepository',
'Modules\User\Repositories\Sentinel\SentinelRoleRepository'
);
} }
} }
<?php namespace Modules\User\Repositories;
interface RoleRepository
{
/**
* Return all the roles
* @return mixed
*/
public function all();
/**
* Create a role resource
* @return mixed
*/
public function create($data);
}
\ No newline at end of file
<?php namespace Modules\User\Repositories\Sentinel;
use Cartalyst\Sentinel\Laravel\Facades\Sentinel;
use Modules\User\Repositories\RoleRepository;
class SentinelRoleRepository implements RoleRepository
{
/**
* @var \Cartalyst\Sentinel\Roles\EloquentRole
*/
protected $role;
public function __construct()
{
$this->role = Sentinel::getRoleRepository()->createModel();
}
/**
* Return all the roles
* @return mixed
*/
public function all()
{
return $this->role->all();
}
/**
* Create a role resource
* @return mixed
*/
public function create($data)
{
// TODO: Implement create() method.
}
}
\ No newline at end of file
<?php namespace Modules\User\Repositories\Sentinel;
use Cartalyst\Sentinel\Laravel\Facades\Activation;
use Cartalyst\Sentinel\Laravel\Facades\Sentinel;
use Modules\Session\Exceptions\UserNotFoundException;
use Modules\User\Repositories\UserRepository;
class SentinelUserRepository implements UserRepository
{
/**
* @var \Modules\Session\Entities\User
*/
protected $user;
/**
* @var \Cartalyst\Sentinel\Roles\EloquentRole
*/
protected $role;
public function __construct()
{
$this->user = Sentinel::getUserRepository()->createModel();
$this->role = Sentinel::getRoleRepository()->createModel();
}
/**
* Returns all the users
* @return object
*/
public function all()
{
return $this->user->all();
}
/**
* Create a user resource
* @return mixed
*/
public function create($data)
{
return $this->user->create($data);
}
/**
* Create a user and assign roles to it
* @param array $data
* @param array $roles
* @return void
*/
public function createWithRoles($data, $roles)
{
$user = $this->user->create($data);
$user->roles()->attach($roles);
$code = Activation::create($user);
Activation::complete($user, $code);
}
/**
* Find a user by its ID
* @param $id
* @return mixed
*/
public function find($id)
{
return $this->user->find($id);
}
/**
* Update a user
* @param $user
* @param $data
* @return mixed
*/
public function update($user, $data)
{
return $this->user->update($user, $data);
}
/**
* @param $userId
* @param $data
* @param $roles
* @internal param $user
* @return mixed
*/
public function updateAndSyncRoles($userId, $data, $roles)
{
$user = $this->user->find($userId);
$user = $user->fill($data);
$user->save();
$user->roles()->sync($roles);
}
/**
* Deletes a user
* @param $id
* @throws UserNotFoundException
* @return mixed
*/
public function delete($id)
{
if ($user = $this->user->find($id)) {
return $user->delete();
};
throw new UserNotFoundException;
}
}
\ No newline at end of file
<?php namespace Modules\User\Repositories;
/**
* Interface UserRepository
* @package Modules\User\Repositories
*/
interface UserRepository
{
/**
* Returns all the users
* @return object
*/
public function all();
/**
* Create a user resource
* @return mixed
*/
public function create($data);
/**
* Create a user and assign roles to it
* @param array $data
* @param array $roles
* @return void
*/
public function createWithRoles($data, $roles);
/**
* Find a user by its ID
* @param $id
* @return mixed
*/
public function find($id);
/**
* Update a user
* @param $user
* @param $data
* @return mixed
*/
public function update($user, $data);
/**
* Update a user and sync its roles
* @param $user
* @param $data
* @param $roles
* @return mixed
*/
public function updateAndSyncRoles($user, $data, $roles);
/**
* Deletes a user
* @param $id
* @return mixed
*/
public function delete($id);
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment