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编程中的一些基本概念. 这是一篇翻译的文章,你可能会 ...
随机推荐
- JSON 序列化和反序列化——JavaScriptSerializer实现
一. JavaScriptSerializer 类由异步通信层内部使用,用于序列化和反序列化在浏览器和 Web 服务器之间传递的数据.您无法访问序列化程序的此实例.但是,此类公开了公共 API.因此, ...
- python_Day3[set集合,函数,全局变量之篇]
一.set集合 1.Set集合特点:无序.不重复,可嵌套 2.set集合创建规则:set = {"123","234"} 字典创建规则:dic = {“Key” ...
- Suricata+Barnyard2+Base的IDS前端Snorby
搭建基于Suricata+Barnyard2+Base的IDS前端Snorby 4.Barnyard2:http://www.securixlive.com/barnyard2/download.ph ...
- fiddler filter 过滤css 图片等
找到 request header->Show only if yrl contains: REGEX:(?insx)/[^\?/]*\.(css|ico|jpg|png|gif|bmp|wav ...
- 彻底解决ASP.NET MVC 3 404错误码返回302的问题
转自:http://blog.csdn.net/mycloudke/article/details/9746333 404状态码:,意味着当在页面上显示用户点击不存在,提高用户体验度,搜索引擎会放弃这 ...
- Protocol Buffer基本介绍
转自:http://www.cnblogs.com/stephen-liu74/archive/2013/01/02/2841485.html 该系列Blog的内容主体主要源自于Protocol Bu ...
- [原]Fedora Linux环境下的应用工具总结
一.办公类软件 1.Office办公:WPS 二.网络通信类软件 1.浏览器:Chrome 2.远程桌面:rdesktop(适用于Windows系列) 三.操作系统设置与优化 1.3D桌面管理:Com ...
- TCP长连接与短连接
1.概念区别 所谓TCP短连接,是指通信双方有数据交互时,就建立一个TCP连接,数据发送完成后,则断开此TCP连接.也就是说TCP连接维持的时间比较短.一般银行网页数据交互都使用短连接.再比如说htt ...
- astats日志分析系统
Awstats是一个免费非常简洁而且强大有个性的网站日志分析工具. 功能: 一:访问量,访问次数,页面浏览量,点击数,数据流量等 二:精确到每月.每日.每小时的数据 三:访问者国家 四:访问者IP 五 ...
- .NET去掉HTML标记
using System.Text.RegularExpressions; /// <summary> /// 去除HTML标记 /// </summary> /// < ...