crm 4 UserHasRole
//获取当前人员是否含有指定角色权限
function UserHasRole(roleName)
{
//get Current User Roles, oXml is an object
var oXml = GetCurrentUserRoles();
if(oXml != null)
{
//select the node text
var roles = oXml.selectNodes("//BusinessEntity/q1:name");
if(roles != null)
{
for( i = 0; i < roles.length; i++)
{
if(roles[i].text == roleName)
{
//return true if user has this role
return true;
}
}
}
}
//otherwise return false
return false;
}
CRM 2011
Xrm.Page.context.getUserRoles()
function UserHasRole(roleName)
{
var serverUrl = Xrm.Page.context.getServerUrl(); var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/";
oDataEndpointUrl += "RoleSet?$top=1&$filter=Name eq '" + roleName + "'"; var service = GetRequestObject(); if (service != null)
{
service.open("GET", oDataEndpointUrl, false);
service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");
service.setRequestHeader("Accept", "application/json, text/javascript, */*");
service.send(null); var requestResults = eval('(' + service.responseText + ')').d; if (requestResults != null && requestResults.length == 1)
{
var role = requestResults[0]; var id = role.RoleId; var currentUserRoles = Xrm.Page.context.getUserRoles(); for (var i = 0; i < currentUserRoles.length; i++)
{
var userRole = currentUserRoles[i]; if (GuidsAreEqual(userRole, id))
{
return true;
}
}
}
} return false;
} function GetRequestObject()
{
if (window.XMLHttpRequest)
{
return new window.XMLHttpRequest;
}
else
{
try
{
return new ActiveXObject("MSXML2.XMLHTTP.3.0");
}
catch (ex)
{
return null;
}
}
} function GuidsAreEqual(guid1, guid2)
{
var isEqual = false; if (guid1 == null || guid2 == null)
{
isEqual = false;
}
else
{
isEqual = guid1.replace(/[{}]/g, "").toLowerCase() == guid2.replace(/[{}]/g, "").toLowerCase();
} return isEqual;
}
以下文章引自: http://community.dynamics.com/crm/b/crmmitchmilam/archive/2010/11/16/retreiving-user-roles-in-crm-2011
Retreiving User Roles in CRM 2011
As I was producing the associated documentation for my CRM Migration Assistant application, I decided to explore a comparison between a technique that we had to do the “hard way” in CRM 4.0 and a technique that is built into CRM 2011
The technique in question is retrieving a user’s security roles in order to perform some role-specific actions.
CRM 4.0 JavaScript
Jim Wang, friend and fellow MVP has an excellent article and JavaScript describing how to retrieve a user’s security roles in order to perform operations that are specifically for a certain type of CRM user:
//check if the current user has the 'System Administrator' role
alert(UserHasRole("System Administrator"));
function UserHasRole(roleName)
{
//get Current User Roles, oXml is an object
var oXml = GetCurrentUserRoles();
if(oXml != null)
{
//select the node text
var roles = oXml.selectNodes("//BusinessEntity/q1:name");
if(roles != null)
{
for( i = 0; i < roles.length; i++)
{
if(roles[i].text == roleName)
{
//return true if user has this role
return true;
}
}
}
}
//otherwise return false
return false;
}
I use this code in many different situations to show and hide CRM form elements for specific people.
CRM 2011 SDK Samples
If you look at the files included with the CRM 2011 SDK, you’ll find some sample JavaScript in the folder:
sdk\SampleCode\JS\FormScripts
If you load that solution into Visual Studio, you can see the many cool and interesting additions to the CRM 2011 JavaScript object model.
If you open SDK.ContextSamples.js, you’ll see some of the code I’ll be using today.
Xrm.Page.context.getUserRoles()
Jim’s CRM 4.0 code uses a SOAP call retrieve the security roles for a user. Lucky for us, this functionality is now built into CRM 2011 in the method:
Xrm.Page.context.getUserRoles()
Which returns an array of strings representing the GUID values of each of the security roles that the user is associated with.
This is really great, but I would like to refer to my security roles by name since it’s easier to remember and understand than a GUID. So, I had to add some extra code to handle that requirement.
CRM 2011 JavaScript
As I mentioned, I took the SDK sample code, modified it a bit, and replicated Jim’s functionality exactly, using the following BLOCKED SCRIPT
function UserHasRole(roleName)
{
var serverUrl = Xrm.Page.context.getServerUrl(); var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/";
oDataEndpointUrl += "RoleSet?$top=1&$filter=Name eq '" + roleName + "'"; var service = GetRequestObject(); if (service != null)
{
service.open("GET", oDataEndpointUrl, false);
service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");
service.setRequestHeader("Accept", "application/json, text/javascript, */*");
service.send(null); var requestResults = eval('(' + service.responseText + ')').d; if (requestResults != null && requestResults.length == 1)
{
var role = requestResults[0]; var id = role.RoleId; var currentUserRoles = Xrm.Page.context.getUserRoles(); for (var i = 0; i < currentUserRoles.length; i++)
{
var userRole = currentUserRoles[i]; if (GuidsAreEqual(userRole, id))
{
return true;
}
}
}
} return false;
} function GetRequestObject()
{
if (window.XMLHttpRequest)
{
return new window.XMLHttpRequest;
}
else
{
try
{
return new ActiveXObject("MSXML2.XMLHTTP.3.0");
}
catch (ex)
{
return null;
}
}
} function GuidsAreEqual(guid1, guid2)
{
var isEqual = false; if (guid1 == null || guid2 == null)
{
isEqual = false;
}
else
{
isEqual = guid1.replace(/[{}]/g, "").toLowerCase() == guid2.replace(/[{}]/g, "").toLowerCase();
} return isEqual;
}
Conclusion
As you can see, this is not a lot of code and with CRM 2011’s ability to create a Web Resource, I can add these functions to a JavaScript library, reference that library on the form, and just use the following code where necessary:
If (UserHasRole("System Administrator"))
{
// do something important
}
The secondary affect of using this new code is I don’t have to change my CRM 4.0 JavaScript since I duplicated the CRM 4.0 functionality in CRM 2011 and the usage show above, remains the same.
crm 4 UserHasRole的更多相关文章
- Dynamic CRM 2013学习笔记(四十六)简单审批流的实现
前面介绍过自定义审批流: Dynamic CRM 2013学习笔记(十九)自定义审批流1 - 效果演示 Dynamic CRM 2013学习笔记(二十一)自定义审批流2 - 配置按钮 Dynamic ...
- Enterprise Solution 3.1 企业应用开发框架 .NET ERP/CRM/MIS 开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms
行业:基于数据库的制造行业管理软件,包含ERP.MRP.CRM.MIS.MES等企业管理软件 数据库平台:SQL Server 2005或以上 系统架构:C/S 开发技术 序号 领域 技术 1 数据库 ...
- SAP CRM 性能小技巧
导言 本页面打算收集SAP CRM实施中可以用于避免性能问题的注意事项,重要的事项会由图标标识. 如果你有其他的技巧想要说出来,别犹豫! 性能注意事项 通用 缓存读取类访问,特别是在性能关键的地方,比 ...
- SAP CRM 树视图(TREE VIEW)
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...
- SAP CRM 用户界面对象类型和设计对象
在CRM中的用户界面对象类型的帮助下,我们可以做这些工作: 进行不同的视图配置 创建动态导航 从设计层控制字段标签.值帮助 控制BOL对象的属性的可视性 从导航栏访问自定义组件 一个用户界面对象类型之 ...
- SAP CRM 显示消息/在消息中进行导航
向用户展示消息,在任何软件中都是十分重要的. 在SAP CRM WEB UI中展示消息,不是一项很难的任务,只需要创建消息并在之后调用方法来显示它 消息类和消息号: 我在SE91中创建了如下的消息类和 ...
- Dynamics CRM 2015-Data Encryption激活报错
在CRM的日常开发中,Data Encryption经常是不得不开启的一个功能.但是有时,我们可能遇到一种情况,Organization导入之后,查看Data Encryption是已激活的状态,但是 ...
- SAP CRM 客户控制器与数据绑定
当用户从视图离开时,视图将失去它的数据.解决这个问题,需要引入客户控制器(Custom Controller)(译者注:SAP CRM客户端中,不同地方的Custom Controller会翻译为“客 ...
- SAP CRM BOL编程基础,代码+详细注释
网络上可以找到一些使用BOL查询.维护数据的DEMO,但几乎都是单纯的代码,缺乏说明,难以理解.本文除了代码外,还给出了详细的注释,有助于理解BOL编程中的一些基本概念. 这是一篇翻译的文章,你可能会 ...
随机推荐
- shell之条件表达式
conditional expressions are used by the [[ compound command and the test and [ builtin commands. ari ...
- OpenJudge 计算概论-判断闰年
/*======================================================================== 判断闰年 总时间限制: 1000ms 内存限制: ...
- .net托管平台appharbor使用
这篇文章是网上转过来的,在AppHarbor使用Git上传Code的时候,需要输入用户名和密码,就是appharbor登陆的用户名和密码. 可以参考这篇文章http://www.freehao123. ...
- bootstrapDialog插件集成datatables插件遇到的异常
最近项目中,涉及到很多细分领域的东西,有好些目前还没有详细的方案.这是后话,当前起步阶段,我要把握技术路线,搭建基础架构!其中,有好几个地方都用到模态框(Modal), 虽然Bootstrap框架里面 ...
- javascript 定义正则表达式
js中定义正则表达式有两种,使用RegExp和使用字面量. 使用字面量定义时需要注意:必须以/开始,以/结束,就像定义字符串一样("test"). 但是,js的正则表达式可以通过指 ...
- jsonp get 和 post
原文地址:http://blog.sina.com.cn/s/blog_4a7e719d0100zqzh.html jsonp获取服务器的数据,有两种一,跨域二,不跨域如果跨域js的写法有两种1,&l ...
- 线性判别分析(Linear Discriminant Analysis)转载
1. 问题 之前我们讨论的PCA.ICA也好,对样本数据来言,可以是没有类别标签y的.回想我们做回归时,如果特征太多,那么会产生不相关特征引入.过度拟合等问题.我们可以使用PCA来降维,但PCA没有将 ...
- Python基础教程【读书笔记】 - 2016/7/10
希望通过博客园持续的更新,分享和记录Python基础知识到高级应用的点点滴滴! 第五波:第1章 基础知识 [总览] 介绍如何得到所需的软件,然后讲一点点算法及其主要的组成.学习变量variable ...
- 剑指offer系列57---整数中1出现的次数
[题目]求出1~n的整数中1出现的次数.(10进制) package com.exe11.offer; /** * [题目]求出1~n的整数中1出现的次数. * @author WGS * */ pu ...
- Windows2012 cannot access netapp CIFS share
NAS1> options cifs.smb2.signing.requiredcifs.smb2.signing.required off NAS1> options cifs.smb2 ...