Skip to content

Commit 05ddd4c

Browse files
committed
Add: auth user data retrieval
1 parent 48b17d4 commit 05ddd4c

File tree

4 files changed

+74
-24
lines changed

4 files changed

+74
-24
lines changed

assets/vue/layouts/AdminLayout.vue

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
<!-- User dropdown -->
3030
<div class="flex items-center gap-3 pl-2 group cursor-pointer">
3131
<div class="flex flex-col items-end hidden sm:flex">
32-
<span class="text-sm font-bold text-slate-800 leading-none">Admin User</span>
33-
<span class="text-[10px] text-slate-500 mt-0.5">Administrator</span>
32+
<span class="text-sm font-bold text-slate-800 leading-none">{{ adminData.login_name || 'Admin User' }}</span>
33+
<span class="text-[10px] text-slate-500 mt-0.5">{{ adminData.super_user ? 'Super Admin' : 'Administrator' }}</span>
3434
</div>
3535

3636
<BaseIcon name="chevronDown" />
@@ -49,6 +49,24 @@
4949
/* No imports required */
5050
import BaseIcon from "../components/base/BaseIcon.vue";
5151
import { useSidebar } from "../composables/useSidebar";
52+
import { onMounted, ref } from "vue";
5253
5354
const { openSidebar } = useSidebar();
55+
const adminData = ref({});
56+
57+
onMounted(async () => {
58+
try {
59+
const response = await fetch('/admin-about', {
60+
headers: {
61+
'Accept': 'application/json',
62+
'X-Requested-With': 'XMLHttpRequest'
63+
}
64+
});
65+
if (response.ok) {
66+
adminData.value = await response.json();
67+
}
68+
} catch (error) {
69+
console.error('Failed to fetch admin data:', error);
70+
}
71+
});
5472
</script>

config/services.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,10 @@ services:
2727
PhpList\RestApiClient\Client:
2828
$baseUrl: '%api_base_url%'
2929

30-
PhpList\RestApiClient\Endpoint\AuthClient:
31-
autoconfigure: true
30+
PhpList\RestApiClient\Endpoint\:
31+
resource: '../vendor/tatevikgr/rest-api-client/src/Endpoint/'
3232
autowire: true
33-
34-
PhpList\RestApiClient\Endpoint\SubscribersClient:
3533
autoconfigure: true
36-
autowire: true
3734

3835
PhpList\WebFrontend\EventListener\ApiSessionListener:
3936
tags:

src/Controller/AuthController.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@
88
use GuzzleHttp\Exception\GuzzleException;
99
use PhpList\RestApiClient\Endpoint\AuthClient;
1010
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11+
use Symfony\Component\HttpFoundation\JsonResponse;
1112
use Symfony\Component\HttpFoundation\Request;
1213
use Symfony\Component\HttpFoundation\Response;
1314
use Symfony\Component\Routing\Attribute\Route;
1415

1516
class AuthController extends AbstractController
1617
{
17-
private AuthClient $apiClient;
18-
19-
public function __construct(AuthClient $apiClient)
18+
public function __construct(private readonly AuthClient $authClient)
2019
{
21-
$this->apiClient = $apiClient;
2220
}
2321

2422
#[Route('/login', name: 'login', methods: ['GET', 'POST'])]
@@ -46,10 +44,10 @@ public function login(Request $request): Response
4644
}
4745

4846
try {
49-
$authData = $this->apiClient->login($username, $password);
47+
$authData = $this->authClient->login($username, $password);
5048
$request->getSession()->set('auth_token', $authData['key']);
5149
$request->getSession()->set('auth_expiry_date', $authData['key']);
52-
$request->getSession()->set('auth_id', $authData['id']);
50+
$request->getSession()->set('auth_id', (int) $authData['id']);
5351

5452
return $this->redirectToRoute('home');
5553
} catch (Exception $e) {
@@ -69,8 +67,14 @@ public function logout(Request $request): Response
6967
{
7068
$request->getSession()->remove('auth_token');
7169
$request->getSession()->remove('auth_id');
72-
$this->apiClient->logout();
70+
$this->authClient->logout();
7371

7472
return $this->redirectToRoute('login');
7573
}
74+
75+
#[Route('/admin-about', name: 'admin_about')]
76+
public function about(): JsonResponse
77+
{
78+
return new JsonResponse($this->authClient->getSessionUser()->toArray());
79+
}
7680
}

tests/Unit/Controller/AuthControllerTest.php

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,29 @@
44

55
namespace PhpList\WebFrontend\Tests\Unit\Controller;
66

7+
use PhpList\RestApiClient\Entity\Administrator;
78
use PhpList\WebFrontend\Controller\AuthController;
89
use PhpList\RestApiClient\Endpoint\AuthClient;
910
use PHPUnit\Framework\MockObject\MockObject;
1011
use PHPUnit\Framework\TestCase;
1112
use RuntimeException;
13+
use Symfony\Component\HttpFoundation\JsonResponse;
1214
use Symfony\Component\HttpFoundation\RedirectResponse;
1315
use Symfony\Component\HttpFoundation\Request;
1416
use Symfony\Component\HttpFoundation\Response;
1517
use Symfony\Component\HttpFoundation\Session\SessionInterface;
1618

1719
class AuthControllerTest extends TestCase
1820
{
19-
private AuthClient&MockObject $apiClient;
21+
private AuthClient&MockObject $authClient;
2022
private AuthController $controller;
2123

2224
protected function setUp(): void
2325
{
24-
$this->apiClient = $this->createMock(AuthClient::class);
26+
$this->authClient = $this->createMock(AuthClient::class);
2527

2628
$this->controller = $this->getMockBuilder(AuthController::class)
27-
->setConstructorArgs([$this->apiClient])
29+
->setConstructorArgs([$this->authClient])
2830
->onlyMethods(['render', 'redirectToRoute', 'generateUrl'])
2931
->getMock();
3032

@@ -105,11 +107,12 @@ public function testLoginWithPostRequestSuccess(): void
105107
['login_error', false]
106108
]);
107109

108-
$session->expects($this->exactly(2))
110+
$session->expects($this->exactly(3))
109111
->method('set')
110112
->withConsecutive(
111113
['auth_token', 'test-token'],
112-
['auth_expiry_date', 'test-token']
114+
['auth_expiry_date', 'test-token'],
115+
['auth_id', 1]
113116
);
114117

115118
$request = Request::create('/login', 'POST', [
@@ -118,9 +121,9 @@ public function testLoginWithPostRequestSuccess(): void
118121
]);
119122
$request->setSession($session);
120123

121-
$this->apiClient->method('login')
124+
$this->authClient->method('login')
122125
->with('testuser', 'testpass')
123-
->willReturn(['key' => 'test-token']);
126+
->willReturn(['key' => 'test-token', 'id' => 1]);
124127

125128
$response = $this->controller->login($request);
126129

@@ -143,7 +146,7 @@ public function testLoginWithPostRequestFailure(): void
143146
]);
144147
$request->setSession($session);
145148

146-
$this->apiClient->method('login')
149+
$this->authClient->method('login')
147150
->with('testuser', 'testpass')
148151
->willThrowException(new RuntimeException('Invalid credentials'));
149152

@@ -178,9 +181,12 @@ public function testLoginWithExistingSession(): void
178181
public function testLogout(): void
179182
{
180183
$session = $this->createMock(SessionInterface::class);
181-
$session->expects($this->once())
184+
$session->expects($this->exactly(2))
182185
->method('remove')
183-
->with('auth_token');
186+
->withConsecutive(
187+
['auth_token'],
188+
['auth_id']
189+
);
184190

185191
$request = $this->createMock(Request::class);
186192
$request->method('getSession')
@@ -191,4 +197,29 @@ public function testLogout(): void
191197
$this->assertInstanceOf(RedirectResponse::class, $response);
192198
$this->assertStringContainsString('login', $response->getTargetUrl());
193199
}
200+
201+
public function testAbout(): void
202+
{
203+
$adminMock = $this->createMock(Administrator::class);
204+
$adminMock->method('toArray')
205+
->willReturn([
206+
'id' => 123,
207+
'login_name' => 'testadmin',
208+
'email' => 'admin@example.com',
209+
'super_user' => true
210+
]);
211+
212+
$this->authClient->expects($this->once())
213+
->method('getSessionUser')
214+
->willReturn($adminMock);
215+
216+
$response = $this->controller->about();
217+
218+
$this->assertInstanceOf(JsonResponse::class, $response);
219+
$this->assertEquals(200, $response->getStatusCode());
220+
$this->assertEquals(
221+
'{"id":123,"login_name":"testadmin","email":"admin@example.com","super_user":true}',
222+
$response->getContent()
223+
);
224+
}
194225
}

0 commit comments

Comments
 (0)