Browse Source

Merge branch 'hotfix/V8-7722-trouver-une-solution-pour-viter-'

Olivier Massot 2 months ago
parent
commit
6f72faf407

+ 2 - 0
.gitignore

@@ -34,3 +34,5 @@ coverage/
 models/models.ts
 
 .maintenance
+
+config/abilities/config-precompiled.ts

+ 1 - 1
components/Layout/Header/Menu.vue

@@ -93,7 +93,7 @@ header principal (configuration, paramètres du compte...)
 <script setup lang="ts">
 import { computed, ref } from 'vue'
 import { useMenu } from '~/composables/layout/useMenu'
-import {IMAGE_SIZE} from "~/types/enum/enums";
+import { IMAGE_SIZE } from '~/types/enum/enums'
 
 const props = defineProps({
   name: {

+ 1 - 1
components/Ui/EventList.vue

@@ -93,7 +93,7 @@ import type { Collection } from 'pinia-orm'
 import type Event from '~/models/Freemium/Event'
 import type { Pagination } from '~/types/data'
 import { useDate } from 'vuetify'
-import {IMAGE_SIZE} from "~/types/enum/enums";
+import { IMAGE_SIZE } from '~/types/enum/enums'
 
 const props = defineProps<{
   events: Collection<Event>

+ 0 - 2
config/abilities/config.yaml

@@ -1,2 +0,0 @@
-abilities: !!import/shallow
-  - pages/

+ 88 - 0
prepare/compileAbilitiesConfig.ts

@@ -0,0 +1,88 @@
+/**
+ * Precompile the abilities config from Yaml to TS
+ */
+
+import * as fs from 'fs'
+import * as path from 'path'
+import * as yaml from 'js-yaml'
+
+interface AbilityCondition {
+  function: string
+  parameters?: unknown[]
+  expectedResult?: boolean
+}
+
+interface AbilityConfig {
+  action: string
+  conditions: AbilityCondition[]
+}
+
+interface CompiledAbilities {
+  [key: string]: AbilityConfig
+}
+
+function loadYamlFile(filePath: string): unknown {
+  try {
+    const fileContents = fs.readFileSync(filePath, 'utf8')
+    return yaml.load(fileContents)
+  } catch (error) {
+    console.error(`Error loading YAML file ${filePath}:`, error)
+    return {}
+  }
+}
+
+function compileAbilitiesConfig(): void {
+  const configDir = path.join(process.cwd(), 'config/abilities/pages')
+  const outputPath = path.join(
+    process.cwd(),
+    'config/abilities/config-precompiled.ts',
+  )
+
+  console.log('Starting abilities config compilation...')
+
+  // Read all YAML files from the pages directory
+  const yamlFiles = fs
+    .readdirSync(configDir)
+    .filter((file) => file.endsWith('.yaml'))
+  const compiledAbilities: CompiledAbilities = {}
+
+  yamlFiles.forEach((file) => {
+    const filePath = path.join(configDir, file)
+    console.log(`Processing ${file}...`)
+
+    const config = loadYamlFile(filePath)
+
+    // Merge all abilities from this file into the compiled config
+    Object.assign(compiledAbilities, config)
+  })
+
+  // Generate TypeScript content
+  const header = `/**
+ * AUTO-GENERATED FILE - DO NOT MODIFY MANUALLY
+ *
+ * This file is automatically generated from YAML configuration files
+ * in config/abilities/pages/ directory.
+ *
+ * To make changes, edit the source YAML files and run the compilation script :
+ *     yarn prepare
+ *
+ * Generated on: ${new Date().toISOString()}
+ */
+
+`
+
+  const tsContent = `${header}export default ${JSON.stringify(compiledAbilities, null, 2)} as const
+`
+
+  // Write the compiled TypeScript file
+  fs.writeFileSync(outputPath, tsContent, 'utf8')
+
+  console.log(`✓ Abilities config compiled successfully to ${outputPath}`)
+  console.log(`✓ Processed ${yamlFiles.length} YAML files`)
+  console.log(
+    `✓ Generated ${Object.keys(compiledAbilities).length} ability definitions`,
+  )
+}
+
+// Run the compilation
+compileAbilitiesConfig()

+ 2 - 7
services/rights/abilityBuilder.ts

@@ -1,10 +1,10 @@
-import * as yaml from 'yaml-import'
 import * as _ from 'lodash-es'
 import type { MongoAbility } from '@casl/ability/dist/types/Ability'
 import RoleUtils from '~/services/rights/roleUtils'
 import type { AbilitiesType, AccessProfile } from '~/types/interfaces'
 import type { ABILITIES } from '~/types/enum/enums'
 import type OrganizationProfile from '~/models/Organization/OrganizationProfile'
+import abilitiesConfig from '~/config/abilities/config-precompiled'
 
 interface ConditionParameters {
   action: string
@@ -25,8 +25,6 @@ class AbilityBuilder {
   private readonly accessProfile: AccessProfile
   private readonly organizationProfile: OrganizationProfile
 
-  private readonly configFile = './config/abilities/config.yaml'
-
   private abilities: Array<AbilitiesType> = []
 
   /**
@@ -73,11 +71,8 @@ class AbilityBuilder {
   buildAbilitiesFromConfig() {
     const abilitiesByConfig: Array<AbilitiesType> = []
 
-    const doc = yaml.read(this.configFile)
-    const fromConfig = doc.abilities
-
     _.each(
-      fromConfig,
+      abilitiesConfig,
       (
         ability: { action: ABILITIES; conditions: Array<Condition> },
         subject: string,