Skip to content

Commit

Permalink
Creating the Api user controller
Browse files Browse the repository at this point in the history
  • Loading branch information
nWidart committed Oct 12, 2017
1 parent 03e6af5 commit 423332d
Show file tree
Hide file tree
Showing 3 changed files with 19,462 additions and 17,716 deletions.
105 changes: 105 additions & 0 deletions Modules/User/Http/Controllers/Api/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace Modules\User\Http\Controllers\Api;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\User\Contracts\Authentication;
use Modules\User\Entities\Sentinel\User;
use Modules\User\Events\UserHasBegunResetProcess;
use Modules\User\Http\Requests\CreateUserRequest;
use Modules\User\Http\Requests\UpdateUserRequest;
use Modules\User\Permissions\PermissionManager;
use Modules\User\Repositories\UserRepository;
use Modules\User\Transformers\FullUserTransformer;
use Modules\User\Transformers\UserTransformer;

class UserController extends Controller
{
/**
* @var UserRepository
*/
private $user;
/**
* @var PermissionManager
*/
private $permissions;

public function __construct(UserRepository $user, PermissionManager $permissions)
{
$this->user = $user;
$this->permissions = $permissions;
}

public function index(Request $request)
{
return UserTransformer::collection($this->user->serverPaginationFilteringFor($request));
}

public function find(User $user)
{
return new FullUserTransformer($user->load('roles'));
}

public function findNew(User $user)
{
return (new FullUserTransformer($user))->additional(['data' => ['is_new' => true]]);
}

public function store(CreateUserRequest $request)
{
$data = $this->mergeRequestWithPermissions($request);

$this->user->createWithRoles($data, $request->get('roles'), $request->get('is_activated'));

return response()->json([
'errors' => false,
'message' => trans('user::messages.user created'),
]);
}

public function update(User $user, UpdateUserRequest $request)
{
$data = $this->mergeRequestWithPermissions($request);

$this->user->updateAndSyncRoles($user->id, $data, $request->get('roles'));

return response()->json([
'errors' => false,
'message' => trans('user::messages.user updated'),
]);
}

public function destroy(User $user)
{
$this->user->delete($user->id);

return response()->json([
'errors' => false,
'message' => trans('user::messages.user deleted'),
]);
}

public function sendResetPassword(User $user, Authentication $auth)
{
$code = $auth->createReminderCode($user);

event(new UserHasBegunResetProcess($user, $code));

return response()->json([
'errors' => false,
'message' => trans('user::auth.reset password email was sent'),
]);
}

/**
* @param Request $request
* @return array
*/
private function mergeRequestWithPermissions(Request $request) : array
{
$permissions = $this->permissions->clean($request->get('permissions'));

return array_merge($request->all(), ['permissions' => $permissions]);
}
}
5 changes: 5 additions & 0 deletions Modules/User/Http/apiRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@
'uses' => 'UserController@update',
'middleware' => 'token-can:user.users.edit',
]);
$router->get('{user}/sendResetPassword', [
'as' => 'api.user.user.sendResetPassword',
'uses' => 'UserController@sendResetPassword',
'middleware' => 'token-can:user.users.edit',
]);
$router->delete('{user}', [
'as' => 'api.user.user.destroy',
'uses' => 'UserController@destroy',
Expand Down
Loading

0 comments on commit 423332d

Please sign in to comment.