Skip to content

Commit 9ffa06b

Browse files
authored
Merge pull request #185 from kenjis/feat-toolbar
feat: add debug toolbar
2 parents 9741258 + 22cb624 commit 9ffa06b

File tree

4 files changed

+187
-5
lines changed

4 files changed

+187
-5
lines changed

src/Collectors/Auth.php

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

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: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
private Auth $collector;
23+
24+
protected function setUp(): void
25+
{
26+
parent::setUp();
27+
28+
$this->user = fake(UserModel::class, ['username' => 'John Smith']);
29+
30+
$this->collector = new Auth();
31+
}
32+
33+
public function testDisplayNotLoggedIn()
34+
{
35+
$output = $this->collector->display();
36+
37+
$this->assertStringContainsString('Not logged in', $output);
38+
}
39+
40+
public function testtestDisplayLoggedIn()
41+
{
42+
/** @var Session $authenticator */
43+
$authenticator = service('auth')->getAuthenticator();
44+
$authenticator->login($this->user);
45+
$this->user->addGroup('admin', 'beta');
46+
47+
$output = $this->collector->display();
48+
49+
$this->assertStringContainsString('Current Use', $output);
50+
$this->assertStringContainsString('<td>Username</td><td>John Smith</td>', $output);
51+
$this->assertStringContainsString('<td>Groups</td><td>admin, beta</td>', $output);
52+
}
53+
54+
public function testGetTitleDetails()
55+
{
56+
$output = $this->collector->getTitleDetails();
57+
58+
$this->assertStringContainsString(Session::class, $output);
59+
}
60+
61+
public function testGetBadgeValueReturnsUserId()
62+
{
63+
/** @var Session $authenticator */
64+
$authenticator = service('auth')->getAuthenticator();
65+
$authenticator->login($this->user);
66+
67+
$output = (string) $this->collector->getBadgeValue();
68+
69+
$this->assertStringContainsString('1', $output);
70+
}
71+
}

tests/_support/FakeUser.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@
77

88
trait FakeUser
99
{
10-
/**
11-
* @var User
12-
*/
13-
private $user;
10+
private User $user;
1411

15-
protected function setUpFakeUser()
12+
protected function setUpFakeUser(): void
1613
{
1714
$this->user = fake(UserModel::class);
1815
}

0 commit comments

Comments
 (0)