feat: add UserSecret

This commit is contained in:
2025-06-20 16:22:59 +08:00
parent d29f69452c
commit 633eee4bee
17 changed files with 588 additions and 526 deletions

View File

@@ -1,3 +1,3 @@
import { FileSyncModel, FileSyncModelType } from '@kevisual/file-listener/src/file-sync/model.ts';
import { FileSyncModel } from '@kevisual/file-listener/src/file-sync/model.ts';
import type { FileSyncModelType } from '@kevisual/file-listener/src/file-sync/model.ts';
export { FileSyncModel, FileSyncModelType };

View File

@@ -19,7 +19,7 @@ app
const tokenUser = ctx.state.tokenUser;
const data = ctx.query?.data;
const { id, key, force, install, appKey: postAppKey, version: postVersion = '1.0.0' } = data;
if (!id || !postAppKey) {
if (!id && !postAppKey) {
ctx.throw(400, 'Invalid id or postAppKey');
}
let username = tokenUser.username;

View File

@@ -3,12 +3,14 @@ import './org.ts';
import './me.ts';
import './update.ts'
import './update.ts';
import './init.ts'
import './init.ts';
import './web-login.ts'
import './web-login.ts';
import './org-user/list.ts'
import './org-user/list.ts';
import './admin/user.ts';
import './admin/user.ts';
import './secret-key/list.ts';

View File

@@ -0,0 +1,136 @@
import { Op } from 'sequelize';
import { User, UserSecret } from '@/models/user.ts';
import { app } from '@/app.ts';
app
.route({
path: 'secret',
key: 'list',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { page = 1, pageSize = 20, search, sort = 'DESC', orgId } = ctx.query;
const searchWhere: Record<string, any> = search
? {
[Op.or]: [{ title: { [Op.like]: `%${search}%` } }, { description: { [Op.like]: `%${search}%` } }],
}
: {};
if (orgId) {
searchWhere.orgId = orgId;
}
const { rows: secrets, count } = await UserSecret.findAndCountAll({
where: {
userId: tokenUser.userId,
...searchWhere,
},
offset: (page - 1) * pageSize,
limit: pageSize,
attributes: {
exclude: ['token'], // Exclude sensitive token field
},
order: [['updatedAt', sort]],
});
ctx.body = {
list: secrets,
pagination: {
page,
current: page,
pageSize,
total: count,
},
};
})
.addTo(app);
app
.route({
path: 'secret',
key: 'update',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { id, updatedAt: _clear, createdAt: _clear2, token, ...rest } = ctx.query.data;
let secret: UserSecret;
let isNew = false;
if (id) {
secret = await UserSecret.findByPk(id);
if (!secret) {
ctx.throw(404, 'Secret not found');
}
if (secret.userId !== tokenUser.userId) {
ctx.throw(403, 'No permission');
}
} else {
secret = await UserSecret.createSecret(tokenUser);
isNew = true;
}
if (secret) {
secret = await secret.update({
...rest,
});
}
ctx.body = secret;
})
.addTo(app);
app
.route({
path: 'secret',
key: 'delete',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { id } = ctx.query.data || {};
if (!id) {
ctx.throw(400, 'id is required');
}
const secret = await UserSecret.findByPk(id);
if (!secret) {
ctx.throw(404, 'Secret not found');
}
if (secret.userId !== tokenUser.userId) {
ctx.throw(403, 'No permission');
}
await secret.destroy();
ctx.body = secret;
})
.addTo(app);
app
.route({
path: 'secret',
key: 'get',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { id } = ctx.query.data || {};
if (!id) {
ctx.throw(400, 'id is required');
}
const secret = await UserSecret.findByPk(id);
if (!secret) {
ctx.throw(404, 'Secret not found');
}
if (secret.userId !== tokenUser.uid) {
ctx.throw(403, 'No permission');
}
ctx.body = secret;
})
.addTo(app);