misuzu/src/Users/role.php

148 lines
4.3 KiB
PHP
Raw Normal View History

2018-05-27 02:20:35 +02:00
<?php
define('MSZ_ROLE_MAIN', 1);
2019-06-10 19:04:53 +02:00
function user_role_add(int $userId, int $roleId): bool {
2019-09-29 00:38:39 +02:00
$addRole = \Misuzu\DB::prepare('
2018-05-27 02:20:35 +02:00
INSERT INTO `msz_user_roles`
(`user_id`, `role_id`)
VALUES
(:user_id, :role_id)
');
2019-09-29 00:38:39 +02:00
$addRole->bind('user_id', $userId);
$addRole->bind('role_id', $roleId);
2018-05-27 02:20:35 +02:00
return $addRole->execute();
}
2018-05-27 03:59:57 +02:00
2019-06-10 19:04:53 +02:00
function user_role_remove(int $userId, int $roleId): bool {
2019-09-29 00:38:39 +02:00
$removeRole = \Misuzu\DB::prepare('
2018-05-27 03:59:57 +02:00
DELETE FROM `msz_user_roles`
WHERE `user_id` = :user_id
AND `role_id` = :role_id
');
2019-09-29 00:38:39 +02:00
$removeRole->bind('user_id', $userId);
$removeRole->bind('role_id', $roleId);
2018-05-27 03:59:57 +02:00
return $removeRole->execute();
}
2019-06-10 19:04:53 +02:00
function user_role_can_leave(int $roleId): bool {
2019-09-29 00:38:39 +02:00
$canLeaveRole = \Misuzu\DB::prepare('
2018-11-17 21:37:18 +01:00
SELECT `role_can_leave` != 0
FROM `msz_roles`
WHERE `role_id` = :role_id
');
2019-09-29 00:38:39 +02:00
$canLeaveRole->bind('role_id', $roleId);
return (bool)$canLeaveRole->fetchColumn();
2018-11-17 21:37:18 +01:00
}
2019-06-10 19:04:53 +02:00
function user_role_has(int $userId, int $roleId): bool {
2019-09-29 00:38:39 +02:00
$hasRole = \Misuzu\DB::prepare('
2018-05-27 03:59:57 +02:00
SELECT COUNT(`role_id`) > 0
FROM `msz_user_roles`
WHERE `user_id` = :user_id
AND `role_id` = :role_id
');
2019-09-29 00:38:39 +02:00
$hasRole->bind('user_id', $userId);
$hasRole->bind('role_id', $roleId);
return (bool)$hasRole->fetchColumn();
2018-05-27 03:59:57 +02:00
}
2019-06-10 19:04:53 +02:00
function user_role_set_display(int $userId, int $roleId): bool {
if(!user_role_has($userId, $roleId)) {
2018-05-27 03:59:57 +02:00
return false;
}
2019-09-29 00:38:39 +02:00
$setDisplay = \Misuzu\DB::prepare('
2018-05-27 03:59:57 +02:00
UPDATE `msz_users`
SET `display_role` = :role_id
WHERE `user_id` = :user_id
');
2019-09-29 00:38:39 +02:00
$setDisplay->bind('user_id', $userId);
$setDisplay->bind('role_id', $roleId);
2018-05-27 03:59:57 +02:00
return $setDisplay->execute();
}
2018-11-17 21:37:18 +01:00
2019-06-10 19:04:53 +02:00
function user_role_get_display(int $userId): int {
if($userId < 1) {
2018-11-17 21:37:18 +01:00
return MSZ_ROLE_MAIN;
}
2019-09-29 00:38:39 +02:00
$fetchRole = \Misuzu\DB::prepare('
2018-11-17 21:37:18 +01:00
SELECT `display_role`
FROM `msz_users`
WHERE `user_id` = :user_id
');
2019-09-29 00:38:39 +02:00
$fetchRole->bind('user_id', $userId);
return (int)$fetchRole->fetchColumn(0, MSZ_ROLE_MAIN);
2018-11-17 21:37:18 +01:00
}
2019-06-10 19:04:53 +02:00
function user_role_all_user(int $userId): array {
2019-09-29 00:38:39 +02:00
$getUserRoles = \Misuzu\DB::prepare('
SELECT
r.`role_id`, r.`role_name`, r.`role_description`,
r.`role_colour`, r.`role_can_leave`, r.`role_created`
FROM `msz_user_roles` AS ur
LEFT JOIN `msz_roles` AS r
ON r.`role_id` = ur.`role_id`
WHERE ur.`user_id` = :user_id
ORDER BY r.`role_hierarchy` DESC
');
2019-09-29 00:38:39 +02:00
$getUserRoles->bind('user_id', $userId);
return $getUserRoles->fetchAll();
}
2019-06-10 19:04:53 +02:00
function user_role_all(bool $withHidden = false) {
2019-09-29 00:38:39 +02:00
return \Misuzu\DB::query(sprintf(
'
SELECT
r.`role_id`, r.`role_name`, r.`role_description`,
r.`role_colour`, r.`role_can_leave`, r.`role_created`,
(
SELECT COUNT(`user_id`)
FROM `msz_user_roles`
WHERE `role_id` = r.`role_id`
) AS `role_user_count`
FROM `msz_roles` AS r
%s
ORDER BY `role_id`
',
$withHidden ? '' : 'WHERE `role_hidden` = 0'
2019-09-29 00:38:39 +02:00
))->fetchAll();
}
2019-06-10 19:04:53 +02:00
function user_role_get(int $roleId): array {
2019-09-29 00:38:39 +02:00
$getRole = \Misuzu\DB::prepare('
SELECT
r.`role_id`, r.`role_name`, r.`role_description`,
r.`role_colour`, r.`role_can_leave`, r.`role_created`,
(
SELECT COUNT(`user_id`)
FROM `msz_user_roles`
WHERE `role_id` = r.`role_id`
) AS `role_user_count`
FROM `msz_roles` AS r
WHERE `role_id` = :role_id
');
2019-09-29 00:38:39 +02:00
$getRole->bind('role_id', $roleId);
return $getRole->fetch();
}
2019-06-10 19:04:53 +02:00
function user_role_check_authority(int $userId, int $roleId): bool {
2019-09-29 00:38:39 +02:00
$checkHierarchy = \Misuzu\DB::prepare('
SELECT (
SELECT MAX(r.`role_hierarchy`)
FROM `msz_roles` AS r
LEFT JOIN `msz_user_roles` AS ur
ON ur.`role_id` = r.`role_id`
WHERE ur.`user_id` = :user_id
) > (
SELECT `role_hierarchy`
FROM `msz_roles`
WHERE `role_id` = :role_id
)
');
2019-09-29 00:38:39 +02:00
$checkHierarchy->bind('user_id', $userId);
$checkHierarchy->bind('role_id', $roleId);
return (bool)$checkHierarchy->fetchColumn();
}