|
| 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 | +} |
0 commit comments