mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
* Start the xOne/xMany work * Update update/upsert * Finish updating itemsservice * Add comments, add nested revisions on update * Use new internal api * Update collectionService to one/many structure * Move files to one/many structure * Move permissions to one/many structure * Move relations service to one/many structure * Move roles to one/many structure * Move users service over * Move webhooks to updated structure * Move deprecated methods to the bottom * Replace deprecated uses * Use new methods in controllers, add batch update/delete by query * Use updated methods in API * Fix email being required * Remove unnecessary DB call * Fix batch update/delete validation
89 lines
2.7 KiB
TypeScript
89 lines
2.7 KiB
TypeScript
import { AbstractServiceOptions, PermissionsAction, Query, Item, PrimaryKey } from '../types';
|
|
import { ItemsService, MutationOptions, QueryOptions } from '../services/items';
|
|
import { filterItems } from '../utils/filter-items';
|
|
import logger from '../logger';
|
|
|
|
import { appAccessMinimalPermissions } from '../database/system-data/app-access-permissions';
|
|
|
|
export class PermissionsService extends ItemsService {
|
|
constructor(options: AbstractServiceOptions) {
|
|
super('directus_permissions', options);
|
|
}
|
|
|
|
getAllowedFields(action: PermissionsAction, collection?: string) {
|
|
const results = this.schema.permissions.filter((permission) => {
|
|
let matchesCollection = true;
|
|
|
|
if (collection) {
|
|
matchesCollection = permission.collection === collection;
|
|
}
|
|
|
|
return permission.action === action;
|
|
});
|
|
|
|
const fieldsPerCollection: Record<string, string[]> = {};
|
|
|
|
for (const result of results) {
|
|
const { collection, fields } = result;
|
|
if (!fieldsPerCollection[collection]) fieldsPerCollection[collection] = [];
|
|
fieldsPerCollection[collection].push(...(fields ?? []));
|
|
}
|
|
|
|
return fieldsPerCollection;
|
|
}
|
|
|
|
async readByQuery(query: Query, opts?: QueryOptions): Promise<Partial<Item>[]> {
|
|
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: PrimaryKey[], query: Query = {}, opts?: QueryOptions): Promise<Partial<Item>[]> {
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* @deprecated Use `readOne` or `readMany` instead
|
|
*/
|
|
readByKey(keys: PrimaryKey[], query?: Query, action?: PermissionsAction): Promise<null | Partial<Item>[]>;
|
|
readByKey(key: PrimaryKey, query?: Query, action?: PermissionsAction): Promise<null | Partial<Item>>;
|
|
async readByKey(
|
|
key: PrimaryKey | PrimaryKey[],
|
|
query: Query = {},
|
|
action: PermissionsAction = 'read'
|
|
): Promise<null | Partial<Item> | Partial<Item>[]> {
|
|
logger.warn(
|
|
'PermissionsService.readByKey is deprecated and will be removed before v9.0.0. Use readOne or readMany instead.'
|
|
);
|
|
|
|
if (Array.isArray(key)) return await this.readMany(key, query, { permissionsAction: action });
|
|
return await this.readOne(key, query, { permissionsAction: action });
|
|
}
|
|
}
|