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