Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide creator fields from public api by default #8052

Merged
merged 15 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions docs/v3.x/concepts/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,16 @@ The options key on the model-json states.

- `privateAttributes`: This configuration allows to treat a set of attributes as private, even if they're not actually defined as attributes in the model. Accepts an `Array` of strings. It could be used to remove from API responses timestamps or `_v` when using MongoDB. The set of `privateAttributes` defined in the model are merged with the `privateAttributes` defined in the global Strapi configuration.

- `populateCreatorFields`: Configure whether the API response should include `created_by` and `updated_by` fields or not. Accepts a `boolean`. The default value is `false`.

**Path β€”** `User.settings.json`.

```json
{
"options": {
"timestamps": true,
"privateAttributes": ["id", "created_at"]
"privateAttributes": ["id", "created_at"],
"populateCreatorFields": true
}
}
```
Expand Down Expand Up @@ -461,7 +464,7 @@ xhr.send(

::: tab "Polymorphic" id="polymorphic"

Polymorphic relationships are the solution when you don't know which kind of model will be associated to your entry, or when you want to connect different types of models to a model.
Polymorphic relationships are the solution when you don't know which kind of model will be associated to your entry, or when you want to connect different types of models to a model.
A common use case is an `Image` model that can be associated to different types of models (Article, Product, User, etc.).

#### Single vs Many
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ describe('Permissions Manager', () => {
global.strapi = {
getModel() {
return {
privateAttributes: [],
attributes: {
title: {
type: 'text',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module.exports = (ability, action, model) => ({
} = options;

if (_.isArray(data)) {
return data.map(this.sanitize.bind(this));
return data.map(entity => this.sanitize(entity, { action, withPrivate, isOutput }));
}

const permittedFields = permittedFieldsOf(ability, actionOverride, subject);
Expand Down
7 changes: 5 additions & 2 deletions packages/strapi-admin/test/admin-role.test.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { registerAndLogin } = require('../../../test/helpers/auth');
const { createAuthRequest } = require('../../../test/helpers/request');

const edition = process.env.STRAPI_DISABLE_EE === 'true' ? 'CE' : 'EE';
const sortPermissionArray = arr => _.sortBy(arr, ['action', 'subject']);

let rq;

Expand Down Expand Up @@ -661,8 +662,10 @@ describe('Role CRUD End to End', () => {

expect(res.statusCode).toBe(200);
expect(res.body.data.length > 0).toBe(true);
expect(res.body.data).toMatchObject(
permissions.map(perm => ({ subject: null, fields: null, conditions: [], ...perm }))
expect(sortPermissionArray(res.body.data)).toMatchObject(
sortPermissionArray(
permissions.map(perm => ({ subject: null, fields: null, conditions: [], ...perm }))
)
);
});

Expand Down
8 changes: 7 additions & 1 deletion packages/strapi-connector-bookshelf/lib/mount-models.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const _ = require('lodash');
const { singular } = require('pluralize');

const utilsModels = require('strapi-utils').models;
const { models: utilsModels, contentTypes } = require('strapi-utils');
const relations = require('./relations');
const buildDatabaseSchema = require('./build-database-schema');
const {
Expand Down Expand Up @@ -115,15 +115,19 @@ module.exports = ({ models, target }, ctx) => {
GLOBALS,
});

const isPrivate = !_.get(definition, 'options.populateCreatorFields', false);

if (!definition.uid.startsWith('strapi::') && definition.modelType !== 'component') {
definition.attributes['created_by'] = {
model: 'user',
plugin: 'admin',
private: isPrivate,
};

definition.attributes['updated_by'] = {
model: 'user',
plugin: 'admin',
private: isPrivate,
};
}

Expand Down Expand Up @@ -609,6 +613,8 @@ module.exports = ({ models, target }, ctx) => {
target[model].updateRelations = relations.update;
target[model].deleteRelations = relations.deleteRelations;

target[model].privateAttributes = contentTypes.getPrivateAttributes(target[model]);

await buildDatabaseSchema({
ORM,
definition,
Expand Down
10 changes: 8 additions & 2 deletions packages/strapi-connector-mongoose/lib/mount-models.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const _ = require('lodash');
const mongoose = require('mongoose');

const utilsModels = require('strapi-utils').models;
const { models: utilsModels, contentTypes } = require('strapi-utils');
const utils = require('./utils');
const relations = require('./relations');
const { findComponentByGlobalId } = require('./utils/helpers');
Expand All @@ -27,15 +27,19 @@ module.exports = ({ models, target }, ctx) => {
primaryKeyType: 'string',
});

const isPrivate = !_.get(definition, 'options.populateCreatorFields', false);

if (!definition.uid.startsWith('strapi::') && definition.modelType !== 'component') {
definition.attributes['created_by'] = {
model: 'user',
plugin: 'admin',
private: isPrivate,
};

definition.attributes['updated_by'] = {
model: 'user',
plugin: 'admin',
private: isPrivate,
};
}

Expand Down Expand Up @@ -128,7 +132,7 @@ module.exports = ({ models, target }, ctx) => {

target[model].allAttributes = _.clone(definition.attributes);

// Use provided timestamps if the elemnets in the array are string else use default.
// Use provided timestamps if the elements in the array are string else use default.
const timestampsOption = _.get(definition, 'options.timestamps', true);
if (_.isArray(timestampsOption)) {
const [createAtCol = 'createdAt', updatedAtCol = 'updatedAt'] = timestampsOption;
Expand Down Expand Up @@ -269,6 +273,8 @@ module.exports = ({ models, target }, ctx) => {
target[model]._attributes = definition.attributes;
target[model].updateRelations = relations.update;
target[model].deleteRelations = relations.deleteRelations;

target[model].privateAttributes = contentTypes.getPrivateAttributes(target[model]);
}

// Parse every authenticated model.
Expand Down