Skip to content

Commit

Permalink
Update User Api
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Oct 19, 2024
1 parent 262c6c9 commit 1afd920
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 31 deletions.
4 changes: 2 additions & 2 deletions app/src/Controller/Group/GroupApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function __construct(
public function __invoke(GroupInterface $group, Response $response): Response
{
$this->validateAccess($group);
$group = $this->mutateGroup($group);
$group = $this->handle($group);
$payload = json_encode($group, JSON_THROW_ON_ERROR);
$response->getBody()->write($payload);

Expand All @@ -61,7 +61,7 @@ public function __invoke(GroupInterface $group, Response $response): Response
*
* @return GroupInterface
*/
protected function mutateGroup(GroupInterface $group): GroupInterface
protected function handle(GroupInterface $group): GroupInterface
{
// Add the user count to the group object
$group->loadCount('users');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Views\Twig;
use UserFrosting\Sprinkle\Account\Authenticate\Authenticator;
use UserFrosting\Sprinkle\Account\Database\Models\Interfaces\UserInterface;
use UserFrosting\Sprinkle\Account\Exceptions\ForbiddenException;
use UserFrosting\Sprinkle\Core\I18n\SiteLocaleInterface;
use UserFrosting\Sprinkle\Core\Util\RouteParserInterface;

/**
* Renders a page displaying a user's information, in read-only mode.
Expand All @@ -31,19 +29,15 @@
* This page requires authentication.
* Request type: GET
*/
class UserPageAction
// TODO : Eventually this class could be moved to the Account sprinkle.
class UserApi
{
/** @var string Page template */
protected string $template = 'pages/user.html.twig';

/**
* Inject dependencies.
*/
public function __construct(
protected Authenticator $authenticator,
protected SiteLocaleInterface $siteLocale,
protected RouteParserInterface $routeParser,
protected Twig $view,
) {
}

Expand All @@ -56,20 +50,15 @@ public function __construct(
*/
public function __invoke(UserInterface $user, Response $response): Response
{
$payload = $this->handle($user);
$this->validateAccess($user);
$user = $this->handle($user);
$payload = json_encode($user, JSON_THROW_ON_ERROR);
$response->getBody()->write($payload);

// TODO : Turn into JSON API endpoint
return $this->view->render($response, $this->template, $payload);
return $response->withHeader('Content-Type', 'application/json');
}

/**
* Handle the request and return the payload.
*
* @param UserInterface $user
*
* @return mixed[]
*/
protected function handle(UserInterface $user): array
protected function validateAccess(UserInterface $user): void
{
// Access-controlled page
if (!$this->authenticator->checkAccess('uri_user', [
Expand All @@ -78,6 +67,8 @@ protected function handle(UserInterface $user): array
throw new ForbiddenException();
}

// Determine fields that currentUser is authorized to view
/*
// Determine fields that currentUser is authorized to view
$fieldNames = ['user_name', 'name', 'email', 'locale', 'group', 'roles'];
Expand Down Expand Up @@ -173,7 +164,19 @@ protected function handle(UserInterface $user): array
'fields' => $fields,
'tools' => $editButtons,
'widgets' => $widgets,
'delete_redirect' => $this->routeParser->urlFor('uri_users'),
];
];*/
}

/**
* Add or remove fields from the user object before returning it.
* TIP : When extending this class, you can use this method to add your own fields.
*
* @param UserInterface $user
*
* @return UserInterface
*/
protected function handle(UserInterface $user): UserInterface
{
return $user;
}
}
4 changes: 2 additions & 2 deletions app/src/Routes/UsersRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
use UserFrosting\Routes\RouteDefinitionInterface;
use UserFrosting\Sprinkle\Account\Authenticate\AuthGuard;
use UserFrosting\Sprinkle\Admin\Controller\User\UserActivitySprunje;
use UserFrosting\Sprinkle\Admin\Controller\User\UserApi as UserApi;
use UserFrosting\Sprinkle\Admin\Controller\User\UserCreateAction;
use UserFrosting\Sprinkle\Admin\Controller\User\UserDeleteAction;
use UserFrosting\Sprinkle\Admin\Controller\User\UserEditAction;
use UserFrosting\Sprinkle\Admin\Controller\User\UserPageAction;
use UserFrosting\Sprinkle\Admin\Controller\User\UserPasswordAction;
use UserFrosting\Sprinkle\Admin\Controller\User\UserPermissionSprunje;
use UserFrosting\Sprinkle\Admin\Controller\User\UserRoleSprunje;
Expand All @@ -37,7 +37,7 @@ class UsersRoutes implements RouteDefinitionInterface
public function register(App $app): void
{
$app->group('/api/users', function (RouteCollectorProxy $group) {
$group->get('/u/{user_name}', UserPageAction::class)
$group->get('/u/{user_name}', UserApi::class)
->add(UserInjector::class)
->setName('api_user');
$group->get('', UsersSprunje::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use UserFrosting\Sprinkle\Admin\Tests\AdminTestCase;
use UserFrosting\Sprinkle\Core\Testing\RefreshDatabase;

class UserPageActionTest extends AdminTestCase
class UserApiTest extends AdminTestCase
{
use RefreshDatabase;
use WithTestUser;
Expand Down Expand Up @@ -61,13 +61,13 @@ public function testPageForForbiddenException(): void
}

// TODO : Turn into JSON API endpoint
/*public function testPage(): void
public function testPage(): void
{
/** @var User * /
/** @var User */
$user = User::factory()->create();
$this->actAsUser($user, permissions: ['uri_user']);

/** @var Config * /
/** @var Config */
$config = $this->ci->get(Config::class);

// Force locale config.
Expand All @@ -80,6 +80,21 @@ public function testPageForForbiddenException(): void

// Assert response status & body
$this->assertResponseStatus(200, $response);
$this->assertNotEmpty((string) $response->getBody());
}*/
$this->assertJsonStructure([
'id',
'user_name',
'email',
'first_name',
'last_name',
'locale',
'group_id',
'flag_verified',
'flag_enabled',
'deleted_at',
'created_at',
'updated_at',
'full_name',
'avatar',
], $response);
}
}

0 comments on commit 1afd920

Please sign in to comment.