fix(users): improve users:updateProfile (#4162)

* chore(users): improve `users:updateProfile`

* chore: use lodash.pick
This commit is contained in:
YANG QIA 2024-04-25 16:54:53 +08:00 committed by GitHub
parent b25db239a3
commit 5da0d4e75b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 36 additions and 14 deletions

View File

@ -165,17 +165,6 @@ describe('actions', () => {
expect(data.length).toBe(1);
});
it('update profile with roles', async () => {
const res2 = await adminAgent.resource('users').updateProfile({
filterByTk: adminUser.id,
values: {
nickname: 'a',
roles: adminUser.roles,
},
});
expect(res2.status).toBe(200);
});
it('can destroy users role', async () => {
const role2 = await db.getRepository('roles').create({
values: {

View File

@ -14,7 +14,7 @@ describe('actions', () => {
process.env.INIT_ROOT_PASSWORD = '123456';
process.env.INIT_ROOT_NICKNAME = 'Test';
app = await createMockServer({
plugins: ['auth', 'users'],
plugins: ['auth', 'users', 'acl', 'data-source-manager'],
});
db = app.db;
@ -23,6 +23,7 @@ describe('actions', () => {
filter: {
email: process.env.INIT_ROOT_EMAIL,
},
appends: ['roles'],
});
agent = app.agent();
@ -50,4 +51,35 @@ describe('actions', () => {
});
expect(res2.status).toBe(200);
});
it('update profile, but not roles', async () => {
expect(adminUser.roles.length).not.toBe(0);
const res2 = await adminAgent.resource('users').updateProfile({
filterByTk: adminUser.id,
values: {
nickname: 'a',
username: 'a',
email: 'test@nocobase.com',
phone: '12345678901',
systemSettings: {
...adminUser.systemSettings,
themeId: 1,
},
appLang: 'zh-CN',
roles: [],
},
});
expect(res2.status).toBe(200);
const user = await db.getRepository('users').findOne({
filterByTk: adminUser.id,
appends: ['roles'],
});
expect(user.nickname).toBe('a');
expect(user.username).toBe('a');
expect(user.email).toBe('test@nocobase.com');
expect(user.phone).toBe('12345678901');
expect(user.systemSettings.themeId).toBe(1);
expect(user.appLang).toBe('zh-CN');
expect(user.roles.length).not.toBe(0);
});
});

View File

@ -1,7 +1,8 @@
import { Context, DEFAULT_PAGE, DEFAULT_PER_PAGE, Next } from '@nocobase/actions';
import _ from 'lodash';
export async function updateProfile(ctx: Context, next: Next) {
const { values } = ctx.action.params;
const values = ctx.action.params.values || {};
const { currentUser } = ctx.state;
if (!currentUser) {
ctx.throw(401);
@ -9,7 +10,7 @@ export async function updateProfile(ctx: Context, next: Next) {
const UserRepo = ctx.db.getRepository('users');
const result = await UserRepo.update({
filterByTk: currentUser.id,
values,
values: _.pick(values, ['nickname', 'username', 'email', 'phone', 'systemSettings', 'appLang']),
});
ctx.body = result;
await next();