@strapi/i18n 5.48.1 → 5.50.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/admin/components/CMHeaderActions.js +3 -2
- package/dist/admin/components/CMHeaderActions.js.map +1 -1
- package/dist/admin/components/CheckboxConfirmation.js +3 -2
- package/dist/admin/components/CheckboxConfirmation.js.map +1 -1
- package/dist/admin/components/CreateLocale.js +4 -3
- package/dist/admin/components/CreateLocale.js.map +1 -1
- package/dist/admin/components/DeleteLocale.js +3 -2
- package/dist/admin/components/DeleteLocale.js.map +1 -1
- package/dist/admin/components/EditLocale.js +3 -2
- package/dist/admin/components/EditLocale.js.map +1 -1
- package/dist/admin/components/LocalePicker.js +3 -2
- package/dist/admin/components/LocalePicker.js.map +1 -1
- package/dist/admin/components/LocaleTable.js +3 -2
- package/dist/admin/components/LocaleTable.js.map +1 -1
- package/dist/admin/hooks/useAILocalizationJobsPolling.js +3 -2
- package/dist/admin/hooks/useAILocalizationJobsPolling.js.map +1 -1
- package/dist/admin/hooks/useI18n.js +7 -3
- package/dist/admin/hooks/useI18n.js.map +1 -1
- package/dist/admin/index.js +8 -3
- package/dist/admin/index.js.map +1 -1
- package/dist/admin/index.mjs +1 -0
- package/dist/admin/index.mjs.map +1 -1
- package/dist/admin/middlewares/extendCTBAttributeInitialData.js +6 -2
- package/dist/admin/middlewares/extendCTBAttributeInitialData.js.map +1 -1
- package/dist/admin/middlewares/rbac-middleware.js +3 -2
- package/dist/admin/middlewares/rbac-middleware.js.map +1 -1
- package/dist/admin/src/contentReleasesHooks/releaseDetailsView.d.ts +1 -1
- package/dist/admin/src/services/relations.d.ts +2 -2
- package/dist/admin/src/services/settings.d.ts +1 -1
- package/dist/admin/src/utils/schemas.d.ts +6 -6
- package/dist/admin/translations/ja.json.js +92 -0
- package/dist/admin/translations/ja.json.js.map +1 -0
- package/dist/admin/translations/ja.json.mjs +90 -0
- package/dist/admin/translations/ja.json.mjs.map +1 -0
- package/dist/admin/utils/fields.js +5 -1
- package/dist/admin/utils/fields.js.map +1 -1
- package/dist/admin/utils/schemas.js +6 -2
- package/dist/admin/utils/schemas.js.map +1 -1
- package/dist/server/controllers/locales.js +3 -2
- package/dist/server/controllers/locales.js.map +1 -1
- package/dist/server/register.js +10 -2
- package/dist/server/register.js.map +1 -1
- package/dist/server/register.mjs +4 -0
- package/dist/server/register.mjs.map +1 -1
- package/dist/server/routes/validation/locale.js +3 -2
- package/dist/server/routes/validation/locale.js.map +1 -1
- package/dist/server/services/content-types.js +6 -2
- package/dist/server/services/content-types.js.map +1 -1
- package/dist/server/services/content-types.mjs.map +1 -1
- package/dist/server/services/localizations.js.map +1 -1
- package/dist/server/services/localizations.mjs.map +1 -1
- package/dist/server/services/permissions/actions.js +95 -1
- package/dist/server/services/permissions/actions.js.map +1 -1
- package/dist/server/services/permissions/actions.mjs +95 -1
- package/dist/server/services/permissions/actions.mjs.map +1 -1
- package/dist/server/services/permissions/sections-builder.js +6 -4
- package/dist/server/services/permissions/sections-builder.js.map +1 -1
- package/dist/server/services/permissions/sections-builder.mjs +6 -4
- package/dist/server/services/permissions/sections-builder.mjs.map +1 -1
- package/dist/server/src/controllers/ai-localization-jobs.d.ts.map +1 -1
- package/dist/server/src/controllers/content-types.d.ts +2 -3
- package/dist/server/src/controllers/content-types.d.ts.map +1 -1
- package/dist/server/src/controllers/index.d.ts +2 -3
- package/dist/server/src/controllers/index.d.ts.map +1 -1
- package/dist/server/src/domain/locale.d.ts.map +1 -1
- package/dist/server/src/graphql.d.ts.map +1 -1
- package/dist/server/src/index.d.ts +12 -11
- package/dist/server/src/index.d.ts.map +1 -1
- package/dist/server/src/register.d.ts.map +1 -1
- package/dist/server/src/services/ai-localization-jobs.d.ts +1 -1
- package/dist/server/src/services/ai-localization-jobs.d.ts.map +1 -1
- package/dist/server/src/services/ai-localizations.d.ts.map +1 -1
- package/dist/server/src/services/content-types.d.ts +1 -1
- package/dist/server/src/services/content-types.d.ts.map +1 -1
- package/dist/server/src/services/fill-from-locale.d.ts.map +1 -1
- package/dist/server/src/services/index.d.ts +10 -8
- package/dist/server/src/services/index.d.ts.map +1 -1
- package/dist/server/src/services/localizations.d.ts.map +1 -1
- package/dist/server/src/services/permissions/actions.d.ts +5 -0
- package/dist/server/src/services/permissions/actions.d.ts.map +1 -1
- package/dist/server/src/services/permissions/conditions.d.ts.map +1 -1
- package/dist/server/src/services/permissions/engine.d.ts.map +1 -1
- package/dist/server/src/services/permissions/sections-builder.d.ts.map +1 -1
- package/dist/server/src/services/permissions.d.ts +5 -0
- package/dist/server/src/services/permissions.d.ts.map +1 -1
- package/dist/server/src/services/sanitize/index.d.ts +0 -1
- package/dist/server/src/services/sanitize/index.d.ts.map +1 -1
- package/dist/server/src/services/settings.d.ts.map +1 -1
- package/dist/server/src/utils/index.d.ts +15 -15
- package/dist/server/src/utils/index.d.ts.map +1 -1
- package/dist/server/src/validation/content-types.d.ts +2 -2
- package/dist/server/src/validation/content-types.d.ts.map +1 -1
- package/dist/server/src/validation/locales.d.ts +2 -2
- package/dist/server/src/validation/settings.d.ts +1 -1
- package/package.json +9 -9
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
var z = require('zod/v4');
|
|
4
4
|
|
|
5
|
-
function
|
|
5
|
+
function _interopNamespace(e) {
|
|
6
|
+
if (e && e.__esModule) return e;
|
|
6
7
|
var n = Object.create(null);
|
|
7
8
|
if (e) {
|
|
8
9
|
Object.keys(e).forEach(function (k) {
|
|
@@ -19,7 +20,7 @@ function _interopNamespaceDefault(e) {
|
|
|
19
20
|
return Object.freeze(n);
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
var z__namespace = /*#__PURE__*/
|
|
23
|
+
var z__namespace = /*#__PURE__*/_interopNamespace(z);
|
|
23
24
|
|
|
24
25
|
/**
|
|
25
26
|
* A validator for i18n locale routes.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"locale.js","sources":["../../../../server/src/routes/validation/locale.ts"],"sourcesContent":["import type { Core } from '@strapi/types';\nimport * as z from 'zod/v4';\n\n/**\n * A validator for i18n locale routes.\n *\n */\nexport class I18nLocaleRouteValidator {\n protected readonly _strapi: Core.Strapi;\n\n public constructor(strapi: Core.Strapi) {\n this._strapi = strapi;\n }\n\n /**\n * Generates a validation schema for a single locale.\n *\n * @returns A schema for validating locale objects\n */\n get locale() {\n return z.object({\n id: z.number().int().positive(),\n documentId: z.string().uuid(),\n name: z.string(),\n code: z.string().length(2, 'Locale code must be exactly 2 characters'),\n createdAt: z.string(),\n updatedAt: z.string(),\n publishedAt: z.string().nullable(),\n isDefault: z.boolean(),\n });\n }\n\n /**\n * Generates a validation schema for an array of locales\n *\n * @returns A schema for validating arrays of locales\n */\n get locales() {\n return z.array(this.locale);\n }\n}\n"],"names":["I18nLocaleRouteValidator","locale","z","object","id","number","int","positive","documentId","string","uuid","name","code","length","createdAt","updatedAt","publishedAt","nullable","isDefault","boolean","locales","array","strapi","_strapi"],"mappings":"
|
|
1
|
+
{"version":3,"file":"locale.js","sources":["../../../../server/src/routes/validation/locale.ts"],"sourcesContent":["import type { Core } from '@strapi/types';\nimport * as z from 'zod/v4';\n\n/**\n * A validator for i18n locale routes.\n *\n */\nexport class I18nLocaleRouteValidator {\n protected readonly _strapi: Core.Strapi;\n\n public constructor(strapi: Core.Strapi) {\n this._strapi = strapi;\n }\n\n /**\n * Generates a validation schema for a single locale.\n *\n * @returns A schema for validating locale objects\n */\n get locale() {\n return z.object({\n id: z.number().int().positive(),\n documentId: z.string().uuid(),\n name: z.string(),\n code: z.string().length(2, 'Locale code must be exactly 2 characters'),\n createdAt: z.string(),\n updatedAt: z.string(),\n publishedAt: z.string().nullable(),\n isDefault: z.boolean(),\n });\n }\n\n /**\n * Generates a validation schema for an array of locales\n *\n * @returns A schema for validating arrays of locales\n */\n get locales() {\n return z.array(this.locale);\n }\n}\n"],"names":["I18nLocaleRouteValidator","locale","z","object","id","number","int","positive","documentId","string","uuid","name","code","length","createdAt","updatedAt","publishedAt","nullable","isDefault","boolean","locales","array","strapi","_strapi"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAGA;;;AAGC,IACM,MAAMA,wBAAAA,CAAAA;AAOX;;;;AAIC,MACD,IAAIC,MAAAA,GAAS;QACX,OAAOC,YAAAA,CAAEC,MAAM,CAAC;AACdC,YAAAA,EAAAA,EAAIF,YAAAA,CAAEG,MAAM,EAAA,CAAGC,GAAG,GAAGC,QAAQ,EAAA;YAC7BC,UAAAA,EAAYN,YAAAA,CAAEO,MAAM,EAAA,CAAGC,IAAI,EAAA;AAC3BC,YAAAA,IAAAA,EAAMT,aAAEO,MAAM,EAAA;AACdG,YAAAA,IAAAA,EAAMV,YAAAA,CAAEO,MAAM,EAAA,CAAGI,MAAM,CAAC,CAAA,EAAG,0CAAA,CAAA;AAC3BC,YAAAA,SAAAA,EAAWZ,aAAEO,MAAM,EAAA;AACnBM,YAAAA,SAAAA,EAAWb,aAAEO,MAAM,EAAA;YACnBO,WAAAA,EAAad,YAAAA,CAAEO,MAAM,EAAA,CAAGQ,QAAQ,EAAA;AAChCC,YAAAA,SAAAA,EAAWhB,aAAEiB,OAAO;AACtB,SAAA,CAAA;AACF,IAAA;AAEA;;;;AAIC,MACD,IAAIC,OAAAA,GAAU;AACZ,QAAA,OAAOlB,YAAAA,CAAEmB,KAAK,CAAC,IAAI,CAACpB,MAAM,CAAA;AAC5B,IAAA;AA7BA,IAAA,WAAA,CAAmBqB,MAAmB,CAAE;QACtC,IAAI,CAACC,OAAO,GAAGD,MAAAA;AACjB,IAAA;AA4BF;;;;"}
|
|
@@ -5,6 +5,10 @@ var fp = require('lodash/fp');
|
|
|
5
5
|
var utils = require('@strapi/utils');
|
|
6
6
|
var index = require('../utils/index.js');
|
|
7
7
|
|
|
8
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
|
+
|
|
10
|
+
var ___default = /*#__PURE__*/_interopDefault(_);
|
|
11
|
+
|
|
8
12
|
const { isRelationalAttribute, getVisibleAttributes, isTypedAttribute, getScalarAttributes, getRelationalAttributes } = utils.contentTypes;
|
|
9
13
|
const { ApplicationError } = utils.errors;
|
|
10
14
|
const hasLocalizedOption = (modelOrAttribute)=>{
|
|
@@ -53,7 +57,7 @@ const removeIdsMut = (model, entry)=>{
|
|
|
53
57
|
return entry;
|
|
54
58
|
}
|
|
55
59
|
removeId(entry);
|
|
56
|
-
|
|
60
|
+
___default.default.forEach(model.attributes, (attr, attrName)=>{
|
|
57
61
|
const value = entry[attrName];
|
|
58
62
|
if (attr.type === 'dynamiczone' && fp.isArray(value)) {
|
|
59
63
|
value.forEach((compo)=>{
|
|
@@ -101,7 +105,7 @@ const removeIdsMut = (model, entry)=>{
|
|
|
101
105
|
}
|
|
102
106
|
const modelDef = strapi.getModel(model);
|
|
103
107
|
const relatedEntryCopy = copyNonLocalizedAttributes(modelDef, relatedEntry);
|
|
104
|
-
|
|
108
|
+
___default.default.forEach(relatedEntryCopy, (value, field)=>{
|
|
105
109
|
if (fp.isNil(entry[field])) {
|
|
106
110
|
entry[field] = value;
|
|
107
111
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"content-types.js","sources":["../../../server/src/services/content-types.ts"],"sourcesContent":["import _ from 'lodash';\nimport { pick, pipe, has, prop, isNil, cloneDeep, isArray } from 'lodash/fp';\nimport { errors, contentTypes as contentTypeUtils } from '@strapi/utils';\nimport { getService } from '../utils';\n\nconst {\n isRelationalAttribute,\n getVisibleAttributes,\n isTypedAttribute,\n getScalarAttributes,\n getRelationalAttributes,\n} = contentTypeUtils;\nconst { ApplicationError } = errors;\n\nconst hasLocalizedOption = (modelOrAttribute: any) => {\n return prop('pluginOptions.i18n.localized', modelOrAttribute) === true;\n};\n\nconst getValidLocale = async (locale: any) => {\n const localesService = getService('locales');\n\n if (isNil(locale)) {\n return localesService.getDefaultLocale();\n }\n\n const foundLocale = await localesService.findByCode(locale);\n if (!foundLocale) {\n throw new ApplicationError('Locale not found');\n }\n\n return locale;\n};\n\n/**\n * Returns whether an attribute is localized or not\n * @param {*} attribute\n * @returns\n */\nconst isLocalizedAttribute = (attribute: any) => {\n return (\n hasLocalizedOption(attribute) ||\n isRelationalAttribute(attribute) ||\n isTypedAttribute(attribute, 'uid')\n );\n};\n\n/**\n * Returns whether a model is localized or not\n * @param {*} model\n * @returns\n */\nconst isLocalizedContentType = (model: any) => {\n return hasLocalizedOption(model);\n};\n\n/**\n * Returns the list of attribute names that are not localized\n * @param {object} model\n * @returns {string[]}\n */\nconst getNonLocalizedAttributes = (model: any) => {\n return getVisibleAttributes(model).filter(\n (attrName) => !isLocalizedAttribute(model.attributes[attrName])\n );\n};\n\nconst removeId = (value: any) => {\n if (typeof value === 'object' && has('id', value)) {\n delete value.id;\n }\n};\n\nconst removeIds = (model: any) => (entry: any) => removeIdsMut(model, cloneDeep(entry));\n\nconst removeIdsMut = (model: any, entry: any) => {\n if (isNil(entry)) {\n return entry;\n }\n\n removeId(entry);\n\n _.forEach(model.attributes, (attr, attrName) => {\n const value = entry[attrName];\n if (attr.type === 'dynamiczone' && isArray(value)) {\n value.forEach((compo) => {\n if (has('__component', compo)) {\n const model = strapi.components[compo.__component];\n removeIdsMut(model, compo);\n }\n });\n } else if (attr.type === 'component') {\n const model = strapi.components[attr.component];\n if (isArray(value)) {\n value.forEach((compo) => removeIdsMut(model, compo));\n } else {\n removeIdsMut(model, value);\n }\n }\n });\n\n return entry;\n};\n\n/**\n * Returns a copy of an entry picking only its non localized attributes\n * @param {object} model\n * @param {object} entry\n * @returns {object}\n */\nconst copyNonLocalizedAttributes = (model: any, entry: any) => {\n const nonLocalizedAttributes = getNonLocalizedAttributes(model);\n\n return pipe(pick(nonLocalizedAttributes), removeIds(model))(entry);\n};\n\n/**\n * Returns the list of attribute names that are localized\n * @param {object} model\n * @returns {string[]}\n */\nconst getLocalizedAttributes = (model: any) => {\n return getVisibleAttributes(model).filter((attrName) =>\n isLocalizedAttribute(model.attributes[attrName])\n );\n};\n\n/**\n * Fill non localized fields of an entry if there are nil\n * @param {Object} entry entry to fill\n * @param {Object} relatedEntry values used to fill\n * @param {Object} options\n * @param {Object} options.model corresponding model\n */\nconst fillNonLocalizedAttributes = (entry: any, relatedEntry: any, { model }: any) => {\n if (isNil(relatedEntry)) {\n return;\n }\n\n const modelDef = strapi.getModel(model);\n const relatedEntryCopy = copyNonLocalizedAttributes(modelDef, relatedEntry);\n\n _.forEach(relatedEntryCopy, (value, field) => {\n if (isNil(entry[field])) {\n entry[field] = value;\n }\n });\n};\n\n/**\n * build the populate param to\n * @param {String} modelUID uid of the model, could be of a content-type or a component\n */\nconst getNestedPopulateOfNonLocalizedAttributes = (modelUID: any) => {\n const schema = strapi.getModel(modelUID);\n const scalarAttributes = getScalarAttributes(schema);\n const nonLocalizedAttributes = getNonLocalizedAttributes(schema);\n\n const allAttributes = [...scalarAttributes, ...nonLocalizedAttributes];\n if (schema.modelType === 'component') {\n // When called recursively on a non localized component we\n // need to explicitly populate that components relations\n allAttributes.push(...getRelationalAttributes(schema));\n }\n\n const currentAttributesToPopulate = allAttributes.filter((value, index, self) => {\n return self.indexOf(value) === index && self.lastIndexOf(value) === index;\n });\n\n const attributesToPopulate = [...currentAttributesToPopulate];\n for (const attrName of currentAttributesToPopulate) {\n const attr = schema.attributes[attrName];\n if (attr.type === 'component') {\n const nestedPopulate = getNestedPopulateOfNonLocalizedAttributes(attr.component).map(\n (nestedAttr) => `${attrName}.${nestedAttr}`\n );\n attributesToPopulate.push(...nestedPopulate);\n } else if (attr.type === 'dynamiczone') {\n attr.components.forEach((componentName) => {\n const nestedPopulate = getNestedPopulateOfNonLocalizedAttributes(componentName).map(\n (nestedAttr) => `${attrName}.${nestedAttr}`\n );\n attributesToPopulate.push(...nestedPopulate);\n });\n }\n }\n\n return attributesToPopulate;\n};\n\nconst contentTypes = () => ({\n isLocalizedContentType,\n getValidLocale,\n getLocalizedAttributes,\n getNonLocalizedAttributes,\n copyNonLocalizedAttributes,\n fillNonLocalizedAttributes,\n getNestedPopulateOfNonLocalizedAttributes,\n});\n\ntype ContentTypesService = typeof contentTypes;\n\nexport default contentTypes;\nexport { ContentTypesService };\n"],"names":["isRelationalAttribute","getVisibleAttributes","isTypedAttribute","getScalarAttributes","getRelationalAttributes","contentTypeUtils","ApplicationError","errors","hasLocalizedOption","modelOrAttribute","prop","getValidLocale","locale","localesService","getService","isNil","getDefaultLocale","foundLocale","findByCode","isLocalizedAttribute","attribute","isLocalizedContentType","model","getNonLocalizedAttributes","filter","attrName","attributes","removeId","value","has","id","removeIds","entry","removeIdsMut","cloneDeep","_","forEach","attr","type","isArray","compo","strapi","components","__component","component","copyNonLocalizedAttributes","nonLocalizedAttributes","pipe","pick","getLocalizedAttributes","fillNonLocalizedAttributes","relatedEntry","modelDef","getModel","relatedEntryCopy","field","getNestedPopulateOfNonLocalizedAttributes","modelUID","schema","scalarAttributes","allAttributes","modelType","push","currentAttributesToPopulate","index","self","indexOf","lastIndexOf","attributesToPopulate","nestedPopulate","map","nestedAttr","componentName","contentTypes"],"mappings":";;;;;;;AAKA,MAAM,EACJA,qBAAqB,EACrBC,oBAAoB,EACpBC,gBAAgB,EAChBC,mBAAmB,EACnBC,uBAAuB,EACxB,GAAGC,kBAAAA;AACJ,MAAM,EAAEC,gBAAgB,EAAE,GAAGC,YAAAA;AAE7B,MAAMC,qBAAqB,CAACC,gBAAAA,GAAAA;IAC1B,OAAOC,OAAAA,CAAK,gCAAgCD,gBAAAA,CAAAA,KAAsB,IAAA;AACpE,CAAA;AAEA,MAAME,iBAAiB,OAAOC,MAAAA,GAAAA;AAC5B,IAAA,MAAMC,iBAAiBC,gBAAAA,CAAW,SAAA,CAAA;AAElC,IAAA,IAAIC,SAAMH,MAAAA,CAAAA,EAAS;AACjB,QAAA,OAAOC,eAAeG,gBAAgB,EAAA;AACxC,IAAA;AAEA,IAAA,MAAMC,WAAAA,GAAc,MAAMJ,cAAAA,CAAeK,UAAU,CAACN,MAAAA,CAAAA;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oBAAAA;AACT,CAAA;AAEA,MAAMK,YAAAA,GAAe,KAAO;AAC1BpD,QAAAA,sBAAAA;AACAV,QAAAA,cAAAA;AACAsC,QAAAA,sBAAAA;AACA1B,QAAAA,yBAAAA;AACAsB,QAAAA,0BAAAA;AACAK,QAAAA,0BAAAA;AACAM,QAAAA;KACF;;;;"}
|
|
1
|
+
{"version":3,"file":"content-types.js","sources":["../../../server/src/services/content-types.ts"],"sourcesContent":["import _ from 'lodash';\nimport { pick, pipe, has, prop, isNil, cloneDeep, isArray } from 'lodash/fp';\nimport { errors, contentTypes as contentTypeUtils } from '@strapi/utils';\nimport { getService } from '../utils';\n\nconst {\n isRelationalAttribute,\n getVisibleAttributes,\n isTypedAttribute,\n getScalarAttributes,\n getRelationalAttributes,\n} = contentTypeUtils;\nconst { ApplicationError } = errors;\n\nconst hasLocalizedOption = (modelOrAttribute: any) => {\n return prop('pluginOptions.i18n.localized', modelOrAttribute) === true;\n};\n\nconst getValidLocale = async (locale: any) => {\n const localesService = getService('locales');\n\n if (isNil(locale)) {\n return localesService.getDefaultLocale();\n }\n\n const foundLocale = await localesService.findByCode(locale);\n if (!foundLocale) {\n throw new ApplicationError('Locale not found');\n }\n\n return locale;\n};\n\n/**\n * Returns whether an attribute is localized or not\n * @param {*} attribute\n * @returns\n */\nconst isLocalizedAttribute = (attribute: any) => {\n return (\n hasLocalizedOption(attribute) ||\n isRelationalAttribute(attribute) ||\n isTypedAttribute(attribute, 'uid')\n );\n};\n\n/**\n * Returns whether a model is localized or not\n * @param {*} model\n * @returns\n */\nconst isLocalizedContentType = (model: any) => {\n return hasLocalizedOption(model);\n};\n\n/**\n * Returns the list of attribute names that are not localized\n * @param {object} model\n * @returns {string[]}\n */\nconst getNonLocalizedAttributes = (model: any) => {\n return getVisibleAttributes(model).filter(\n (attrName) => !isLocalizedAttribute(model.attributes[attrName])\n );\n};\n\nconst removeId = (value: any) => {\n if (typeof value === 'object' && has('id', value)) {\n delete value.id;\n }\n};\n\nconst removeIds = (model: any) => (entry: any) => removeIdsMut(model, cloneDeep(entry));\n\nconst removeIdsMut = (model: any, entry: any): Record<string, any> => {\n if (isNil(entry)) {\n return entry as unknown as Record<string, any>;\n }\n\n removeId(entry);\n\n _.forEach(model.attributes, (attr, attrName) => {\n const value = entry[attrName];\n if (attr.type === 'dynamiczone' && isArray(value)) {\n value.forEach((compo) => {\n if (has('__component', compo)) {\n const model = strapi.components[compo.__component];\n removeIdsMut(model, compo);\n }\n });\n } else if (attr.type === 'component') {\n const model = strapi.components[attr.component];\n if (isArray(value)) {\n value.forEach((compo) => removeIdsMut(model, compo));\n } else {\n removeIdsMut(model, value);\n }\n }\n });\n\n return entry;\n};\n\n/**\n * Returns a copy of an entry picking only its non localized attributes\n * @param {object} model\n * @param {object} entry\n * @returns {object}\n */\nconst copyNonLocalizedAttributes = (model: any, entry: any) => {\n const nonLocalizedAttributes = getNonLocalizedAttributes(model);\n\n return pipe(pick(nonLocalizedAttributes), removeIds(model))(entry);\n};\n\n/**\n * Returns the list of attribute names that are localized\n * @param {object} model\n * @returns {string[]}\n */\nconst getLocalizedAttributes = (model: any) => {\n return getVisibleAttributes(model).filter((attrName) =>\n isLocalizedAttribute(model.attributes[attrName])\n );\n};\n\n/**\n * Fill non localized fields of an entry if there are nil\n * @param {Object} entry entry to fill\n * @param {Object} relatedEntry values used to fill\n * @param {Object} options\n * @param {Object} options.model corresponding model\n */\nconst fillNonLocalizedAttributes = (entry: any, relatedEntry: any, { model }: any) => {\n if (isNil(relatedEntry)) {\n return;\n }\n\n const modelDef = strapi.getModel(model);\n const relatedEntryCopy = copyNonLocalizedAttributes(modelDef, relatedEntry);\n\n _.forEach(relatedEntryCopy, (value, field) => {\n if (isNil(entry[field])) {\n entry[field] = value;\n }\n });\n};\n\n/**\n * build the populate param to\n * @param {String} modelUID uid of the model, could be of a content-type or a component\n */\nconst getNestedPopulateOfNonLocalizedAttributes = (modelUID: any): string[] => {\n const schema = strapi.getModel(modelUID);\n const scalarAttributes = getScalarAttributes(schema);\n const nonLocalizedAttributes = getNonLocalizedAttributes(schema);\n\n const allAttributes = [...scalarAttributes, ...nonLocalizedAttributes];\n if (schema.modelType === 'component') {\n // When called recursively on a non localized component we\n // need to explicitly populate that components relations\n allAttributes.push(...getRelationalAttributes(schema));\n }\n\n const currentAttributesToPopulate = allAttributes.filter((value, index, self) => {\n return self.indexOf(value) === index && self.lastIndexOf(value) === index;\n });\n\n const attributesToPopulate = [...currentAttributesToPopulate];\n for (const attrName of currentAttributesToPopulate) {\n const attr = schema.attributes[attrName];\n if (attr.type === 'component') {\n const nestedPopulate = getNestedPopulateOfNonLocalizedAttributes(attr.component).map(\n (nestedAttr) => `${attrName}.${nestedAttr}`\n );\n attributesToPopulate.push(...nestedPopulate);\n } else if (attr.type === 'dynamiczone') {\n attr.components.forEach((componentName) => {\n const nestedPopulate = getNestedPopulateOfNonLocalizedAttributes(componentName).map(\n (nestedAttr) => `${attrName}.${nestedAttr}`\n );\n attributesToPopulate.push(...nestedPopulate);\n });\n }\n }\n\n return attributesToPopulate;\n};\n\nconst contentTypes = () => ({\n isLocalizedContentType,\n getValidLocale,\n getLocalizedAttributes,\n getNonLocalizedAttributes,\n copyNonLocalizedAttributes,\n fillNonLocalizedAttributes,\n getNestedPopulateOfNonLocalizedAttributes,\n});\n\ntype ContentTypesService = typeof contentTypes;\n\nexport default contentTypes;\nexport { ContentTypesService };\n"],"names":["isRelationalAttribute","getVisibleAttributes","isTypedAttribute","getScalarAttributes","getRelationalAttributes","contentTypeUtils","ApplicationError","errors","hasLocalizedOption","modelOrAttribute","prop","getValidLocale","locale","localesService","getService","isNil","getDefaultLocale","foundLocale","findByCode","isLocalizedAttribute","attribute","isLocalizedContentType","model","getNonLocalizedAttributes","filter","attrName","attributes","removeId","value","has","id","removeIds","entry","removeIdsMut","cloneDeep","_","forEach","attr","type","isArray","compo","strapi","components","__component","component","copyNonLocalizedAttributes","nonLocalizedAttributes","pipe","pick","getLocalizedAttributes","fillNonLocalizedAttributes","relatedEntry","modelDef","getModel","relatedEntryCopy","field","getNestedPopulateOfNonLocalizedAttributes","modelUID","schema","scalarAttributes","allAttributes","modelType","push","currentAttributesToPopulate","index","self","indexOf","lastIndexOf","attributesToPopulate","nestedPopulate","map","nestedAttr","componentName","contentTypes"],"mappings":";;;;;;;;;;;AAKA,MAAM,EACJA,qBAAqB,EACrBC,oBAAoB,EACpBC,gBAAgB,EAChBC,mBAAmB,EACnBC,uBAAuB,EACxB,GAAGC,kBAAAA;AACJ,MAAM,EAAEC,gBAAgB,EAAE,GAAGC,YAAAA;AAE7B,MAAMC,qBAAqB,CAACC,gBAAAA,GAAAA;IAC1B,OAAOC,OAAAA,CAAK,gCAAgCD,gBAAAA,CAAAA,KAAsB,IAAA;AACpE,CAAA;AAEA,MAAME,iBAAiB,OAAOC,MAAAA,GAAAA;AAC5B,IAAA,MAAMC,iBAAiBC,gBAAAA,CAAW,SAAA,CAAA;AAElC,IAAA,IAAIC,SAAMH,MAAAA,CAAAA,EAAS;AACjB,QAAA,OAAOC,eAAeG,gBAAgB,EAAA;AACxC,IAAA;AAEA,IAAA,MAAMC,WAAAA,GAAc,MAAMJ,cAAAA,CAAeK,UAAU,CAACN,MAAAA,CAAAA;AACpD,IAAA,IAAI,CAACK,WAAAA,EAAa;AAChB,QAAA,MAAM,IAAIX,gBAAAA,CAAiB,kBAAA,CAAA;AAC7B,IAAA;IAEA,OAAOM,MAAAA;AACT,CAAA;AAEA;;;;IAKA,MAAMO,uBAAuB,CAACC,SAAAA,GAAAA;AAC5B,IAAA,OACEZ,kBAAAA,CAAmBY,SAAAA,CAAAA,IACnBpB,qBAAAA,CAAsBoB,SAAAA,CAAAA,IACtBlB,iBAAiBkB,SAAAA,EAAW,KAAA,CAAA;AAEhC,CAAA;AAEA;;;;IAKA,MAAMC,yBAAyB,CAACC,KAAAA,GAAAA;AAC9B,IAAA,OAAOd,kBAAAA,CAAmBc,KAAAA,CAAAA;AAC5B,CAAA;AAEA;;;;IAKA,MA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AA,CAAS;QACxC,IAAIY,IAAAA,CAAKC,IAAI,KAAK,WAAA,EAAa;AAC7B,YAAA,MAAM+B,cAAAA,GAAiBb,yCAAAA,CAA0CnB,IAAAA,CAAKO,SAAS,CAAA,CAAE0B,GAAG,CAClF,CAACC,UAAAA,GAAe,CAAA,EAAG9C,QAAAA,CAAS,CAAC,EAAE8C,UAAAA,CAAAA,CAAY,CAAA;AAE7CH,YAAAA,oBAAAA,CAAqBN,IAAI,CAAA,GAAIO,cAAAA,CAAAA;AAC/B,QAAA,CAAA,MAAO,IAAIhC,IAAAA,CAAKC,IAAI,KAAK,aAAA,EAAe;AACtCD,YAAAA,IAAAA,CAAKK,UAAU,CAACN,OAAO,CAAC,CAACoC,aAAAA,GAAAA;gBACvB,MAAMH,cAAAA,GAAiBb,yCAAAA,CAA0CgB,aAAAA,CAAAA,CAAeF,GAAG,CACjF,CAACC,UAAAA,GAAe,CAAA,EAAG9C,QAAAA,CAAS,CAAC,EAAE8C,UAAAA,CAAAA,CAAY,CAAA;AAE7CH,gBAAAA,oBAAAA,CAAqBN,IAAI,CAAA,GAAIO,cAAAA,CAAAA;AAC/B,YAAA,CAAA,CAAA;AACF,QAAA;AACF,IAAA;IAEA,OAAOD,oBAAAA;AACT,CAAA;AAEA,MAAMK,YAAAA,GAAe,KAAO;AAC1BpD,QAAAA,sBAAAA;AACAV,QAAAA,cAAAA;AACAsC,QAAAA,sBAAAA;AACA1B,QAAAA,yBAAAA;AACAsB,QAAAA,0BAAAA;AACAK,QAAAA,0BAAAA;AACAM,QAAAA;KACF;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"content-types.mjs","sources":["../../../server/src/services/content-types.ts"],"sourcesContent":["import _ from 'lodash';\nimport { pick, pipe, has, prop, isNil, cloneDeep, isArray } from 'lodash/fp';\nimport { errors, contentTypes as contentTypeUtils } from '@strapi/utils';\nimport { getService } from '../utils';\n\nconst {\n isRelationalAttribute,\n getVisibleAttributes,\n isTypedAttribute,\n getScalarAttributes,\n getRelationalAttributes,\n} = contentTypeUtils;\nconst { ApplicationError } = errors;\n\nconst hasLocalizedOption = (modelOrAttribute: any) => {\n return prop('pluginOptions.i18n.localized', modelOrAttribute) === true;\n};\n\nconst getValidLocale = async (locale: any) => {\n const localesService = getService('locales');\n\n if (isNil(locale)) {\n return localesService.getDefaultLocale();\n }\n\n const foundLocale = await localesService.findByCode(locale);\n if (!foundLocale) {\n throw new ApplicationError('Locale not found');\n }\n\n return locale;\n};\n\n/**\n * Returns whether an attribute is localized or not\n * @param {*} attribute\n * @returns\n */\nconst isLocalizedAttribute = (attribute: any) => {\n return (\n hasLocalizedOption(attribute) ||\n isRelationalAttribute(attribute) ||\n isTypedAttribute(attribute, 'uid')\n );\n};\n\n/**\n * Returns whether a model is localized or not\n * @param {*} model\n * @returns\n */\nconst isLocalizedContentType = (model: any) => {\n return hasLocalizedOption(model);\n};\n\n/**\n * Returns the list of attribute names that are not localized\n * @param {object} model\n * @returns {string[]}\n */\nconst getNonLocalizedAttributes = (model: any) => {\n return getVisibleAttributes(model).filter(\n (attrName) => !isLocalizedAttribute(model.attributes[attrName])\n );\n};\n\nconst removeId = (value: any) => {\n if (typeof value === 'object' && has('id', value)) {\n delete value.id;\n }\n};\n\nconst removeIds = (model: any) => (entry: any) => removeIdsMut(model, cloneDeep(entry));\n\nconst removeIdsMut = (model: any, entry: any) => {\n if (isNil(entry)) {\n return entry;\n }\n\n removeId(entry);\n\n _.forEach(model.attributes, (attr, attrName) => {\n const value = entry[attrName];\n if (attr.type === 'dynamiczone' && isArray(value)) {\n value.forEach((compo) => {\n if (has('__component', compo)) {\n const model = strapi.components[compo.__component];\n removeIdsMut(model, compo);\n }\n });\n } else if (attr.type === 'component') {\n const model = strapi.components[attr.component];\n if (isArray(value)) {\n value.forEach((compo) => removeIdsMut(model, compo));\n } else {\n removeIdsMut(model, value);\n }\n }\n });\n\n return entry;\n};\n\n/**\n * Returns a copy of an entry picking only its non localized attributes\n * @param {object} model\n * @param {object} entry\n * @returns {object}\n */\nconst copyNonLocalizedAttributes = (model: any, entry: any) => {\n const nonLocalizedAttributes = getNonLocalizedAttributes(model);\n\n return pipe(pick(nonLocalizedAttributes), removeIds(model))(entry);\n};\n\n/**\n * Returns the list of attribute names that are localized\n * @param {object} model\n * @returns {string[]}\n */\nconst getLocalizedAttributes = (model: any) => {\n return getVisibleAttributes(model).filter((attrName) =>\n isLocalizedAttribute(model.attributes[attrName])\n );\n};\n\n/**\n * Fill non localized fields of an entry if there are nil\n * @param {Object} entry entry to fill\n * @param {Object} relatedEntry values used to fill\n * @param {Object} options\n * @param {Object} options.model corresponding model\n */\nconst fillNonLocalizedAttributes = (entry: any, relatedEntry: any, { model }: any) => {\n if (isNil(relatedEntry)) {\n return;\n }\n\n const modelDef = strapi.getModel(model);\n const relatedEntryCopy = copyNonLocalizedAttributes(modelDef, relatedEntry);\n\n _.forEach(relatedEntryCopy, (value, field) => {\n if (isNil(entry[field])) {\n entry[field] = value;\n }\n });\n};\n\n/**\n * build the populate param to\n * @param {String} modelUID uid of the model, could be of a content-type or a component\n */\nconst getNestedPopulateOfNonLocalizedAttributes = (modelUID: any) => {\n const schema = strapi.getModel(modelUID);\n const scalarAttributes = getScalarAttributes(schema);\n const nonLocalizedAttributes = getNonLocalizedAttributes(schema);\n\n const allAttributes = [...scalarAttributes, ...nonLocalizedAttributes];\n if (schema.modelType === 'component') {\n // When called recursively on a non localized component we\n // need to explicitly populate that components relations\n allAttributes.push(...getRelationalAttributes(schema));\n }\n\n const currentAttributesToPopulate = allAttributes.filter((value, index, self) => {\n return self.indexOf(value) === index && self.lastIndexOf(value) === index;\n });\n\n const attributesToPopulate = [...currentAttributesToPopulate];\n for (const attrName of currentAttributesToPopulate) {\n const attr = schema.attributes[attrName];\n if (attr.type === 'component') {\n const nestedPopulate = getNestedPopulateOfNonLocalizedAttributes(attr.component).map(\n (nestedAttr) => `${attrName}.${nestedAttr}`\n );\n attributesToPopulate.push(...nestedPopulate);\n } else if (attr.type === 'dynamiczone') {\n attr.components.forEach((componentName) => {\n const nestedPopulate = getNestedPopulateOfNonLocalizedAttributes(componentName).map(\n (nestedAttr) => `${attrName}.${nestedAttr}`\n );\n attributesToPopulate.push(...nestedPopulate);\n });\n }\n }\n\n return attributesToPopulate;\n};\n\nconst contentTypes = () => ({\n isLocalizedContentType,\n getValidLocale,\n getLocalizedAttributes,\n getNonLocalizedAttributes,\n copyNonLocalizedAttributes,\n fillNonLocalizedAttributes,\n getNestedPopulateOfNonLocalizedAttributes,\n});\n\ntype ContentTypesService = typeof contentTypes;\n\nexport default contentTypes;\nexport { ContentTypesService };\n"],"names":["isRelationalAttribute","getVisibleAttributes","isTypedAttribute","getScalarAttributes","getRelationalAttributes","contentTypeUtils","ApplicationError","errors","hasLocalizedOption","modelOrAttribute","prop","getValidLocale","locale","localesService","getService","isNil","getDefaultLocale","foundLocale","findByCode","isLocalizedAttribute","attribute","isLocalizedContentType","model","getNonLocalizedAttributes","filter","attrName","attributes","removeId","value","has","id","removeIds","entry","removeIdsMut","cloneDeep","_","forEach","attr","type","isArray","compo","strapi","components","__component","component","copyNonLocalizedAttributes","nonLocalizedAttributes","pipe","pick","getLocalizedAttributes","fillNonLocalizedAttributes","relatedEntry","modelDef","getModel","relatedEntryCopy","field","getNestedPopulateOfNonLocalizedAttributes","modelUID","schema","scalarAttributes","allAttributes","modelType","push","currentAttributesToPopulate","index","self","indexOf","lastIndexOf","attributesToPopulate","nestedPopulate","map","nestedAttr","componentName","contentTypes"],"mappings":";;;;;AAKA,MAAM,EACJA,qBAAqB,EACrBC,oBAAoB,EACpBC,gBAAgB,EAChBC,mBAAmB,EACnBC,uBAAuB,EACxB,GAAGC,cAAAA;AACJ,MAAM,EAAEC,gBAAgB,EAAE,GAAGC,MAAAA;AAE7B,MAAMC,qBAAqB,CAACC,gBAAAA,GAAAA;IAC1B,OAAOC,IAAAA,CAAK,gCAAgCD,gBAAAA,CAAAA,KAAsB,IAAA;AACpE,CAAA;AA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,GAAiBb,yCAAAA,CAA0CgB,aAAAA,CAAAA,CAAeF,GAAG,CACjF,CAACC,UAAAA,GAAe,CAAA,EAAG9C,QAAAA,CAAS,CAAC,EAAE8C,UAAAA,CAAAA,CAAY,CAAA;AAE7CH,gBAAAA,oBAAAA,CAAqBN,IAAI,CAAA,GAAIO,cAAAA,CAAAA;AAC/B,YAAA,CAAA,CAAA;AACF,QAAA;AACF,IAAA;IAEA,OAAOD,oBAAAA;AACT,CAAA;AAEA,MAAMK,YAAAA,GAAe,KAAO;AAC1BpD,QAAAA,sBAAAA;AACAV,QAAAA,cAAAA;AACAsC,QAAAA,sBAAAA;AACA1B,QAAAA,yBAAAA;AACAsB,QAAAA,0BAAAA;AACAK,QAAAA,0BAAAA;AACAM,QAAAA;KACF;;;;"}
|
|
1
|
+
{"version":3,"file":"content-types.mjs","sources":["../../../server/src/services/content-types.ts"],"sourcesContent":["import _ from 'lodash';\nimport { pick, pipe, has, prop, isNil, cloneDeep, isArray } from 'lodash/fp';\nimport { errors, contentTypes as contentTypeUtils } from '@strapi/utils';\nimport { getService } from '../utils';\n\nconst {\n isRelationalAttribute,\n getVisibleAttributes,\n isTypedAttribute,\n getScalarAttributes,\n getRelationalAttributes,\n} = contentTypeUtils;\nconst { ApplicationError } = errors;\n\nconst hasLocalizedOption = (modelOrAttribute: any) => {\n return prop('pluginOptions.i18n.localized', modelOrAttribute) === true;\n};\n\nconst getValidLocale = async (locale: any) => {\n const localesService = getService('locales');\n\n if (isNil(locale)) {\n return localesService.getDefaultLocale();\n }\n\n const foundLocale = await localesService.findByCode(locale);\n if (!foundLocale) {\n throw new ApplicationError('Locale not found');\n }\n\n return locale;\n};\n\n/**\n * Returns whether an attribute is localized or not\n * @param {*} attribute\n * @returns\n */\nconst isLocalizedAttribute = (attribute: any) => {\n return (\n hasLocalizedOption(attribute) ||\n isRelationalAttribute(attribute) ||\n isTypedAttribute(attribute, 'uid')\n );\n};\n\n/**\n * Returns whether a model is localized or not\n * @param {*} model\n * @returns\n */\nconst isLocalizedContentType = (model: any) => {\n return hasLocalizedOption(model);\n};\n\n/**\n * Returns the list of attribute names that are not localized\n * @param {object} model\n * @returns {string[]}\n */\nconst getNonLocalizedAttributes = (model: any) => {\n return getVisibleAttributes(model).filter(\n (attrName) => !isLocalizedAttribute(model.attributes[attrName])\n );\n};\n\nconst removeId = (value: any) => {\n if (typeof value === 'object' && has('id', value)) {\n delete value.id;\n }\n};\n\nconst removeIds = (model: any) => (entry: any) => removeIdsMut(model, cloneDeep(entry));\n\nconst removeIdsMut = (model: any, entry: any): Record<string, any> => {\n if (isNil(entry)) {\n return entry as unknown as Record<string, any>;\n }\n\n removeId(entry);\n\n _.forEach(model.attributes, (attr, attrName) => {\n const value = entry[attrName];\n if (attr.type === 'dynamiczone' && isArray(value)) {\n value.forEach((compo) => {\n if (has('__component', compo)) {\n const model = strapi.components[compo.__component];\n removeIdsMut(model, compo);\n }\n });\n } else if (attr.type === 'component') {\n const model = strapi.components[attr.component];\n if (isArray(value)) {\n value.forEach((compo) => removeIdsMut(model, compo));\n } else {\n removeIdsMut(model, value);\n }\n }\n });\n\n return entry;\n};\n\n/**\n * Returns a copy of an entry picking only its non localized attributes\n * @param {object} model\n * @param {object} entry\n * @returns {object}\n */\nconst copyNonLocalizedAttributes = (model: any, entry: any) => {\n const nonLocalizedAttributes = getNonLocalizedAttributes(model);\n\n return pipe(pick(nonLocalizedAttributes), removeIds(model))(entry);\n};\n\n/**\n * Returns the list of attribute names that are localized\n * @param {object} model\n * @returns {string[]}\n */\nconst getLocalizedAttributes = (model: any) => {\n return getVisibleAttributes(model).filter((attrName) =>\n isLocalizedAttribute(model.attributes[attrName])\n );\n};\n\n/**\n * Fill non localized fields of an entry if there are nil\n * @param {Object} entry entry to fill\n * @param {Object} relatedEntry values used to fill\n * @param {Object} options\n * @param {Object} options.model corresponding model\n */\nconst fillNonLocalizedAttributes = (entry: any, relatedEntry: any, { model }: any) => {\n if (isNil(relatedEntry)) {\n return;\n }\n\n const modelDef = strapi.getModel(model);\n const relatedEntryCopy = copyNonLocalizedAttributes(modelDef, relatedEntry);\n\n _.forEach(relatedEntryCopy, (value, field) => {\n if (isNil(entry[field])) {\n entry[field] = value;\n }\n });\n};\n\n/**\n * build the populate param to\n * @param {String} modelUID uid of the model, could be of a content-type or a component\n */\nconst getNestedPopulateOfNonLocalizedAttributes = (modelUID: any): string[] => {\n const schema = strapi.getModel(modelUID);\n const scalarAttributes = getScalarAttributes(schema);\n const nonLocalizedAttributes = getNonLocalizedAttributes(schema);\n\n const allAttributes = [...scalarAttributes, ...nonLocalizedAttributes];\n if (schema.modelType === 'component') {\n // When called recursively on a non localized component we\n // need to explicitly populate that components relations\n allAttributes.push(...getRelationalAttributes(schema));\n }\n\n const currentAttributesToPopulate = allAttributes.filter((value, index, self) => {\n return self.indexOf(value) === index && self.lastIndexOf(value) === index;\n });\n\n const attributesToPopulate = [...currentAttributesToPopulate];\n for (const attrName of currentAttributesToPopulate) {\n const attr = schema.attributes[attrName];\n if (attr.type === 'component') {\n const nestedPopulate = getNestedPopulateOfNonLocalizedAttributes(attr.component).map(\n (nestedAttr) => `${attrName}.${nestedAttr}`\n );\n attributesToPopulate.push(...nestedPopulate);\n } else if (attr.type === 'dynamiczone') {\n attr.components.forEach((componentName) => {\n const nestedPopulate = getNestedPopulateOfNonLocalizedAttributes(componentName).map(\n (nestedAttr) => `${attrName}.${nestedAttr}`\n );\n attributesToPopulate.push(...nestedPopulate);\n });\n }\n }\n\n return attributesToPopulate;\n};\n\nconst contentTypes = () => ({\n isLocalizedContentType,\n getValidLocale,\n getLocalizedAttributes,\n getNonLocalizedAttributes,\n copyNonLocalizedAttributes,\n fillNonLocalizedAttributes,\n getNestedPopulateOfNonLocalizedAttributes,\n});\n\ntype ContentTypesService = typeof contentTypes;\n\nexport default contentTypes;\nexport { ContentTypesService };\n"],"names":["isRelationalAttribute","getVisibleAttributes","isTypedAttribute","getScalarAttributes","getRelationalAttributes","contentTypeUtils","ApplicationError","errors","hasLocalizedOption","modelOrAttribute","prop","getValidLocale","locale","localesService","getService","isNil","getDefaultLocale","foundLocale","findByCode","isLocalizedAttribute","attribute","isLocalizedContentType","model","getNonLocalizedAttributes","filter","attrName","attributes","removeId","value","has","id","removeIds","entry","removeIdsMut","cloneDeep","_","forEach","attr","type","isArray","compo","strapi","components","__component","component","copyNonLocalizedAttributes","nonLocalizedAttributes","pipe","pick","getLocalizedAttributes","fillNonLocalizedAttributes","relatedEntry","modelDef","getModel","relatedEntryCopy","field","getNestedPopulateOfNonLocalizedAttributes","modelUID","schema","scalarAttributes","allAttributes","modelType","push","currentAttributesToPopulate","index","self","indexOf","lastIndexOf","attributesToPopulate","nestedPopulate","map","nestedAttr","componentName","contentTypes"],"mappings":";;;;;AAKA,MAAM,EACJA,qBAAqB,EACrBC,oBAAoB,EACpBC,gBAAgB,EAChBC,mBAAmB,EACnBC,uBAAuB,EACxB,GAAGC,cAAAA;AACJ,MAAM,EAAEC,gBAAgB,EAAE,GAAGC,MAAAA;AAE7B,MAAMC,qBAAqB,CAACC,gBAAAA,GAAAA;IAC1B,OAAOC,IAAAA,CAAK,gCAAgCD,gBAAAA,CAAAA,KAAsB,IAAA;AACpE,CAAA;AAEA,MAAME,iBAAiB,OAAOC,MAAAA,GAAAA;AAC5B,IAAA,MAAMC,iBAAiBC,UAAAA,CAAW,SAAA,CAAA;AAElC,IAAA,IAAIC,MAAMH,MAAAA,CAAAA,EAAS;AACjB,QAAA,OAAOC,eAAeG,gBAAgB,EAAA;AACxC,IAAA;AAEA,IAAA,MAAMC,WAAAA,GAAc,MAAMJ,cAAAA,CAAeK,UAAU,CAACN,MAAAA,CAAAA;AACpD,IAAA,IAAI,CAACK,WAAAA,EAAa;AAChB,QAAA,MAAM,IAAIX,gBAAAA,CAAiB,kBAAA,CAAA;AAC7B,IAAA;IAEA,OAAOM,MAAAA;AACT,CAAA;AAEA;;;;IAKA,MAAMO,uBAAuB,CAACC,SAAAA,GAAAA;AAC5B,IAAA,OAC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F;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localizations.js","sources":["../../../server/src/services/localizations.ts"],"sourcesContent":["import { cloneDeep, isEmpty } from 'lodash/fp';\n\nimport type { Schema } from '@strapi/types';\nimport { async } from '@strapi/utils';\nimport { getService } from '../utils';\n\n/**\n * Mutates the provided data object in place and returns the same reference\n * with populated media values replaced by their upload file IDs.\n */\nconst normalizeMediaIds = (\n schema: Schema.ContentType | Schema.Component,\n data: Record<string, any>\n) => {\n if (!schema?.attributes || !data || typeof data !== 'object') {\n return data;\n }\n\n Object.entries(schema.attributes).forEach(([attributeName, attribute]) => {\n const value = data[attributeName];\n\n if (value == null) {\n return;\n }\n\n if (attribute.type === 'media') {\n if (attribute.multiple) {\n data[attributeName] = Array.isArray(value)\n ? value.map((file: unknown) =>\n file && typeof file === 'object' && 'id' in file ? file.id : file\n )\n : value;\n } else {\n data[attributeName] =\n value && typeof value === 'object' && 'id' in value ? value.id : value;\n }\n\n return;\n }\n\n if (attribute.type === 'component') {\n const componentSchema = strapi.getModel(attribute.component);\n\n if (attribute.repeatable && Array.isArray(value)) {\n value.forEach((componentValue: Record<string, any>) =>\n normalizeMediaIds(componentSchema, componentValue)\n );\n } else {\n normalizeMediaIds(componentSchema, value);\n }\n\n return;\n }\n\n if (attribute.type === 'dynamiczone' && Array.isArray(value)) {\n value.forEach((componentValue: Record<string, any>) => {\n if (componentValue?.__component) {\n normalizeMediaIds(strapi.getModel(componentValue.__component), componentValue);\n }\n });\n }\n });\n\n return data;\n};\n\n/**\n * Update non localized fields of all the related localizations of an entry with the entry values\n */\nconst syncNonLocalizedAttributes = async (sourceEntry: any, model: Schema.ContentType) => {\n const { copyNonLocalizedAttributes } = getService('content-types');\n\n const nonLocalizedAttributes = copyNonLocalizedAttributes(model, sourceEntry);\n if (isEmpty(nonLocalizedAttributes)) {\n return;\n }\n\n const normalizedNonLocalizedAttributes = normalizeMediaIds(model, nonLocalizedAttributes);\n\n const uid = model.uid;\n const documentId = sourceEntry.documentId;\n const locale = sourceEntry.locale;\n const status = sourceEntry?.publishedAt ? 'published' : 'draft';\n\n // Find all the entries that need to be updated\n // this is every other entry of the document in the same status but a different locale\n const localeEntriesToUpdate = await strapi.db.query(uid).findMany({\n where: {\n documentId,\n publishedAt: status === 'published' ? { $ne: null } : null,\n locale: { $ne: locale },\n },\n select: ['locale', 'id'],\n });\n\n const entryData = await strapi.documents(uid).omitComponentData(normalizedNonLocalizedAttributes);\n\n await async.map(localeEntriesToUpdate, async (entry: any) => {\n const transformedData = await strapi.documents.utils.transformData(\n cloneDeep(normalizedNonLocalizedAttributes),\n {\n uid,\n status,\n locale: entry.locale,\n allowMissingId: true,\n }\n );\n\n // Update or create non localized components for the entry\n const componentData = await strapi\n .documents(uid)\n .updateComponents(entry, transformedData as any);\n\n // Update every other locale entry of this documentId in the same status\n await strapi.db.query(uid).update({\n where: {\n documentId,\n publishedAt: status === 'published' ? { $ne: null } : null,\n locale: { $eq: entry.locale },\n },\n // The data we send to the update function is the entry data merged with\n // the updated component data\n data: Object.assign(cloneDeep(entryData), componentData),\n });\n });\n};\n\nconst localizations = () => ({\n syncNonLocalizedAttributes,\n});\n\ntype LocalizationsService = typeof localizations;\n\nexport default localizations;\nexport type { LocalizationsService };\n"],"names":["normalizeMediaIds","schema","data","attributes","Object","entries","forEach","attributeName","attribute","value","type","multiple","Array","isArray","map","file","id","componentSchema","strapi","getModel","component","repeatable","componentValue","__component","syncNonLocalizedAttributes","sourceEntry","model","copyNonLocalizedAttributes","getService","nonLocalizedAttributes","isEmpty","normalizedNonLocalizedAttributes","uid","documentId","locale","status","publishedAt","localeEntriesToUpdate","db","query","findMany","where","$ne","select","entryData","documents","omitComponentData","async","entry","transformedData","utils","transformData","cloneDeep","allowMissingId","componentData","updateComponents","update","$eq","assign","localizations"],"mappings":";;;;;;AAMA;;;IAIA,MAAMA,iBAAAA,GAAoB,CACxBC,MAAAA,EACAC,IAAAA,GAAAA;AAEA,IAAA,IAAI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;AACF,IAAA,CAAA,CAAA;AACF,CAAA;AAEA,MAAMK,aAAAA,GAAgB,KAAO;AAC3BnC,QAAAA;KACF;;;;"}
|
|
1
|
+
{"version":3,"file":"localizations.js","sources":["../../../server/src/services/localizations.ts"],"sourcesContent":["import { cloneDeep, isEmpty } from 'lodash/fp';\n\nimport type { Schema } from '@strapi/types';\nimport { async } from '@strapi/utils';\nimport { getService } from '../utils';\n\n/**\n * Mutates the provided data object in place and returns the same reference\n * with populated media values replaced by their upload file IDs.\n */\nconst normalizeMediaIds = (\n schema: Schema.ContentType | Schema.Component,\n data: Record<string, any>\n): Record<string, any> => {\n if (!schema?.attributes || !data || typeof data !== 'object') {\n return data;\n }\n\n Object.entries(schema.attributes).forEach(([attributeName, attribute]) => {\n const value = data[attributeName];\n\n if (value == null) {\n return;\n }\n\n if (attribute.type === 'media') {\n if (attribute.multiple) {\n data[attributeName] = Array.isArray(value)\n ? value.map((file: unknown) =>\n file && typeof file === 'object' && 'id' in file ? file.id : file\n )\n : value;\n } else {\n data[attributeName] =\n value && typeof value === 'object' && 'id' in value ? value.id : value;\n }\n\n return;\n }\n\n if (attribute.type === 'component') {\n const componentSchema = strapi.getModel(attribute.component);\n\n if (attribute.repeatable && Array.isArray(value)) {\n value.forEach((componentValue: Record<string, any>) =>\n normalizeMediaIds(componentSchema, componentValue)\n );\n } else {\n normalizeMediaIds(componentSchema, value);\n }\n\n return;\n }\n\n if (attribute.type === 'dynamiczone' && Array.isArray(value)) {\n value.forEach((componentValue: Record<string, any>) => {\n if (componentValue?.__component) {\n normalizeMediaIds(strapi.getModel(componentValue.__component), componentValue);\n }\n });\n }\n });\n\n return data;\n};\n\n/**\n * Update non localized fields of all the related localizations of an entry with the entry values\n */\nconst syncNonLocalizedAttributes = async (sourceEntry: any, model: Schema.ContentType) => {\n const { copyNonLocalizedAttributes } = getService('content-types');\n\n const nonLocalizedAttributes = copyNonLocalizedAttributes(model, sourceEntry);\n if (isEmpty(nonLocalizedAttributes)) {\n return;\n }\n\n const normalizedNonLocalizedAttributes = normalizeMediaIds(model, nonLocalizedAttributes);\n\n const uid = model.uid;\n const documentId = sourceEntry.documentId;\n const locale = sourceEntry.locale;\n const status = sourceEntry?.publishedAt ? 'published' : 'draft';\n\n // Find all the entries that need to be updated\n // this is every other entry of the document in the same status but a different locale\n const localeEntriesToUpdate = await strapi.db.query(uid).findMany({\n where: {\n documentId,\n publishedAt: status === 'published' ? { $ne: null } : null,\n locale: { $ne: locale },\n },\n select: ['locale', 'id'],\n });\n\n const entryData = await strapi.documents(uid).omitComponentData(normalizedNonLocalizedAttributes);\n\n await async.map(localeEntriesToUpdate, async (entry: any) => {\n const transformedData = await strapi.documents.utils.transformData(\n cloneDeep(normalizedNonLocalizedAttributes),\n {\n uid,\n status,\n locale: entry.locale,\n allowMissingId: true,\n }\n );\n\n // Update or create non localized components for the entry\n const componentData = await strapi\n .documents(uid)\n .updateComponents(entry, transformedData as any);\n\n // Update every other locale entry of this documentId in the same status\n await strapi.db.query(uid).update({\n where: {\n documentId,\n publishedAt: status === 'published' ? { $ne: null } : null,\n locale: { $eq: entry.locale },\n },\n // The data we send to the update function is the entry data merged with\n // the updated component data\n data: Object.assign(cloneDeep(entryData), componentData),\n });\n });\n};\n\nconst localizations = () => ({\n syncNonLocalizedAttributes,\n});\n\ntype LocalizationsService = typeof localizations;\n\nexport default localizations;\nexport type { LocalizationsService };\n"],"names":["normalizeMediaIds","schema","data","attributes","Object","entries","forEach","attributeName","attribute","value","type","multiple","Array","isArray","map","file","id","componentSchema","strapi","getModel","component","repeatable","componentValue","__component","syncNonLocalizedAttributes","sourceEntry","model","copyNonLocalizedAttributes","getService","nonLocalizedAttributes","isEmpty","normalizedNonLocalizedAttributes","uid","documentId","locale","status","publishedAt","localeEntriesToUpdate","db","query","findMany","where","$ne","select","entryData","documents","omitComponentData","async","entry","transformedData","utils","transformData","cloneDeep","allowMissingId","componentData","updateComponents","update","$eq","assign","localizations"],"mappings":";;;;;;AAMA;;;IAIA,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,MAAM,CAACN,YAAAA,CAAUR,SAAAA,CAAAA,EAAYU,aAAAA;AAC5C,SAAA,CAAA;AACF,IAAA,CAAA,CAAA;AACF,CAAA;AAEA,MAAMK,aAAAA,GAAgB,KAAO;AAC3BnC,QAAAA;KACF;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localizations.mjs","sources":["../../../server/src/services/localizations.ts"],"sourcesContent":["import { cloneDeep, isEmpty } from 'lodash/fp';\n\nimport type { Schema } from '@strapi/types';\nimport { async } from '@strapi/utils';\nimport { getService } from '../utils';\n\n/**\n * Mutates the provided data object in place and returns the same reference\n * with populated media values replaced by their upload file IDs.\n */\nconst normalizeMediaIds = (\n schema: Schema.ContentType | Schema.Component,\n data: Record<string, any>\n) => {\n if (!schema?.attributes || !data || typeof data !== 'object') {\n return data;\n }\n\n Object.entries(schema.attributes).forEach(([attributeName, attribute]) => {\n const value = data[attributeName];\n\n if (value == null) {\n return;\n }\n\n if (attribute.type === 'media') {\n if (attribute.multiple) {\n data[attributeName] = Array.isArray(value)\n ? value.map((file: unknown) =>\n file && typeof file === 'object' && 'id' in file ? file.id : file\n )\n : value;\n } else {\n data[attributeName] =\n value && typeof value === 'object' && 'id' in value ? value.id : value;\n }\n\n return;\n }\n\n if (attribute.type === 'component') {\n const componentSchema = strapi.getModel(attribute.component);\n\n if (attribute.repeatable && Array.isArray(value)) {\n value.forEach((componentValue: Record<string, any>) =>\n normalizeMediaIds(componentSchema, componentValue)\n );\n } else {\n normalizeMediaIds(componentSchema, value);\n }\n\n return;\n }\n\n if (attribute.type === 'dynamiczone' && Array.isArray(value)) {\n value.forEach((componentValue: Record<string, any>) => {\n if (componentValue?.__component) {\n normalizeMediaIds(strapi.getModel(componentValue.__component), componentValue);\n }\n });\n }\n });\n\n return data;\n};\n\n/**\n * Update non localized fields of all the related localizations of an entry with the entry values\n */\nconst syncNonLocalizedAttributes = async (sourceEntry: any, model: Schema.ContentType) => {\n const { copyNonLocalizedAttributes } = getService('content-types');\n\n const nonLocalizedAttributes = copyNonLocalizedAttributes(model, sourceEntry);\n if (isEmpty(nonLocalizedAttributes)) {\n return;\n }\n\n const normalizedNonLocalizedAttributes = normalizeMediaIds(model, nonLocalizedAttributes);\n\n const uid = model.uid;\n const documentId = sourceEntry.documentId;\n const locale = sourceEntry.locale;\n const status = sourceEntry?.publishedAt ? 'published' : 'draft';\n\n // Find all the entries that need to be updated\n // this is every other entry of the document in the same status but a different locale\n const localeEntriesToUpdate = await strapi.db.query(uid).findMany({\n where: {\n documentId,\n publishedAt: status === 'published' ? { $ne: null } : null,\n locale: { $ne: locale },\n },\n select: ['locale', 'id'],\n });\n\n const entryData = await strapi.documents(uid).omitComponentData(normalizedNonLocalizedAttributes);\n\n await async.map(localeEntriesToUpdate, async (entry: any) => {\n const transformedData = await strapi.documents.utils.transformData(\n cloneDeep(normalizedNonLocalizedAttributes),\n {\n uid,\n status,\n locale: entry.locale,\n allowMissingId: true,\n }\n );\n\n // Update or create non localized components for the entry\n const componentData = await strapi\n .documents(uid)\n .updateComponents(entry, transformedData as any);\n\n // Update every other locale entry of this documentId in the same status\n await strapi.db.query(uid).update({\n where: {\n documentId,\n publishedAt: status === 'published' ? { $ne: null } : null,\n locale: { $eq: entry.locale },\n },\n // The data we send to the update function is the entry data merged with\n // the updated component data\n data: Object.assign(cloneDeep(entryData), componentData),\n });\n });\n};\n\nconst localizations = () => ({\n syncNonLocalizedAttributes,\n});\n\ntype LocalizationsService = typeof localizations;\n\nexport default localizations;\nexport type { LocalizationsService };\n"],"names":["normalizeMediaIds","schema","data","attributes","Object","entries","forEach","attributeName","attribute","value","type","multiple","Array","isArray","map","file","id","componentSchema","strapi","getModel","component","repeatable","componentValue","__component","syncNonLocalizedAttributes","sourceEntry","model","copyNonLocalizedAttributes","getService","nonLocalizedAttributes","isEmpty","normalizedNonLocalizedAttributes","uid","documentId","locale","status","publishedAt","localeEntriesToUpdate","db","query","findMany","where","$ne","select","entryData","documents","omitComponentData","async","entry","transformedData","utils","transformData","cloneDeep","allowMissingId","componentData","updateComponents","update","$eq","assign","localizations"],"mappings":";;;;AAMA;;;IAIA,MAAMA,iBAAAA,GAAoB,CACxBC,MAAAA,EACAC,IAAAA,GAAAA;AAEA,IAAA,IAAI,CAACD,MAAAA,EAAQE,UAAAA,IAAc,CAACD,IAAAA,IAAQ,OAAOA,SAAS,QAAA,EAAU;QAC5D,OAAOA,IAAAA;AACT,IAAA;IAEAE,MAAAA,CAAOC,OAAO,CAACJ,MAAAA,CAAOE,UAAU,CAAA,CAAEG,OAAO,CAAC,CAAC,CAACC,aAAAA,EAAeC,SAAAA,CAAU,GAAA;QACnE,MAAMC,KAAAA,GAAQP,IAAI,CAACK,aAAAA,CAAc;AAEjC,QAAA,IAAIE,SAAS,IAAA,EAAM;AACjB,YAAA;AACF,QAAA;QAEA,IAAID,SAAAA,CAAUE,IAAI,KAAK,OAAA,EAAS;YAC9B,IAAIF,SAAAA,CAAUG,QAAQ,EAAE;gBACtBT,IAAI,CAACK,cAAc,GAAGK,KAAAA,CAAMC,OAAO,CAACJ,KAAAA,CAAAA,GAChCA,MAAMK,GAAG,CAAC,CAACC,IAAAA,GACTA,IAAAA,IAAQ,OAAOA,IAAAA,KAAS,QAAA,IAAY,QAAQA,IAAAA,GAAOA,IAAAA,CAAKC,EAAE,GAAGD,IAAAA,CAAAA,GAE/DN,KAAAA;YACN,CAAA,MAAO;gBACLP,IAAI,CAACK,aAAAA,CAAc,GACjBE,KAAAA,IAAS,OAAOA,KAAAA,KAAU,QAAA,IAAY,IAAA,IAAQA,KAAAA,GAAQA,KAAAA,CAAMO,EAAE,GAAGP,KAAAA;AACrE,YAAA;AAEA,YAAA;AACF,QAAA;QAEA,IAAID,SAAAA,CAAUE,IAAI,KAAK,WAAA,EAAa;AAClC,YAAA,MAAMO,eAAAA,GAAkBC,MAAAA,CAAOC,QAAQ,CAACX,UAAUY,SAAS,CAAA;AAE3D,YAAA,IAAIZ,UAAUa,UAAU,IAAIT,K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GAAAA,EAAKT,MAAMd;AAAO;AAC9B,aAAA;;;AAGAhC,YAAAA,IAAAA,EAAME,MAAAA,CAAOsD,MAAM,CAACN,SAAAA,CAAUR,SAAAA,CAAAA,EAAYU,aAAAA;AAC5C,SAAA,CAAA;AACF,IAAA,CAAA,CAAA;AACF,CAAA;AAEA,MAAMK,aAAAA,GAAgB,KAAO;AAC3BnC,QAAAA;KACF;;;;"}
|
|
1
|
+
{"version":3,"file":"localizations.mjs","sources":["../../../server/src/services/localizations.ts"],"sourcesContent":["import { cloneDeep, isEmpty } from 'lodash/fp';\n\nimport type { Schema } from '@strapi/types';\nimport { async } from '@strapi/utils';\nimport { getService } from '../utils';\n\n/**\n * Mutates the provided data object in place and returns the same reference\n * with populated media values replaced by their upload file IDs.\n */\nconst normalizeMediaIds = (\n schema: Schema.ContentType | Schema.Component,\n data: Record<string, any>\n): Record<string, any> => {\n if (!schema?.attributes || !data || typeof data !== 'object') {\n return data;\n }\n\n Object.entries(schema.attributes).forEach(([attributeName, attribute]) => {\n const value = data[attributeName];\n\n if (value == null) {\n return;\n }\n\n if (attribute.type === 'media') {\n if (attribute.multiple) {\n data[attributeName] = Array.isArray(value)\n ? value.map((file: unknown) =>\n file && typeof file === 'object' && 'id' in file ? file.id : file\n )\n : value;\n } else {\n data[attributeName] =\n value && typeof value === 'object' && 'id' in value ? value.id : value;\n }\n\n return;\n }\n\n if (attribute.type === 'component') {\n const componentSchema = strapi.getModel(attribute.component);\n\n if (attribute.repeatable && Array.isArray(value)) {\n value.forEach((componentValue: Record<string, any>) =>\n normalizeMediaIds(componentSchema, componentValue)\n );\n } else {\n normalizeMediaIds(componentSchema, value);\n }\n\n return;\n }\n\n if (attribute.type === 'dynamiczone' && Array.isArray(value)) {\n value.forEach((componentValue: Record<string, any>) => {\n if (componentValue?.__component) {\n normalizeMediaIds(strapi.getModel(componentValue.__component), componentValue);\n }\n });\n }\n });\n\n return data;\n};\n\n/**\n * Update non localized fields of all the related localizations of an entry with the entry values\n */\nconst syncNonLocalizedAttributes = async (sourceEntry: any, model: Schema.ContentType) => {\n const { copyNonLocalizedAttributes } = getService('content-types');\n\n const nonLocalizedAttributes = copyNonLocalizedAttributes(model, sourceEntry);\n if (isEmpty(nonLocalizedAttributes)) {\n return;\n }\n\n const normalizedNonLocalizedAttributes = normalizeMediaIds(model, nonLocalizedAttributes);\n\n const uid = model.uid;\n const documentId = sourceEntry.documentId;\n const locale = sourceEntry.locale;\n const status = sourceEntry?.publishedAt ? 'published' : 'draft';\n\n // Find all the entries that need to be updated\n // this is every other entry of the document in the same status but a different locale\n const localeEntriesToUpdate = await strapi.db.query(uid).findMany({\n where: {\n documentId,\n publishedAt: status === 'published' ? { $ne: null } : null,\n locale: { $ne: locale },\n },\n select: ['locale', 'id'],\n });\n\n const entryData = await strapi.documents(uid).omitComponentData(normalizedNonLocalizedAttributes);\n\n await async.map(localeEntriesToUpdate, async (entry: any) => {\n const transformedData = await strapi.documents.utils.transformData(\n cloneDeep(normalizedNonLocalizedAttributes),\n {\n uid,\n status,\n locale: entry.locale,\n allowMissingId: true,\n }\n );\n\n // Update or create non localized components for the entry\n const componentData = await strapi\n .documents(uid)\n .updateComponents(entry, transformedData as any);\n\n // Update every other locale entry of this documentId in the same status\n await strapi.db.query(uid).update({\n where: {\n documentId,\n publishedAt: status === 'published' ? { $ne: null } : null,\n locale: { $eq: entry.locale },\n },\n // The data we send to the update function is the entry data merged with\n // the updated component data\n data: Object.assign(cloneDeep(entryData), componentData),\n });\n });\n};\n\nconst localizations = () => ({\n syncNonLocalizedAttributes,\n});\n\ntype LocalizationsService = typeof localizations;\n\nexport default localizations;\nexport type { LocalizationsService };\n"],"names":["normalizeMediaIds","schema","data","attributes","Object","entries","forEach","attributeName","attribute","value","type","multiple","Array","isArray","map","file","id","componentSchema","strapi","getModel","component","repeatable","componentValue","__component","syncNonLocalizedAttributes","sourceEntry","model","copyNonLocalizedAttributes","getService","nonLocalizedAttributes","isEmpty","normalizedNonLocalizedAttributes","uid","documentId","locale","status","publishedAt","localeEntriesToUpdate","db","query","findMany","where","$ne","select","entryData","documents","omitComponentData","async","entry","transformedData","utils","transformData","cloneDeep","allowMissingId","componentData","updateComponents","update","$eq","assign","localizations"],"mappings":";;;;AAMA;;;IAIA,MAAMA,iBAAAA,GAAoB,CACxBC,MAAAA,EACAC,IAAAA,GAAAA;AAEA,IAAA,IAAI,CAACD,MAAAA,EAAQE,UAAAA,IAAc,CAACD,IAAAA,IAAQ,OAAOA,SAAS,QAAA,EAAU;QAC5D,OAAOA,IAAAA;AACT,IAAA;IAEAE,MAAAA,CAAOC,OAAO,CAACJ,MAAAA,CAAOE,UAAU,CAAA,CAAEG,OAAO,CAAC,CAAC,CAACC,aAAAA,EAAeC,SAAAA,CAAU,GAAA;QACnE,MAAMC,KAAAA,GAAQP,IAAI,CAACK,aAAAA,CAAc;AAEjC,QAAA,IAAIE,SAAS,IAAA,EAAM;AACjB,YAAA;AACF,QAAA;QAEA,IAAID,SAAAA,CAAUE,IAAI,KAAK,OAAA,EAAS;YAC9B,IAAIF,SAAAA,CAAUG,QAAQ,EAAE;gBACtBT,IAAI,CAACK,cAAc,GAAGK,KAAAA,CAAMC,OAAO,CAACJ,KAAAA,CAAAA,GAChCA,MAAMK,GAAG,CAAC,CAACC,IAAAA,GACTA,IAAAA,IAAQ,OAAOA,IAAAA,KAAS,QAAA,IAAY,QAAQA,IAAAA,GAAOA,IAAAA,CAAKC,EAAE,GAAGD,IAAAA,CAAAA,GAE/DN,KAAAA;YACN,CAAA,MAAO;gBACLP,IAAI,CAACK,aAAAA,CAAc,GACjBE,KAAAA,IAAS,OAAOA,KAAAA,KAAU,QAAA,IAAY,IAAA,IAAQA,KAAAA,GAAQA,KAAAA,CAAMO,EAAE,GAAGP,KAAAA;AACrE,YAAA;AAEA,YAAA;AACF,QAAA;QAEA,IAAID,SAAAA,CAAUE,IAAI,KAAK,WAAA,EAAa;AAClC,YAAA,MAAMO,eAAAA,GAAkBC,MAAAA,CAAOC,QA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,GAAAA,EAAK;iBAAK,GAAI,IAAA;gBACtDR,MAAAA,EAAQ;AAAEuB,oBAAAA,GAAAA,EAAKT,MAAMd;AAAO;AAC9B,aAAA;;;AAGAhC,YAAAA,IAAAA,EAAME,MAAAA,CAAOsD,MAAM,CAACN,SAAAA,CAAUR,SAAAA,CAAAA,EAAYU,aAAAA;AAC5C,SAAA,CAAA;AACF,IAAA,CAAA,CAAA;AACF,CAAA;AAEA,MAAMK,aAAAA,GAAgB,KAAO;AAC3BnC,QAAAA;KACF;;;;"}
|
|
@@ -88,6 +88,97 @@ const addAllLocalesToPermissions = async (permissions)=>{
|
|
|
88
88
|
};
|
|
89
89
|
}));
|
|
90
90
|
};
|
|
91
|
+
/**
|
|
92
|
+
* `properties.locales === null` means access to all locales (see engine handler) — do not patch.
|
|
93
|
+
*/ const needsLocalesPatch = (properties = {})=>{
|
|
94
|
+
if (!('locales' in properties)) {
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
const { locales } = properties;
|
|
98
|
+
if (locales === null) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
return Array.isArray(locales) && locales.length === 0;
|
|
102
|
+
};
|
|
103
|
+
const normalizeRolePermissionsLocales = async (permissions)=>{
|
|
104
|
+
const { isLocalizedContentType } = index.getService('content-types');
|
|
105
|
+
const { actionProvider } = strapi.service('admin::permission');
|
|
106
|
+
const defaultLocale = await index.getService('locales').getDefaultLocale();
|
|
107
|
+
return Promise.all(permissions.map(async (permission)=>{
|
|
108
|
+
const { subject, action, properties = {} } = permission;
|
|
109
|
+
if (!subject) {
|
|
110
|
+
return permission;
|
|
111
|
+
}
|
|
112
|
+
const model = strapi.getModel(subject);
|
|
113
|
+
if (!model || !isLocalizedContentType(model)) {
|
|
114
|
+
return permission;
|
|
115
|
+
}
|
|
116
|
+
const appliesToLocales = await actionProvider.appliesToProperty('locales', action, subject);
|
|
117
|
+
if (!appliesToLocales) {
|
|
118
|
+
return permission;
|
|
119
|
+
}
|
|
120
|
+
// null means access to all locales — same semantics as the permission engine handler
|
|
121
|
+
if (properties.locales === null) {
|
|
122
|
+
return permission;
|
|
123
|
+
}
|
|
124
|
+
if (needsLocalesPatch(properties) && defaultLocale) {
|
|
125
|
+
return {
|
|
126
|
+
...permission,
|
|
127
|
+
properties: {
|
|
128
|
+
...properties,
|
|
129
|
+
locales: [
|
|
130
|
+
defaultLocale
|
|
131
|
+
]
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
return permission;
|
|
136
|
+
}));
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* Repairs role permissions on content types that just had i18n enabled in this sync.
|
|
140
|
+
* Only patches permissions where `properties.locales` is missing or `[]`; leaves `null` alone.
|
|
141
|
+
* Safe to run at `afterSync` time because it does not depend on the actionProvider.
|
|
142
|
+
*/ const repairPermissionsForNewlyLocalizedTypes = async ({ oldContentTypes, contentTypes })=>{
|
|
143
|
+
if (!oldContentTypes) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const { isLocalizedContentType } = index.getService('content-types');
|
|
147
|
+
const newlyLocalizedUids = Object.keys(contentTypes).filter((uid)=>oldContentTypes[uid] && !isLocalizedContentType(oldContentTypes[uid]) && isLocalizedContentType(contentTypes[uid]));
|
|
148
|
+
if (newlyLocalizedUids.length === 0) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
try {
|
|
152
|
+
const defaultLocaleCode = await index.getService('locales').getDefaultLocale();
|
|
153
|
+
if (!defaultLocaleCode) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const permissionService = strapi.service('admin::permission');
|
|
157
|
+
const allPermissions = await permissionService.findMany({
|
|
158
|
+
where: {
|
|
159
|
+
subject: {
|
|
160
|
+
$in: newlyLocalizedUids
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
const results = await Promise.allSettled(allPermissions.filter((permission)=>needsLocalesPatch(permission.properties)).map((permission)=>strapi.db.query('admin::permission').update({
|
|
165
|
+
where: {
|
|
166
|
+
id: permission.id
|
|
167
|
+
},
|
|
168
|
+
data: {
|
|
169
|
+
properties: {
|
|
170
|
+
...permission.properties,
|
|
171
|
+
locales: [
|
|
172
|
+
defaultLocaleCode
|
|
173
|
+
]
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
})));
|
|
177
|
+
results.filter((r)=>r.status === 'rejected').forEach((r)=>strapi.log.error('Failed to patch i18n permission during newly-localized type repair.', r.reason));
|
|
178
|
+
} catch (error) {
|
|
179
|
+
strapi.log.error('Failed to repair i18n permissions for newly localized content types.', error);
|
|
180
|
+
}
|
|
181
|
+
};
|
|
91
182
|
const syncSuperAdminPermissionsWithLocales = async ()=>{
|
|
92
183
|
const roleService = strapi.service('admin::role');
|
|
93
184
|
const permissionService = strapi.service('admin::permission');
|
|
@@ -114,6 +205,7 @@ const registerI18nActionsHooks = ()=>{
|
|
|
114
205
|
const { hooks } = strapi.service('admin::role');
|
|
115
206
|
actionProvider.hooks.appliesPropertyToSubject.register(shouldApplyLocalesPropertyToSubject);
|
|
116
207
|
hooks.willResetSuperAdminPermissions.register(addAllLocalesToPermissions);
|
|
208
|
+
hooks.willValidateUpdatePermissions.register(normalizeRolePermissionsLocales);
|
|
117
209
|
};
|
|
118
210
|
const updateActionsProperties = ()=>{
|
|
119
211
|
const { actionProvider } = strapi.service('admin::permission');
|
|
@@ -129,7 +221,9 @@ var i18nActionsService = {
|
|
|
129
221
|
registerI18nActions,
|
|
130
222
|
registerI18nActionsHooks,
|
|
131
223
|
updateActionsProperties,
|
|
132
|
-
syncSuperAdminPermissionsWithLocales
|
|
224
|
+
syncSuperAdminPermissionsWithLocales,
|
|
225
|
+
repairPermissionsForNewlyLocalizedTypes,
|
|
226
|
+
normalizeRolePermissionsLocales
|
|
133
227
|
};
|
|
134
228
|
|
|
135
229
|
module.exports = i18nActionsService;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"actions.js","sources":["../../../../server/src/services/permissions/actions.ts"],"sourcesContent":["import { isArray, getOr, prop } from 'lodash/fp';\nimport { getService } from '../../utils';\n\nconst actions = [\n {\n section: 'settings',\n category: 'Internationalization',\n subCategory: 'Locales',\n pluginName: 'i18n',\n displayName: 'Create',\n uid: 'locale.create',\n },\n {\n section: 'settings',\n category: 'Internationalization',\n subCategory: 'Locales',\n pluginName: 'i18n',\n displayName: 'Read',\n uid: 'locale.read',\n aliases: [\n { actionId: 'plugin::content-manager.explorer.read', subjects: ['plugin::i18n.locale'] },\n ],\n },\n {\n section: 'settings',\n category: 'Internationalization',\n subCategory: 'Locales',\n pluginName: 'i18n',\n displayName: 'Update',\n uid: 'locale.update',\n },\n {\n section: 'settings',\n category: 'Internationalization',\n subCategory: 'Locales',\n pluginName: 'i18n',\n displayName: 'Delete',\n uid: 'locale.delete',\n },\n];\n\nconst addLocalesPropertyIfNeeded = ({ value: action }: any) => {\n const {\n section,\n options: { applyToProperties },\n } = action;\n\n // Only add the locales property to contentTypes' actions\n if (section !== 'contentTypes') {\n return;\n }\n\n // If the 'locales' property is already declared within the applyToProperties array, then ignore the next steps\n if (isArray(applyToProperties) && applyToProperties.includes('locales')) {\n return;\n }\n\n // Add the 'locales' property to the applyToProperties array (create it if necessary)\n action.options.applyToProperties = isArray(applyToProperties)\n ? applyToProperties.concat('locales')\n : ['locales'];\n};\n\nconst shouldApplyLocalesPropertyToSubject = ({ property, subject }: any) => {\n if (property === 'locales') {\n const model = strapi.getModel(subject);\n\n return getService('content-types').isLocalizedContentType(model);\n }\n\n return true;\n};\n\nconst addAllLocalesToPermissions = async (permissions: any) => {\n const { actionProvider } = strapi.service('admin::permission');\n const { find: findAllLocales } = getService('locales');\n\n const allLocales = await findAllLocales();\n const allLocalesCode = allLocales.map(prop('code'));\n\n return Promise.all(\n permissions.map(async (permission: any) => {\n const { action, subject } = permission;\n\n const appliesToLocalesProperty = await actionProvider.appliesToProperty(\n 'locales',\n action,\n subject\n );\n\n if (!appliesToLocalesProperty) {\n return permission;\n }\n\n const oldPermissionProperties = getOr({}, 'properties', permission);\n\n return { ...permission, properties: { ...oldPermissionProperties, locales: allLocalesCode } };\n })\n );\n};\n\nconst syncSuperAdminPermissionsWithLocales = async () => {\n const roleService = strapi.service('admin::role');\n const permissionService = strapi.service('admin::permission');\n\n const superAdminRole = await roleService.getSuperAdmin();\n\n if (!superAdminRole) {\n return;\n }\n\n const superAdminPermissions = await permissionService.findMany({\n where: {\n role: {\n id: superAdminRole.id,\n },\n },\n });\n\n const newSuperAdminPermissions = await addAllLocalesToPermissions(superAdminPermissions);\n\n await roleService.assignPermissions(superAdminRole.id, newSuperAdminPermissions);\n};\n\nconst registerI18nActions = async () => {\n const { actionProvider } = strapi.service('admin::permission');\n\n await actionProvider.registerMany(actions);\n};\n\nconst registerI18nActionsHooks = () => {\n const { actionProvider } = strapi.service('admin::permission');\n const { hooks } = strapi.service('admin::role');\n\n actionProvider.hooks.appliesPropertyToSubject.register(shouldApplyLocalesPropertyToSubject);\n hooks.willResetSuperAdminPermissions.register(addAllLocalesToPermissions);\n};\n\nconst updateActionsProperties = () => {\n const { actionProvider } = strapi.service('admin::permission');\n\n // Register the transformation for every new action\n actionProvider.hooks.willRegister.register(addLocalesPropertyIfNeeded);\n\n // Handle already registered actions\n actionProvider.values().forEach((action: any) => addLocalesPropertyIfNeeded({ value: action }));\n};\n\nexport default {\n actions,\n registerI18nActions,\n registerI18nActionsHooks,\n updateActionsProperties,\n syncSuperAdminPermissionsWithLocales,\n};\n"],"names":["actions","section","category","subCategory","pluginName","displayName","uid","aliases","actionId","subjects","addLocalesPropertyIfNeeded","value","action","options","applyToProperties","isArray","includes","concat","shouldApplyLocalesPropertyToSubject","property","subject","model","strapi","getModel","getService","isLocalizedContentType","addAllLocalesToPermissions","permissions","actionProvider","service","find","findAllLocales","allLocales","allLocalesCode","map","prop","Promise","all","permission","appliesToLocalesProperty","appliesToProperty","oldPermissionProperties","getOr","properties","locales","syncSuperAdminPermissionsWithLocales","roleService","permissionService","superAdminRole","getSuperAdmin","superAdminPermissions","findMany","where","role","id","newSuperAdminPermissions","assignPermissions","registerI18nActions","registerMany","registerI18nActionsHooks","hooks","appliesPropertyToSubject","register","willResetSuperAdminPermissions","updateActionsProperties","willRegister","values","forEach"],"mappings":";;;;;AAGA,MAAMA,OAAAA,GAAU;AACd,IAAA;QACEC,OAAAA,EAAS,UAAA;QACTC,QAAAA,EAAU,sBAAA;QACVC,WAAAA,EAAa,SAAA;QACbC,UAAAA,EAAY,MAAA;QACZC,WAAAA,EAAa,QAAA;QACbC,GAAAA,EAAK;AACP,KAAA;AACA,IAAA;QACEL,OAAAA,EAAS,UAAA;QACTC,QAAAA,EAAU,sBAAA;QACVC,WAAAA,EAAa,SAAA;QACbC,UAAAA,EAAY,MAAA;QACZC,WAAAA,EAAa,MAAA;QACbC,GAAAA,EAAK,aAAA;QACLC,OAAAA,EAAS;AACP,YAAA;gBAAEC,QAAAA,EAAU,uCAAA;gBAAyCC,QAAAA,EAAU;AAAC,oBAAA;AAAsB;AAAC;AACxF;AACH,KAAA;AACA,IAAA;QACER,OAAAA,EAAS,UAAA;QACTC,QAAAA,EAAU,sBAAA;QACVC,WAAAA,EAAa,SAAA;QACbC,UAAAA,EAAY,MAAA;QACZC,WAAAA,EAAa,QAAA;QACbC,GAAAA,EAAK;AACP,KAAA;AACA,IAAA;QACEL,OAAAA,EAAS,UAAA;QACTC,QAAAA,EAAU,sBAAA;QACVC,WAAAA,EAAa,SAAA;QACbC,UAAAA,EAAY,MAAA;QACZC,WAAAA,EAAa,QAAA;QACbC,GAAAA,EAAK;AACP;AACD,CAAA;AAED,MAAMI,0BAAAA,GAA6B,CAAC,EAAEC,KAAAA,EAAOC,MAAM,EAAO,GAAA;IACxD,MAAM,EACJX,OAAO,EACPY,OAAAA,EAAS,EAAEC,iBAAiB,EAAE,EAC/B,GAAGF,MAAAA;;AAGJ,IAAA,IAAIX,YAAY,cAAA,EAAgB;AAC9B,QAAA;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AAE,GAAGN,MAAAA,CAAOO,OAAO,CAAC,mBAAA,CAAA;AAC1C,IAAA,MAAM,EAAE+B,KAAK,EAAE,GAAGtC,MAAAA,CAAOO,OAAO,CAAC,aAAA,CAAA;AAEjCD,IAAAA,cAAAA,CAAegC,KAAK,CAACC,wBAAwB,CAACC,QAAQ,CAAC5C,mCAAAA,CAAAA;IACvD0C,KAAAA,CAAMG,8BAA8B,CAACD,QAAQ,CAACpC,0BAAAA,CAAAA;AAChD,CAAA;AAEA,MAAMsC,uBAAAA,GAA0B,IAAA;AAC9B,IAAA,MAAM,EAAEpC,cAAc,EAAE,GAAGN,MAAAA,CAAOO,OAAO,CAAC,mBAAA,CAAA;;AAG1CD,IAAAA,cAAAA,CAAegC,KAAK,CAACK,YAAY,CAACH,QAAQ,CAACpD,0BAAAA,CAAAA;;AAG3CkB,IAAAA,cAAAA,CAAesC,MAAM,EAAA,CAAGC,OAAO,CAAC,CAACvD,SAAgBF,0BAAAA,CAA2B;YAAEC,KAAAA,EAAOC;AAAO,SAAA,CAAA,CAAA;AAC9F,CAAA;AAEA,yBAAe;AACbZ,IAAAA,OAAAA;AACAyD,IAAAA,mBAAAA;AACAE,IAAAA,wBAAAA;AACAK,IAAAA,uBAAAA;AACAnB,IAAAA;AACF,CAAA;;;;"}
|
|
1
|
+
{"version":3,"file":"actions.js","sources":["../../../../server/src/services/permissions/actions.ts"],"sourcesContent":["import { isArray, getOr, prop } from 'lodash/fp';\nimport { getService } from '../../utils';\n\nconst actions = [\n {\n section: 'settings',\n category: 'Internationalization',\n subCategory: 'Locales',\n pluginName: 'i18n',\n displayName: 'Create',\n uid: 'locale.create',\n },\n {\n section: 'settings',\n category: 'Internationalization',\n subCategory: 'Locales',\n pluginName: 'i18n',\n displayName: 'Read',\n uid: 'locale.read',\n aliases: [\n { actionId: 'plugin::content-manager.explorer.read', subjects: ['plugin::i18n.locale'] },\n ],\n },\n {\n section: 'settings',\n category: 'Internationalization',\n subCategory: 'Locales',\n pluginName: 'i18n',\n displayName: 'Update',\n uid: 'locale.update',\n },\n {\n section: 'settings',\n category: 'Internationalization',\n subCategory: 'Locales',\n pluginName: 'i18n',\n displayName: 'Delete',\n uid: 'locale.delete',\n },\n];\n\nconst addLocalesPropertyIfNeeded = ({ value: action }: any) => {\n const {\n section,\n options: { applyToProperties },\n } = action;\n\n // Only add the locales property to contentTypes' actions\n if (section !== 'contentTypes') {\n return;\n }\n\n // If the 'locales' property is already declared within the applyToProperties array, then ignore the next steps\n if (isArray(applyToProperties) && applyToProperties.includes('locales')) {\n return;\n }\n\n // Add the 'locales' property to the applyToProperties array (create it if necessary)\n action.options.applyToProperties = isArray(applyToProperties)\n ? applyToProperties.concat('locales')\n : ['locales'];\n};\n\nconst shouldApplyLocalesPropertyToSubject = ({ property, subject }: any) => {\n if (property === 'locales') {\n const model = strapi.getModel(subject);\n\n return getService('content-types').isLocalizedContentType(model);\n }\n\n return true;\n};\n\nconst addAllLocalesToPermissions = async (permissions: any) => {\n const { actionProvider } = strapi.service('admin::permission');\n const { find: findAllLocales } = getService('locales');\n\n const allLocales = await findAllLocales();\n const allLocalesCode = allLocales.map(prop('code'));\n\n return Promise.all(\n permissions.map(async (permission: any) => {\n const { action, subject } = permission;\n\n const appliesToLocalesProperty = await actionProvider.appliesToProperty(\n 'locales',\n action,\n subject\n );\n\n if (!appliesToLocalesProperty) {\n return permission;\n }\n\n const oldPermissionProperties = getOr({}, 'properties', permission);\n\n return { ...permission, properties: { ...oldPermissionProperties, locales: allLocalesCode } };\n })\n );\n};\n\n/**\n * `properties.locales === null` means access to all locales (see engine handler) — do not patch.\n */\nconst needsLocalesPatch = (properties: Record<string, unknown> = {}) => {\n if (!('locales' in properties)) {\n return true;\n }\n\n const { locales } = properties;\n\n if (locales === null) {\n return false;\n }\n\n return Array.isArray(locales) && locales.length === 0;\n};\n\nconst normalizeRolePermissionsLocales = async (permissions: any[]): Promise<any[]> => {\n const { isLocalizedContentType } = getService('content-types');\n const { actionProvider } = strapi.service('admin::permission');\n const defaultLocale = await getService('locales').getDefaultLocale();\n\n return Promise.all(\n permissions.map(async (permission) => {\n const { subject, action, properties = {} } = permission;\n\n if (!subject) {\n return permission;\n }\n\n const model = strapi.getModel(subject);\n\n if (!model || !isLocalizedContentType(model)) {\n return permission;\n }\n\n const appliesToLocales = await actionProvider.appliesToProperty('locales', action, subject);\n\n if (!appliesToLocales) {\n return permission;\n }\n\n // null means access to all locales — same semantics as the permission engine handler\n if (properties.locales === null) {\n return permission;\n }\n\n if (needsLocalesPatch(properties) && defaultLocale) {\n return { ...permission, properties: { ...properties, locales: [defaultLocale] } };\n }\n\n return permission;\n })\n );\n};\n\n/**\n * Repairs role permissions on content types that just had i18n enabled in this sync.\n * Only patches permissions where `properties.locales` is missing or `[]`; leaves `null` alone.\n * Safe to run at `afterSync` time because it does not depend on the actionProvider.\n */\nconst repairPermissionsForNewlyLocalizedTypes = async ({\n oldContentTypes,\n contentTypes,\n}: {\n oldContentTypes: Record<string, any> | null | undefined;\n contentTypes: Record<string, any>;\n}) => {\n if (!oldContentTypes) {\n return;\n }\n\n const { isLocalizedContentType } = getService('content-types');\n\n const newlyLocalizedUids = Object.keys(contentTypes).filter(\n (uid) =>\n oldContentTypes[uid] &&\n !isLocalizedContentType(oldContentTypes[uid]) &&\n isLocalizedContentType(contentTypes[uid])\n );\n\n if (newlyLocalizedUids.length === 0) {\n return;\n }\n\n try {\n const defaultLocaleCode = await getService('locales').getDefaultLocale();\n if (!defaultLocaleCode) {\n return;\n }\n\n const permissionService = strapi.service('admin::permission');\n const allPermissions = await permissionService.findMany({\n where: { subject: { $in: newlyLocalizedUids } },\n });\n\n const results = await Promise.allSettled(\n allPermissions\n .filter((permission: any) => needsLocalesPatch(permission.properties))\n .map((permission: any) =>\n strapi.db.query('admin::permission').update({\n where: { id: permission.id },\n data: {\n properties: { ...permission.properties, locales: [defaultLocaleCode] },\n },\n })\n )\n );\n\n results\n .filter((r): r is PromiseRejectedResult => r.status === 'rejected')\n .forEach((r) =>\n strapi.log.error(\n 'Failed to patch i18n permission during newly-localized type repair.',\n r.reason\n )\n );\n } catch (error) {\n strapi.log.error('Failed to repair i18n permissions for newly localized content types.', error);\n }\n};\n\nconst syncSuperAdminPermissionsWithLocales = async () => {\n const roleService = strapi.service('admin::role');\n const permissionService = strapi.service('admin::permission');\n\n const superAdminRole = await roleService.getSuperAdmin();\n\n if (!superAdminRole) {\n return;\n }\n\n const superAdminPermissions = await permissionService.findMany({\n where: {\n role: {\n id: superAdminRole.id,\n },\n },\n });\n\n const newSuperAdminPermissions = await addAllLocalesToPermissions(superAdminPermissions);\n\n await roleService.assignPermissions(superAdminRole.id, newSuperAdminPermissions);\n};\n\nconst registerI18nActions = async () => {\n const { actionProvider } = strapi.service('admin::permission');\n\n await actionProvider.registerMany(actions);\n};\n\nconst registerI18nActionsHooks = () => {\n const { actionProvider } = strapi.service('admin::permission');\n const { hooks } = strapi.service('admin::role');\n\n actionProvider.hooks.appliesPropertyToSubject.register(shouldApplyLocalesPropertyToSubject);\n hooks.willResetSuperAdminPermissions.register(addAllLocalesToPermissions);\n hooks.willValidateUpdatePermissions.register(normalizeRolePermissionsLocales);\n};\n\nconst updateActionsProperties = () => {\n const { actionProvider } = strapi.service('admin::permission');\n\n // Register the transformation for every new action\n actionProvider.hooks.willRegister.register(addLocalesPropertyIfNeeded);\n\n // Handle already registered actions\n actionProvider.values().forEach((action: any) => addLocalesPropertyIfNeeded({ value: action }));\n};\n\nexport default {\n actions,\n registerI18nActions,\n registerI18nActionsHooks,\n updateActionsProperties,\n syncSuperAdminPermissionsWithLocales,\n repairPermissionsForNewlyLocalizedTypes,\n normalizeRolePermissionsLocales,\n};\n"],"names":["actions","section","category","subCategory","pluginName","displayName","uid","aliases","actionId","subjects","addLocalesPropertyIfNeeded","value","action","options","applyToProperties","isArray","includes","concat","shouldApplyLocalesPropertyToSubject","property","subject","model","strapi","getModel","getService","isLocalizedContentType","addAllLocalesToPermissions","permissions","actionProvider","service","find","findAllLocales","allLocales","allLocalesCode","map","prop","Promise","all","permission","appliesToLocalesProperty","appliesToProperty","oldPermissionProperties","getOr","properties","locales","needsLocalesPatch","Array","length","normalizeRolePermissionsLocales","defaultLocale","getDefaultLocale","appliesToLocales","repairPermissionsForNewlyLocalizedTypes","oldContentTypes","contentTypes","newlyLocalizedUids","Object","keys","filter","defaultLocaleCode","permissionService","allPermissions","findMany","where","$in","results","allSettled","db","query","update","id","data","r","status","forEach","log","error","reason","syncSuperAdminPermissionsWithLocales","roleService","superAdminRole","getSuperAdmin","superAdminPermissions","role","newSuperAdminPermissions","assignPermissions","registerI18nActions","registerMany","registerI18nActionsHooks","hooks","appliesPropertyToSubject","register","willResetSuperAdminPermissions","willValidateUpdatePermissions","updateActionsProperties","willRegister","values"],"mappings":";;;;;AAGA,MAAMA,OAAAA,GAAU;AACd,IAAA;QACEC,OAAAA,EAAS,UAAA;QACTC,QAAAA,EAAU,sBAAA;QACVC,WAAAA,EAAa,SAAA;QACbC,UAAAA,EAAY,MAAA;QACZC,WAAAA,EAAa,QAAA;QACbC,GAAAA,EAAK;AACP,KAAA;AACA,IAAA;QACEL,OAAAA,EAAS,UAAA;QACTC,QAAAA,EAAU,sBAAA;QACVC,WAAAA,EAAa,SAAA;QACbC,UAAAA,EAAY,MAAA;QACZC,WAAAA,EAAa,MAAA;QACbC,GAAAA,EAAK,aAAA;QACLC,OAAAA,EAAS;AACP,YAAA;gBAAEC,QAAAA,EAAU,uCAAA;gBAAyCC,QAAAA,EAAU;AAAC,oBAAA;AAAsB;AAAC;AACxF;AACH,KAAA;AACA,IAAA;QACER,OAAAA,EAAS,UAAA;QACTC,QAAAA,EAAU,sBAAA;QACVC,WAAAA,EAAa,SAAA;QACbC,UAAAA,EAAY,MAAA;QACZC,WAAAA,EAAa,QAAA;QACbC,GAAAA,EAAK;AACP,KAAA;AACA,IAAA;QACEL,OAAAA,EAAS,UAAA;QACTC,QAAAA,EAAU,sBAAA;QACVC,WAAAA,EAAa,SAAA;QACbC,UAA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AAA;AAC/B,IAAA,MAAM,EAAE5D,cAAc,EAAE,GAAGN,MAAAA,CAAOO,OAAO,CAAC,mBAAA,CAAA;AAC1C,IAAA,MAAM,EAAE4D,KAAK,EAAE,GAAGnE,MAAAA,CAAOO,OAAO,CAAC,aAAA,CAAA;AAEjCD,IAAAA,cAAAA,CAAe6D,KAAK,CAACC,wBAAwB,CAACC,QAAQ,CAACzE,mCAAAA,CAAAA;IACvDuE,KAAAA,CAAMG,8BAA8B,CAACD,QAAQ,CAACjE,0BAAAA,CAAAA;IAC9C+D,KAAAA,CAAMI,6BAA6B,CAACF,QAAQ,CAAC3C,+BAAAA,CAAAA;AAC/C,CAAA;AAEA,MAAM8C,uBAAAA,GAA0B,IAAA;AAC9B,IAAA,MAAM,EAAElE,cAAc,EAAE,GAAGN,MAAAA,CAAOO,OAAO,CAAC,mBAAA,CAAA;;AAG1CD,IAAAA,cAAAA,CAAe6D,KAAK,CAACM,YAAY,CAACJ,QAAQ,CAACjF,0BAAAA,CAAAA;;AAG3CkB,IAAAA,cAAAA,CAAeoE,MAAM,EAAA,CAAGtB,OAAO,CAAC,CAAC9D,SAAgBF,0BAAAA,CAA2B;YAAEC,KAAAA,EAAOC;AAAO,SAAA,CAAA,CAAA;AAC9F,CAAA;AAEA,yBAAe;AACbZ,IAAAA,OAAAA;AACAsF,IAAAA,mBAAAA;AACAE,IAAAA,wBAAAA;AACAM,IAAAA,uBAAAA;AACAhB,IAAAA,oCAAAA;AACA1B,IAAAA,uCAAAA;AACAJ,IAAAA;AACF,CAAA;;;;"}
|
|
@@ -86,6 +86,97 @@ const addAllLocalesToPermissions = async (permissions)=>{
|
|
|
86
86
|
};
|
|
87
87
|
}));
|
|
88
88
|
};
|
|
89
|
+
/**
|
|
90
|
+
* `properties.locales === null` means access to all locales (see engine handler) — do not patch.
|
|
91
|
+
*/ const needsLocalesPatch = (properties = {})=>{
|
|
92
|
+
if (!('locales' in properties)) {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
const { locales } = properties;
|
|
96
|
+
if (locales === null) {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
return Array.isArray(locales) && locales.length === 0;
|
|
100
|
+
};
|
|
101
|
+
const normalizeRolePermissionsLocales = async (permissions)=>{
|
|
102
|
+
const { isLocalizedContentType } = getService('content-types');
|
|
103
|
+
const { actionProvider } = strapi.service('admin::permission');
|
|
104
|
+
const defaultLocale = await getService('locales').getDefaultLocale();
|
|
105
|
+
return Promise.all(permissions.map(async (permission)=>{
|
|
106
|
+
const { subject, action, properties = {} } = permission;
|
|
107
|
+
if (!subject) {
|
|
108
|
+
return permission;
|
|
109
|
+
}
|
|
110
|
+
const model = strapi.getModel(subject);
|
|
111
|
+
if (!model || !isLocalizedContentType(model)) {
|
|
112
|
+
return permission;
|
|
113
|
+
}
|
|
114
|
+
const appliesToLocales = await actionProvider.appliesToProperty('locales', action, subject);
|
|
115
|
+
if (!appliesToLocales) {
|
|
116
|
+
return permission;
|
|
117
|
+
}
|
|
118
|
+
// null means access to all locales — same semantics as the permission engine handler
|
|
119
|
+
if (properties.locales === null) {
|
|
120
|
+
return permission;
|
|
121
|
+
}
|
|
122
|
+
if (needsLocalesPatch(properties) && defaultLocale) {
|
|
123
|
+
return {
|
|
124
|
+
...permission,
|
|
125
|
+
properties: {
|
|
126
|
+
...properties,
|
|
127
|
+
locales: [
|
|
128
|
+
defaultLocale
|
|
129
|
+
]
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
return permission;
|
|
134
|
+
}));
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* Repairs role permissions on content types that just had i18n enabled in this sync.
|
|
138
|
+
* Only patches permissions where `properties.locales` is missing or `[]`; leaves `null` alone.
|
|
139
|
+
* Safe to run at `afterSync` time because it does not depend on the actionProvider.
|
|
140
|
+
*/ const repairPermissionsForNewlyLocalizedTypes = async ({ oldContentTypes, contentTypes })=>{
|
|
141
|
+
if (!oldContentTypes) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const { isLocalizedContentType } = getService('content-types');
|
|
145
|
+
const newlyLocalizedUids = Object.keys(contentTypes).filter((uid)=>oldContentTypes[uid] && !isLocalizedContentType(oldContentTypes[uid]) && isLocalizedContentType(contentTypes[uid]));
|
|
146
|
+
if (newlyLocalizedUids.length === 0) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
try {
|
|
150
|
+
const defaultLocaleCode = await getService('locales').getDefaultLocale();
|
|
151
|
+
if (!defaultLocaleCode) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const permissionService = strapi.service('admin::permission');
|
|
155
|
+
const allPermissions = await permissionService.findMany({
|
|
156
|
+
where: {
|
|
157
|
+
subject: {
|
|
158
|
+
$in: newlyLocalizedUids
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
const results = await Promise.allSettled(allPermissions.filter((permission)=>needsLocalesPatch(permission.properties)).map((permission)=>strapi.db.query('admin::permission').update({
|
|
163
|
+
where: {
|
|
164
|
+
id: permission.id
|
|
165
|
+
},
|
|
166
|
+
data: {
|
|
167
|
+
properties: {
|
|
168
|
+
...permission.properties,
|
|
169
|
+
locales: [
|
|
170
|
+
defaultLocaleCode
|
|
171
|
+
]
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
})));
|
|
175
|
+
results.filter((r)=>r.status === 'rejected').forEach((r)=>strapi.log.error('Failed to patch i18n permission during newly-localized type repair.', r.reason));
|
|
176
|
+
} catch (error) {
|
|
177
|
+
strapi.log.error('Failed to repair i18n permissions for newly localized content types.', error);
|
|
178
|
+
}
|
|
179
|
+
};
|
|
89
180
|
const syncSuperAdminPermissionsWithLocales = async ()=>{
|
|
90
181
|
const roleService = strapi.service('admin::role');
|
|
91
182
|
const permissionService = strapi.service('admin::permission');
|
|
@@ -112,6 +203,7 @@ const registerI18nActionsHooks = ()=>{
|
|
|
112
203
|
const { hooks } = strapi.service('admin::role');
|
|
113
204
|
actionProvider.hooks.appliesPropertyToSubject.register(shouldApplyLocalesPropertyToSubject);
|
|
114
205
|
hooks.willResetSuperAdminPermissions.register(addAllLocalesToPermissions);
|
|
206
|
+
hooks.willValidateUpdatePermissions.register(normalizeRolePermissionsLocales);
|
|
115
207
|
};
|
|
116
208
|
const updateActionsProperties = ()=>{
|
|
117
209
|
const { actionProvider } = strapi.service('admin::permission');
|
|
@@ -127,7 +219,9 @@ var i18nActionsService = {
|
|
|
127
219
|
registerI18nActions,
|
|
128
220
|
registerI18nActionsHooks,
|
|
129
221
|
updateActionsProperties,
|
|
130
|
-
syncSuperAdminPermissionsWithLocales
|
|
222
|
+
syncSuperAdminPermissionsWithLocales,
|
|
223
|
+
repairPermissionsForNewlyLocalizedTypes,
|
|
224
|
+
normalizeRolePermissionsLocales
|
|
131
225
|
};
|
|
132
226
|
|
|
133
227
|
export { i18nActionsService as default };
|