mirror of
https://github.com/strapi/strapi.git
synced 2025-09-26 17:00:55 +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';
|
'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 { getService } = require('./utils');
|
||||||
const adminActions = require('./config/admin-actions');
|
const adminActions = require('./config/admin-actions');
|
||||||
const adminConditions = require('./config/admin-conditions');
|
const adminConditions = require('./config/admin-conditions');
|
||||||
@ -52,6 +53,22 @@ const syncAuthSettings = async () => {
|
|||||||
await adminStore.set({ key: 'auth', value: newAuthSettings });
|
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 () => {
|
module.exports = async () => {
|
||||||
await registerAdminConditions();
|
await registerAdminConditions();
|
||||||
await registerPermissionActions();
|
await registerPermissionActions();
|
||||||
@ -73,6 +90,7 @@ module.exports = async () => {
|
|||||||
await userService.displayWarningIfUsersDontHaveRole();
|
await userService.displayWarningIfUsersDontHaveRole();
|
||||||
|
|
||||||
await syncAuthSettings();
|
await syncAuthSettings();
|
||||||
|
await syncAPITokensPermissions();
|
||||||
|
|
||||||
apiTokenService.checkSaltIsDefined();
|
apiTokenService.checkSaltIsDefined();
|
||||||
tokenService.checkSecretIsDefined();
|
tokenService.checkSecretIsDefined();
|
||||||
|
@ -451,11 +451,10 @@ class Strapi {
|
|||||||
await this.server.initMiddlewares();
|
await this.server.initMiddlewares();
|
||||||
await this.server.initRouting();
|
await this.server.initRouting();
|
||||||
|
|
||||||
await this.runLifecyclesFunctions(LIFECYCLES.BOOTSTRAP);
|
|
||||||
|
|
||||||
// TODO: is this the best place for this?
|
|
||||||
await this.contentAPI.permissions.registerActions();
|
await this.contentAPI.permissions.registerActions();
|
||||||
|
|
||||||
|
await this.runLifecyclesFunctions(LIFECYCLES.BOOTSTRAP);
|
||||||
|
|
||||||
this.cron.start();
|
this.cron.start();
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user