mc-cms-namelessmc/modules/Core/queries/admin_users.php

125 lines
3.9 KiB
PHP
Raw Permalink Normal View History

2020-10-18 15:10:28 +01:00
<?php
// Returns set of users for the StaffCP Users tab
header('Content-type: application/json;charset=utf-8');
if (!$user->isLoggedIn() || !$user->hasPermission('admincp.users')) {
2020-12-14 20:40:55 -08:00
die(json_encode('Unauthenticated'));
2020-10-18 15:10:28 +01:00
}
2023-01-13 14:38:57 -08:00
$sortColumns = ['id' => 'id', 'username' => 'username', 'joined' => 'joined'];
2020-10-18 15:10:28 +01:00
$db = DB::getInstance();
2022-05-01 23:16:22 -07:00
$total = $db->query('SELECT COUNT(*) as `total` FROM nl2_users', [])->first()->total;
User Integrations systems (#2606) * Initial work for User Integrations systems * Convert a few endpoints to new user integration system * Finish minecraft part to new User Integration system * Fixes verify banner issue https://github.com/NamelessMC/Nameless/issues/2052 Fixes https://github.com/NamelessMC/Nameless/issues/2052 * Convert discord to new user integrations system * Update fields to show user integrations * Add user integrations to StaffCP user page / move over * Update UserInfo and Rename MinecraftVerify endpoint * Rework user listing endpoint * Password check never returns null * Add support for offline mode uuid * Fix documentation phpstan warnings * else if * remove die() * Revamp registration page * add integrations settings page * Convert authme & fix phpstan issues * refix settings page * Load uuid when user is loaded * Redirect user to complete required integrations * Fix translation for user connections page * Rework integrations registration in Register Endpoint Completes https://github.com/NamelessMC/Nameless/issues/2516 * make getIntegration ignore case sensitive * Update installer * Update upgrader script to convert users * Fix index for user integrations * Remove uuid from user data * Misc * Remove one unused query * Add event for integration linking, verify and unlink * Finish user transformer, Finish user integrations * Thx phpstan * Fix phpdocs * Fix user reports page * Remove validation banner * Change default field value * resolve samerton feedback * don't clean when inserting data * don't clean event data * remove more clean * increase reset_code length from 60 to 64 * pr13 confirmed * one more clean * Remove API verification setting * Register integrations edit permission * Resolve a few requested changes * Rework username and identifier validation * Add button to sync user integration * Fix Synchronisation text * Fix is verified term Co-authored-by: Robin <robinslot@hotmail.nl> Co-authored-by: Robin <robin+git@rkslot.nl>
2022-04-16 18:42:24 +02:00
$query = 'SELECT u.id, u.username, u.nickname, u.joined, u.gravatar, u.email, u.has_avatar, u.avatar_updated, IFNULL(nl2_users_integrations.identifier, \'none\') as uuid FROM nl2_users u LEFT JOIN nl2_users_integrations ON user_id=u.id AND integration_id=1';
$extra_query = '';
$where = [];
2020-10-18 15:10:28 +01:00
$order = '';
2020-12-25 15:00:02 +00:00
$limit = '';
2021-10-29 22:00:05 -07:00
$params = [];
2020-10-18 15:10:28 +01:00
if (isset($_GET['group'])) {
$extra_query .= ' INNER JOIN nl2_users_groups ug ON u.id = ug.user_id';
$where[] = 'ug.group_id = ?';
$params[] = $_GET['group'];
}
if (isset($_GET['integration'])) {
$extra_query .= ' INNER JOIN nl2_users_integrations ui ON ui.user_id=u.id INNER JOIN nl2_integrations i ON i.id=ui.integration_id';
$where[] = 'i.name = ?';
$params[] = $_GET['integration'];
}
if (isset($_GET['banned'])) {
$where[] = '`u`.`isbanned` = ' . ($_GET['banned'] == 'true' ? '1' : '0');
}
if (isset($_GET['active'])) {
$where[] = '`u`.`active` = ' . ($_GET['active'] == 'true' ? '1' : '0');
}
2020-10-18 15:10:28 +01:00
if (isset($_GET['search']) && $_GET['search']['value'] != '') {
$where[] = ' (u.username LIKE ? OR u.nickname LIKE ? OR u.email LIKE ?)';
array_push($params, '%' . $_GET['search']['value'] . '%', '%' . $_GET['search']['value'] . '%', '%' . $_GET['search']['value'] . '%');
2020-10-18 15:10:28 +01:00
}
// Build where string
$where_query = '';
if (!empty($where)) {
$where_query .= ' WHERE ';
foreach ($where as $item) {
$where_query .= $item . ' AND ';
}
$where_query = rtrim($where_query, ' AND ');
}
$where = $where_query;
2020-10-18 15:10:28 +01:00
if (isset($_GET['order']) && count($_GET['order'])) {
2021-10-29 22:00:05 -07:00
$orderBy = [];
2020-10-18 15:10:28 +01:00
2020-12-14 20:40:55 -08:00
for ($i = 0, $j = count($_GET['order']); $i < $j; $i++) {
2022-01-15 15:18:05 -08:00
$column = (int)$_GET['order'][$i]['column'];
2020-12-14 20:40:55 -08:00
$requestColumn = $_GET['columns'][$column];
2020-10-18 15:10:28 +01:00
2020-12-14 20:40:55 -08:00
$column = array_search($requestColumn['data'], $sortColumns);
2020-10-18 15:10:28 +01:00
2020-12-14 20:40:55 -08:00
if ($column) {
$dir = $_GET['order'][$i]['dir'] === 'asc' ?
'ASC' :
'DESC';
2020-10-18 15:10:28 +01:00
2020-12-14 20:40:55 -08:00
$orderBy[] = '`' . $column . '` ' . $dir;
}
}
2020-10-18 15:10:28 +01:00
2020-12-14 20:40:55 -08:00
if (count($orderBy)) {
$order .= ' ORDER BY ' . implode(', ', $orderBy);
} else {
$order .= ' ORDER BY username ASC';
}
2020-10-18 15:10:28 +01:00
} else {
2020-12-14 20:40:55 -08:00
$order .= ' ORDER BY username ASC';
2020-10-18 15:10:28 +01:00
}
if (isset($_GET['start']) && $_GET['length'] != -1) {
2022-01-15 15:18:05 -08:00
$limit .= ' LIMIT ' . (int)$_GET['start'] . ', ' . (int)$_GET['length'];
2020-10-18 15:10:28 +01:00
} else {
2020-12-14 20:40:55 -08:00
// default 10
$limit .= ' LIMIT 10';
2020-10-18 15:10:28 +01:00
}
if (strlen($where) > 0) {
$totalFiltered = $db->query('SELECT COUNT(*) as `total` FROM nl2_users u ' . $extra_query . $where, $params)->first()->total;
2020-10-18 15:10:28 +01:00
}
$results = $db->query($query . $extra_query . $where . $order . $limit, $params)->results();
2021-10-29 22:00:05 -07:00
$data = [];
$groups = [];
2020-10-18 15:10:28 +01:00
if (count($results)) {
2020-12-14 20:40:55 -08:00
foreach ($results as $result) {
$img = AvatarSource::getAvatarFromUserData($result, true, 30, true);
2020-12-14 20:40:55 -08:00
$obj = new stdClass();
$obj->id = $result->id;
$obj->username = "<img src='{$img}' style='padding-right: 5px; max-height: 30px;'>" . Output::getClean($result->username) . "</img>";
2023-01-13 14:38:57 -08:00
$obj->joined = date(DATE_FORMAT, $result->joined);
2020-12-14 20:40:55 -08:00
2020-12-25 15:00:02 +00:00
// Get group
2022-05-01 23:16:22 -07:00
$group = DB::getInstance()->query('SELECT `name` FROM nl2_groups g JOIN nl2_users_groups ug ON g.id = ug.group_id WHERE ug.user_id = ? ORDER BY g.order LIMIT 1', [$result->id]);
2020-12-25 15:00:02 +00:00
$obj->groupName = $group->first()->name;
2020-12-14 20:40:55 -08:00
$data[] = $obj;
}
2020-10-18 15:10:28 +01:00
}
echo json_encode(
2021-10-29 22:00:05 -07:00
[
2022-01-15 15:18:05 -08:00
'draw' => isset($_GET['draw']) ? (int)$_GET['draw'] : 0,
2020-12-14 20:40:55 -08:00
'recordsTotal' => $total,
2021-10-28 13:14:57 -07:00
'recordsFiltered' => $totalFiltered ?? $total,
2020-12-14 20:40:55 -08:00
'data' => $data
2021-10-29 22:00:05 -07:00
],
2020-12-14 20:40:55 -08:00
JSON_PRETTY_PRINT
2020-10-18 15:10:28 +01:00
);