Merge pull request #14248 from strapi/api-token-v2/db-auto-sync-and-cleanup

Automatically synchronize and cleanup API tokens' permissions in database
This commit is contained in:
Jean-Sébastien Herbaux 2022-08-30 10:54:46 +02:00 committed by GitHub
commit bd9ddaa84c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 4 deletions

View File

@ -1,6 +1,7 @@
'use strict';
const { merge } = require('lodash/fp');
const { merge, map, difference, uniq } = require('lodash/fp');
const { pipeAsync } = require('@strapi/utils');
const { getService } = require('./utils');
const adminActions = require('./config/admin-actions');
const adminConditions = require('./config/admin-conditions');
@ -52,6 +53,22 @@ const syncAuthSettings = async () => {
await adminStore.set({ key: 'auth', value: newAuthSettings });
};
const syncAPITokensPermissions = async () => {
const validPermissions = strapi.contentAPI.permissions.providers.action.keys();
const permissionsInDB = await pipeAsync(
strapi.query('admin::api-token-permission').findMany,
map('action')
)();
const unknownPermissions = uniq(difference(permissionsInDB, validPermissions));
if (unknownPermissions.length > 0) {
await strapi
.query('admin::api-token-permission')
.deleteMany({ where: { action: { $in: unknownPermissions } } });
}
};
module.exports = async () => {
await registerAdminConditions();
await registerPermissionActions();
@ -73,6 +90,7 @@ module.exports = async () => {
await userService.displayWarningIfUsersDontHaveRole();
await syncAuthSettings();
await syncAPITokensPermissions();
apiTokenService.checkSaltIsDefined();
tokenService.checkSecretIsDefined();

View File

@ -451,11 +451,10 @@ class Strapi {
await this.server.initMiddlewares();
await this.server.initRouting();
await this.runLifecyclesFunctions(LIFECYCLES.BOOTSTRAP);
// TODO: is this the best place for this?
await this.contentAPI.permissions.registerActions();
await this.runLifecyclesFunctions(LIFECYCLES.BOOTSTRAP);
this.cron.start();
return this;