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

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

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

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

c. 实现代码如下:

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

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. function Onsuccess()
  11. {
  12. if(currentUser.get_isSiteAdmin())
  13. {
  14. // Do something for the user who is the current site collection admin
  15. }
  16. }
  17. function OnFailed(request, message)
  18. {
  19. alert('error'  + message);
  20. }
  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. function Onsuccess()
  11. {
  12. if(currentUser.get_isSiteAdmin())
  13. {
  14. // Do something for the user who is the current site collection admin
  15. }
  16. }
  17. function OnFailed(request, message)
  18. {
  19. alert('error' + message);
  20. }

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. function CheckPermissionOnWeb() {
  4. context = new SP.ClientContext.get_current();
  5. web = context.get_web();
  6. this._currentUser = web.get_currentUser();
  7. context.load(this._currentUser);
  8. context.load(web, 'EffectiveBasePermissions');
  9. context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod), Function.createDelegate(this, this.onFailureMethod));
  10. }
  11. function onSuccessMethod(sender, args) {
  12. if (web.get_effectiveBasePermissions().has(SP.PermissionKind.manageWeb)) {
  13. // User Has permission to manage web
  14. //  Do something you want to do for the user who can manage the web
  15. }
  16. }
  17. Function onFailureMethod(sender, args)
  18. {
  19. alert('error'  +args.message);
  20. }
  21. </script>

原文地址:http://blog.csdn.net/abrahamcheng/article/details/17447479

[转]SharePoint 2010/2013 使用Javascript来判断权限的三种方法的更多相关文章

  1. [转]Javascript定义类的三种方法

    作者: 阮一峰 原文地址:http://www.ruanyifeng.com/blog/2012/07/three_ways_to_define_a_javascript_class.html 将近2 ...

  2. JavaScript去除空格的三种方法(正则/传参函数/trim)

    方法一: 个人认为最好的方法.采用的是正则表达式,这是最核心的原理. 其次.这个方法使用了JavaScript 的prototype 属性 其实你不使用这个属性一样可以用函数实现.但这样做后用起来比较 ...

  3. JavaScript 复制变量的三种方法

    参考:Copying Objects in JavaScript - Orinami Olatunji(@orinamio_) October 23, 2017    直接将一个变量赋给另一个变量时, ...

  4. JavaScript RegExp 对象的三种方法

    JavaScript RegExp 对象有 3 个方法:test().exec() 和 compile().(1) test() 方法用来检测一个字符串是否匹配某个正则表达式,如果匹配成功,返回 tr ...

  5. JavaScript调用后台的三种方法实例(包含两种Ajax)

    方法一:直接使用<%=%>调用(ASPX页面) 前台JS,代码如下: <script type="text/javascript"> var methodS ...

  6. 获得Window窗口权限的三种方法

    1.第一种方法:利用视图控制器自带的View的window属性:  具体使用 self.view.window.rootViewController = ... 2.第二种方法:通过导入APPDele ...

  7. javascript生成对象的三种方法

    /** js生成对象的三种方法*/ // 1.通过new Object,然后添加属性 示例如下: var people1 = new Object(); people1.name = 'xiaohai ...

  8. javascript获取DOM对象三种方法

    1. getElementByID() getElementByID()方法可返回对拥有指定ID的第一个对象的引用 2. getElementByTagName() getElementByTagNa ...

  9. JavaScript实现选项卡(三种方法)

    本文实例讲述了js选项卡的实现方法. 一.html代码: <div id="div1"> <input class="active" type ...

随机推荐

  1. epub格式电子书剖析之三:NCX文件构成

    ncx文件是epub电子书的又一个核心文件,用于制作电子书的目录,其文件的命名通常为toc.ncx. ncx文件是一个XML文件,该标准由DAISY Consortium发布(参见http://www ...

  2. C# 操作 Word 修改word的高级属性中的自定义属性

    为 Microsoft Word 创建自动化客户端 启动 Visual Studio .NET. 在文件菜单上,单击新建,然后单击项目.从 Visual C# 项目类型中选择 Windows 应用程序 ...

  3. uboot环境变量(设置bootargs向linux内核传递正确的参数)

    这是我uboot的环境变量设置,在该设置下可以运行initram内核(从内存下载到nandflash再运行),但是运行nfs根文件系统的时候一直出错,各种错误.查看了很多资料后猜想应该是uboot传递 ...

  4. POJ 1847 Tram dij

    分析:d[i]表示到i点,最少的操作数 #include<cstdio> #include<cstring> #include<queue> #include< ...

  5. CentOS 安装nagios

    Nagios的介绍: 1.Nagios是一个监控系统运行状态和网络信息的监控系统.它能监控所指定的本地或远程主机的系统状态以及运行的服务,同时提供异常通知的功能. 2. Nagios可运行在Linux ...

  6. C程序设计 习题之1-20 detab

    码农一定是最需要动手实操的行业之一.有一句话叫,好记性不如烂笔头,牵强附会引申到这里,变成看书百遍,不如码字运行一遍.是不是有那么一点味道?哈哈! 这几天看的<C程序设计>,看完每个章节还 ...

  7. 关于VNC黑屏的问题

    注意: 1.vncserver启动后生成的ID号(1,2,3)要和VNCview里面填入的 ip:ID要保持一致  不然看到的就是黑屏 比如:vncserver启动后 产生: localhost.lo ...

  8. 泰泽新闻:英特尔三星双否认泰泽Tizen系统已死

    7月8日 据媒体TizenExperts报道,关于“Tizen系统跳票”的传闻已经遭到了英特尔和三星否认. 此前传闻三星自行研制的智能手机Tizen操作系统流产,但如今已经遭到了官方的否认. 英特尔三 ...

  9. [读书笔记]算法(Sedgewick著)·第一章(1)

    到家放松放松之后就开始学习算法了,手里拿的是拿的是一本Robert Sedgewick的橙皮书<算法(第四版)>的.这本书与导论那本书的不同之处在于轻数学思想.重实现,也就是说这是一本很不 ...

  10. Eclipse的下载、安装和WordCount的初步使用(本地模式和集群模式)

    包括:    Eclipse的下载 Eclipse的安装 Eclipse的使用 本地模式或集群模式 Scala IDE for Eclipse的下载.安装和WordCount的初步使用(本地模式和集群 ...