SCA_GetPrivilgesByControlName - Song Nghia - Microsoft Dynamics Partner

Song Nghia - Microsoft Dynamics Partner

Song Nghia - Microsoft Dynamics Partner

Breaking

Thursday, January 2, 2020

SCA_GetPrivilgesByControlName


class SCA_GetPrivilgesByControlName extends RunBaseBatch
{
    str                 currentFile;
    container           lineCon;
    DialogField         fieldControlName, fieldFormName;
    str                 controlName, formName;

    #AviFiles
    #define.CurrentVersion(1)
    #localmacro.CurrentList
        controlName,
        formName
    #endmacro

    Public static void main(Args _args)
    {
        SCA_GetPrivilgesByControlName    getPrivilges = SCA_GetPrivilgesByControlName::construct();

        ;

        if (getPrivilges.prompt())
        {
            getPrivilges.run();
        }
    }

    public void run()
    {
        Form                myForm;
        FormBuildDesign     design;
        FormBuildControl    control;
        int                 cnt = 0;
        Set                 buttonsSet = new Set(Types::Class);
        Args    args;
        Object              _Object;
        FormControl         _FormControl;
        FormMenuButtonControl _buttonControl;
        FormBuildFunctionButtonControl  _menuButton;
        Name    privilegeName, grantType, grantAllow;
        void findButtons(FormBuildControl _buildControl)
        {
            FormBuildControl    buildControl;
            int                 i;
            ;

            if (_buildControl.controlCount() > 0)
            {
                for (i = 1; i <= _buildControl.controlCount(); i++)
                {
                    try
                    {
                        _menuButton = _buildControl.controlNum(i);
                        if(_menuButton.name() == controlName)
                        {
                            [privilegeName, grantType,  grantAllow] =  this.findPrivileges(_menuButton.menuItemName());
                            if(privilegeName && grantType && grantAllow)
                            {
                                info(strFmt("Menu item name: %1, privilege name: %2, grant type: %3, grant allow: %4", _menuButton.menuItemName(), privilegeName, grantType, grantAllow ));
                            }

                            break;
                        }
                    }
                    catch
                    {
                    }
                    buildControl = _buildControl.controlNum(i);

                    findButtons(buildControl);
                }
            }
            else
            {
                if (classidget(_buildControl) == classnum(FormBuildButtonControl) ||
                    classidget(_buildControl) == classnum(FormBuildCommandButtonControl) ||
                    classidget(_buildControl) == classnum(FormBuildDropDialogButtonControl) ||
                    classidget(_buildControl) == classnum(FormBuildMenuButtonControl) ||
                    classidget(_buildControl) == classnum(FormBuildFunctionButtonControl))
                {
                    buttonsSet.add(_buildControl);
                }
            }
        }
        ;

        myForm = new Form(formName);
        design = myForm.design();

        for (cnt = 1; cnt <= design.controlCount(); cnt++)
        {
            control = design.controlNum(cnt);
            findButtons(control);
        }

    }

    private container findPrivileges(Name _objectName)
    {
        System.IO.StreamReader reader;
        System.String stringLine;
        InteropPermission interopPermission;

        str file;
        int row = 1;
        int currentFileCount;
        str stringFile;

        container   conResult;
        Name        privilegesName, grantName, grantAllow;

        System.String[] filePaths = System.IO.Directory::GetFiles(@"C:\AOSService\PackagesLocalDirectory\", "*.xml*",System.IO.SearchOption::AllDirectories);

        int fileCount = filepaths.get_Length();

        for(currentFileCount = 0; currentFileCount < fileCount ; ++currentFileCount)
        {
            file = filepaths.GetValue(currentFileCount).ToString();
            if(strScan(file, "AxSecurityPrivilege", 0, strLen(file)) || strScan(file, "axsecurityprivilege", 0, strLen(file)) )
            {
                reader = new System.IO.StreamReader(file);
                stringLine = reader.ReadLine();
                stringFile = "";
                while (!System.String::IsNullOrEmpty(stringLine))
                {
                    stringFile += stringLine;
                    stringLine = reader.ReadLine();
                }
                //Get privilegs of meuitem display
                conResult = this.getXML(stringFile, _objectName);
             
                //get all source in fom dc goi toi

                if(conPeek(conResult, 1))
                {
                    return conResult;
                }
                reader.Close();
                reader.Dispose();
                CodeAccessPermission::revertAssert();
            }
        }

        return conResult;
    }

    /// <summary>
    /// Find privilege from object Type and object name
    /// </summary>
    /// <param name = "strXML"></param>
    /// <returns></returns>
    private container getXML(str strXML, str objectName)
    {
        // Define XML Document and its nodes
        XmlDocument     doc;
        XmlNodeList     vtvNodeList;
        XmlNode         vtvNode;
        Name            privilegesName;
        str             grantAllow;
        //Nguyen doan script
        XmlElement      elementScript,
                        elementName,
                        elementEntryPoints,
                        elementEntryPointsRef,
                        elementRefGrant, elementGrantDetail;
        XmlElement      elementTemp;
        XMLParseError   xmlError;
        int             i ;

        try
        {
            doc = new XmlDocument();
            doc.loadXml(strXML);
            elementScript           = doc.getNamedElement("AxSecurityPrivilege");
            elementName             = elementScript.getNamedElement("Name");
            elementEntryPoints      = elementScript.getNamedElement("EntryPoints");
            vtvNodeList             = elementEntryPoints.childNodes();

            for(i = 0; i < vtvNodeList.length() ; i++ )
            {
                //Doan nay phia loop data ben trong, moi get het duoc
                //elementEntryPointsRef   = elementEntryPoints.getNamedElement//("AxSecurityEntryPointReference");
                elementEntryPointsRef      = vtvNodeList.item(i);
                elementRefGrant       = elementEntryPointsRef.getNamedElement("Grant");
     
                //Get quyen cao nhat cua privileges
                if(elementRefGrant.getNamedElement("Delete") && elementRefGrant.getNamedElement("Delete").text() == "Allow")
                {
                    if(elementEntryPointsRef.getNamedElement("ObjectName").text() == objectName)
                    {
                        return [elementName.text() ,
                            elementRefGrant.getNamedElement("Delete").name(),
                            elementRefGrant.getNamedElement("Delete").text()];
                    }
                }
                //else get read
                else
                {
                    if(elementRefGrant.getNamedElement("Read") && elementRefGrant.getNamedElement("Read").text() == "Allow")
                    {
                        if(elementEntryPointsRef.getNamedElement("ObjectName").text() == objectName)
                        {
                            return [elementName.text() ,
                            elementRefGrant.getNamedElement("Read").name(),
                            elementRefGrant.getNamedElement("Read").text()];
                        }
                    }
                }
            }
         
        }
        catch
        {
            return conNull();
        }

        return conNull();
    }

    Public static SCA_GetPrivilgesByControlName construct()
    {
        return new SCA_GetPrivilgesByControlName();
    }

    public boolean getFromDialog()
    {
        // Retrieve values from Dialog
        controlName = fieldControlName.value();
        formName    = fieldFormName.value();

        return super();
    }

    public Object dialog()
    {
        Dialog dialog;
        ;

        dialog = super();

        // Set a title for dialog
        dialog.caption( 'get privileges');

        // Add a new field to Dialog
        fieldControlName = dialog.addField( extendedTypeStr(Str1260), 'Control Name' );
        fieldFormName    =  dialog.addField( extendedTypeStr(Str1260), 'Form Name' );
        return dialog;
    }

    public container pack()
    {
        return [#CurrentVersion, #CurrentList];
    }

    public boolean unpack(container _packedClass)
    {
        Version    version = RunBase::getVersion(_packedClass);
        switch (version)
        {
            case #CurrentVersion:
                [version,#CurrentList] = _packedClass;
                break;
            default:
                return false;
        }
        return true;
    }

}