feat: 初始化项目用户初始化修改
This commit is contained in:
parent
8cdd54af04
commit
03c473d24a
@ -176,19 +176,18 @@ export const initializeUser = async () => {
|
||||
console.info('[User count]', w.count);
|
||||
const password = '2e8a305521bba54f49638ed25e46adf3'; //123456
|
||||
const salt = '123';
|
||||
const users = [{ username: 'admin' }, { username: 'user' }, { username: 'root' }];
|
||||
if (w.count < 1) {
|
||||
const newUsers = await User.bulkCreate(
|
||||
users.map((user) => {
|
||||
return {
|
||||
...user,
|
||||
password,
|
||||
needChangePassword: true,
|
||||
salt,
|
||||
};
|
||||
}),
|
||||
);
|
||||
console.info('[create new Users]', newUsers);
|
||||
const root = await User.create({
|
||||
username: 'root',
|
||||
password: password,
|
||||
needChangePassword: true,
|
||||
type: 'user',
|
||||
description: '系统管理员',
|
||||
salt,
|
||||
});
|
||||
const org = await User.createOrg('admin', root.id, '管理员');
|
||||
console.info(' new Users name', root.username, org.username);
|
||||
console.info('new Users id', root.id, org.id);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -12,7 +12,9 @@ app
|
||||
.define(async (ctx) => {
|
||||
const state = ctx.state?.tokenUser || {};
|
||||
const { id } = state;
|
||||
const user = await User.findByPk(id);
|
||||
const user = await User.findByPk(id, {
|
||||
logging: false,
|
||||
});
|
||||
if (!user) {
|
||||
throw new CustomError(500, 'user not found');
|
||||
}
|
||||
|
@ -23,6 +23,7 @@ app
|
||||
],
|
||||
},
|
||||
},
|
||||
logging: false,
|
||||
});
|
||||
|
||||
ctx.body = list;
|
||||
@ -30,21 +31,6 @@ app
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'org',
|
||||
key: 'get',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const id = ctx.query.id;
|
||||
if (!id) {
|
||||
throw new CustomError('id is required');
|
||||
}
|
||||
ctx.body = await Org.findByPk(id);
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'org',
|
||||
@ -53,10 +39,27 @@ app
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { username, description } = ctx.query.data;
|
||||
const { username, description, id } = ctx.query.data;
|
||||
if (!username) {
|
||||
throw new CustomError('username is required');
|
||||
}
|
||||
if (id) {
|
||||
const org = await Org.findByPk(id);
|
||||
if (!org) {
|
||||
throw new CustomError('org not found');
|
||||
}
|
||||
org.description = description;
|
||||
await org.save();
|
||||
const user = await User.findOne({ where: { username } });
|
||||
user.description = description;
|
||||
await user.save();
|
||||
ctx.body = {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
description: user.description,
|
||||
};
|
||||
return;
|
||||
}
|
||||
const user = await User.createOrg(username, tokenUser.id, description);
|
||||
ctx.body = {
|
||||
id: user.id,
|
||||
@ -96,3 +99,47 @@ app
|
||||
ctx.body = 'success';
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'org',
|
||||
key: 'get',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const id = ctx.query.id;
|
||||
if (!id) {
|
||||
throw new CustomError('id is required');
|
||||
}
|
||||
const org = await Org.findByPk(id);
|
||||
if (!org) {
|
||||
throw new CustomError('org not found');
|
||||
}
|
||||
const usersIds = org.users;
|
||||
const me = usersIds.find((u) => u.uid === tokenUser.id);
|
||||
if (!me) {
|
||||
throw new CustomError('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,
|
||||
};
|
||||
});
|
||||
ctx.body = {
|
||||
org,
|
||||
users,
|
||||
};
|
||||
})
|
||||
.addTo(app);
|
||||
|
Loading…
x
Reference in New Issue
Block a user