2018-05-13 00:19:51 +08:00
|
|
|
import {DirectoryDTO} from './DirectoryDTO';
|
|
|
|
import {Utils} from '../Utils';
|
2018-02-04 08:50:42 +08:00
|
|
|
|
|
|
|
export enum UserRoles {
|
2018-05-29 02:03:12 +08:00
|
|
|
LimitedGuest = 1,
|
|
|
|
Guest = 2,
|
|
|
|
User = 3,
|
|
|
|
Admin = 4,
|
2020-02-07 06:57:59 +08:00
|
|
|
Developer = 5
|
2016-12-27 23:09:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface UserDTO {
|
2017-06-11 04:32:56 +08:00
|
|
|
id: number;
|
|
|
|
name: string;
|
|
|
|
password: string;
|
|
|
|
role: UserRoles;
|
2020-01-08 05:17:54 +08:00
|
|
|
csrfToken?: string;
|
2019-02-23 06:39:01 +08:00
|
|
|
usedSharingKey?: string;
|
2018-05-13 00:19:51 +08:00
|
|
|
permissions: string[]; // user can only see these permissions. if ends with *, its recursive
|
2017-07-04 01:17:49 +08:00
|
|
|
}
|
|
|
|
|
2021-04-18 21:48:35 +08:00
|
|
|
export const UserDTOUtils = {
|
2017-07-04 01:17:49 +08:00
|
|
|
|
2021-04-18 21:48:35 +08:00
|
|
|
isDirectoryPathAvailable: (path: string, permissions: string[]): boolean => {
|
2020-01-08 05:17:54 +08:00
|
|
|
if (permissions == null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
permissions = permissions.map(p => Utils.canonizePath(p));
|
|
|
|
path = Utils.canonizePath(path);
|
|
|
|
if (permissions.length === 0 || permissions[0] === '/*') {
|
2017-07-04 01:17:49 +08:00
|
|
|
return true;
|
|
|
|
}
|
2021-04-18 21:48:35 +08:00
|
|
|
for (let permission of permissions) {
|
|
|
|
if (permission === '/*') {
|
2019-02-23 07:12:59 +08:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-13 00:19:51 +08:00
|
|
|
if (permission[permission.length - 1] === '*') {
|
2017-07-04 01:17:49 +08:00
|
|
|
permission = permission.slice(0, -1);
|
2020-01-08 05:17:54 +08:00
|
|
|
if (path.startsWith(permission) && (!path[permission.length] || path[permission.length] === '/')) {
|
2018-02-04 08:50:42 +08:00
|
|
|
return true;
|
2017-07-04 01:17:49 +08:00
|
|
|
}
|
2018-05-13 00:19:51 +08:00
|
|
|
} else if (path === permission) {
|
2018-02-04 08:50:42 +08:00
|
|
|
return true;
|
2020-01-08 05:17:54 +08:00
|
|
|
} else if (path === '.' && permission === '/') {
|
2018-02-04 08:50:42 +08:00
|
|
|
return true;
|
2017-07-04 01:17:49 +08:00
|
|
|
}
|
2018-02-04 08:50:42 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
}
|
|
|
|
return false;
|
2021-04-18 21:48:35 +08:00
|
|
|
},
|
2017-07-04 01:17:49 +08:00
|
|
|
|
2021-04-18 21:48:35 +08:00
|
|
|
|
|
|
|
isDirectoryAvailable: (directory: DirectoryDTO, permissions: string[]): boolean => {
|
|
|
|
return UserDTOUtils.isDirectoryPathAvailable(
|
2020-01-08 05:17:54 +08:00
|
|
|
Utils.concatUrls(directory.path, directory.name), permissions);
|
2021-04-18 21:48:35 +08:00
|
|
|
}
|
|
|
|
};
|