feat: change permission for mino -resources
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import { ConfigModel } from '../models/model.ts';
|
||||
import { ConfigModel, Config } from '../models/model.ts';
|
||||
import { CustomError } from '@kevisual/router';
|
||||
import { redis } from '@/app.ts';
|
||||
import { User } from '@/models/user.ts';
|
||||
import { UserPermission, UserPermissionOptions } from '@kevisual/permission';
|
||||
|
||||
export class ShareConfigService extends ConfigModel {
|
||||
/**
|
||||
* 获取分享的配置
|
||||
@@ -9,10 +11,23 @@ export class ShareConfigService extends ConfigModel {
|
||||
* @param username 分享者的username
|
||||
* @returns 配置
|
||||
*/
|
||||
static async getShareConfig(key: string, username: string) {
|
||||
const shareCacheConfig = await redis.get(`config:share:${username}:${key}`);
|
||||
static async getShareConfig(key: string, username: string, options: UserPermissionOptions) {
|
||||
const shareCacheConfigString = await redis.get(`config:share:${username}:${key}`);
|
||||
let shareCacheConfig: Config;
|
||||
try {
|
||||
shareCacheConfig = JSON.parse(shareCacheConfigString);
|
||||
} catch (e) {
|
||||
await redis.set(`config:share:${username}:${key}`, '', 'EX', 0); // 删除缓存
|
||||
throw new CustomError(400, 'config parse error');
|
||||
}
|
||||
const owner = username;
|
||||
if (shareCacheConfig) {
|
||||
return JSON.parse(shareCacheConfig);
|
||||
const permission = new UserPermission({ permission: shareCacheConfig?.data?.permission, owner });
|
||||
const result = permission.checkPermissionSuccess(options);
|
||||
if (!result.success) {
|
||||
throw new CustomError(403, 'no permission');
|
||||
}
|
||||
return shareCacheConfig;
|
||||
}
|
||||
const user = await User.findOne({
|
||||
where: { username },
|
||||
@@ -26,8 +41,9 @@ export class ShareConfigService extends ConfigModel {
|
||||
if (!config) {
|
||||
throw new CustomError(404, 'config not found');
|
||||
}
|
||||
const configData = config?.data?.permission;
|
||||
if (configData?.share !== 'public') {
|
||||
const permission = new UserPermission({ permission: config?.data?.permission, owner });
|
||||
const result = permission.checkPermissionSuccess(options);
|
||||
if (!result.success) {
|
||||
throw new CustomError(403, 'no permission');
|
||||
}
|
||||
await redis.set(`config:share:${username}:${key}`, JSON.stringify(config), 'EX', 60 * 60 * 24 * 7); // 7天
|
||||
@@ -35,7 +51,7 @@ export class ShareConfigService extends ConfigModel {
|
||||
}
|
||||
static async expireShareConfig(key: string, username: string) {
|
||||
if (key && username) {
|
||||
await redis.del(`config:share:${username}:${key}`);
|
||||
await redis.set(`config:share:${username}:${key}`, '', 'EX', 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user