feat: add login for plugin
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import { app } from '@/app.ts';
|
||||
import { ConfigModel } from './models/model.ts';
|
||||
import { ShareConfigService } from './services/share.ts';
|
||||
import { oss } from '@/app.ts';
|
||||
import { ConfigOssService } from '@kevisual/oss/services';
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'config',
|
||||
@@ -31,56 +34,57 @@ app
|
||||
const tokernUser = ctx.state.tokenUser;
|
||||
const tuid = tokernUser.id;
|
||||
const { id, data, ...rest } = ctx.query?.data || {};
|
||||
let config: ConfigModel;
|
||||
if (id) {
|
||||
const config = await ConfigModel.findByPk(id);
|
||||
config = await ConfigModel.findByPk(id);
|
||||
let keyIsChange = false;
|
||||
if (rest?.key) {
|
||||
keyIsChange = rest.key !== config?.key;
|
||||
}
|
||||
if (config && config.uid === tuid) {
|
||||
if (keyIsChange) {
|
||||
const key = rest.key;
|
||||
const keyConfig = await ConfigModel.findOne({
|
||||
where: {
|
||||
key,
|
||||
uid: tuid,
|
||||
},
|
||||
});
|
||||
if (keyConfig && keyConfig.id !== id) {
|
||||
ctx.throw(403, 'key is already exists');
|
||||
}
|
||||
}
|
||||
await config.update({
|
||||
data: {
|
||||
...config.data,
|
||||
...data,
|
||||
},
|
||||
...rest,
|
||||
});
|
||||
if (config.data?.permission?.share === 'public') {
|
||||
await ShareConfigService.expireShareConfig(config.key, tokernUser.username);
|
||||
}
|
||||
ctx.body = config;
|
||||
} else {
|
||||
if (!config || config.uid !== tuid) {
|
||||
ctx.throw(403, 'no permission');
|
||||
}
|
||||
if (keyIsChange) {
|
||||
const key = rest.key;
|
||||
const keyConfig = await ConfigModel.findOne({
|
||||
where: {
|
||||
key,
|
||||
uid: tuid,
|
||||
},
|
||||
});
|
||||
if (keyConfig && keyConfig.id !== id) {
|
||||
ctx.throw(403, 'key is already exists');
|
||||
}
|
||||
}
|
||||
await config.update({
|
||||
data: {
|
||||
...config.data,
|
||||
...data,
|
||||
},
|
||||
...rest,
|
||||
});
|
||||
if (config.data?.permission?.share === 'public') {
|
||||
await ShareConfigService.expireShareConfig(config.key, tokernUser.username);
|
||||
}
|
||||
ctx.body = config;
|
||||
} else if (rest?.key) {
|
||||
// id 不存在,key存在,则属于更新,key不能重复
|
||||
const key = rest.key;
|
||||
const keyConfig = await ConfigModel.findOne({
|
||||
config = await ConfigModel.findOne({
|
||||
where: {
|
||||
key,
|
||||
uid: tuid,
|
||||
},
|
||||
});
|
||||
if (keyConfig) {
|
||||
await keyConfig.update({
|
||||
data: { ...keyConfig.data, ...data },
|
||||
if (config) {
|
||||
await config.update({
|
||||
data: { ...config.data, ...data },
|
||||
...rest,
|
||||
});
|
||||
ctx.body = keyConfig;
|
||||
ctx.body = config;
|
||||
} else {
|
||||
const config = await ConfigModel.create({
|
||||
// 根据key创建一个配置
|
||||
config = await ConfigModel.create({
|
||||
key,
|
||||
...rest,
|
||||
data: data,
|
||||
@@ -89,15 +93,33 @@ app
|
||||
ctx.body = config;
|
||||
}
|
||||
}
|
||||
if (id || rest?.key) return;
|
||||
const key = config?.key;
|
||||
const ossConfig = ConfigOssService.fromBase({
|
||||
oss,
|
||||
opts: {
|
||||
owner: tokernUser.username,
|
||||
},
|
||||
});
|
||||
if (ossConfig.isEndWithJson(key)) {
|
||||
const data = config.data;
|
||||
const hash = ossConfig.hash(data);
|
||||
if (config.hash !== hash) {
|
||||
config.hash = hash;
|
||||
await config.save({
|
||||
fields: ['hash'],
|
||||
});
|
||||
await ossConfig.putJsonObject(key, data);
|
||||
}
|
||||
}
|
||||
if (config) return;
|
||||
|
||||
// id和key不存在。创建一个新的配置
|
||||
const config = await ConfigModel.create({
|
||||
// id和key不存在。创建一个新的配置, 而且没有id的
|
||||
const newConfig = await ConfigModel.create({
|
||||
...rest,
|
||||
data: data,
|
||||
uid: tuid,
|
||||
});
|
||||
ctx.body = config;
|
||||
ctx.body = newConfig;
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
@@ -154,6 +176,18 @@ app
|
||||
},
|
||||
});
|
||||
if (config && config.uid === tuid) {
|
||||
const key = config.key;
|
||||
const ossConfig = ConfigOssService.fromBase({
|
||||
oss,
|
||||
opts: {
|
||||
owner: tokernUser.username,
|
||||
},
|
||||
});
|
||||
if (ossConfig.isEndWithJson(key)) {
|
||||
try {
|
||||
await ossConfig.deleteObject(key);
|
||||
} catch (e) {}
|
||||
}
|
||||
await config.destroy();
|
||||
} else {
|
||||
ctx.throw(403, 'no permission');
|
||||
|
||||
Reference in New Issue
Block a user