Adding additional api routes and implementing their api controlleur

parent b6ce3745
<?php
namespace Modules\User\Http\Controllers\Api;
use Illuminate\Routing\Controller;
use Modules\User\Permissions\PermissionManager;
class PermissionsController extends Controller
{
/**
* @var PermissionManager
*/
private $permissionManager;
public function __construct(PermissionManager $permissionManager)
{
$this->permissionManager = $permissionManager;
}
public function index()
{
return response()->json([
'permissions' => $this->permissionManager->all(),
]);
}
}
...@@ -2,9 +2,14 @@ ...@@ -2,9 +2,14 @@
namespace Modules\User\Http\Controllers\Api; namespace Modules\User\Http\Controllers\Api;
use Cartalyst\Sentinel\Roles\EloquentRole;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Routing\Controller; use Illuminate\Routing\Controller;
use Modules\User\Http\Requests\CreateRoleRequest;
use Modules\User\Http\Requests\UpdateRoleRequest;
use Modules\User\Permissions\PermissionManager;
use Modules\User\Repositories\RoleRepository; use Modules\User\Repositories\RoleRepository;
use Modules\User\Transformers\FullRoleTransformer;
use Modules\User\Transformers\RoleTransformer; use Modules\User\Transformers\RoleTransformer;
class RoleController extends Controller class RoleController extends Controller
...@@ -13,14 +18,74 @@ class RoleController extends Controller ...@@ -13,14 +18,74 @@ class RoleController extends Controller
* @var RoleRepository * @var RoleRepository
*/ */
private $role; private $role;
/**
* @var PermissionManager
*/
private $permissions;
public function __construct(RoleRepository $role) public function __construct(RoleRepository $role, PermissionManager $permissions)
{ {
$this->role = $role; $this->role = $role;
$this->permissions = $permissions;
} }
public function index(Request $request) public function index(Request $request)
{ {
return RoleTransformer::collection($this->role->serverPaginationFilteringFor($request)); return RoleTransformer::collection($this->role->serverPaginationFilteringFor($request));
} }
public function find(EloquentRole $role)
{
return new FullRoleTransformer($role->load('users'));
}
public function findNew(EloquentRole $role)
{
return new FullRoleTransformer($role);
}
public function store(CreateRoleRequest $request)
{
$data = $this->mergeRequestWithPermissions($request);
$this->role->create($data);
return response()->json([
'errors' => false,
'message' => trans('user::messages.role created'),
]);
}
public function update(EloquentRole $role, UpdateRoleRequest $request)
{
$data = $this->mergeRequestWithPermissions($request);
$this->role->update($role->id, $data);
return response()->json([
'errors' => false,
'message' => trans('user::messages.role updated'),
]);
}
public function destroy(EloquentRole $role)
{
$this->role->delete($role->id);
return response()->json([
'errors' => false,
'message' => trans('user::messages.role deleted'),
]);
}
/**
* @param Request $request
* @return array
*/
private function mergeRequestWithPermissions(Request $request)
{
$permissions = $this->permissions->clean($request->get('permissions'));
return array_merge($request->all(), ['permissions' => $permissions]);
}
} }
...@@ -18,20 +18,30 @@ $router->group(['prefix' => '/user', 'middleware' => ['api.token', 'auth.admin'] ...@@ -18,20 +18,30 @@ $router->group(['prefix' => '/user', 'middleware' => ['api.token', 'auth.admin']
'uses' => 'RoleController@store', 'uses' => 'RoleController@store',
'middleware' => 'token-can:user.roles.create', 'middleware' => 'token-can:user.roles.create',
]); ]);
$router->post('roles/{role}', [ $router->post('roles/find/{role}', [
'as' => 'api.user.role.find', 'as' => 'api.user.role.find',
'uses' => 'RoleController@find', 'uses' => 'RoleController@find',
'middleware' => 'token-can:user.roles.edit', 'middleware' => 'token-can:user.roles.edit',
]); ]);
$router->post('roles/find-new', [
'as' => 'api.user.role.find-new',
'uses' => 'RoleController@findNew',
'middleware' => 'token-can:user.roles.edit',
]);
$router->post('roles/{role}/edit', [ $router->post('roles/{role}/edit', [
'as' => 'api.user.role.update', 'as' => 'api.user.role.update',
'uses' => 'RoleController@update', 'uses' => 'RoleController@update',
'middleware' => 'token-can:user.roles.edit', 'middleware' => 'token-can:user.roles.edit',
]); ]);
$router->delete('roles/{role}', [ $router->delete('roles/{role}', [
'as' => 'api.user.role.destroy', 'as' => 'api.user.role.destroy',
'uses' => 'RoleController@destroy', 'uses' => 'RoleController@destroy',
'middleware' => 'token-can:user.roles.destroy', 'middleware' => 'token-can:user.roles.destroy',
]); ]);
$router->get('permissions', [
'as' => 'api.user.permissions.index',
'uses' => 'PermissionsController@index',
'middleware' => 'token-can:user.roles.index',
]);
}); });
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