[转]SharePoint 2010/2013 使用Javascript来判断权限的三种方法

本文讲述SharePoint 2010/2013 使用Javascript来判断权限的三种方法的实现方式及其优缺点。

1. 根据用户所在的SharePoint组(比如用户在Leader 组才可以使用审批按钮)

    a. 优点,简单明了,容易理解,要获得这个权限只有一个入口,就是将用户加入到SharePoint组

    b. 缺点, 不能兼容AD group套SharePoint组的情况,只能将用户直接加入到SharePoint组的情况下起作用

   c. 实现代码如下:

  1. function IsCurrentUserMemberOfGroup(strGroupName, functionComplete) {  
  2.    
  3.         //Setup Vars  
  4.         currentContext  = null;  
  5.         currentWeb  = null;  
  6.         allGroups   = null;  
  7.         leaderGroup     = null;  
  8.         currentUser     = null;  
  9.         groupUsers  = null;  
  10.    
  11.         //Get an instance of the Client Content.  
  12.         currentContext = new SP.ClientContext.get_current();  
  13.    
  14.         //Grab the client web object.  
  15.         currentWeb = currentContext.get_web();  
  16.    
  17.         //Get the current user object  
  18.         currentUser = currentContext.get_web().get_currentUser();  
  19.         currentContext.load(currentUser);  
  20.    
  21.         //Setup the groupColletion.  
  22.         allGroups = currentWeb.get_siteGroups();  
  23.         currentContext.load(allGroups);  
  24.    
  25.         //Now populate the objects above.  
  26.         currentContext.executeQueryAsync(  
  27.             Function.createDelegate(this, GetAllGroupsExecuteOnSuccess),  
  28.             Function.createDelegate(this, ExecuteOnFailure)  
  29.         );  
  30.    
  31.         // GroupCollection - Load - SUCCESS  
  32.         function GetAllGroupsExecuteOnSuccess(sender, args) {  
  33.    
  34.             // CHECK THE GROUPS  
  35.             // Time to Enumerate through the group collection that was returned.  
  36.             var groupEnumerator = allGroups.getEnumerator();  
  37.    
  38.             // Loop for the collection.  
  39.             while (groupEnumerator.moveNext()) {  
  40.    
  41.                 //Grab the Group Item.  
  42.                 var group = groupEnumerator.get_current();  
  43.                 if (group.get_title().indexOf(strGroupName) > -1) {  
  44.    
  45.                     // Now that we have the group let's grab the list of users.  
  46.                     groupUsers = group.get_users();  
  47.                     currentContext.load(groupUsers);  
  48.                     currentContext.executeQueryAsync(  
  49.                         Function.createDelegate(this, SingleGroupExecuteOnSuccess),  
  50.                         Function.createDelegate(this, ExecuteOnFailure)  
  51.                     );  
  52.                 }  
  53.             }  
  54.         }  
  55.    
  56.         // Single Group - Load - SUCCESS  
  57.         function SingleGroupExecuteOnSuccess(sender, args) {  
  58.    
  59.             // Time to setup the Enumerator  
  60.             var groupUserEnumerator = groupUsers.getEnumerator();  
  61.    
  62.             // This is the flag to set to true if the user is in the group.  
  63.             var boolUserInGroup = false;  
  64.    
  65.             // and start looping.  
  66.             while (groupUserEnumerator.moveNext()) {  
  67.    
  68.                 //Grab the User Item.  
  69.                 var groupUser = groupUserEnumerator.get_current();  
  70.    
  71.                 // and finally. If a Group User ID Matches the current user ID then they are in the group!  
  72.                 if (groupUser.get_id() == currentUser.get_id()) {  
  73.                     boolUserInGroup = true;  
  74.                 }  
  75.             }  
  76.    
  77.             //Run the delegate function with the bool;  
  78.             functionComplete(boolUserInGroup);  
  79.         }  
  80.    
  81.         // GroupCollection or Single Group - Load - FAILURE  
  82.         function ExecuteOnFailure(sender, args) {  
  83.             //Run the delegate function and return false because there was no match.  
  84.             functionComplete(false);  
  85.         }  
  86.     }  
  87.   
  88.   
  89. IsCurrentUserMemberOfGroup("Lead"function (isCurrentUserInGroup) {  
  90.     if(isCurrentUserInGroup)  
  91.     {  
  92.         // Do something for the user in the correct SP group  
  93.     }  
  94. });  

2. 使用User 类的isSiteAdmin属性

a. 优点:需要写代码少,效率高

b. 缺点:只能判断用户是否为当前站点集管理员,适用场景很少

c. 代码实现如下:

  1. var currentUser;  
  2.   SP.SOD.executeFunc('sp.js''SP.ClientContext', GetCurrentUser);  
  3. function GetCurrentUser() {  
  4.     var clientContext = new SP.ClientContext.get_current();  
  5.     var oWeb = clientContext.get_web();  
  6.     currentUser = oWeb.get_currentUser();  
  7.     clientContext.load(currentUser);  
  8.     clientContext.executeQueryAsync(Onsuccess, OnFailed);  
  9.    }  
  10.   
  11. function Onsuccess()  
  12. {  
  13.      if(currentUser.get_isSiteAdmin())  
  14.         {  
  15.             // Do something for the user who is the current site collection admin  
  16.        }  
  17. }  
  18.   
  19. function OnFailed(request, message)  
  20. {  
  21.      alert('error'  + message);  
  22. }  

3. 使用 EffectiveBasePermissions,这个也是微软推荐的做法

a. 优点:功能上基本没有限制,可以检查所有SharePoint的权限级别: http://msdn.microsoft.com/en-us/library/ee556747(v=office.14).aspx

b. 缺点:获得权限的入口不是唯一的,可以单独给用户权限,也可以由用户加入到某个组来获取权限

c. 代码实现如下:

  1. <script type="text/javascript">  
  2.     SP.SOD.executeFunc('sp.js''SP.ClientContext', CheckPermissionOnWeb);  
  3.   
  4.     function CheckPermissionOnWeb() {  
  5.         context = new SP.ClientContext.get_current();  
  6.   
  7.         web = context.get_web();  
  8.   
  9.         this._currentUser = web.get_currentUser();  
  10.   
  11.         context.load(this._currentUser);  
  12.   
  13.         context.load(web, 'EffectiveBasePermissions');  
  14.   
  15.         context.executeQueryAsync(Function.createDelegate(thisthis.onSuccessMethod), Function.createDelegate(thisthis.onFailureMethod));  
  16.     }  
  17.   
  18.     function onSuccessMethod(sender, args) {  
  19.         if (web.get_effectiveBasePermissions().has(SP.PermissionKind.manageWeb)) {  
  20.             // User Has permission to manage web  
  21.            //  Do something you want to do for the user who can manage the web  
  22.         }  
  23.     }  
  24.   
  25. Function onFailureMethod(sender, args)  
  26. {  
  27.       alert('error'  +args.message);  
  28. }  
  29. </script>  
原文地址:http://blog.csdn.net/abrahamcheng/article/details/17447479
原文地址:https://www.cnblogs.com/52life/p/3494004.html