perf: user delete permission judgement (#1363)

Reviewed-on: daoyoucloud/tachybase#1363
Reviewed-by: sealday <zhanglin@daoyoucloud.com>
Co-authored-by: TomyJan <TomyJan6@gmail.com>
Co-committed-by: TomyJan <TomyJan6@gmail.com>
This commit is contained in:
TomyJan 2024-07-22 15:38:34 +08:00 committed by sealday
parent 270dc33d30
commit 7756c18cb1
5 changed files with 91 additions and 2 deletions

View File

@ -0,0 +1,28 @@
import { Migration } from '@tachybase/server';
export default class AddGuestSpecialRoleMigration extends Migration {
appVersion = '<0.21.88';
async up() {
const repo = this.context.db.getRepository('users');
const user = await repo.findOne({
filter: {
username: 'guest',
},
});
if (user) {
await repo.update({
values: {
specialRole: 'guest',
},
filter: {
id: user.id,
},
});
} else {
throw new Error('Guest user not found');
}
}
async down() {}
}

View File

@ -27,7 +27,7 @@ export class PluginShareServer extends Plugin {
this.app.acl.addFixedParams('users', 'destroy', () => { this.app.acl.addFixedParams('users', 'destroy', () => {
return { return {
filter: { filter: {
'username.$ne': 'guest', 'specialRole.$ne': 'guest',
}, },
}; };
}); });
@ -99,6 +99,7 @@ export class PluginShareServer extends Plugin {
password: guestPassword, password: guestPassword,
nickname: guestNickname, nickname: guestNickname,
username: guestUsername, username: guestUsername,
specialRole: 'guest',
}, },
}); });

View File

@ -98,5 +98,9 @@ export default defineCollection({
name: 'systemSettings', name: 'systemSettings',
defaultValue: {}, defaultValue: {},
}, },
{
type: 'string',
name: 'specialRole',
},
], ],
}); });

View File

@ -0,0 +1,55 @@
import { DataTypes } from '@tachybase/database';
import { Migration } from '@tachybase/server';
export default class AddUsersSpecialRoleMigration extends Migration {
on = 'beforeLoad';
appVersion = '<0.21.88';
async up() {
const collection = this.db.collection({
name: 'users',
fields: [
{
type: 'string',
name: 'specialRole',
},
],
});
const tableNameWithSchema = collection.getTableNameWithSchema();
const field = collection.getField('specialRole');
const exists = await field.existsInDb();
if (!exists) {
await this.db.sequelize.getQueryInterface().addColumn(tableNameWithSchema, field.columnName(), {
type: DataTypes.STRING,
});
}
try {
await this.db.sequelize.getQueryInterface().addConstraint(tableNameWithSchema, {
type: 'unique',
fields: [field.columnName()],
});
} catch (error) {
throw new Error(error);
}
const repo = this.context.db.getRepository('users');
const user = await repo.findOne({
filter: {
email: process.env.INIT_ROOT_EMAIL || 'admin@tachybase.com',
},
});
if (user) {
await repo.update({
values: {
specialRole: 'root',
},
filter: {
id: user.id,
},
});
} else {
throw new Error('Root user not found');
}
}
async down() {}
}

View File

@ -96,7 +96,7 @@ export default class PluginUsersServer extends Plugin {
this.app.acl.addFixedParams('users', 'destroy', () => { this.app.acl.addFixedParams('users', 'destroy', () => {
return { return {
filter: { filter: {
'id.$ne': 1, 'specialRole.$ne': 'root',
}, },
}; };
}); });
@ -175,6 +175,7 @@ export default class PluginUsersServer extends Plugin {
password: rootPassword, password: rootPassword,
nickname: rootNickname, nickname: rootNickname,
username: rootUsername, username: rootUsername,
specialRole: 'root',
}, },
}); });