Label-incoming-studies.lua

Hello,
I have an installation using the Orthanc Team and Authorization Service repositories. I’ve set up a Lua script “label-incoming-studies.lua” to add a label to each exam originating from a remote AeTitle. The script runs correctly, but I’m getting an error in the logs:
Access to a resource is forbidden: Auth plugin: no user profile or UserData found, unable to delete/put label with audit logs enabled

function makeValidLabel(input)
    local invalidCharacters = "[^%w_-]"   -- everything that is not alphanumeric or _ or -
    return input:gsub(invalidCharacters, "-")
end
  
function OnStoredInstance(instanceId, tags, metadata)
    print("OnStoredInstance ...")
    PrintRecursive(metadata)
  
    local parentStudy = ParseJson(RestApiGet("/instances/" .. instanceId .. "/study"))

    if #parentStudy.Labels == 0 and metadata.RemoteAET ~= nil and metadata.RemoteAET ~= '' then
      
        print("OnStoredInstance adding labels to parent study")
        -- RestApiPut("/studies/" .. parentStudy.ID .. "/labels/called-" .. makeValidLabel(metadata.CalledAET), '')
        -- RestApiPut("/studies/" .. parentStudy.ID .. "/labels/ip-" .. makeValidLabel(metadata.RemoteIP), '')
        RestApiPut("/studies/" .. parentStudy.ID .. "/labels/remote-" .. makeValidLabel(metadata.RemoteAET), '')
    end
  
    print("OnStoredInstance ... done")
  
end

thank you for your help

Hi,

setting the builtin parameter to true should bypass the auth-plugin and address directly the core Orthanc API:

Hope this helps,

Alain

Thanks Alain, it works now!!

I’m sharing the code with an aetitle mapping and a manual import;

All that’s missing are the different label colors depending on the case, and it will be perfect.

--
-- 
--
function makeValidLabel(input)
    local invalidCharacters = "[^%w_-]"
    return input:gsub(invalidCharacters, "-")
end

--
-- Mapping AET -> libellé métier
--
local aetToFriendlyName = {
    ["****"]   = "Push-from-",
    ["****"]   = "Push-from-",
    ["****"]   = "Push-from-",
    ["****"]   = "Push-from-",
    ["****"]   = "Push-from-",
    ["****"]   = "Push-from-"
}

local MANUAL_IMPORT_LABEL = "Import-manuel"

function getFriendlyName(aet)
    return aetToFriendlyName[aet] or aet
end

--
-- 
-- Absence de RemoteAET = import manuel (WebUI ou REST)
--
function isManualImport(metadata)
    return metadata.RemoteAET == nil or metadata.RemoteAET == ""
end

--
-- Hook Orthanc
--
function OnStoredInstance(instanceId, tags, metadata)
    print("OnStoredInstance ...")

    -- API REST interne (pas d’audit-log)
    local parentStudy = ParseJson(
        RestApiGet("/instances/" .. instanceId .. "/study", true)
    )

    -- un seul label par étude
    if parentStudy.Labels ~= nil and #parentStudy.Labels == 0 then

        local label

        if isManualImport(metadata) then
            label = MANUAL_IMPORT_LABEL
            print("Import manuel détecté")
        else
            label = getFriendlyName(metadata.RemoteAET)
            print("Push DICOM détecté depuis " .. metadata.RemoteAET)
        end

        -- Application du label
        RestApiPut(
            "/studies/" .. parentStudy.ID .. "/labels/" .. makeValidLabel(label),
            "",
            true
        )
    end

    print("OnStoredInstance ... done")
end