import { appAccessMinimalPermissions } from '../database/system-data/app-access-permissions' ; import { ItemsService , QueryOptions } from '../services/items' ; import { AbstractServiceOptions , Item , PrimaryKey , MutationOptions } from '../types' ; import { Query , PermissionsAction } from '@directus/shared/types' ; import { filterItems } from '../utils/filter-items' ; import Keyv from 'keyv' ; import { getCache } from '../cache' ; export class PermissionsService extends ItemsService { systemCache : Keyv < any > ; constructor ( options ) { super ( 'directus_permissions' , options ) ; const { systemCache } = getCache ( ) ; this . systemCache = systemCache ; } getAllowedFields ( action , collection ? ) { const results = this . accountability ?. permissions ?. filter ( ( permission ) => { let matchesCollection = true ; if ( collection ) { matchesCollection = permission . collection === collection ; } const matchesAction = permission . action === action ; return collection ? matchesCollection && matchesAction : matchesAction ; } ) ?? [ ] ; const fieldsPerCollection = { } ; for ( const result of results ) { const { collection , fields } = result ; if ( ! fieldsPerCollection [ collection ] ) fieldsPerCollection [ collection ] = [ ] ; fieldsPerCollection [ collection ] . push ( ... ( fields ?? [ ] ) ) ; } return fieldsPerCollection ; } async readByQuery ( query , opts ? ) { const result = await super . readByQuery ( query , opts ) ; if ( Array . isArray ( result ) && this . accountability && this . accountability . app === true ) { result . push ( ... filterItems ( appAccessMinimalPermissions . map ( ( permission ) => ( { ... permission , role : this . accountability ! . role , } ) ) , query . filter ) ) ; } return result ; } async readMany ( keys , query = { } , opts ? ) { const result = await super . readMany ( keys , query , opts ) ; if ( this . accountability && this . accountability . app === true ) { result . push ( ... filterItems ( appAccessMinimalPermissions . map ( ( permission ) => ( { ... permission , role : this . accountability ! . role , } ) ) , query . filter ) ) ; } return result ; } async createOne ( data , opts ? ) { const res = await super . createOne ( data , opts ) ; await this . systemCache . clear ( ) ; return res ; } async createMany ( data , opts ? ) { const res = await super . createMany ( data , opts ) ; await this . systemCache . clear ( ) ; return res ; } async updateByQuery ( query , data , opts ? ) { const res = await super . updateByQuery ( query , data , opts ) ; await this . systemCache . clear ( ) ; return res ; } async updateOne ( key , data , opts ? ) { const res = await super . updateOne ( key , data , opts ) ; await this . systemCache . clear ( ) ; return res ; } async updateMany ( keys , data , opts ? ) { const res = await super . updateMany ( keys , data , opts ) ; await this . systemCache . clear ( ) ; return res ; } async upsertOne ( payload , opts ? ) { const res = await super . upsertOne ( payload , opts ) ; await this . systemCache . clear ( ) ; return res ; } async upsertMany ( payloads , opts ? ) { const res = await super . upsertMany ( payloads , opts ) ; await this . systemCache . clear ( ) ; return res ; } async deleteByQuery ( query , opts ? ) { const res = await super . deleteByQuery ( query , opts ) ; await this . systemCache . clear ( ) ; return res ; } async deleteOne ( key , opts ? ) { const res = await super . deleteOne ( key , opts ) ; await this . systemCache . clear ( ) ; return res ; } async deleteMany ( keys , opts ? ) { const res = await super . deleteMany ( keys , opts ) ; await this . systemCache . clear ( ) ; return res ; } }