2020-02-17 22:28:57 +01:00
|
|
|
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
use App\Entity\User;
|
|
|
|
use App\Http\DataResponse;
|
|
|
|
use App\Repository\UserRepository;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
|
2020-02-19 16:28:29 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
|
2020-02-17 22:28:57 +01:00
|
|
|
|
|
|
|
class UserController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @Route("/api/v1/me", name="api_v1_users_me", methods={"GET"})
|
|
|
|
* @IsGranted("ROLE_USER")
|
|
|
|
*/
|
|
|
|
public function showCurrentUser()
|
|
|
|
{
|
|
|
|
/** @var User */
|
|
|
|
$user = $this->getUser();
|
|
|
|
|
|
|
|
return new DataResponse([
|
|
|
|
'username' => $user->getUsername(),
|
|
|
|
'roles' => $user->getRoles(),
|
2020-02-19 14:50:26 +01:00
|
|
|
'projects' => $user->getProjects()->map(function($project) {
|
|
|
|
return [
|
|
|
|
'id' => $project->getId(),
|
|
|
|
'name' => $project->getName(),
|
|
|
|
];
|
|
|
|
})->toArray(),
|
|
|
|
'requests' => $user->getRequests()->map(function($request) {
|
|
|
|
return [
|
|
|
|
'id' => $request->getId(),
|
|
|
|
'title' => $request->getTitle(),
|
|
|
|
];
|
|
|
|
})->toArray(),
|
2020-02-17 22:28:57 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Route("/api/v1/users", name="api_v1_list_users", methods={"GET"})
|
|
|
|
* @IsGranted("ROLE_DEVELOPER")
|
|
|
|
*/
|
|
|
|
public function listUsers()
|
|
|
|
{
|
|
|
|
/** @var array */
|
|
|
|
$users = $this->getDoctrine()
|
|
|
|
->getRepository(User::class)
|
|
|
|
->findAll()
|
|
|
|
;
|
|
|
|
|
|
|
|
$results = [];
|
|
|
|
foreach($users as $u) {
|
|
|
|
$results[] = [
|
|
|
|
'id' => $u->getId(),
|
|
|
|
'username' => $u->getUsername(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return new DataResponse([
|
|
|
|
'users' => $results,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Route("/api/v1/users/{userId}", name="api_v1_get_user", methods={"GET"}, requirements={"userId"="\d+"})
|
|
|
|
* @IsGranted("ROLE_DEVELOPER")
|
|
|
|
*/
|
|
|
|
public function showUser($userId)
|
|
|
|
{
|
|
|
|
/** @var User */
|
|
|
|
$user = $this->getDoctrine()
|
|
|
|
->getRepository(User::class)
|
|
|
|
->find($userId)
|
|
|
|
;
|
|
|
|
|
|
|
|
return new DataResponse([
|
|
|
|
'user' => [
|
|
|
|
'id' => $user->getId(),
|
|
|
|
'username' => $user->getUsername(),
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
}
|
2020-02-19 16:28:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @Route("/api/v1/users", name="api_v1_create_user", methods={"POST"})
|
|
|
|
* @IsGranted("ROLE_DEVELOPER")
|
|
|
|
*/
|
|
|
|
public function createUser(Request $request, UserPasswordEncoderInterface $passwordEncoder)
|
|
|
|
{
|
|
|
|
|
|
|
|
$content = $request->getContent();
|
|
|
|
$projectData = json_decode($content, true);
|
|
|
|
|
|
|
|
$newUser = new User();
|
|
|
|
|
|
|
|
if (!isset($projectData['username'])) {
|
|
|
|
return new ErrorResponse(0, "You must provide a username");
|
|
|
|
}
|
|
|
|
$newUser->setUsername($projectData['username']);
|
|
|
|
|
|
|
|
if (!isset($projectData['password'])) {
|
|
|
|
return new ErrorResponse(1, "You must provide a password");
|
|
|
|
}
|
|
|
|
$newUser->setPassword($passwordEncoder->encodePassword(
|
|
|
|
$newUser,
|
|
|
|
$projectData['password']
|
|
|
|
));
|
|
|
|
|
|
|
|
$manager = $this->getDoctrine()->getManager();
|
|
|
|
$manager->persist($newUser);
|
|
|
|
$manager->flush();
|
|
|
|
|
|
|
|
return new DataResponse([
|
|
|
|
'user' => $newUser->toArray(),
|
|
|
|
]);
|
|
|
|
}
|
2020-02-17 22:28:57 +01:00
|
|
|
}
|