Dynamics 365 JS中如何查詢使用者對實體是否有某個操作許可權

vic0228發表於2018-10-31

   最近碰到幾個人問我,怎麼在js裡使用web api查詢當前使用者是否對某個實體有某某許可權,問我有沒有寫過類似的部落格,找了下還真沒有,那就補上。

   直接上程式碼,主要用到了幾張表的聯合,關注程式碼中的fetchxml即可,兩個condition分別是當前使用者id和你要查詢的這個使用者的某個許可權,這個許可權的value值可以去資料庫的privilege這張表查


var userid = Xrm.Page.context.getUserId();
    var quoteFetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>\
    <entity name='systemuser'>\
      <attribute name='systemuserid' />\
      <link-entity name='systemuserroles' alias='a' from='systemuserid' to='systemuserid'>\
        <link-entity name='role' alias='b' from='roleid' to='roleid'>\
          <link-entity name='roleprivileges' alias='c' from='roleid' to='roleid'>\
            <link-entity name='privilege' alias='d' from='privilegeid' to='privilegeid'>\
              <filter type='and'>\
                <condition attribute='name' operator='eq' value='prvReadLead'/>\
              </filter>\
            </link-entity>\
          </link-entity>\
        </link-entity>\
      </link-entity>\
      <filter type='and'>\
        <condition attribute='systemuserid' operator='eq' value='"  
        + userid + "'/>\
      </filter>\
    </entity>\
  </fetch>";
    var encodedFetchXML = encodeURIComponent(quoteFetchXML);
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/systemusers?fetchXml=" + encodedFetchXML, false);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");
    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var results = JSON.parse(this.response);
            }
        }
    };
    req.send();

   上面程式碼中的fetchxml只是使用者自身的許可權查詢,還有一個所屬團隊的許可權查詢,fetchxml如下

<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
  <entity name='systemuser'>
    <attribute name='systemuserid' />
    <link-entity name='teammembership' alias='a' from='systemuserid' to='systemuserid'>
      <link-entity name='team' alias='e' from='teamid' to='teamid'>
        <link-entity name='teamRoles' alias='f' from='teamid' to='teamid'>
          <link-entity name='role' alias='b' from='roleid' to='roleid'>
            <link-entity name='roleprivileges' alias='c' from='roleid' to='roleid'>
              <link-entity name='privilege' alias='d' from='privilegeid' to='privilegeid'>
                <filter type='and'>
                  <condition attribute='name' operator='eq' value='prvReadLead'/>
                </filter>
              </link-entity>
            </link-entity>
          </link-entity>
        </link-entity>
      </link-entity>
    </link-entity>
    <filter type='and'>
      <condition attribute='systemuserid' operator='eq' value='FCCBB09D-D559-E811-80D4-0017FA01322A'/>
    </filter>
  </entity>
</fetch>

    下面上兩張執行結果圖,下面這張是以管理員id執行,對lead是有讀許可權的

   下面這張是換了個沒有許可權的賬號id,返回資料集就是空了

   這裡要注意個問題,使用web api查詢執行程式碼的許可權用的是當前使用者自身的許可權,在你的實際業務場景中有可能當前賬號對使用者實體就沒讀許可權,那這段程式碼的執行就需要使用web api中的模擬使用者了,使用管理員的身份去執行。

 

 

相關文章