Get User Roles,Duties,Privileges & there Permissions

Below is the code snippet to get the user Roles,Duties,Privileges & there Permissions

 


//To get the information of user, roles duties, Privilege  and access level
//NOTE: PLEASE UPDATE FILE NAME AND USER FILTER IN QUERY
static void Mzk_ExportUsersecurities_User(Args _args)
{
    SysExcelApplication         xlsApplication;
    SysExcelWorkBooks           xlsWorkBookCollection;
    SysExcelWorkBook            xlsWorkBook;
    SysExcelWorkSheets          xlsWorkSheetCollection;
    SysExcelWorkSheet           xlsWorkSheet;
    SysExcelRange               xlsRange;
    CustTable                   custTable;
    int                         row = 1;
    str                         fileName;

    SecurityTask                securityTask, securityTaskDuty, securityTaskPriv;
    SecuritySubTask             securitySubTask;
    SecurityRoleTaskGrant       securityRoleTaskGrant;
    SecurityRole                securityRole, securityRoleDuty,securityRolePriv, securityRoleM;
    SecurityTaskEntryPoint      SecurityTaskEntryPoint;
    SecurableObject             SecurableObject;
    SecurityUserRole            securityUserRole;
    UserInfo                    userInfo;

    SecurityTaskEntryPoint  taskEntryPoint;
    SecurityRole            role;
    SecurityRoleTaskGrant   taskGrant;
    SecuritySubTask         subTask;
    SecurityTask            privilege;

    str privName;
    str dutyName;
    str entrName;
    str accessLevel;

    boolean i = true,j = true;
    ;
    fileName = "UserName.CSV";    //Rename file
    xlsApplication           = SysExcelApplication::construct();
    xlsWorkBookCollection    = xlsApplication.workbooks();
    xlsWorkBook              = xlsWorkBookCollection.add();
    xlsWorkSheetCollection   = xlsWorkBook.worksheets();
    xlsWorkSheet             = xlsWorkSheetCollection.itemFromNum(1);
    xlsWorkSheet.cells().item(row,1).value("User Id");
    xlsWorkSheet.cells().item(row,2).value("User Role");
    xlsWorkSheet.cells().item(row,3).value("Duty");
    xlsWorkSheet.cells().item(row,4).value("Privilege AOT");
    xlsWorkSheet.cells().item(row,5).value("Privilege Description");
    xlsWorkSheet.cells().item(row,6).value("Entry Point");
    xlsWorkSheet.cells().item(row,7).value("Permission");

    row++;

    while select * from securityRole
            join securityUserRole
            where securityUserRole.User like "USERNAME"     //Enter user information here
            && securityUserRole.SecurityRole == securityRole.RecId
        {
        while select * from securityRoleTaskGrant
        join * from securityTask
            where securityRoleTaskGrant.SecurityRole == securityRole.RecId
                && securityTask.RecId == securityRoleTaskGrant.SecurityTask

        {
            if(securityTask.Type == SecurityTaskType::Duty)
            {
               while select SecurityTaskEntryPoint
                            join SecurableObject where SecurableObject.RecId == SecurityTaskEntryPoint.EntryPoint
                            join subTask where subTask.SecuritySubTask == SecurityTaskEntryPoint.SecurityTask
                            join taskGrant where taskGrant.SecurityTask == subTask.SecurityTask
                            join role where role.RecId == taskGrant.SecurityRole
                                &&  role.AotName like securityRole.AotName

                {
                    select privilege
                        where privilege.RecId == taskGrant.SecurityTask
                        && SecurityTaskType::Duty == privilege.Type;
                            dutyName = privilege.AotName;

                    select privilege
                        where privilege.RecId == subTask.SecuritySubTask
                        && SecurityTaskType::Privilege == privilege.Type;
                        privName = privilege.AotName;
                    {
                       //Information to Excel
                        xlsWorkSheet.cells().item(row,1).value(securityUserRole.User);         //User id
                        xlsWorkSheet.cells().item(row,2).value(securityRole.Name);  //Role Name
                        xlsWorkSheet.cells().item(row,3).value(dutyName); //DutyName
                        xlsWorkSheet.cells().item(row,4).value(privName);  //Privilege AOT Name
                        xlsWorkSheet.cells().item(row,5).value(privilege.Name);  //Privilege Description
                        xlsWorkSheet.cells().item(row,6).value(SecurableObject.Name);  //Entry Points
                        xlsWorkSheet.cells().item(row,7).value(enum2str(SecurityTaskEntryPoint.PermissionGroup));  //Access Permission level
                        row++;
                    }
                }
            }
            else             //Role if contain Privilegs directly
            {
                while select * from SecurityTaskEntryPoint
                            join SecurableObject
                            where SecurableObject.RecId == SecurityTaskEntryPoint.EntryPoint
                            && SecurityTaskEntryPoint.SecurityTask == securityTask.RecId
                {
                    xlsWorkSheet.cells().item(row,1).value(securityUserRole.User);         //User id
                    xlsWorkSheet.cells().item(row,2).value(securityRole.Name);  //Role Name
                    xlsWorkSheet.cells().item(row,3).value(""); //DutyName
                    xlsWorkSheet.cells().item(row,4).value(securityTask.AotName);  //Privilege AOT Name
                    xlsWorkSheet.cells().item(row,5).value(securityTask.Name);  //Privilege Description
                    xlsWorkSheet.cells().item(row,6).value(SecurableObject.Name);  //Entry Points
                    xlsWorkSheet.cells().item(row,7).value(enum2str(SecurityTaskEntryPoint.PermissionGroup));  //Access Permission level
                    row++;
                }
            }
        }
    }
    if(WinApi::fileExists(fileName))
        WinApi::deleteFile(fileName);
    xlsWorkbook.saveAs(fileName);
    xlsApplication.visible(true);
    info(strFmt("File has been exported"));
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s