<commit_before>﻿import * as React from "react"
import { Router, Route, Redirect, IndexRoute } from "react-router"
import { Button, OverlayTrigger, Tooltip, MenuItem, DropdownButton } from "react-bootstrap"
import { Dic } from './Globals';
import { ajaxGet, ajaxPost } from './Services';
import { openModal } from './Modals';
import {
    Lite, Entity, ModifiableEntity, EmbeddedEntity, LiteMessage, OperationMessage, EntityPack,
    OperationSymbol, ConstructSymbol_From, ConstructSymbol_FromMany, ConstructSymbol_Simple, ExecuteSymbol, DeleteSymbol
} from './Signum.Entities';
import { OperationLogEntity } from './Signum.Entities.Basics';
import { PropertyRoute, PseudoType, EntityKind, TypeInfo, IType, Type, getTypeInfo, OperationInfo, OperationType, GraphExplorer } from './Reflection';
import { TypeContext, EntityFrame } from './TypeContext';
import * as Finder from './Finder';
import * as Navigator from './Navigator';
import * as QuickLinks from './QuickLinks';
import * as ContexualItems from './SearchControl/ContextualItems';
import ButtonBar from './Frames/ButtonBar';
import { getEntityOperationButtons } from './Operations/EntityOperations';
import { getConstructFromManyContextualItems, getEntityOperationsContextualItems } from './Operations/ContextualOperations';
import { ContextualItemsContext } from './SearchControl/ContextualItems';

export function start() {
    ButtonBar.onButtonBarRender.push(getEntityOperationButtons);
    ContexualItems.onContextualItems.push(getConstructFromManyContextualItems);
    ContexualItems.onContextualItems.push(getEntityOperationsContextualItems);
    QuickLinks.registerGlobalQuickLink(ctx => new QuickLinks.QuickLinkExplore({
        queryName: OperationLogEntity,
        parentColumn: "Target",
        parentValue: ctx.lite
    }, { isVisible: getTypeInfo(ctx.lite.EntityType) && getTypeInfo(ctx.lite.EntityType).requiresSaveOperation }));
}

export const operationSettings: { [operationKey: string]: OperationSettings } = {};

export function addSettings(...settings: OperationSettings[]) {
    settings.forEach(s => Dic.addOrThrow(operationSettings, s.operationSymbol.key!, s));
}


export function getSettings(operation: OperationSymbol | string): OperationSettings | undefined {
    const operationKey = (operation as OperationSymbol).key || operation as string;

    return operationSettings[operationKey];
}

export const isOperationAllowedEvent: Array<(oi: OperationInfo) => boolean> = [];

export function isOperationAllowed(oi: OperationInfo) {
    return isOperationAllowedEvent.every(a => a(oi));
}


export function operationInfos(ti: TypeInfo) {
    return Dic.getValues(ti.operations!).filter(isOperationAllowed);
}

/**
 * Operation Settings
 */
export abstract class OperationSettings {<commit_msg>Refined operation authorization.<commit_after>﻿import * as React from "react"
import { Router, Route, Redirect, IndexRoute } from "react-router"
import { Button, OverlayTrigger, Tooltip, MenuItem, DropdownButton } from "react-bootstrap"
import { Dic } from './Globals';
import { ajaxGet, ajaxPost } from './Services';
import { openModal } from './Modals';
import {
    Lite, Entity, ModifiableEntity, EmbeddedEntity, LiteMessage, OperationMessage, EntityPack,
    OperationSymbol, ConstructSymbol_From, ConstructSymbol_FromMany, ConstructSymbol_Simple, ExecuteSymbol, DeleteSymbol
} from './Signum.Entities';
import { OperationLogEntity } from './Signum.Entities.Basics';
import { PropertyRoute, PseudoType, EntityKind, TypeInfo, IType, Type, getTypeInfo, OperationInfo, OperationType, GraphExplorer } from './Reflection';
import { TypeContext, EntityFrame } from './TypeContext';
import * as Finder from './Finder';
import * as Navigator from './Navigator';
import * as QuickLinks from './QuickLinks';
import * as ContexualItems from './SearchControl/ContextualItems';
import ButtonBar from './Frames/ButtonBar';
import { getEntityOperationButtons } from './Operations/EntityOperations';
import { getConstructFromManyContextualItems, getEntityOperationsContextualItems } from './Operations/ContextualOperations';
import { ContextualItemsContext } from './SearchControl/ContextualItems';

export function start() {
    ButtonBar.onButtonBarRender.push(getEntityOperationButtons);
    ContexualItems.onContextualItems.push(getConstructFromManyContextualItems);
    ContexualItems.onContextualItems.push(getEntityOperationsContextualItems);
    QuickLinks.registerGlobalQuickLink(ctx => new QuickLinks.QuickLinkExplore({
        queryName: OperationLogEntity,
        parentColumn: "Target",
        parentValue: ctx.lite
    }, { isVisible: getTypeInfo(ctx.lite.EntityType) && getTypeInfo(ctx.lite.EntityType).requiresSaveOperation }));
}

export const operationSettings: { [operationKey: string]: OperationSettings } = {};

export function addSettings(...settings: OperationSettings[]) {
    settings.forEach(s => Dic.addOrThrow(operationSettings, s.operationSymbol.key!, s));
}


export function getSettings(operation: OperationSymbol | string): OperationSettings | undefined {
    const operationKey = (operation as OperationSymbol).key || operation as string;

    return operationSettings[operationKey];
}

export const isOperationAllowedEvent: Array<(oi: OperationInfo | OperationSymbol | string) => boolean> = [];

export function isOperationAllowed(oi: OperationInfo | OperationSymbol | string) {
    return isOperationAllowedEvent.every(a => a(oi));
}

export function assertOperationAllowed(operation: OperationInfo | OperationSymbol | string) {
    var key = (operation as OperationInfo | OperationSymbol).key || operation as string;
    if (!isOperationAllowed(key))
        throw new Error(`Operation ${key} is denied`);
}


export function operationInfos(ti: TypeInfo) {
    return Dic.getValues(ti.operations!).filter(isOperationAllowed);
}

/**
 * Operation Settings
 */
export abstract class OperationSettings {
{'fraction_non_alphanumeric': 0.10954961056552659, 'fraction_numerical': 0.0, 'mean_word_length': 5.949411764705882, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6700623', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1542, 'n_words': 393}