Elgg  Version master
UserHover.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Menus;
4 
6 
13 class UserHover {
14 
22  public static function registerAvatarEdit(\Elgg\Event $event) {
23  $user = $event->getEntityParam();
24  if (!$user instanceof \ElggUser || !$user->isEnabled() || !$user->canEdit()) {
25  return;
26  }
27 
28  /* @var $return MenuItems */
29  $return = $event->getValue();
30 
31  $return[] = \ElggMenuItem::factory([
32  'name' => 'avatar:edit',
33  'icon' => 'image',
34  'text' => elgg_echo('avatar:edit'),
35  'href' => elgg_generate_entity_url($user, 'edit', 'avatar'),
36  'section' => (elgg_get_logged_in_user_guid() == $user->guid) ? 'action' : 'admin',
37  ]);
38 
39  return $return;
40  }
41 
49  public static function registerAdminActions(\Elgg\Event $event) {
50  $user = $event->getEntityParam();
51  if (!$user instanceof \ElggUser || !elgg_is_admin_logged_in()) {
52  return;
53  }
54 
55  if ($user->guid === elgg_get_logged_in_user_guid()) {
56  // admins can't perform actions on themselfs
57  return;
58  }
59 
60  /* @var $return MenuItems */
61  $return = $event->getValue();
62 
63  // delete
64  $return[] = \ElggMenuItem::factory([
65  'name' => 'delete',
66  'icon' => 'delete',
67  'text' => elgg_echo('delete'),
68  'href' => elgg_generate_url('delete:user', [
69  'guid' => $user->guid,
70  ]),
71  'section' => 'admin',
72  'priority' => 999,
73  ]);
74 
75  if ($user->isValidated() === false) {
76  $return[] = \ElggMenuItem::factory([
77  'name' => 'change_email',
78  'icon' => 'edit',
79  'text' => elgg_echo('admin:users:unvalidated:change_email'),
80  'href' => elgg_http_add_url_query_elements('ajax/form/admin/user/change_email', [
81  'user_guid' => $user->guid,
82  ]),
83  'link_class' => 'elgg-lightbox',
84  'section' => 'admin',
85  ]);
86 
87  $return[] = \ElggMenuItem::factory([
88  'name' => 'validate',
89  'icon' => 'check',
90  'text' => elgg_echo('validate'),
91  'href' => elgg_generate_action_url('admin/user/validate', [
92  'user_guid' => $user->guid,
93  ]),
94  'confirm' => true,
95  'section' => 'admin',
96  ]);
97  }
98 
99  if (!$user->isEnabled()) {
100  // in certain admin cases
101  return $return;
102  }
103 
104  // (un)ban
105  if (!$user->isBanned()) {
106  $return[] = \ElggMenuItem::factory([
107  'name' => 'ban',
108  'icon' => 'ban',
109  'text' => elgg_echo('ban'),
110  'href' => elgg_generate_action_url('admin/user/ban', [
111  'guid' => $user->guid,
112  ]),
113  'confirm' => true,
114  'section' => 'admin',
115  ]);
116  } else {
117  $return[] = \ElggMenuItem::factory([
118  'name' => 'unban',
119  'icon' => 'ban',
120  'text' => elgg_echo('unban'),
121  'href' => elgg_generate_action_url('admin/user/unban', [
122  'guid' => $user->guid,
123  ]),
124  'confirm' => true,
125  'section' => 'admin',
126  ]);
127  }
128 
129  // reset password
130  $return[] = \ElggMenuItem::factory([
131  'name' => 'resetpassword',
132  'icon' => 'refresh',
133  'text' => elgg_echo('resetpassword'),
134  'href' => elgg_generate_action_url('admin/user/resetpassword', [
135  'guid' => $user->guid,
136  ]),
137  'confirm' => true,
138  'section' => 'admin',
139  ]);
140 
141  // Toggle admin role
142  $is_admin = $user->isAdmin();
143  $return[] = \ElggMenuItem::factory([
144  'name' => 'makeadmin',
145  'icon' => 'level-up-alt',
146  'text' => elgg_echo('makeadmin'),
147  'href' => elgg_generate_action_url('admin/user/makeadmin', [
148  'guid' => $user->guid,
149  ]),
150  'confirm' => true,
151  'section' => 'admin',
152  'item_class' => $is_admin ? 'hidden' : null,
153  'data-toggle' => 'removeadmin',
154  ]);
155 
156  $return[] = \ElggMenuItem::factory([
157  'name' => 'removeadmin',
158  'icon' => 'level-down-alt',
159  'text' => elgg_echo('removeadmin'),
160  'href' => elgg_generate_action_url('admin/user/removeadmin', [
161  'guid' => $user->guid,
162  ]),
163  'confirm' => true,
164  'section' => 'admin',
165  'item_class' => $is_admin ? null : 'hidden',
166  'data-toggle' => 'makeadmin',
167  ]);
168 
169  $return[] = \ElggMenuItem::factory([
170  'name' => 'settings:edit',
171  'icon' => 'cogs',
172  'text' => elgg_echo('settings:edit'),
173  'href' => elgg_generate_url('settings:account', [
174  'username' => $user->username,
175  ]),
176  'section' => 'admin',
177  ]);
178 
179  return $return;
180  }
181 
189  public static function registerLoginAs(\Elgg\Event $event) {
190  $user = $event->getEntityParam();
191  $logged_in_user = elgg_get_logged_in_user_entity();
192 
193  if (!$user instanceof \ElggUser || $user->isBanned() || !$user->isEnabled()) {
194  // no user, banned user or disabled user (is certain admin cases) is unable to login
195  return;
196  }
197 
198  if (!$logged_in_user instanceof \ElggUser || !$logged_in_user->isAdmin()) {
199  // no admin user logged in
200  return;
201  }
202 
203  if ($logged_in_user->guid === $user->guid) {
204  // don't show menu on self
205  return;
206  }
207 
208  if (!empty(elgg_get_session()->get('login_as_original_user_guid'))) {
209  // don't show menu if already logged in as someone else
210  return;
211  }
212 
213  $menu = $event->getValue();
214 
216  'name' => 'login_as',
217  'icon' => 'sign-in-alt',
218  'text' => elgg_echo('action:user:login_as'),
219  'href' => elgg_generate_action_url('admin/user/login_as', [
220  'user_guid' => $user->guid,
221  ]),
222  'section' => $event->getType() === 'menu:user_hover' ? 'admin' : 'default',
223  ]);
224 
225  return $menu;
226  }
227 }
elgg_generate_action_url(string $action, array $query=[], bool $add_csrf_tokens=true)
Generate an action URL.
elgg_is_admin_logged_in()
Returns whether or not the viewer is currently logged in and an admin user.
Definition: sessions.php:52
elgg_get_session()
Gets Elgg&#39;s session object.
Definition: sessions.php:15
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
static factory(array $options)
Create an ElggMenuItem from an associative array.
Register menu items to the user_hover menu.
Definition: UserHover.php:13
static registerAvatarEdit(\Elgg\Event $event)
Add a link to the avatar edit page.
Definition: UserHover.php:22
static registerLoginAs(\Elgg\Event $event)
Register admin action to login as another user.
Definition: UserHover.php:189
elgg_generate_entity_url(ElggEntity $entity, string $resource= 'view', string $subresource=null, array $parameters=[])
Generate entity URL from a named route.
$user
Definition: ban.php:7
elgg_http_add_url_query_elements(string $url, array $elements)
Sets elements in a URL&#39;s query string.
Definition: elgglib.php:181
$menu
Form body for setting up site menu.
Definition: save.php:7
isAdmin()
Is this user admin?
Definition: ElggUser.php:182
elgg_generate_url(string $name, array $parameters=[])
Generate a URL for named route.
elgg_get_logged_in_user_entity()
Return the current logged in user, or null if no user is logged in.
Definition: sessions.php:24
static registerAdminActions(\Elgg\Event $event)
Register admin action.
Definition: UserHover.php:49
elgg_get_logged_in_user_guid()
Return the current logged in user by guid.
Definition: sessions.php:34
Models an event passed to event handlers.
Definition: Event.php:11