当前位置: 首页 > 实施, 开发 > 正文

用X++快速设置Record-level security (RLS

//Created on 24 May 2016 by LuoHu
static void PA_quickSetupRLS(Args _args)
{
    #define.SysAdmin("-SYSADMIN-")
    #define.RoleName("PA_P2_SalesManagerBJ")
    #define.KeyId("Sales team BJ")

    Map                         targetMap;
    MapEnumerator               mapEnumerator;

    TableId                     currentTable;
    FieldId                     currentField;

    str                         combiValue;

    SecurityRole                securityRole;

    SysRecordLevelSecurity      sysRecordLevelSecurity;
    
    Query                       query;
    SysQueryRun                 queryRun;
    container                   packedQuery;
    
    targetMap = new Map(Types::Int64, Types::Int64); //[Table num, Field num]

    //targetMap.insert(tableNum(smmSalesUnit), fieldNum(smmSalesUnit, SalesUnitId));
    targetMap.insert(tableNum(smmBusRelTable), fieldNum(smmBusRelTable, PA_SalesUnitId));
    targetMap.insert(tableNum(ContactPerson), fieldNum(ContactPerson, PA_SalesUnitId));
    targetMap.insert(tableNum(smmOpportunityTable), fieldNum(smmOpportunityTable, SalesUnitId));
    targetMap.insert(tableNum(smmActivities), fieldNum(smmActivities, PA_SalesUnitId));
    targetMap.insert(tableNum(SalesQuotationTable), fieldNum(SalesQuotationTable, SalesUnitId));
    targetMap.insert(tableNum(SalesTable), fieldNum(SalesTable, SalesUnitId));

    mapEnumerator = targetMap.getEnumerator();
    
    try
    {
        ttsBegin;
        while select securityRole
            where securityRole.AotName == #RoleName 
               && securityRole.AotName != #SysAdmin
        {
            combiValue = #KeyId + "," + SysQuery::valueEmptyString();

            mapEnumerator.reset();
            while(mapEnumerator.moveNext())
            {
                currentTable = mapEnumerator.currentKey();
                currentField = mapEnumerator.currentValue();

                query = new query();
                query.addDataSource(currentTable);
                SysQuery::findOrCreateRange(query.dataSourceTable(currentTable), currentField).value(combiValue);
                
                queryRun = new SysQueryRun(query);
                packedQuery = queryRun.pack();
                //find
                select firstOnly sysRecordLevelSecurity
                    where sysRecordLevelSecurity.SecurityRole == securityRole.RecId
                    && sysRecordLevelSecurity.tabId == currentTable
                    && sysRecordLevelSecurity.companyId == curext();

                if(sysRecordLevelSecurity)
                {
                    sysRecordLevelSecurity.selectForUpdate(true);
                    sysRecordLevelSecurity.restriction = packedQuery;
                    sysRecordLevelSecurity.doUpdate();
                }
                else
                {
                    sysRecordLevelSecurity.SecurityRole = securityRole.RecId;
                    sysRecordLevelSecurity.tabId = currentTable;
                    sysRecordLevelSecurity.companyId = curext();
                    sysRecordLevelSecurity.restriction = packedQuery;
                    sysRecordLevelSecurity.doInsert();
                }
            }
        }
        ttsCommit;
        info("@SYS153513");
    }
    catch(Exception::Error)
    {
        ttsAbort;

    }
}

本文固定链接: http://www.erpgolive.com/archives/246 | 我心依旧

该日志由 虎哥 于2016年07月31日发表在 实施, 开发 分类下,
原创文章转载请注明: 用X++快速设置Record-level security (RLS | 我心依旧

报歉!评论已关闭.