Skip to content

Commit 013a73e

Browse files
committed
feat: add collector for Authenticated User
1 parent cb7e0c0 commit 013a73e

3 files changed

Lines changed: 165 additions & 0 deletions

File tree

src/Collectors/Auth.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
namespace CodeIgniter\Shield\Collectors;
4+
5+
use CodeIgniter\Debug\Toolbar\Collectors\BaseCollector;
6+
7+
/**
8+
* Debug Toolbar Collector for Auth
9+
*/
10+
class Auth extends BaseCollector
11+
{
12+
/**
13+
* Whether this collector has data that can
14+
* be displayed in the Timeline.
15+
*
16+
* @var bool
17+
*/
18+
protected $hasTimeline = false;
19+
20+
/**
21+
* Whether this collector needs to display
22+
* content in a tab or not.
23+
*
24+
* @var bool
25+
*/
26+
protected $hasTabContent = true;
27+
28+
/**
29+
* Whether this collector has data that
30+
* should be shown in the Vars tab.
31+
*
32+
* @var bool
33+
*/
34+
protected $hasVarData = false;
35+
36+
/**
37+
* The 'title' of this Collector.
38+
* Used to name things in the toolbar HTML.
39+
*
40+
* @var string
41+
*/
42+
protected $title = 'Auth';
43+
44+
//--------------------------------------------------------------------
45+
46+
/**
47+
* Returns any information that should be shown next to the title.
48+
*/
49+
public function getTitleDetails(): string
50+
{
51+
return get_class(service('auth'));
52+
}
53+
54+
/**
55+
* Returns the data of this collector to be formatted in the toolbar
56+
*/
57+
public function display(): string
58+
{
59+
/** @var \CodeIgniter\Shield\Auth $auth */
60+
$auth = service('auth');
61+
62+
if ($auth->loggedIn()) {
63+
$user = $auth->user();
64+
$groups = $user->getGroups();
65+
66+
$groupsForUser = implode(', ', $groups);
67+
68+
$html = '<h3>Current User</h3>';
69+
$html .= '<table><tbody>';
70+
$html .= "<tr><td style='width:150px;'>User ID</td><td>#{$user->id}</td></tr>";
71+
$html .= "<tr><td>Username</td><td>{$user->username}</td></tr>";
72+
$html .= "<tr><td>Email</td><td>{$user->email}</td></tr>";
73+
$html .= "<tr><td>Groups</td><td>{$groupsForUser}</td></tr>";
74+
$html .= '</tbody></table>';
75+
} else {
76+
$html = '<p>Not logged in.</p>';
77+
}
78+
79+
return $html;
80+
}
81+
82+
/**
83+
* Gets the "badge" value for the button.
84+
*
85+
* @return int|null ID of the current User, or null when not logged in
86+
*/
87+
public function getBadgeValue(): ?int
88+
{
89+
return service('auth')->loggedIn() ? service('auth')->id() : null;
90+
}
91+
92+
/**
93+
* Display the icon.
94+
*
95+
* Icon from https://icons8.com - 1em package
96+
*/
97+
public function icon(): string
98+
{
99+
return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAADLSURBVEhL5ZRLCsIwGAa7UkE9gd5HUfEoekxxJx7AhXoCca/fhESkJiQxBHwMDG3S/9EmJc0n0JMruZVXK/fMdWQRY7mXt4A7OZJvwZu74hRayIEc2nv3jGtXZrOWrnifiRY0OkhiWK5sWGeS52bkZymJ2ZhRJmwmySxLCL6CmIsZZUIixkiNezCRR+kSUyWH3Cgn6SuQIk2iuOBckvN+t8FMnq1TJloUN3jefN9mhvJeCAVWb8CyUDj0vxc3iPFHDaofFdUPu2+iae7nYJMCY/1bpAAAAABJRU5ErkJggg==';
100+
}
101+
}

src/Config/Registrar.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CodeIgniter\Shield\Config;
44

55
use CodeIgniter\Shield\Authentication\Passwords\ValidationRules as PasswordRules;
6+
use CodeIgniter\Shield\Collectors\Auth;
67
use CodeIgniter\Shield\Filters\AuthRates;
78
use CodeIgniter\Shield\Filters\ChainAuth;
89
use CodeIgniter\Shield\Filters\SessionAuth;
@@ -37,4 +38,13 @@ public static function Validation(): array
3738
],
3839
];
3940
}
41+
42+
public static function Toolbar(): array
43+
{
44+
return [
45+
'collectors' => [
46+
Auth::class,
47+
],
48+
];
49+
}
4050
}

tests/Collectors/AuthTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Tests\Collectors;
4+
5+
use CodeIgniter\Shield\Authentication\Authenticators\Session;
6+
use CodeIgniter\Shield\Collectors\Auth;
7+
use CodeIgniter\Shield\Entities\User;
8+
use CodeIgniter\Shield\Models\UserModel;
9+
use CodeIgniter\Test\DatabaseTestTrait;
10+
use Tests\Support\TestCase;
11+
12+
/**
13+
* @internal
14+
*/
15+
final class AuthTest extends TestCase
16+
{
17+
use DatabaseTestTrait;
18+
19+
protected $namespace;
20+
protected $refresh = true;
21+
private User $user;
22+
23+
protected function setUp(): void
24+
{
25+
parent::setUp();
26+
27+
$this->user = fake(UserModel::class, ['username' => 'John Smith']);
28+
}
29+
30+
public function testDisplayNotLoggedIn()
31+
{
32+
$collector = new Auth();
33+
34+
$output = $collector->display();
35+
36+
$this->assertStringContainsString('Not logged in', $output);
37+
}
38+
39+
public function testtestDisplayLoggedIn()
40+
{
41+
/** @var Session $authenticator */
42+
$authenticator = service('auth')->getAuthenticator();
43+
$authenticator->login($this->user);
44+
$this->user->addGroup('admin', 'beta');
45+
46+
$collector = new Auth();
47+
48+
$output = $collector->display();
49+
50+
$this->assertStringContainsString('Current Use', $output);
51+
$this->assertStringContainsString('<td>Username</td><td>John Smith</td>', $output);
52+
$this->assertStringContainsString('<td>Groups</td><td>admin, beta</td>', $output);
53+
}
54+
}

0 commit comments

Comments
 (0)