feat: add UserSecret
This commit is contained in:
@@ -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 };
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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';
|
||||
|
||||
136
src/routes/user/secret-key/list.ts
Normal file
136
src/routes/user/secret-key/list.ts
Normal 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);
|
||||
Reference in New Issue
Block a user