project BLF > class Business Component > method apiActivityIsAllowed

Description

check if user has security rights for an activity


Parameters


icUsrLogininputcharacterlogin name
icCompanyCodeinputcharacterEntity Code
icActivityCodeinputcharacterActivity Code
olIsAllowedoutputlogicalhas access
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


BLF
method database.ValidateActivityCode

QadFinancials
method BCompany.ValidateComponentCheckSecurity
method BDaemonFunctions.GetDaemonStatus


program code (program3/business.p)

if icActivityCode = ""
or icActivityCode = ?
then return.

if oiReturnStatus = 0
then oiReturnStatus = -98.

if icUsrLogin   = ""
or icUsrLogin   = ?
then icUsrLogin = vcUserLogin.

if icUsrLogin = vcUserLogin
then viUserId = viUsrId.
else do:
    <Q-16 run UserPrim (all) (Read) (NoCache)
       (input ?, (UsrId)
        input icUsrLogin, (UsrLogin)
        output dataset tqUserPrim) in BUser >
    find first tqUserPrim where 
               tqUserPrim.tcUsrLogin = icUsrLogin no-error.
    if available tqUserPrim
    then assign viUserId = tqUserPrim.tiUsr_ID.
    else do: 
        assign oiReturnStatus = 0.
        return.
    end.
end.

if icCompanyCode  = ""
or icCompanyCode  = ?
then icCompanyCode = vcCompanyCode.

if icCompanyCode = vcCompanyCode
then Assign viCyID = viCompanyId.
else do:
    <Q-15 run CompanyPrim (all) (Read) (NoCache)
       (input ?, (LookupCompanyId)
        input icCompanyCode, (CompanyCode)
        output dataset tqCompanyPrim) in BCompany >

    find first tqCompanyPrim no-error.
    if available tqCompanyPrim
    then Assign viCyID = tqCompanyPrim.tiCompany_ID.
    else return.
end.

<Q-17 run BusComponentForResources (all) (Read) (NoCache)
   (input vcFcComponentName, (BusComponentCode)
    output dataset tqBusComponentForResources) in BBusinessComponent >
find first tqBusComponentForResources where
           tqBusComponentForResources.tcBusComponentCode = vcFcComponentName and
           tqBusComponentForResources.tcBusActivityCode  = icActivityCode
           no-error.
if not available tqBusComponentForResources
then do: 
     /*activity does not exist*/
     assign olIsAllowed = false.
end.
else do:
    if tqBusComponentForResources.tiResource_ID = 0 or tqBusComponentForResources.tiResource_ID = ?
    then do: 
         /*activity does not exist*/
         assign olIsAllowed = false.
    end.
    else do:
        /*look if the user company combination has access to this resource*/
        <Q-18 run RoleResourceByResUserComp (all) (Read) (NoCache)
           (input tqBusComponentForResources.tiResource_ID, (ResourceId)
            input viCyID, (CompanyId)
            input viUserId, (UserId)
            output dataset tqRoleResourceByResUserComp) in BRole >
        find first tqRoleResourceByResUserComp no-error.
        if available tqRoleResourceByResUserComp
        then do:
            /*allowed*/
            assign olIsAllowed = true
                   oiReturnStatus = 0.
            return.
        end.
        else do:
            /*not allowed*/
            assign olIsAllowed = false.
        end.
    end.
end.

/* check if there is any activity in the db */
/* When I start with a totaly empty database (and thus no activities at all), then the validation in 
 * method Database.ApiActivityIsAllowed? (called from ValidateComponent?) produces an error as it cannot find 
 * the lik between the current-user/current-activity/class. 
 * If this find failes (which is normal in an empty db), then it should first check whether there 
 * is any activity at all, and not go into error-mode when there are no activities. 
 */
<Q-13 assign vlFcQueryRecordsAvailable = BusActivityPrim (NoCache)
   (input 0, (BusActivityID)
    input '', (BusActivityCode)
    input '', (BusComponentCode)) in BBusinessComponent >
If vlFcQueryRecordsAvailable = False
Then Do:
    Assign olIsAllowed = yes
           oiReturnStatus = 0. 
    Return.
End. /*If vlFcQueryRecordsAvailable = False*/

if oiReturnStatus = -98
then oiReturnStatus = 0.


Sample code: how to call this method through RPCRequestService (QXtend Inbound)

define temp-table ttContext no-undo
    field propertyQualifier as character
    field propertyName as character
    field propertyValue as character
    index entityContext is primary unique
        propertyQualifier
        propertyName
    index propertyQualifier
        propertyQualifier.

define dataset dsContext for ttContext.

define variable vhContextDS as handle no-undo.
define variable vhExceptionDS as handle no-undo.
define variable vhServer as handle no-undo.
define variable vhInputDS as handle no-undo.
define variable vhInputOutputDS as handle no-undo.
define variable vhOutputDS as handle no-undo.
define variable vhParameter as handle no-undo.

/* Create context */
create ttContext.
assign ttContext.propertyName = "programName"
       ttContext.propertyValue = "business".
create ttContext.
assign ttContext.propertyName = "methodName"
       ttContext.propertyValue = "apiActivityIsAllowed".
create ttContext.
assign ttContext.propertyName = "applicationId"
       ttContext.propertyValue = "fin".
create ttContext.
assign ttContext.propertyName = "entity"
       ttContext.propertyValue = "1000".
create ttContext.
assign ttContext.propertyName = "userName"
       ttContext.propertyValue = "mfg".
create ttContext.
assign ttContext.propertyName = "password"
       ttContext.propertyValue = "".

/* Create input dataset */
create dataset vhInputDS.
vhInputDS:read-xmlschema("file", "xml/business.apiactivityisallowed.i.xsd", ?).
vhParameter = vhInputDS:get-buffer-handle("tParameterI").
vhParameter:buffer-create().
assign vhParameter::icUsrLogin = <parameter value>
       vhParameter::icCompanyCode = <parameter value>
       vhParameter::icActivityCode = <parameter value>.

/* Connect the AppServer */
create server vhServer.
vhServer:connect("-URL <appserver-url>").

if not vhServer:connected()
then do:
    message "Could not connect AppServer" view-as alert-box error title "Error".
    return.
end.

/* Run */
assign vhContextDS = dataset dsContext:handle.

run program/rpcrequestservice.p on vhServer
    (input-output dataset-handle vhContextDS by-reference,
           output dataset-handle vhExceptionDS,
     input        dataset-handle vhInputDS by-reference,
     input-output dataset-handle vhInputOutputDS by-reference,
           output dataset-handle vhOutputDS).

/* Handle output however you want, in this example, we dump it to xml */
if valid-handle(vhExceptionDS)
then vhExceptionDS:write-xml("file", "Exceptions.xml", true).

if valid-handle(vhOutputDS)
then vhOutputDS:write-xml("file", "Output.xml", true).

/* Cleanup */
vhServer:disconnect().
assign vhServer = ?.

if valid-handle(vhInputDS)
then delete object vhInputDS.

if valid-handle(vhOutputDS)
then delete object vhOutputDS.

if valid-handle(vhExceptionDS)
then delete object vhExceptionDS.