feat: update org operate

This commit is contained in:
2025-02-28 13:35:13 +08:00
parent 409067f13f
commit d2280f6b89
14 changed files with 542 additions and 400 deletions

View File

@@ -60,20 +60,11 @@ add.run = async (ctx) => {
},
});
await containerModel.save();
if (containerModel.code || containerModel.source || containerModel.sourceType) {
ctx.emit?.('pageEdit', {
source: 'container',
data: containerModel.toJSON(),
operation: 'edit',
});
}
}
} else {
try {
containerModel = await ContainerModel.create({
...container,
source: '',
sourceType: '',
uid: tokenUser.id,
});
} catch (e) {
@@ -127,25 +118,29 @@ app
if (!key || !version || !fileName) {
return;
}
const uploadResult = await uploadMinioContainer({
key,
tokenUser: ctx.state.tokenUser,
version: version,
code: container.code,
filePath: fileName,
saveHTML,
});
await ctx.call({
path: 'app',
key: 'uploadFiles',
payload: {
token,
data: {
appKey: key,
version,
files: uploadResult,
if (container.type === 'render-js') {
const uploadResult = await uploadMinioContainer({
key,
tokenUser: ctx.state.tokenUser,
version: version,
code: container.code,
filePath: fileName,
saveHTML,
});
await ctx.call({
path: 'app',
key: 'uploadFiles',
payload: {
token,
data: {
appKey: key,
version,
files: uploadResult,
},
},
},
});
});
} else {
ctx.throw(500, 'container type not supported:' + container.type);
}
})
.addTo(app);

View File

@@ -16,16 +16,23 @@ export type Container = Partial<InstanceType<typeof ContainerModel>>;
*/
export class ContainerModel extends Model {
declare id: string;
// 标题
declare title: string;
// 描述
declare description: string;
// 类型
declare type: string;
// 标签
declare tags: string[];
// 代码
declare code: string;
// hash 值
declare hash: string;
declare source: string;
declare sourceType: string;
// 数据
declare data: ContainerData;
// 发布
declare publish: ContainerPublish;
// 用户 id
declare uid: string;
declare updatedAt: Date;
declare createdAt: Date;
@@ -45,11 +52,11 @@ ContainerModel.init(
comment: 'id',
},
title: {
type: DataTypes.STRING,
type: DataTypes.TEXT,
defaultValue: '',
},
description: {
type: DataTypes.STRING,
type: DataTypes.TEXT,
defaultValue: '',
},
tags: {
@@ -57,8 +64,8 @@ ContainerModel.init(
defaultValue: [],
},
type: {
type: DataTypes.STRING,
defaultValue: '',
type: DataTypes.STRING, // 代码类型, html, js, render-js
defaultValue: 'render-js',
},
code: {
type: DataTypes.TEXT,
@@ -68,14 +75,6 @@ ContainerModel.init(
type: DataTypes.TEXT,
defaultValue: '',
},
source: {
type: DataTypes.STRING,
defaultValue: '',
},
sourceType: {
type: DataTypes.STRING,
defaultValue: '',
},
data: {
type: DataTypes.JSON,
defaultValue: {},

View File

@@ -7,4 +7,6 @@ import './update.ts'
import './init.ts'
import './web-login.ts'
import './web-login.ts'
import './org-user/list.ts'

View File

@@ -72,6 +72,7 @@ app
if (!user.checkPassword(password)) {
ctx.throw(500, 'Password error');
}
user.expireOrgs();
const token = await user.createToken(null, loginType);
createCookie(token, ctx);
ctx.body = token;

View File

View File

@@ -0,0 +1,55 @@
import { app } from '@/app.ts';
import { User } from '@/models/user.ts';
import { Org } from '@/models/org.ts';
// 获取组织的用户列表
app
.route({
path: 'org-user',
key: 'list',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { orgId } = ctx.query;
const org = await Org.findByPk(orgId);
if (!org) {
ctx.throw('组织不存在');
}
// const users = await user.getUsers();
ctx.body = org;
})
.addTo(app);
app
.route({
path: 'org-user',
key: 'operate',
middleware: ['check-auth-admin'],
})
.define(async (ctx) => {
const tokenAdmin = ctx.state.tokenAdmin;
const tokenUser = ctx.state.tokenUser;
const data = ctx.query.data;
const { orgId, userId, action } = data;
const org = await Org.findByPk(orgId);
if (!org) {
ctx.throw('组织不存在');
}
const user = await User.findByPk(userId);
if (!user) {
ctx.throw('用户不存在');
}
if (user.type !== 'user') {
ctx.throw('用户类型错误');
}
const operateId = tokenUser.uid || tokenUser.id;
if (action === 'add') {
await org.addUser(user, { needPermission: true, role: 'user', operateId, isAdmin: !!tokenAdmin });
} else if (action === 'remove') {
await org.removeUser(user, { needPermission: true, operateId, isAdmin: !!tokenAdmin });
} else {
ctx.throw('操作错误');
}
ctx.body = 'ok';
})
.addTo(app);

View File

@@ -1,7 +1,6 @@
import { app, sequelize } from '@/app.ts';
import { Org } from '@/models/org.ts';
import { User } from '@/models/user.ts';
import { CustomError } from '@kevisual/router';
import { Op } from 'sequelize';
app
@@ -35,18 +34,19 @@ app
.route({
path: 'org',
key: 'update',
middleware: ['auth'],
middleware: ['auth-admin'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
// username 为org的名字在用户表中也是唯一的
const { username, description, id } = ctx.query.data;
if (!username) {
throw new CustomError('username is required');
ctx.throw('username is required');
}
if (id) {
const org = await Org.findByPk(id);
if (!org) {
throw new CustomError('org not found');
ctx.throw('org not found');
}
org.description = description;
await org.save();
@@ -62,11 +62,11 @@ app
}
const user = await User.findByPk(tokenUser.id);
if (!user) {
throw new CustomError('user not found');
ctx.throw('user not found');
}
const orgs = await user.getOrgs();
if (!orgs.includes('admin')) {
throw new CustomError('Permission denied');
ctx.throw('Permission denied');
}
const newUser = await User.createOrg(username, tokenUser.id, description);
ctx.body = {
@@ -87,17 +87,17 @@ app
const tokenUser = ctx.state.tokenUser;
const id = ctx.query.id;
if (!id) {
throw new CustomError('id is required');
ctx.throw('id is required');
}
const org = await Org.findByPk(id);
if (!org) {
throw new CustomError('org not found');
ctx.throw('org not found');
}
const username = org.username;
const users = org.users;
const owner = users.find((u) => u.role === 'owner');
if (owner.uid !== tokenUser.id) {
throw new CustomError('Permission denied');
ctx.throw('Permission denied');
}
await org.destroy({ force: true });
const orgUser = await User.findOne({
@@ -118,36 +118,21 @@ app
const tokenUser = ctx.state.tokenUser;
const id = ctx.query.id;
if (!id) {
throw new CustomError('id is required');
ctx.throw('id is required');
}
const org = await Org.findByPk(id);
if (!org) {
throw new CustomError('org not found');
ctx.throw('org not found');
}
const usersIds = org.users;
const me = usersIds.find((u) => u.uid === tokenUser.id);
if (!me) {
throw new CustomError('Permission denied');
ctx.throw('Permission denied');
}
const _users = await User.findAll({
where: {
id: {
[Op.in]: usersIds.map((u) => u.uid),
},
},
});
const users = _users.map((u) => {
const role = usersIds.find((r) => r.uid === u.id)?.role;
return {
id: u.id,
username: u.username,
role: role,
};
});
const orgGetUser = await org.getUsers();
ctx.body = {
org,
users,
users: orgGetUser.users,
};
})
.addTo(app);