mirror of
https://github.com/directus/directus.git
synced 2026-02-01 16:24:59 -05:00
41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
import { AbstractServiceOptions, PermissionsAction, Item, PrimaryKey } from '../types';
|
|
import { ItemsService } from '../services/items';
|
|
|
|
export class PermissionsService extends ItemsService {
|
|
constructor(options?: AbstractServiceOptions) {
|
|
super('directus_permissions', options);
|
|
}
|
|
|
|
async getAllowedCollections(role: string | null, action: PermissionsAction) {
|
|
const query = this.knex
|
|
.select('collection')
|
|
.from('directus_permissions')
|
|
.where({ role, action });
|
|
const results = await query;
|
|
return results.map((result) => result.collection);
|
|
}
|
|
|
|
async getAllowedFields(role: string | null, action: PermissionsAction, collection?: string) {
|
|
const query = this.knex
|
|
.select('collection', 'fields')
|
|
.from('directus_permissions')
|
|
.where({ role, action });
|
|
|
|
if (collection) {
|
|
query.andWhere({ collection });
|
|
}
|
|
|
|
const results = await query;
|
|
|
|
const fieldsPerCollection: Record<string, string[]> = {};
|
|
|
|
for (const result of results) {
|
|
const { collection, fields } = result;
|
|
if (!fieldsPerCollection[collection]) fieldsPerCollection[collection] = [];
|
|
fieldsPerCollection[collection].push(...(fields || '').split(','));
|
|
}
|
|
|
|
return fieldsPerCollection;
|
|
}
|
|
}
|