1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/src/common/entities/UserDTO.ts

58 lines
1.5 KiB
TypeScript
Raw Normal View History

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,
Developer = 5
2016-12-27 23:09:47 +08:00
}
export interface UserDTO {
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;
permissions: string[]; // user can only see these permissions. if ends with *, its recursive
2017-07-04 01:17:49 +08:00
}
export const UserDTOUtils = {
2017-07-04 01:17:49 +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;
}
for (let permission of permissions) {
if (permission === '/*') {
2019-02-23 07:12:59 +08:00
return true;
}
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
}
} 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;
},
2017-07-04 01:17:49 +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);
}
};