Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #18: pre-check access on user lists #19

Merged
merged 1 commit into from
Feb 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions masquerade.module
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ function masquerade_block_1() {
}
else {
$quick_switches = $config->get('quick_switches');
$admin_roles = array_filter($config->get('admin_roles'));

// Add in user-specific switches, and prevent duplicates.
$user_switches = db_query("SELECT uid_to FROM {masquerade_users} WHERE uid_from = :uid_from", array(':uid_from' => $user->uid))->fetchCol();
Expand All @@ -633,7 +634,10 @@ function masquerade_block_1() {
$account = user_load($switch_user);
if (isset($account->uid)) {
$switch_link = 'masquerade/switch/' . $account->uid;
if ($account->uid) {
$perm = $user->uid == 1 || array_intersect((array) $account->roles, $admin_roles) ?
'masquerade as admin' :
'masquerade as user';
if ($account->uid && user_access($perm)) {
$quick_switch_links[] = l($account->name, $switch_link, array('query' => array('token' => backdrop_get_token($switch_link))));
}
if ($switch_user == 0) {
Expand Down Expand Up @@ -745,18 +749,29 @@ function masquerade_block_1_submit($form, &$form_state) {
*/
function masquerade_autocomplete($string) {
$config = config('masquerade.settings');

// Check if user qualifies as admin.
$admin_roles = array_filter($config->get('admin_roles'));
global $user;

$matches = array();
// Anonymous user goes first to be visible for user.
$anonymous = t(config_get('system.core', 'anonymous'));
if (stripos($anonymous, $string) === 0) {
$matches[$anonymous] = $anonymous;
}
// Other suggestions.
$result = db_query_range("SELECT name FROM {users} WHERE LOWER(name) LIKE LOWER(:string)", 0, 10, array(
$result = db_query_range("SELECT uid, name FROM {users} WHERE LOWER(name) LIKE LOWER(:string)", 0, 10, array(
':string' => $string . '%',
));
foreach ($result as $user) {
$matches[$user->name] = check_plain($user->name);
foreach ($result as $switch_user) {
$account = user_load($switch_user->uid);
$perm = $user->uid == 1 || array_intersect((array) $account->roles, $admin_roles) ?
'masquerade as admin' :
'masquerade as user';
if (user_access($perm)) {
$matches[$account->name] = check_plain($account->name);
}
}
if (module_exists('devel')) {
$GLOBALS['devel_shutdown'] = FALSE;
Expand Down