feat: add org user info

This commit is contained in:
xion 2025-02-28 13:26:00 +08:00
parent e96a246c14
commit 425b0d5286
4 changed files with 167 additions and 6 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@kevisual/code-center-module", "name": "@kevisual/code-center-module",
"version": "0.0.7", "version": "0.0.9",
"description": "", "description": "",
"main": "dist/system.mjs", "main": "dist/system.mjs",
"module": "dist/system.mjs", "module": "dist/system.mjs",

View File

@ -1,16 +1,123 @@
import { DataTypes, Model, Sequelize } from 'sequelize'; import { DataTypes, Model, Op, Sequelize } from 'sequelize';
import { useContextKey } from '@kevisual/use-config/context'; import { useContextKey } from '@kevisual/use-config/context';
import { SyncOpts } from './user.ts'; import { SyncOpts, User } from './user.ts';
type AddUserOpts = {
role: string;
};
export enum OrgRole {
admin = 'admin',
member = 'member',
owner = 'owner',
}
export class Org extends Model { export class Org extends Model {
declare id: string; declare id: string;
declare username: string; declare username: string;
declare description: string; declare description: string;
declare users: { role: string; uid: string }[]; declare users: { role: string; uid: string }[];
/**
* operateId id
* @param user
* @param opts
*/
async addUser(user: User, opts?: { operateId?: string; role: string; needPermission?: boolean; isAdmin?: boolean }) {
const hasUser = this.users.find((u) => u.uid === user.id);
if (hasUser) {
return;
}
if (user.type !== 'user') {
throw Error('Only user can be added to org');
}
if (opts?.needPermission) {
if (opts?.isAdmin) {
} else {
const adminUsers = this.users.filter((u) => u.role === 'admin' || u.role === 'owner');
const adminIds = adminUsers.map((u) => u.uid);
const hasPermission = adminIds.includes(opts.operateId);
if (!hasPermission) {
throw Error('No permission');
}
}
}
await user.expireOrgs();
const users = [...this.users];
users.push({ role: opts?.role || 'member', uid: user.id });
await Org.update({ users }, { where: { id: this.id } });
}
/**
* operateId id
* @param user
* @param opts
*/
async removeUser(user: User, opts?: { operateId?: string; needPermission?: boolean; isAdmin?: boolean }) {
if (opts?.needPermission) {
if (opts?.isAdmin) {
} else {
const adminUsers = this.users.filter((u) => u.role === 'admin' || u.role === 'owner');
const adminIds = adminUsers.map((u) => u.uid);
const hasPermission = adminIds.includes(opts.operateId);
if (!hasPermission) {
throw Error('No permission');
}
}
}
await user.expireOrgs();
const users = this.users.filter((u) => u.uid !== user.id && u.role !== 'owner');
await Org.update({ users }, { where: { id: this.id } });
}
/**
* operateId id
* @param user
* @param opts
*/
async getUsers(opts?: { operateId: string; needPermission?: boolean; isAdmin?: boolean }) {
const usersIds = this.users.map((u) => u.uid);
const orgUser = this.users;
if (opts?.needPermission) {
// 不在组织内或者不是管理员,如果需要权限,返回空
if (opts.isAdmin) {
} else {
const hasPermission = usersIds.includes(opts.operateId);
if (!hasPermission) {
return {
hasPermission: false,
users: [],
};
}
}
}
const _users = await User.findAll({
where: {
id: {
[Op.in]: usersIds,
},
},
});
const users = _users.map((u) => {
const role = orgUser.find((r) => r.uid === u.id)?.role;
return {
id: u.id,
username: u.username,
role: role,
};
});
return { users };
}
/**
* role
* @param user
* @param opts
*/
async getInRole(userId: string, role = 'admin') {
const user = this.users.find((u) => u.uid === userId && u.role === role);
return !!user;
}
} }
/** /**
* sequelize之后初始化 * sequelize之后初始化
*/ */
export const OrgInit = (newSequelize?: any, tableName?: string, sync?: SyncOpts) => { export const OrgInit = async (newSequelize?: any, tableName?: string, sync?: SyncOpts) => {
const sequelize = useContextKey<Sequelize>('sequelize'); const sequelize = useContextKey<Sequelize>('sequelize');
Org.init( Org.init(
{ {
@ -41,7 +148,7 @@ export const OrgInit = (newSequelize?: any, tableName?: string, sync?: SyncOpts)
}, },
); );
if (sync) { if (sync) {
Org.sync({ alter: true, logging: false, ...sync }).catch((e) => { await Org.sync({ alter: true, logging: false, ...sync }).catch((e) => {
console.error('Org sync', e); console.error('Org sync', e);
}); });
return Org; return Org;

View File

@ -14,6 +14,11 @@ const config = useConfig<{ tokenSecret: string }>();
type UserData = { type UserData = {
orgs?: string[]; orgs?: string[];
}; };
export enum UserTypes {
'user' = 'user',
'org' = 'org',
'visitor' = 'visitor',
}
/** /**
* ,sequelize和Org之后初始化 * ,sequelize和Org之后初始化
*/ */
@ -96,10 +101,11 @@ export class User extends Model {
await redis.del(`user:${me.id}:orgs`); await redis.del(`user:${me.id}:orgs`);
return newUser; return newUser;
} }
createPassword(password: string) { async createPassword(password: string) {
const salt = this.salt; const salt = this.salt;
const cPassword = cryptPwd(password, salt); const cPassword = cryptPwd(password, salt);
this.password = cPassword; this.password = cPassword;
await this.update({ password: cPassword });
return cPassword; return cPassword;
} }
checkPassword(password: string) { checkPassword(password: string) {

48
src/scripts/org-test.ts Normal file
View File

@ -0,0 +1,48 @@
import '../modules/init.ts';
import { User, UserInit } from '../models/user.ts';
import { Org, OrgInit } from '../models/org.ts';
import { where } from 'sequelize';
import { useContextKey } from '@kevisual/use-config/context';
const sequelize = useContextKey('sequelize');
const meUserName = 'o-NDO62XGeyEQoz_Sytz-1UUB7kw';
const id = '09cef25c-719d-422a-9ec4-2c266cc6e73d';
const rootId = '14206305-8b5c-44cc-b177-766cfe2e452f';
const systemOrgId = 'ca129111-4f30-476e-adf5-acf51ed5ce28';
export const main = async () => {
await UserInit();
await OrgInit();
// const users = await User.findAll();
// for (const user of users) {
// console.log('user', user.id, user.username);
// }
const me = await User.findByPk(id);
const org = await Org.findOne({
where: {
username: 'system',
},
});
console.log('org', org.toJSON());
const res = await org.addUser(me, {
operateId: rootId,
// operateId: id,
role: 'admin',
needPermission: true,
});
// await org.removeUser(me, {
// operateId: rootId,
// });
process.exit(0);
};
// main();
const updatePassword = async () => {
await UserInit();
await OrgInit();
const user = await User.findByPk(rootId);
await user.createPassword('xxxxxx');
process.exit(0);
};
updatePassword();