Skip to content

Commit

Permalink
Add audit logs support & update the features API
Browse files Browse the repository at this point in the history
  • Loading branch information
fdel-car committed Jan 25, 2023
1 parent 0da8157 commit f3550ce
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 16 deletions.
1 change: 0 additions & 1 deletion packages/admin-test-utils/lib/setup/strapi.js
Expand Up @@ -17,7 +17,6 @@ global.strapi = {
isEE: false,
features: {
SSO: 'sso',
allFeatures: [],
isEnabled: () => false,
},
projectType: 'Community',
Expand Down
7 changes: 3 additions & 4 deletions packages/core/admin/admin/src/index.js
@@ -1,7 +1,7 @@
import ReactDOM from 'react-dom';
import appCustomisations from './app';
import { Components, Fields, Middlewares, Reducers } from './core/apis';
import { axiosInstance } from './core/utils';
import appCustomisations from './app';
// eslint-disable-next-line import/extensions
import plugins from './plugins';
import appReducers from './reducers';
Expand All @@ -12,7 +12,7 @@ window.strapi = {
telemetryDisabled: process.env.STRAPI_TELEMETRY_DISABLED ?? false,
features: {
SSO: 'sso',
auditLogs: 'audit-logs',
AUDIT_LOGS: 'audit-logs',
},
projectType: 'Community',
};
Expand All @@ -39,8 +39,7 @@ const run = async () => {
window.strapi.isEE = isEE;
window.strapi.features = {
...window.strapi.features,
allFeatures: features,
isEnabled: (f) => features.includes(f),
isEnabled: (featureName) => features.some((feature) => feature.name === featureName),
};

window.strapi.projectType = isEE ? 'Enterprise' : 'Community';
Expand Down
@@ -1,6 +1,6 @@
import adminPermissions from '../../../../../admin/src/permissions';

const auditLogsRoutes = strapi.features.isEnabled(strapi.features.auditLogs)
const auditLogsRoutes = strapi.features.isEnabled(strapi.features.AUDIT_LOGS)
? [
{
intlLabel: { id: 'global.auditLogs', defaultMessage: 'Audit Logs' },
Expand Down
Expand Up @@ -14,7 +14,7 @@ if (strapi.features.isEnabled(strapi.features.SSO)) {
});
}

if (strapi.features.isEnabled(strapi.features.auditLogs)) {
if (strapi.features.isEnabled(strapi.features.AUDIT_LOGS)) {
routes.push({
async Component() {
const component = await import(
Expand Down
4 changes: 2 additions & 2 deletions packages/core/admin/ee/server/services/__tests__/sso.test.js
Expand Up @@ -5,8 +5,8 @@ jest.mock('@strapi/strapi/ee', () => ({
isEnabled() {
return true;
},
getEnabled() {
return ['sso'];
list() {
return [{ name: 'sso' }];
},
},
}));
Expand Down
7 changes: 5 additions & 2 deletions packages/core/admin/ee/server/services/audit-logs.js
Expand Up @@ -2,8 +2,9 @@

const localProvider = require('@strapi/provider-audit-logs-local');
const { scheduleJob } = require('node-schedule');
const { features } = require('@strapi/strapi/lib/utils/ee');

const RETENTION_DAYS = 90;
const DEFAULT_RETENTION_DAYS = 90;

const defaultEvents = [
'entry.create',
Expand Down Expand Up @@ -88,10 +89,12 @@ const createAuditLogsService = (strapi) => {

return {
async register() {
const retentionDays =
features.get('audit-logs')?.options.retentionDays ?? DEFAULT_RETENTION_DAYS;
this._provider = await localProvider.register({ strapi });
this._eventHubUnsubscribe = strapi.eventHub.subscribe(handleEvent.bind(this));
this._deleteExpiredJob = scheduleJob('0 0 * * *', () => {
const expirationDate = new Date(Date.now() - RETENTION_DAYS * 24 * 60 * 60 * 1000);
const expirationDate = new Date(Date.now() - retentionDays * 24 * 60 * 60 * 1000);
this._provider.deleteExpiredEvents(expirationDate);
});

Expand Down
Expand Up @@ -8,7 +8,7 @@ jest.mock('@strapi/strapi/lib/utils/ee', () => {
isEnabled() {
return false;
},
getEnabled() {
list() {
return [];
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/core/admin/server/controllers/admin.js
Expand Up @@ -37,7 +37,7 @@ module.exports = {
async getProjectType() {
// FIXME
try {
return { data: { isEE: strapi.EE, features: ee.features.getEnabled() } };
return { data: { isEE: strapi.EE, features: ee.features.list() } };
} catch (err) {
return { data: { isEE: false, features: [] } };
}
Expand Down
15 changes: 13 additions & 2 deletions packages/core/strapi/ee/index.js
Expand Up @@ -146,6 +146,16 @@ const checkLicense = async ({ strapi }) => {
}
};

const list = () => {
return (
ee.licenseInfo.features?.map((feature) =>
typeof feature === 'object' ? feature : { name: feature }
) || []
);
};

const get = (featureName) => list().find((feature) => feature.name === featureName);

module.exports = Object.freeze({
init,
checkLicense,
Expand All @@ -155,7 +165,8 @@ module.exports = Object.freeze({
},

features: Object.freeze({
isEnabled: (feature) => (ee.enabled && ee.licenseInfo.features?.includes(feature)) || false,
getEnabled: () => (ee.enabled && ee.licenseInfo.features) || [],
list,
get,
isEnabled: (featureName) => get(featureName) !== undefined,
}),
});
2 changes: 1 addition & 1 deletion packages/core/strapi/ee/license.js
Expand Up @@ -10,7 +10,7 @@ const machineId = require('../lib/utils/machine-id');
const DEFAULT_FEATURES = {
bronze: [],
silver: [],
gold: ['sso'],
gold: ['sso', { name: 'audit-logs', options: { retentionDays: 90 } }],
};

const publicKey = fs.readFileSync(join(__dirname, 'resources/key.pub'));
Expand Down

0 comments on commit f3550ce

Please sign in to comment.