mirror of
https://github.com/strapi/strapi.git
synced 2025-09-25 08:19:07 +00:00
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:
commit
bd9ddaa84c
20
packages/core/admin/server/bootstrap.js
vendored
20
packages/core/admin/server/bootstrap.js
vendored
@ -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();
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user