72 lines
1.8 KiB
PHP
72 lines
1.8 KiB
PHP
|
<?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;
|
||
|
|
||
|
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(),
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @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(),
|
||
|
]
|
||
|
]);
|
||
|
}
|
||
|
}
|