TeaWeb/shared/js/permission/PermissionManager.ts

810 lines
30 KiB
TypeScript
Raw Normal View History

import * as log from "../log";
import {LogCategory, logDebug, logInfo, logTrace, LogType, logWarn} from "../log";
import {PermissionType} from "../permission/PermissionType";
import {LaterPromise} from "../utils/LaterPromise";
import {ServerCommand} from "../connection/ConnectionBase";
import {CommandResult} from "../connection/ServerConnectionDeclaration";
import {ConnectionHandler} from "../ConnectionHandler";
import {AbstractCommandHandler} from "../connection/AbstractCommandHandler";
import {Registry} from "../events";
import {tr} from "../i18n/localize";
import {ErrorCode} from "../connection/ErrorCode";
2020-03-30 11:44:18 +00:00
export class PermissionInfo {
2018-02-27 16:20:49 +00:00
name: string;
id: number;
description: string;
Implemented the Material Design and fixed some bugs (#33) * cleaned up some files * Fundamental style update * Redesigned some style * fixed hostbanner popup * Removed old identity stuff * fixed close listener * Fixed changelog date * fixed release chat icons * fixed url * Fixed hostbanner * Uploaded missing images * Improved update handling * Improved script files * Fixed loading error and icon error * fixed Yes/No modal * Fixed loader issues with MS Edge * fixed modal style bug * Fixed control bar overflow for small devices * Improved error handling on identity creation * Logging generate error to terminal * fixed possible php error * fixed some possible loading errors when other files have'nt been already loaded. * removed debug message * Changed emsrcypten flags * Improved codec error handling * removed webassembly as required dependency * Improved and fixed channel tree issues * Improved the sliders * Removed unneeded files * fixed loader versions cache * second slight performance improved (dont animate elements anymore if they are not shown) * Fixed query visibility setting * not showing useless client infos for query clients * Added an auto reconnect system * Added a canceled message and increased reconnect interval * removed implemented todo * fixed repetitive channel names * Reworked the channel tree selected lines * Fixed channel tree names * Fixed name alignment * fixed the native client * added min width to the server select groups to avoid a disappearing effect on shrink * fixed bugged downloaded icons
2019-02-17 15:08:10 +00:00
is_boolean() { return this.name.startsWith("b_"); }
id_grant() : number {
return this.id | (1 << 15);
}
2018-02-27 16:20:49 +00:00
}
2020-03-30 11:44:18 +00:00
export class PermissionGroup {
2018-09-30 19:50:59 +00:00
begin: number;
end: number;
deep: number;
name: string;
}
2020-03-30 11:44:18 +00:00
export class GroupedPermissions {
2018-09-30 19:50:59 +00:00
group: PermissionGroup;
permissions: PermissionInfo[];
children: GroupedPermissions[];
parent: GroupedPermissions;
}
2020-03-30 11:44:18 +00:00
export class PermissionValue {
2018-02-27 16:20:49 +00:00
readonly type: PermissionInfo;
2020-06-15 14:56:05 +00:00
value: number | undefined; /* undefined if no permission is given */
2018-09-30 19:50:59 +00:00
flag_skip: boolean;
flag_negate: boolean;
granted_value: number;
2018-02-27 16:20:49 +00:00
Implemented the Material Design and fixed some bugs (#33) * cleaned up some files * Fundamental style update * Redesigned some style * fixed hostbanner popup * Removed old identity stuff * fixed close listener * Fixed changelog date * fixed release chat icons * fixed url * Fixed hostbanner * Uploaded missing images * Improved update handling * Improved script files * Fixed loading error and icon error * fixed Yes/No modal * Fixed loader issues with MS Edge * fixed modal style bug * Fixed control bar overflow for small devices * Improved error handling on identity creation * Logging generate error to terminal * fixed possible php error * fixed some possible loading errors when other files have'nt been already loaded. * removed debug message * Changed emsrcypten flags * Improved codec error handling * removed webassembly as required dependency * Improved and fixed channel tree issues * Improved the sliders * Removed unneeded files * fixed loader versions cache * second slight performance improved (dont animate elements anymore if they are not shown) * Fixed query visibility setting * not showing useless client infos for query clients * Added an auto reconnect system * Added a canceled message and increased reconnect interval * removed implemented todo * fixed repetitive channel names * Reworked the channel tree selected lines * Fixed channel tree names * Fixed name alignment * fixed the native client * added min width to the server select groups to avoid a disappearing effect on shrink * fixed bugged downloaded icons
2019-02-17 15:08:10 +00:00
constructor(type, value?) {
2018-02-27 16:20:49 +00:00
this.type = type;
this.value = value;
}
granted(requiredValue: number, required: boolean = true) : boolean {
2018-07-03 10:33:31 +00:00
let result;
result = this.value == -1 || this.value >= requiredValue || (this.value == -2 && requiredValue == -2 && !required);
2019-10-19 15:13:40 +00:00
logTrace(LogCategory.PERMISSIONS,
2019-10-19 15:13:40 +00:00
tr("Required permission test resulted for permission %s: %s. Required value: %s, Granted value: %s"),
this.type ? this.type.name : "unknown",
2019-10-19 15:13:40 +00:00
result ? tr("granted") : tr("denied"),
requiredValue + (required ? " (" + tr("required") + ")" : ""),
this.hasValue() ? this.value : tr("none")
);
2018-04-16 18:38:35 +00:00
return result;
2018-02-27 16:20:49 +00:00
}
hasValue() : boolean {
Implemented the Material Design and fixed some bugs (#33) * cleaned up some files * Fundamental style update * Redesigned some style * fixed hostbanner popup * Removed old identity stuff * fixed close listener * Fixed changelog date * fixed release chat icons * fixed url * Fixed hostbanner * Uploaded missing images * Improved update handling * Improved script files * Fixed loading error and icon error * fixed Yes/No modal * Fixed loader issues with MS Edge * fixed modal style bug * Fixed control bar overflow for small devices * Improved error handling on identity creation * Logging generate error to terminal * fixed possible php error * fixed some possible loading errors when other files have'nt been already loaded. * removed debug message * Changed emsrcypten flags * Improved codec error handling * removed webassembly as required dependency * Improved and fixed channel tree issues * Improved the sliders * Removed unneeded files * fixed loader versions cache * second slight performance improved (dont animate elements anymore if they are not shown) * Fixed query visibility setting * not showing useless client infos for query clients * Added an auto reconnect system * Added a canceled message and increased reconnect interval * removed implemented todo * fixed repetitive channel names * Reworked the channel tree selected lines * Fixed channel tree names * Fixed name alignment * fixed the native client * added min width to the server select groups to avoid a disappearing effect on shrink * fixed bugged downloaded icons
2019-02-17 15:08:10 +00:00
return typeof(this.value) !== "undefined" && this.value != -2;
}
2020-06-15 14:56:05 +00:00
Implemented the Material Design and fixed some bugs (#33) * cleaned up some files * Fundamental style update * Redesigned some style * fixed hostbanner popup * Removed old identity stuff * fixed close listener * Fixed changelog date * fixed release chat icons * fixed url * Fixed hostbanner * Uploaded missing images * Improved update handling * Improved script files * Fixed loading error and icon error * fixed Yes/No modal * Fixed loader issues with MS Edge * fixed modal style bug * Fixed control bar overflow for small devices * Improved error handling on identity creation * Logging generate error to terminal * fixed possible php error * fixed some possible loading errors when other files have'nt been already loaded. * removed debug message * Changed emsrcypten flags * Improved codec error handling * removed webassembly as required dependency * Improved and fixed channel tree issues * Improved the sliders * Removed unneeded files * fixed loader versions cache * second slight performance improved (dont animate elements anymore if they are not shown) * Fixed query visibility setting * not showing useless client infos for query clients * Added an auto reconnect system * Added a canceled message and increased reconnect interval * removed implemented todo * fixed repetitive channel names * Reworked the channel tree selected lines * Fixed channel tree names * Fixed name alignment * fixed the native client * added min width to the server select groups to avoid a disappearing effect on shrink * fixed bugged downloaded icons
2019-02-17 15:08:10 +00:00
hasGrant() : boolean {
return typeof(this.granted_value) !== "undefined" && this.granted_value != -2;
2018-02-27 16:20:49 +00:00
}
2020-06-15 14:56:05 +00:00
valueOr(fallback: number) {
return this.hasValue() ? this.value : fallback;
}
2020-12-17 10:55:53 +00:00
valueNormalOr(fallback: number) {
if(this.hasValue()) {
if(this.value === -1) {
return Number.MAX_SAFE_INTEGER;
}
return this.value;
} else {
return fallback;
}
}
2018-02-27 16:20:49 +00:00
}
2020-03-30 11:44:18 +00:00
export class NeededPermissionValue extends PermissionValue {
2018-02-27 16:20:49 +00:00
constructor(type, value) {
super(type, value);
}
}
2020-03-30 11:44:18 +00:00
export type PermissionRequestKeys = {
client_id?: number;
channel_id?: number;
playlist_id?: number;
}
2020-02-22 13:30:17 +00:00
2020-03-30 11:44:18 +00:00
export type PermissionRequest = PermissionRequestKeys & {
timeout_id: any;
promise: LaterPromise<PermissionValue[]>;
};
2020-02-22 13:30:17 +00:00
2020-03-30 11:44:18 +00:00
export namespace find {
export type Entry = {
type: "server" | "channel" | "client" | "client_channel" | "channel_group" | "server_group";
value: number;
id: number;
}
2020-02-22 13:30:17 +00:00
2020-03-30 11:44:18 +00:00
export type Client = Entry & {
type: "client",
2020-02-22 13:30:17 +00:00
2020-03-30 11:44:18 +00:00
client_id: number;
}
2020-02-22 13:30:17 +00:00
2020-03-30 11:44:18 +00:00
export type Channel = Entry & {
type: "channel",
2020-02-22 13:30:17 +00:00
2020-03-30 11:44:18 +00:00
channel_id: number;
}
2020-02-22 13:30:17 +00:00
2020-03-30 11:44:18 +00:00
export type Server = Entry & {
type: "server"
}
2018-07-03 10:33:31 +00:00
2020-03-30 11:44:18 +00:00
export type ClientChannel = Entry & {
type: "client_channel",
2020-02-22 13:30:17 +00:00
2020-03-30 11:44:18 +00:00
client_id: number;
channel_id: number;
}
2020-02-22 13:30:17 +00:00
2020-03-30 11:44:18 +00:00
export type ChannelGroup = Entry & {
type: "channel_group",
2020-02-22 13:30:17 +00:00
2020-03-30 11:44:18 +00:00
group_id: number;
}
2020-02-22 13:30:17 +00:00
2020-03-30 11:44:18 +00:00
export type ServerGroup = Entry & {
type: "server_group",
2020-02-22 13:30:17 +00:00
2020-03-30 11:44:18 +00:00
group_id: number;
2020-02-22 13:30:17 +00:00
}
2018-09-30 19:50:59 +00:00
}
2020-06-15 14:56:05 +00:00
export interface PermissionManagerEvents {
client_permissions_changed: {}
}
2020-03-30 11:44:18 +00:00
export type RequestLists =
2020-02-22 13:30:17 +00:00
"requests_channel_permissions" |
"requests_client_permissions" |
"requests_client_channel_permissions" |
"requests_playlist_permissions" |
"requests_playlist_client_permissions";
2020-03-30 11:44:18 +00:00
export class PermissionManager extends AbstractCommandHandler {
2020-06-15 14:56:05 +00:00
readonly events = new Registry<PermissionManagerEvents>();
2019-04-04 19:47:52 +00:00
readonly handle: ConnectionHandler;
2018-02-27 16:20:49 +00:00
permissionList: PermissionInfo[] = [];
2018-09-30 19:50:59 +00:00
permissionGroups: PermissionGroup[] = [];
2018-07-03 10:33:31 +00:00
neededPermissions: NeededPermissionValue[] = [];
needed_permission_change_listener: {[permission: string]:((value?: PermissionValue) => void)[]} = {};
2019-08-21 08:00:01 +00:00
2020-03-30 11:44:18 +00:00
requests_channel_permissions: PermissionRequest[] = [];
requests_client_permissions: PermissionRequest[] = [];
requests_client_channel_permissions: PermissionRequest[] = [];
requests_playlist_permissions: PermissionRequest[] = [];
requests_playlist_client_permissions: PermissionRequest[] = [];
2020-02-22 13:30:17 +00:00
requests_permfind: {
timeout_id: number,
permission: string,
callback: (status: "success" | "error", data: any) => void
}[] = [];
2018-02-27 16:20:49 +00:00
2018-04-16 18:38:35 +00:00
initializedListener: ((initialized: boolean) => void)[] = [];
2021-02-15 17:23:56 +00:00
private cacheNeededPermissions: any;
2018-04-16 18:38:35 +00:00
2018-09-30 19:50:59 +00:00
/* Static info mapping until TeaSpeak implements a detailed info */
static readonly group_mapping: {name: string, deep: number}[] = [
2020-02-22 13:30:17 +00:00
{name: tr("Global"), deep: 0},
{name: tr("Information"), deep: 1},
{name: tr("Virtual server management"), deep: 1},
{name: tr("Administration"), deep: 1},
{name: tr("Settings"), deep: 1},
{name: tr("Virtual Server"), deep: 0},
{name: tr("Information"), deep: 1},
{name: tr("Administration"), deep: 1},
{name: tr("Settings"), deep: 1},
{name: tr("Channel"), deep: 0},
{name: tr("Information"), deep: 1},
{name: tr("Create"), deep: 1},
{name: tr("Modify"), deep: 1},
{name: tr("Delete"), deep: 1},
{name: tr("Access"), deep: 1},
{name: tr("Group"), deep: 0},
{name: tr("Information"), deep: 1},
{name: tr("Create"), deep: 1},
{name: tr("Modify"), deep: 1},
{name: tr("Delete"), deep: 1},
{name: tr("Client"), deep: 0},
{name: tr("Information"), deep: 1},
{name: tr("Admin"), deep: 1},
{name: tr("Basics"), deep: 1},
{name: tr("Modify"), deep: 1},
2018-09-30 19:50:59 +00:00
//TODO Music bot
2020-02-22 13:30:17 +00:00
{name: tr("File Transfer"), deep: 0},
2018-09-30 19:50:59 +00:00
];
private _group_mapping;
public static parse_permission_bulk(json: any[], manager: PermissionManager) : PermissionValue[] {
let permissions: PermissionValue[] = [];
for(let perm of json) {
Implemented the Material Design and fixed some bugs (#33) * cleaned up some files * Fundamental style update * Redesigned some style * fixed hostbanner popup * Removed old identity stuff * fixed close listener * Fixed changelog date * fixed release chat icons * fixed url * Fixed hostbanner * Uploaded missing images * Improved update handling * Improved script files * Fixed loading error and icon error * fixed Yes/No modal * Fixed loader issues with MS Edge * fixed modal style bug * Fixed control bar overflow for small devices * Improved error handling on identity creation * Logging generate error to terminal * fixed possible php error * fixed some possible loading errors when other files have'nt been already loaded. * removed debug message * Changed emsrcypten flags * Improved codec error handling * removed webassembly as required dependency * Improved and fixed channel tree issues * Improved the sliders * Removed unneeded files * fixed loader versions cache * second slight performance improved (dont animate elements anymore if they are not shown) * Fixed query visibility setting * not showing useless client infos for query clients * Added an auto reconnect system * Added a canceled message and increased reconnect interval * removed implemented todo * fixed repetitive channel names * Reworked the channel tree selected lines * Fixed channel tree names * Fixed name alignment * fixed the native client * added min width to the server select groups to avoid a disappearing effect on shrink * fixed bugged downloaded icons
2019-02-17 15:08:10 +00:00
if(perm["permid"] === undefined) continue;
2018-09-30 19:50:59 +00:00
let perm_id = parseInt(perm["permid"]);
let perm_grant = (perm_id & (1 << 15)) > 0;
if(perm_grant)
perm_id &= ~(1 << 15);
let perm_info = manager.resolveInfo(perm_id);
if(!perm_info) {
logWarn(LogCategory.PERMISSIONS, tr("Got unknown permission id (%o/%o (%o))!"), perm["permid"], perm_id, perm["permsid"]);
2018-09-30 19:50:59 +00:00
return;
}
let permission: PermissionValue;
for(let ref_perm of permissions) {
if(ref_perm.type == perm_info) {
permission = ref_perm;
break;
}
}
if(!permission) {
permission = new PermissionValue(perm_info, 0);
permission.granted_value = undefined;
permission.value = undefined;
permissions.push(permission);
}
if(perm_grant) {
permission.granted_value = parseInt(perm["permvalue"]);
} else {
permission.value = parseInt(perm["permvalue"]);
permission.flag_negate = perm["permnegated"] == "1";
permission.flag_skip = perm["permskip"] == "1";
}
}
return permissions;
}
2019-04-04 19:47:52 +00:00
constructor(client: ConnectionHandler) {
2019-02-23 13:15:22 +00:00
super(client.serverConnection);
2018-02-27 16:20:49 +00:00
2019-02-23 13:15:22 +00:00
//FIXME? Dont register the handler like this?
this.volatile_handler_boss = true;
2021-04-27 11:30:33 +00:00
client.serverConnection.getCommandHandler().registerHandler(this);
2019-01-20 17:43:14 +00:00
2019-02-23 13:15:22 +00:00
this.handle = client;
}
2019-08-21 08:00:01 +00:00
destroy() {
2021-04-27 11:30:33 +00:00
this.handle.serverConnection && this.handle.serverConnection.getCommandHandler().unregisterHandler(this);
2019-08-21 08:00:01 +00:00
this.needed_permission_change_listener = {};
this.permissionList = undefined;
this.permissionGroups = undefined;
this.neededPermissions = undefined;
2020-02-22 13:30:17 +00:00
/* delete all requests */
for(const key of Object.keys(this))
if(key.startsWith("requests"))
delete this[key];
2019-08-21 08:00:01 +00:00
this.initializedListener = undefined;
2021-02-15 17:23:56 +00:00
this.cacheNeededPermissions = undefined;
2019-08-21 08:00:01 +00:00
}
2020-03-30 11:44:18 +00:00
handle_command(command: ServerCommand): boolean {
2019-02-23 13:15:22 +00:00
switch (command.command) {
case "notifyclientneededpermissions":
this.onNeededPermissions(command.arguments);
return true;
case "notifypermissionlist":
this.onPermissionList(command.arguments);
return true;
case "notifychannelpermlist":
this.onChannelPermList(command.arguments);
return true;
case "notifyclientpermlist":
this.onClientPermList(command.arguments);
return true;
case "notifychannelclientpermlist":
this.onChannelClientPermList(command.arguments);
return true;
2019-02-23 13:15:22 +00:00
case "notifyplaylistpermlist":
this.onPlaylistPermList(command.arguments);
return true;
2020-02-22 13:30:17 +00:00
case "notifyplaylistclientpermlist":
this.onPlaylistClientPermList(command.arguments);
return true;
2019-02-23 13:15:22 +00:00
}
return false;
2018-02-27 16:20:49 +00:00
}
2018-04-16 18:38:35 +00:00
initialized() : boolean {
return this.permissionList.length > 0;
}
2018-02-27 16:20:49 +00:00
public requestPermissionList() {
2019-02-23 13:15:22 +00:00
this.handle.serverConnection.send_command("permissionlist");
2018-02-27 16:20:49 +00:00
}
private onPermissionList(json) {
this.permissionList = [];
2018-09-30 19:50:59 +00:00
this.permissionGroups = [];
this._group_mapping = PermissionManager.group_mapping.slice();
2018-04-16 18:38:35 +00:00
let group = log.group(log.LogType.TRACE, LogCategory.PERMISSIONS, tr("Permission mapping"));
2019-03-25 19:04:04 +00:00
const table_entries = [];
2019-04-15 13:33:51 +00:00
let permission_id = 0;
2018-04-16 18:38:35 +00:00
for(let e of json) {
2018-09-30 19:50:59 +00:00
if(e["group_id_end"]) {
let group = new PermissionGroup();
group.begin = this.permissionGroups.length ? this.permissionGroups.last().end : 0;
group.end = parseInt(e["group_id_end"]);
group.deep = 0;
group.name = tr("Group ") + e["group_id_end"];
2018-09-30 19:50:59 +00:00
let info = this._group_mapping.pop_front();
if(info) {
group.name = info.name;
group.deep = info.deep;
}
this.permissionGroups.push(group);
2019-03-25 19:04:04 +00:00
continue;
2018-09-30 19:50:59 +00:00
}
2018-02-27 16:20:49 +00:00
let perm = new PermissionInfo();
2019-04-15 13:33:51 +00:00
permission_id++;
2018-02-27 16:20:49 +00:00
perm.name = e["permname"];
2019-04-15 13:33:51 +00:00
perm.id = parseInt(e["permid"]) || permission_id; /* using permission_id as fallback if we dont have permid */
2018-02-27 16:20:49 +00:00
perm.description = e["permdesc"];
this.permissionList.push(perm);
2019-03-25 19:04:04 +00:00
table_entries.push({
"id": perm.id,
"name": perm.name,
"description": perm.description
});
2018-02-27 16:20:49 +00:00
}
2019-08-30 21:06:39 +00:00
log.table(LogType.DEBUG, LogCategory.PERMISSIONS, "Permission list", table_entries);
2018-04-16 18:38:35 +00:00
group.end();
2018-02-27 16:20:49 +00:00
logInfo(LogCategory.PERMISSIONS, tr("Got %i permissions"), this.permissionList.length);
2021-02-15 17:23:56 +00:00
if(this.cacheNeededPermissions) {
this.onNeededPermissions(this.cacheNeededPermissions);
}
for(let listener of this.initializedListener) {
2018-04-16 18:38:35 +00:00
listener(true);
2021-02-15 17:23:56 +00:00
}
2018-02-27 16:20:49 +00:00
}
2021-02-15 17:23:56 +00:00
private onNeededPermissions(json: any[]) {
2018-04-16 18:38:35 +00:00
if(this.permissionList.length == 0) {
logWarn(LogCategory.PERMISSIONS, tr("Got needed permissions but don't have a permission list!"));
2021-02-15 17:23:56 +00:00
this.cacheNeededPermissions = json;
2018-04-16 18:38:35 +00:00
return;
}
2021-02-15 17:23:56 +00:00
this.cacheNeededPermissions = undefined;
2018-04-16 18:38:35 +00:00
2021-02-15 17:23:56 +00:00
let permissionsCopy = this.neededPermissions.slice();
let permissionAddCount = 0;
let permissionRemoveCount = 0;
2018-02-27 16:20:49 +00:00
let group = log.group(log.LogType.TRACE, LogCategory.PERMISSIONS, tr("Got %d needed permissions."), json.length);
2021-02-15 17:23:56 +00:00
const tableEntries = [];
2019-03-25 19:04:04 +00:00
2021-02-15 17:23:56 +00:00
for(let notifyEntry of json) {
2018-07-03 10:33:31 +00:00
let entry: NeededPermissionValue = undefined;
2021-02-15 17:23:56 +00:00
for(let permission of permissionsCopy) {
if(permission.type.id == notifyEntry["permid"]) {
entry = permission;
permissionsCopy.remove(permission);
2018-04-16 18:38:35 +00:00
break;
2018-02-27 16:20:49 +00:00
}
2018-04-16 18:38:35 +00:00
}
2021-02-15 17:23:56 +00:00
const permissionValue = parseInt(notifyEntry["permvalue"]);
if(permissionValue === 0) {
if(entry) {
permissionRemoveCount++;
entry.value = -2;
for(const listener of this.needed_permission_change_listener[entry.type.name] || []) {
listener();
}
}
/*
* Permission hasn't been granted.
* TeamSpeak uses this as "permission removed".
*/
continue;
}
2018-04-16 18:38:35 +00:00
if(!entry) {
2021-02-15 17:23:56 +00:00
let info = this.resolveInfo(notifyEntry["permid"]);
2018-04-16 18:38:35 +00:00
if(info) {
2018-07-03 10:33:31 +00:00
entry = new NeededPermissionValue(info, -2);
2018-04-16 18:38:35 +00:00
this.neededPermissions.push(entry);
} else {
2021-02-15 17:23:56 +00:00
logWarn(LogCategory.PERMISSIONS, tr("Could not resolve perm for id %s (%o|%o)"), notifyEntry["permid"], notifyEntry, info);
2018-04-16 18:38:35 +00:00
continue;
}
2021-02-15 17:23:56 +00:00
permissionAddCount++;
2018-04-16 18:38:35 +00:00
}
2021-02-15 17:23:56 +00:00
entry.value = permissionValue;
2018-04-16 18:38:35 +00:00
2021-02-15 17:23:56 +00:00
for(const listener of this.needed_permission_change_listener[entry.type.name] || []) {
2019-08-21 08:00:01 +00:00
listener();
2021-02-15 17:23:56 +00:00
}
2019-03-25 19:04:04 +00:00
2021-02-15 17:23:56 +00:00
tableEntries.push({
2019-03-25 19:04:04 +00:00
"permission": entry.type.name,
"value": entry.value
});
2018-02-27 16:20:49 +00:00
}
2019-03-25 19:04:04 +00:00
2021-02-15 17:23:56 +00:00
log.table(LogType.DEBUG, LogCategory.PERMISSIONS, "Needed client permissions", tableEntries);
2018-04-16 18:38:35 +00:00
group.end();
2018-02-27 16:20:49 +00:00
2021-02-15 17:23:56 +00:00
if(this.handle.serverConnection.getServerType() === "teamspeak" || json[0]["relative"] === "1") {
/* We don't update the full list every time. Instead we're only propagating changes. */
} else {
permissionRemoveCount = permissionsCopy.length;
for(let entry of permissionsCopy) {
entry.value = -2;
for(const listener of this.needed_permission_change_listener[entry.type.name] || []) {
listener();
}
}
2018-02-27 16:20:49 +00:00
}
2021-02-15 17:23:56 +00:00
logDebug(LogCategory.PERMISSIONS, tr("Dropping %o needed permissions and added %o permissions."), permissionRemoveCount, permissionAddCount);
2020-06-15 14:56:05 +00:00
this.events.fire("client_permissions_changed");
2018-02-27 16:20:49 +00:00
}
register_needed_permission(key: PermissionType, listener: () => any) : () => void {
2019-08-21 08:00:01 +00:00
const array = this.needed_permission_change_listener[key] || [];
array.push(listener);
this.needed_permission_change_listener[key] = array;
return () => this.needed_permission_change_listener[key]?.remove(listener);
2019-08-21 08:00:01 +00:00
}
unregister_needed_permission(key: PermissionType, listener: () => any) {
const array = this.needed_permission_change_listener[key];
if(!array) return;
array.remove(listener);
this.needed_permission_change_listener[key] = array.length > 0 ? array : undefined;
}
2018-02-27 16:20:49 +00:00
resolveInfo?(key: number | string | PermissionType) : PermissionInfo {
for(let perm of this.permissionList)
if(perm.id == key || perm.name == key)
return perm;
return undefined;
}
2020-02-22 13:30:17 +00:00
/* channel permission request */
private onChannelPermList(json) {
let channelId: number = parseInt(json[0]["cid"]);
2020-02-22 13:30:17 +00:00
this.fullfill_permission_request("requests_channel_permissions", {
channel_id: channelId
}, "success", PermissionManager.parse_permission_bulk(json, this.handle.permissions));
2019-01-20 17:43:14 +00:00
}
private execute_channel_permission_request(request: PermissionRequestKeys, processResult?: boolean) {
this.handle.serverConnection.send_command("channelpermlist", {"cid": request.channel_id}, { process_result: !!processResult }).catch(error => {
if(error instanceof CommandResult && error.id == ErrorCode.DATABASE_EMPTY_RESULT)
2020-02-22 13:30:17 +00:00
this.fullfill_permission_request("requests_channel_permissions", request, "success", []);
2018-09-30 19:50:59 +00:00
else
2020-02-22 13:30:17 +00:00
this.fullfill_permission_request("requests_channel_permissions", request, "error", error);
2018-09-30 19:50:59 +00:00
});
2020-02-22 13:30:17 +00:00
}
2018-09-30 19:50:59 +00:00
requestChannelPermissions(channelId: number, processResult?: boolean) : Promise<PermissionValue[]> {
2020-03-30 11:44:18 +00:00
const keys: PermissionRequestKeys = {
2020-02-22 13:30:17 +00:00
channel_id: channelId
};
return this.execute_permission_request("requests_channel_permissions", keys, criteria => this.execute_channel_permission_request(criteria, processResult));
2018-09-30 19:50:59 +00:00
}
2020-02-22 13:30:17 +00:00
/* client permission request */
private onClientPermList(json: any[]) {
let client = parseInt(json[0]["cldbid"]);
2020-02-22 13:30:17 +00:00
this.fullfill_permission_request("requests_client_permissions", {
client_id: client
}, "success", PermissionManager.parse_permission_bulk(json, this.handle.permissions));
}
2020-03-30 11:44:18 +00:00
private execute_client_permission_request(request: PermissionRequestKeys) {
2020-02-22 13:30:17 +00:00
this.handle.serverConnection.send_command("clientpermlist", {cldbid: request.client_id}).catch(error => {
if(error instanceof CommandResult && error.id == ErrorCode.DATABASE_EMPTY_RESULT)
2020-02-22 13:30:17 +00:00
this.fullfill_permission_request("requests_client_permissions", request, "success", []);
2018-09-30 19:50:59 +00:00
else
2020-02-22 13:30:17 +00:00
this.fullfill_permission_request("requests_client_permissions", request, "error", error);
2018-09-30 19:50:59 +00:00
});
2020-02-22 13:30:17 +00:00
}
2018-09-30 19:50:59 +00:00
2020-02-22 13:30:17 +00:00
requestClientPermissions(client_id: number) : Promise<PermissionValue[]> {
2020-03-30 11:44:18 +00:00
const keys: PermissionRequestKeys = {
2020-02-22 13:30:17 +00:00
client_id: client_id
};
return this.execute_permission_request("requests_client_permissions", keys, this.execute_client_permission_request.bind(this));
2018-09-30 19:50:59 +00:00
}
2020-02-22 13:30:17 +00:00
/* client channel permission request */
private onChannelClientPermList(json: any[]) {
let client_id = parseInt(json[0]["cldbid"]);
let channel_id = parseInt(json[0]["cid"]);
2020-02-22 13:30:17 +00:00
this.fullfill_permission_request("requests_client_channel_permissions", {
client_id: client_id,
channel_id: channel_id
}, "success", PermissionManager.parse_permission_bulk(json, this.handle.permissions));
}
2020-03-30 11:44:18 +00:00
private execute_client_channel_permission_request(request: PermissionRequestKeys) {
2020-02-22 13:30:17 +00:00
this.handle.serverConnection.send_command("channelclientpermlist", {cldbid: request.client_id, cid: request.channel_id})
.catch(error => {
if(error instanceof CommandResult && error.id == ErrorCode.DATABASE_EMPTY_RESULT)
2020-02-22 13:30:17 +00:00
this.fullfill_permission_request("requests_client_channel_permissions", request, "success", []);
else
this.fullfill_permission_request("requests_client_channel_permissions", request, "error", error);
});
}
2019-01-20 17:43:14 +00:00
2020-02-22 13:30:17 +00:00
requestClientChannelPermissions(client_id: number, channel_id: number) : Promise<PermissionValue[]> {
2020-03-30 11:44:18 +00:00
const keys: PermissionRequestKeys = {
client_id: client_id,
channel_id: channel_id
2020-02-22 13:30:17 +00:00
};
return this.execute_permission_request("requests_client_channel_permissions", keys, this.execute_client_channel_permission_request.bind(this));
}
2019-01-20 17:43:14 +00:00
2020-02-22 13:30:17 +00:00
/* playlist permissions */
2019-01-20 17:43:14 +00:00
private onPlaylistPermList(json: any[]) {
let playlist_id = parseInt(json[0]["playlist_id"]);
2020-02-22 13:30:17 +00:00
this.fullfill_permission_request("requests_playlist_permissions", {
playlist_id: playlist_id
}, "success", PermissionManager.parse_permission_bulk(json, this.handle.permissions));
}
2020-03-30 11:44:18 +00:00
private execute_playlist_permission_request(request: PermissionRequestKeys) {
2020-02-22 13:30:17 +00:00
this.handle.serverConnection.send_command("playlistpermlist", {playlist_id: request.playlist_id})
.catch(error => {
if(error instanceof CommandResult && error.id == ErrorCode.DATABASE_EMPTY_RESULT)
2020-02-22 13:30:17 +00:00
this.fullfill_permission_request("requests_playlist_permissions", request, "success", []);
else
this.fullfill_permission_request("requests_playlist_permissions", request, "error", error);
});
2018-09-30 19:50:59 +00:00
}
2019-01-20 17:43:14 +00:00
requestPlaylistPermissions(playlist_id: number) : Promise<PermissionValue[]> {
2020-03-30 11:44:18 +00:00
const keys: PermissionRequestKeys = {
2020-02-22 13:30:17 +00:00
playlist_id: playlist_id
};
return this.execute_permission_request("requests_playlist_permissions", keys, this.execute_playlist_permission_request.bind(this));
}
/* playlist client permissions */
private onPlaylistClientPermList(json: any[]) {
let playlist_id = parseInt(json[0]["playlist_id"]);
let client_id = parseInt(json[0]["cldbid"]);
this.fullfill_permission_request("requests_playlist_client_permissions", {
playlist_id: playlist_id,
client_id: client_id
}, "success", PermissionManager.parse_permission_bulk(json, this.handle.permissions));
}
2020-03-30 11:44:18 +00:00
private execute_playlist_client_permission_request(request: PermissionRequestKeys) {
2020-02-22 13:30:17 +00:00
this.handle.serverConnection.send_command("playlistclientpermlist", {playlist_id: request.playlist_id, cldbid: request.client_id})
.catch(error => {
if(error instanceof CommandResult && error.id == ErrorCode.DATABASE_EMPTY_RESULT)
2020-02-22 13:30:17 +00:00
this.fullfill_permission_request("requests_playlist_client_permissions", request, "success", []);
else
this.fullfill_permission_request("requests_playlist_client_permissions", request, "error", error);
});
}
requestPlaylistClientPermissions(playlist_id: number, client_database_id: number) : Promise<PermissionValue[]> {
2020-03-30 11:44:18 +00:00
const keys: PermissionRequestKeys = {
2020-02-22 13:30:17 +00:00
playlist_id: playlist_id,
client_id: client_database_id
};
return this.execute_permission_request("requests_playlist_client_permissions", keys, this.execute_playlist_client_permission_request.bind(this));
}
private readonly criteria_equal = (a, b) => {
for(const criteria of ["client_id", "channel_id", "playlist_id"]) {
if((typeof a[criteria] === "undefined") !== (typeof b[criteria] === "undefined")) return false;
if(a[criteria] != b[criteria]) return false;
}
return true;
};
private execute_permission_request(list: RequestLists,
2020-03-30 11:44:18 +00:00
criteria: PermissionRequestKeys,
execute: (criteria: PermissionRequestKeys) => any) : Promise<PermissionValue[]> {
2020-02-22 13:30:17 +00:00
for(const request of this[list])
if(this.criteria_equal(request, criteria) && request.promise.time() + 1000 < Date.now())
2019-01-20 17:43:14 +00:00
return request.promise;
2020-02-22 13:30:17 +00:00
const result = Object.assign({
timeout_id: setTimeout(() => this.fullfill_permission_request(list, criteria, "error", tr("timeout")), 5000),
promise: new LaterPromise<PermissionValue[]>()
}, criteria);
this[list].push(result);
execute(criteria);
return result.promise;
};
2020-03-30 11:44:18 +00:00
private fullfill_permission_request(list: RequestLists, criteria: PermissionRequestKeys, status: "success" | "error", result: any) {
2020-02-22 13:30:17 +00:00
for(const request of this[list]) {
if(this.criteria_equal(request, criteria)) {
this[list].remove(request);
clearTimeout(request.timeout_id);
status === "error" ? request.promise.rejected(result) : request.promise.resolved(result);
}
}
}
2019-01-20 17:43:14 +00:00
2020-03-30 11:44:18 +00:00
find_permission(...permissions: string[]) : Promise<find.Entry[]> {
2020-02-22 13:30:17 +00:00
const permission_ids = [];
for(const permission of permissions) {
const info = this.resolveInfo(permission);
if(!info) continue;
permission_ids.push(info.id);
}
if(!permission_ids.length) return Promise.resolve([]);
2020-03-30 11:44:18 +00:00
return new Promise<find.Entry[]>((resolve, reject) => {
2020-02-22 13:30:17 +00:00
const single_handler = {
command: "notifypermfind",
function: command => {
2020-03-30 11:44:18 +00:00
const result: find.Entry[] = [];
2020-02-22 13:30:17 +00:00
for(const entry of command.arguments) {
const perm_id = parseInt(entry["p"]);
if(permission_ids.indexOf(perm_id) === -1) return; /* not our permfind result */
const value = parseInt(entry["v"]);
const type = parseInt(entry["t"]);
let data;
switch (type) {
case 0:
data = {
type: "server_group",
group_id: parseInt(entry["id1"]),
2020-03-30 11:44:18 +00:00
} as find.ServerGroup;
2020-02-22 13:30:17 +00:00
break;
case 1:
data = {
type: "client",
client_id: parseInt(entry["id2"]),
2020-03-30 11:44:18 +00:00
} as find.Client;
2020-02-22 13:30:17 +00:00
break;
case 2:
data = {
type: "channel",
channel_id: parseInt(entry["id2"]),
2020-03-30 11:44:18 +00:00
} as find.Channel;
2020-02-22 13:30:17 +00:00
break;
case 3:
data = {
type: "channel_group",
group_id: parseInt(entry["id1"]),
2020-03-30 11:44:18 +00:00
} as find.ChannelGroup;
2020-02-22 13:30:17 +00:00
break;
case 4:
data = {
type: "client_channel",
client_id: parseInt(entry["id1"]),
channel_id: parseInt(entry["id1"]),
2020-03-30 11:44:18 +00:00
} as find.ClientChannel;
2020-02-22 13:30:17 +00:00
break;
default:
continue;
}
data.id = perm_id;
data.value = value;
result.push(data);
}
2019-01-20 17:43:14 +00:00
2020-02-22 13:30:17 +00:00
resolve(result);
return true;
}
};
2021-04-27 11:30:33 +00:00
this.handler_boss.registerSingleHandler(single_handler);
2020-02-22 13:30:17 +00:00
this.connection.send_command("permfind", permission_ids.map(e => { return {permid: e }})).catch(error => {
2021-04-27 11:30:33 +00:00
this.handler_boss.removeSingleHandler(single_handler);
2020-02-22 13:30:17 +00:00
if(error instanceof CommandResult && error.id == ErrorCode.DATABASE_EMPTY_RESULT) {
2020-02-22 13:30:17 +00:00
resolve([]);
return;
}
reject(error);
});
});
2019-01-20 17:43:14 +00:00
}
2019-08-21 08:00:01 +00:00
neededPermission(key: number | string | PermissionType | PermissionInfo) : NeededPermissionValue {
2018-02-27 16:20:49 +00:00
for(let perm of this.neededPermissions)
if(perm.type.id == key || perm.type.name == key || perm.type == key)
return perm;
2018-12-23 16:41:14 +00:00
logDebug(LogCategory.PERMISSIONS, tr("Could not resolve grant permission %o. Creating a new one."), key);
2018-04-16 18:38:35 +00:00
let info = key instanceof PermissionInfo ? key : this.resolveInfo(key);
if(!info) {
logWarn(LogCategory.PERMISSIONS, tr("Requested needed permission with invalid key! (%o)"), key);
2018-12-23 16:41:14 +00:00
return new NeededPermissionValue(undefined, -2);
2018-04-16 18:38:35 +00:00
}
2018-07-03 10:33:31 +00:00
let result = new NeededPermissionValue(info, -2);
2018-04-16 18:38:35 +00:00
this.neededPermissions.push(result);
return result;
2018-09-30 19:50:59 +00:00
}
2018-04-16 18:38:35 +00:00
2018-09-30 19:50:59 +00:00
groupedPermissions() : GroupedPermissions[] {
let result: GroupedPermissions[] = [];
let current: GroupedPermissions;
for(let group of this.permissionGroups) {
if(group.deep == 0) {
current = new GroupedPermissions();
current.group = group;
current.parent = undefined;
current.children = [];
current.permissions = [];
result.push(current);
} else {
if(!current) {
throw tr("invalid order!");
2018-09-30 19:50:59 +00:00
} else {
while(group.deep <= current.group.deep)
current = current.parent;
let parent = current;
current = new GroupedPermissions();
current.group = group;
current.parent = parent;
current.children = [];
current.permissions = [];
parent.children.push(current);
}
}
for(let permission of this.permissionList)
if(permission.id > current.group.begin && permission.id <= current.group.end)
current.permissions.push(permission);
}
return result;
2018-02-27 16:20:49 +00:00
}
2019-01-20 17:43:14 +00:00
/**
* Generates an enum with all know permission types, used for the enum above
*/
export_permission_types() {
let result = "";
result = result + "enum PermissionType {\n";
for(const permission of this.permissionList) {
if(!permission.name) continue;
result = result + "\t" + permission.name.toUpperCase() + " = \"" + permission.name.toLowerCase() + "\", /* Permission ID: " + permission.id + " */\n";
}
result = result + "}";
return result;
}
getFailedPermission(command: CommandResult, index?: number) {
const json = command.bulks[typeof index === "number" ? index : 0] || {};
if("failed_permsid" in json) {
return json["failed_permsid"];
} else if("failed_permid" in json) {
const info = this.resolveInfo(parseInt(json["failed_permid"]));
return info ? info.name : "permission id " + json["failed_permid"];
} else {
return tr("unknown permission");
}
}
2018-02-27 16:20:49 +00:00
}