cancel
Showing results for 
Search instead for 
Did you mean: 

[SOLVED] Rule 'execute script' and search.luceneSearch gives SecurityContextError

technaton
Champ in-the-making
Champ in-the-making
Hello,

I'm trying to use Alfresco's search in a script executed from the "Execute Script" rules action. However, the result of
search.luceneSearch
is the following string: "A valid SecureContext was not provided in the RequestContext".

Here are the relevant lines of the script:

<javascript>var courseNumber = document.properties.name.match(/^SPO_(\d{4,4})_/)[1];
var parentFolders = search.luceneSearch(
        "title:" + courseNumber + " -* " +
            "AND TYPE:folder");</javascript>

Is there a separate login/get ticket functionality I need to use?

Thanks a lot in advance for any answers.

— Eric
4 REPLIES 4

afaust
Legendary Innovator
Legendary Innovator
Hello,

what version of Alfresco are you using? I would not expect any such error on the simple use case of "Execute Script" with "just" a simple search. But you should probably fix up your Lucene query, i.e. the '-*' bit is unusual, a propery field query should be '@cm\:title:"<value>"' and 'TYPE:folder' should rather be 'TYPE:"cm:folder"'.

Regards
Axel

technaton
Champ in-the-making
Champ in-the-making
Hello Axel,

thanks for your answer.

I'm using Alfresco Community 4.2.c in a vanilla installation on my development machine. No external database is attached; no modifications have been made. I also just re-installed Alfresco, just to make sure. No configs were touched.

I modified the source code as you suggested. On my developer machine, the script has only the following code:

<javascript>var courseNumber = document.properties.name.match(/^SPO_(\d{4,4})_/)[1];
var guest = guest;
var parentFolders = search.luceneSearch(
        "@cm\\:title:\"" + courseNumber + " -* \" " +
            "AND TYPE:\"cm:folder\"");

var oldFiles = search.luceneSearch(
        "PARENT:" + parentFolders[0].properties.nodeRef + " " +
            "AND @cm\\:name:\"SPO_" + courseNumber + "_*\"");

for (var i = 0; i != oldFiles.length; ++i) {
    oldFiles.remove();
}</javascript>

However, the javascript debugger still says that "parentFolders" holds the mentioned error string, i.e. "A valid SecureContext was not provided in the RequestContext".

I noticed that the "companyHome" root object also has the same text, regardless of who the script runs, i.e., even the admin user gets this error message.

I'm absolutely clueless here. Is there any way I need to "log in" separately for the script to run?

TIA!

— Eric

technaton
Champ in-the-making
Champ in-the-making
I just tried 4.2.b community edition to see if there's a bug in 4.2.c, but as expected, the script still fails.

I'm totally lost here, but willing to debug. I just need a hint on where to start.

TIA!

technaton
Champ in-the-making
Champ in-the-making
After some debugging via logging to files, I finally found the error — it was the search term just as Axel suggested. The final code is:

<javascript>function moveSPO(document, search)
{
    var nameMatch = document.properties.name.match(/^SPO_(\d{4,4})_/);
    if (null == nameMatch) {
        return;
    }

    var courseNumber = nameMatch[1];

    var parentFolders = search.luceneSearch(
            "@cm\\:title:\"" + courseNumber + " -*\" " +
                "AND TYPE:\"folder\"");
    parentFolders.forEach(function(o, i) {
        var oldFiles = search.luceneSearch(
                "PARENT:\"" + parentFolders[0].nodeRef + "\" " +
                    "AND @cm\\:name:\"SPO_" + courseNumber + "_*\"");
        oldFiles.forEach(function(o, i) {
            if (document.nodeRef !== o.nodeRef) {
                o.remove();
            }
        });
    });
}


moveSPO(document, search);</javascript>

I still do not know why the debugger show the "SecureContext" error, but it has nothing to do with the actual result of the search, which is correct.