NAME

RestTools Module with shared, general utility/tool methods for the AURORA REST-server

SYNOPSIS

use RestTools;

# merge metadata with same keys from several entity sources
my $md=mergeMetadata($db,"DATASET",0,0,@my_entities);

# check if a subject has given permissions on an entity object (all permissions, on any level).
# we ask for levels: effective, inherit and grant.
my $ok=hasPerm($db,$subject,$object,["DATASET_CREATE","DATASET_DELETE"],"ALL","ANY",1,1,undef,1);

# get invalid textual permissions in list
my $invalid=getInvalidPermArrayElements($db,["DATASET_BLIPP","DATASET_CREATE"]);

# deconstruct a bitmask into its textual permission names and deliver as an array
my $perms=permToArray($db,$mask)

# create a bitmask based on a list-reference of permission names
my $mask=arrayToPerm($db,$perms);

# get invalid textual flag names returned as a list-reference
my $invalid=getInvalidFlagArrayElements($db,["OMIT","BLIPPBLAPP","MANDATORI"]);

# convert flag bitmask into its textual flag names and deliver as an array
my $flags=flagsToArray($db,$flagmask);

# convert textual flag names into a flag bitmask
my $flagmask=arrayToFlags($db,["OMIT","MANDATORY"]);

# extract and clean parameters from query hash
my ($id, $token, $expire) = _get_params( $query, 
     id     => [$Schema::CLEAN{entity}],          
     token  => [$SysSchema::CLEAN{token}], 
     expire => [$Schema::CLEAN_GLOBAL{trueint}],
     );

# return an error message 
my $result=_with_error($content, "User does not have the $required permission on the dataset $id. Unable to fulfill request.")

# return a success message
my $return=_with_success($content);

# get closest task of an entity
my $task=getEntityTask($db,$entity);

DESCRIPTION

Collection of general utility/tool functions used by the REST-server. All functions are exported and can be used directly in the namespace of the code that uses it.

METHODS

_get_params()

Extract and clean parameters from query hash

Input parameters:

Returns a LIST of cleaned parameters in return in the same order as they were delivered to the function.

It is useful to assign the LIST to single parameters right away, like so:

my ($my_param1,$my_param2) = _get_param($query,my_param1=>[Schema::CLEAN{entity}],my_param2=>[Schema::CLEAN{entitytype}]);

_with_error()

Returns a correctly formatted error message for the AURORA REST-server.

Accepts input in the following order: content,reason. Content is a HASH-reference to the hash that will contain the error-message and its values. Reason is the textual reason given for the error that just happened.

Always returns 0

_with_success()

Returns a correctly formatted success message for the AURORA REST-server.

Accepts input in the following order: content. Content is the HASH-reference to the hash that will contain the success-message and its values.

Always returns 1

arrayToFlags()

Converts a textual array of flag names into its bitmask

Accepts the following input in this order: db, array. Db is the reference to the AuroraDB-instance used by the REST-server. Array is a LIST-reference to the textual flag names that are to be converted into a bitmask (as if all the flag names are set in the bitmask).

Returns a bitmask.

arrayToPerm()

Convert a textual array of permission names into its bitmask.

Accepts the following input in this order: db, array. Db is the reference to the AuroraDB-instance used by the REST-server. Array is the LIST-reference to the textual permission names that are to be converted into a bitmask (as if all the permission names are set in the bitmask).

Returns a bitmask.

flagsToArray()

Convert flag bitmask into its textual flag names

Accepts the following input in this order: db, bitmask. Db is the reference to the AuroraDB-instance used by the REST-server. Bitmask is the bitmask of flags that are to be converted to its textual counterpart.

Returns a LIST-reference of the textual flags names that have been set in the bitmask.

getEntityTask()

Get closest task on an entity in the entity tree.

This method accepts this input in the following order: db, id. Db is the reference to the AuroraDB-instance used by the REST-server. Id is the entity ID from the AURORA database that one wants to get the task of (if any defined).

This method will search from the entity ID specified in id and up the tree to find the closest matching task. If several tasks are defined in the same place in the entity tree, it will select the one that comes first alphabetically on display name of the task entity.

Returns the task ID or 0 if not anyone found or something failed.

getInvalidFlagArrayElements()

Returns the textual flag names in the array that are not valid names for flags.

Accepts the following input in this order: db, array. Db is the reference to the AuroraDB-instance used by the REST-server. Array is the LIST-reference to the list of textual flag names to check for possible invalid items.

Returns a LIST-reference to the flag names that were invalid (if any). If none were invalid, the returned array is empty.

getInvalidPermArrayElements()

Returns the textual permission names in the array that are not valid names for permissions.

Accepts the following input in this order: db, array. Db is the reference to the AuroraDB-instance used by the REST-server. Array is the LIST-reference to the list of textual permission names to check for possible invalid items.

Returns a LIST-reference to the permission names that were invalid (if any). If none were invalid, the returned array is empty.

hasPerm()

Checks if a subject has the specified permission(s) on an object.

Accepts the following input in this order: db, subject, object, perms, pop, oop, effective, inherit, deny, grant.

The meaning of these inputs are as follows:

Will return an expression that either evaluates to true or false. In the case of false it will be either undef or 0. In the case of true it will be a value >= 1. If the expression returned is false, the subject does not fulfill the given permission(s) on the object. If true is returned, the subject has the given permission(s) on the object (and on the levels enabled with the stated logical operators).

listFolders()

List a folder and its subfolders if so specified and return the result in a hash.

Accepts input in the following order: path, recursive, md5sum, tag.

The meaning of these input options are as follows:

This method will return a HASH-reference upon success. Undef upon some failure.

The structure of the HASH-reference is as follows:

%result = (
   "FolderA" => { 
      "." => { 
         name => "FolderA",
         type => "D",
         size => SCALAR,
         atime => SCALAR,
         mtime => SCALAR,
             },
      "FolderB" => { ... }
      "FileX" => { ... }
   "FileA" => {
      "." => {
         name => "FileA",
         type => "F",
         size => SCALAR,
         atime => SCALAR,
         mtime => SCALAR,
         ctime => SCALAR,
         md5 => SCALAR,
             },
   .
   .
   "FileZ" > { ... }
)

Information on the folder entry itself is stored in the dot-folder ".". This is true for both files and folders and is done for consistency between files and folders. Any entry within a folder is stored inside that folders sub-hash with a entry equal to its name. The "recursive" parameters governs if method recurses into sub-folders or not. Also, the md5-sum of a file might not be present if the md5sum parameters is set to false/undef/0. Errors performing md5-sum will also be saved in the md5-attribute on the sub-hash. It might also be undef, such as when no md5-summing has been requested.

Error-messages on the md5 attribute will have the form: "N/A: Some Error Message", such as when failing to open or sum the file in question.

mergeMetadata()

Merge metadata with same keys from several entity sources

Accepts this input in the following order: db, entitytype, notemplate, noentity, entities.

The meaning of these input options are as follows:

This method will merge metadata from the enabled sources (template and/or entity itself) and from all the entities in the entities-option into one HASH with key->value entries. In other words it merges metadata from both templates and entities into one hash as if the result is for one entity. The merging will take precedence in the order of the entities-option for keys with the same name, where first entity specified is antecedent to the next and so on and the last entity in the list takes precedence over all the previous.

Returns a HASH-reference with all the merged data in a key=>value structure.

permToArray()

Convert permission bitmask into its textual permission names

Accepts the following input in this order: db, bitmask. Db is the reference to the AuroraDB-instance used by the REST-server. Bitmask is the bitmask of permission that are to be converted to its textual counterpart.

Returns a LIST-reference of the textual permission names that have been set in the bitmask.

recurseListing()

Recurse a listing from the listFolders()-method and produce a iterable array with textual entries.

Accepts these parameters in the following order: listing, newlist, level, path.

The meaning of these parameters are as follows:

Returns an array list reference in the newlist-option as explained above.