[转]SharePoint 2010/2013 使用Javascript来判断权限的三种方法
本文讲述SharePoint 2010/2013 使用Javascript来判断权限的三种方法的实现方式及其优缺点。
1. 根据用户所在的SharePoint组(比如用户在Leader 组才可以使用审批按钮)
a. 优点,简单明了,容易理解,要获得这个权限只有一个入口,就是将用户加入到SharePoint组
b. 缺点, 不能兼容AD group套SharePoint组的情况,只能将用户直接加入到SharePoint组的情况下起作用
c. 实现代码如下:
- function IsCurrentUserMemberOfGroup(strGroupName, functionComplete) {
- //Setup Vars
- currentContext = null;
- currentWeb = null;
- allGroups = null;
- leaderGroup = null;
- currentUser = null;
- groupUsers = null;
- //Get an instance of the Client Content.
- currentContext = new SP.ClientContext.get_current();
- //Grab the client web object.
- currentWeb = currentContext.get_web();
- //Get the current user object
- currentUser = currentContext.get_web().get_currentUser();
- currentContext.load(currentUser);
- //Setup the groupColletion.
- allGroups = currentWeb.get_siteGroups();
- currentContext.load(allGroups);
- //Now populate the objects above.
- currentContext.executeQueryAsync(
- Function.createDelegate(this, GetAllGroupsExecuteOnSuccess),
- Function.createDelegate(this, ExecuteOnFailure)
- );
- // GroupCollection - Load - SUCCESS
- function GetAllGroupsExecuteOnSuccess(sender, args) {
- // CHECK THE GROUPS
- // Time to Enumerate through the group collection that was returned.
- var groupEnumerator = allGroups.getEnumerator();
- // Loop for the collection.
- while (groupEnumerator.moveNext()) {
- //Grab the Group Item.
- var group = groupEnumerator.get_current();
- if (group.get_title().indexOf(strGroupName) > -1) {
- // Now that we have the group let's grab the list of users.
- groupUsers = group.get_users();
- currentContext.load(groupUsers);
- currentContext.executeQueryAsync(
- Function.createDelegate(this, SingleGroupExecuteOnSuccess),
- Function.createDelegate(this, ExecuteOnFailure)
- );
- }
- }
- }
- // Single Group - Load - SUCCESS
- function SingleGroupExecuteOnSuccess(sender, args) {
- // Time to setup the Enumerator
- var groupUserEnumerator = groupUsers.getEnumerator();
- // This is the flag to set to true if the user is in the group.
- var boolUserInGroup = false;
- // and start looping.
- while (groupUserEnumerator.moveNext()) {
- //Grab the User Item.
- var groupUser = groupUserEnumerator.get_current();
- // and finally. If a Group User ID Matches the current user ID then they are in the group!
- if (groupUser.get_id() == currentUser.get_id()) {
- boolUserInGroup = true;
- }
- }
- //Run the delegate function with the bool;
- functionComplete(boolUserInGroup);
- }
- // GroupCollection or Single Group - Load - FAILURE
- function ExecuteOnFailure(sender, args) {
- //Run the delegate function and return false because there was no match.
- functionComplete(false);
- }
- }
- IsCurrentUserMemberOfGroup("Lead", function (isCurrentUserInGroup) {
- if(isCurrentUserInGroup)
- {
- // Do something for the user in the correct SP group
- }
- });
function IsCurrentUserMemberOfGroup(strGroupName, functionComplete) {
//Setup Vars
currentContext = null;
currentWeb = null;
allGroups = null;
leaderGroup = null;
currentUser = null;
groupUsers = null;
//Get an instance of the Client Content.
currentContext = new SP.ClientContext.get_current();
//Grab the client web object.
currentWeb = currentContext.get_web();
//Get the current user object
currentUser = currentContext.get_web().get_currentUser();
currentContext.load(currentUser);
//Setup the groupColletion.
allGroups = currentWeb.get_siteGroups();
currentContext.load(allGroups);
//Now populate the objects above.
currentContext.executeQueryAsync(
Function.createDelegate(this, GetAllGroupsExecuteOnSuccess),
Function.createDelegate(this, ExecuteOnFailure)
);
// GroupCollection - Load - SUCCESS
function GetAllGroupsExecuteOnSuccess(sender, args) {
// CHECK THE GROUPS
// Time to Enumerate through the group collection that was returned.
var groupEnumerator = allGroups.getEnumerator();
// Loop for the collection.
while (groupEnumerator.moveNext()) {
//Grab the Group Item.
var group = groupEnumerator.get_current();
if (group.get_title().indexOf(strGroupName) > -1) {
// Now that we have the group let's grab the list of users.
groupUsers = group.get_users();
currentContext.load(groupUsers);
currentContext.executeQueryAsync(
Function.createDelegate(this, SingleGroupExecuteOnSuccess),
Function.createDelegate(this, ExecuteOnFailure)
);
}
}
}
// Single Group - Load - SUCCESS
function SingleGroupExecuteOnSuccess(sender, args) {
// Time to setup the Enumerator
var groupUserEnumerator = groupUsers.getEnumerator();
// This is the flag to set to true if the user is in the group.
var boolUserInGroup = false;
// and start looping.
while (groupUserEnumerator.moveNext()) {
//Grab the User Item.
var groupUser = groupUserEnumerator.get_current();
// and finally. If a Group User ID Matches the current user ID then they are in the group!
if (groupUser.get_id() == currentUser.get_id()) {
boolUserInGroup = true;
}
}
//Run the delegate function with the bool;
functionComplete(boolUserInGroup);
}
// GroupCollection or Single Group - Load - FAILURE
function ExecuteOnFailure(sender, args) {
//Run the delegate function and return false because there was no match.
functionComplete(false);
}
}
IsCurrentUserMemberOfGroup("Lead", function (isCurrentUserInGroup) {
if(isCurrentUserInGroup)
{
// Do something for the user in the correct SP group
}
});
2. 使用User 类的isSiteAdmin属性
a. 优点:需要写代码少,效率高
b. 缺点:只能判断用户是否为当前站点集管理员,适用场景很少
c. 代码实现如下:
- var currentUser;
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', GetCurrentUser);
- function GetCurrentUser() {
- var clientContext = new SP.ClientContext.get_current();
- var oWeb = clientContext.get_web();
- currentUser = oWeb.get_currentUser();
- clientContext.load(currentUser);
- clientContext.executeQueryAsync(Onsuccess, OnFailed);
- }
- function Onsuccess()
- {
- if(currentUser.get_isSiteAdmin())
- {
- // Do something for the user who is the current site collection admin
- }
- }
- function OnFailed(request, message)
- {
- alert('error' + message);
- }
var currentUser;
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', GetCurrentUser);
function GetCurrentUser() {
var clientContext = new SP.ClientContext.get_current();
var oWeb = clientContext.get_web();
currentUser = oWeb.get_currentUser();
clientContext.load(currentUser);
clientContext.executeQueryAsync(Onsuccess, OnFailed);
}
function Onsuccess()
{
if(currentUser.get_isSiteAdmin())
{
// Do something for the user who is the current site collection admin
}
}
function OnFailed(request, message)
{
alert('error' + message);
}
3. 使用 EffectiveBasePermissions,这个也是微软推荐的做法
a. 优点:功能上基本没有限制,可以检查所有SharePoint的权限级别: http://msdn.microsoft.com/en-us/library/ee556747(v=office.14).aspx
b. 缺点:获得权限的入口不是唯一的,可以单独给用户权限,也可以由用户加入到某个组来获取权限
c. 代码实现如下:
- <script type="text/javascript">
- SP.SOD.executeFunc('sp.js', 'SP.ClientContext', CheckPermissionOnWeb);
- function CheckPermissionOnWeb() {
- context = new SP.ClientContext.get_current();
- web = context.get_web();
- this._currentUser = web.get_currentUser();
- context.load(this._currentUser);
- context.load(web, 'EffectiveBasePermissions');
- context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod), Function.createDelegate(this, this.onFailureMethod));
- }
- function onSuccessMethod(sender, args) {
- if (web.get_effectiveBasePermissions().has(SP.PermissionKind.manageWeb)) {
- // User Has permission to manage web
- // Do something you want to do for the user who can manage the web
- }
- }
- Function onFailureMethod(sender, args)
- {
- alert('error' +args.message);
- }
- </script>
原文地址:http://blog.csdn.net/abrahamcheng/article/details/17447479
[转]SharePoint 2010/2013 使用Javascript来判断权限的三种方法的更多相关文章
- [转]Javascript定义类的三种方法
作者: 阮一峰 原文地址:http://www.ruanyifeng.com/blog/2012/07/three_ways_to_define_a_javascript_class.html 将近2 ...
- JavaScript去除空格的三种方法(正则/传参函数/trim)
方法一: 个人认为最好的方法.采用的是正则表达式,这是最核心的原理. 其次.这个方法使用了JavaScript 的prototype 属性 其实你不使用这个属性一样可以用函数实现.但这样做后用起来比较 ...
- JavaScript 复制变量的三种方法
参考:Copying Objects in JavaScript - Orinami Olatunji(@orinamio_) October 23, 2017 直接将一个变量赋给另一个变量时, ...
- JavaScript RegExp 对象的三种方法
JavaScript RegExp 对象有 3 个方法:test().exec() 和 compile().(1) test() 方法用来检测一个字符串是否匹配某个正则表达式,如果匹配成功,返回 tr ...
- JavaScript调用后台的三种方法实例(包含两种Ajax)
方法一:直接使用<%=%>调用(ASPX页面) 前台JS,代码如下: <script type="text/javascript"> var methodS ...
- 获得Window窗口权限的三种方法
1.第一种方法:利用视图控制器自带的View的window属性: 具体使用 self.view.window.rootViewController = ... 2.第二种方法:通过导入APPDele ...
- javascript生成对象的三种方法
/** js生成对象的三种方法*/ // 1.通过new Object,然后添加属性 示例如下: var people1 = new Object(); people1.name = 'xiaohai ...
- javascript获取DOM对象三种方法
1. getElementByID() getElementByID()方法可返回对拥有指定ID的第一个对象的引用 2. getElementByTagName() getElementByTagNa ...
- JavaScript实现选项卡(三种方法)
本文实例讲述了js选项卡的实现方法. 一.html代码: <div id="div1"> <input class="active" type ...
随机推荐
- .net HTMLParser详细使用说明 强大的Filter类 解析HTML文档如此简单
背景: HTMLParser原本是一个在sourceforge上的一个Java开源项目,使用这个Java类库可以用来线性地或嵌套地解析HTML文本.他的 功能强大和开源等特性吸引了大量Web信息提取的 ...
- Discuz!NT中集成Memcached分布式缓存
大约在两年前我写过一篇关于Discuz!NT缓存架构的文章,在那篇文章的结尾介绍了在IIS中如果开启多个应用程序池会造成多个缓存实例之间数据同步的问题.虽然给出了一个解决方案,但无形中却把压力转移到了 ...
- ActiveX添加测试工程, 出现的问题[非选择性参数][找不到成员]
ActiveX 添加测试工程 1.新建工程MFC application, 2.添加完毕,在main Dialog中, 右键[Insert Activex Control],选择你的ActiveX控件 ...
- Jmeter初步使用三--使用jmeter自身录制脚本
今日,小编在网上看到很多人使用badboy来录制,然后再把jmx脚本弄到Jmeter上做性能测试.这种方法在小编刚用Jmeter时也曾经用过,但是感觉太麻烦了,所以就找了下其它资料.结果,小编偶然发现 ...
- Linux入门1
在计算机科学中,Shell俗称壳(用来区别于核),是指“提供使用者使用界面”的软件(命令解析器).它类似于DOS下的command和后来的cmd.exe.它接收用户命令,然后调用相应的应用程序. Li ...
- hotplug\uevent机制(1)
hotplug就是热拔插,在linux里面,这个功能是通过class_device_create这个函数来实现的,那么我们来分析下这个函数: class_device_create(cls, NULL ...
- Storm系列(六)架构分析之Scheduler-调度器[EventScheduler]
任务调度接口定义: 1 IScheduler{ 2 // conf为当前nimbus的stormp配置 3 void prepare(Map conf); // 初始化 4 // to ...
- poj1637--Sightseeing tour(最大流)
最大流求混合图是否存在欧拉回路. 以下内容摘自http://www.cnblogs.com/Missa/archive/2012/12/05/2803107.html 讲的很清楚. 混合图的欧拉回路问 ...
- php中的$_SERVER从哪来
前几个月学了个tcpdump抓包命令,遇到任何问题总想试试,真是程序员的终级武器呀,它像显微镜一下,把任何的丑陋的bug都显示在你的面前. 为什么有题目中所说的疑问呢?因为我发现在不同的环境下面,我获 ...
- js学习之原型prototype(一)
1.javascript中的每个引用类型(原生的.和自定义的)都有prototype属性,Javascript中对象的prototype属性的解释是:返回对象类型原型的引用. A.prototype ...