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编程中的一些基本概念. 这是一篇翻译的文章,你可能会 ...
随机推荐
- linq 和 , 并 , 差 ,交
假如: A = [--.], B = [-.] A 并 B = 全部 linq : a.union(b) A 交 B = 中间那块 linq: a.Intersect(b) ...
- 虚拟化之intel
英特尔VT具体包括分别针对处理器.芯片组.网络的VT-X.VT-D和VT-C技术. 处理器:英特尔虚拟化技术(英特尔VT-x),包括英特尔虚拟化灵活迁移技术(Intel VT FlexMigratio ...
- unity shader在小米2s上的问题
一个很简单的用mask裁剪图片的效果: mask: 被裁剪图片: 正确的效果: 在小米2s上测,其中有一台小米2s出现花屏: 手机型号: shader如下: Shader "UI/Trans ...
- 转载——Python模拟登录代码
''' Created on 2014-2-20 @author: Vincent ''' import urllib.parse import gzip import json import re ...
- 微信网页授权获取用户基本信息--PHP
现在就说说怎么通过网页授权获取用户基本信息(国家,省,市,昵称)等. 必要条件: 1)公众号认证 2)有网页授权获取用户基本信息的权限接口 注意:最近有朋友说:在公众平台申请的测试号,会出现无法取到用 ...
- 前端页面div float 后高度 height 自适应的问题
最近在画项目页面的时候遇到了一个左侧div一旦加上float:left 属性后,设置其高度height:100% 不起作用,后来网上查了半天也没有找到很好的解决方案,只在csdn里发现了这个马上记录下 ...
- eclipse开发Android程序sdk和avd的图标不见了
在eclipse中开发我们的Android程序时,安装sdk是必不可少的,有时候会出现sdk和avd的图标都不见了的情况,一般出现这种情况的原因是你从别处copy了一个sdk的包然后直接在引用造成的, ...
- mongoDB 下载/安装/客户端笔记
1.下载: https://fastdl.mongodb.org/win32/mongodb-win32-x86_64-3.0.3.zip 2.安装 1.解压mongodb-win32-x86 64- ...
- centos 6安装redis 2.8.19
下载安装: wget https://github.com/antirez/redis/archive/2.8.19.tar.gz tar xvzf redis-stable.tar.gz cd re ...
- tomcat如何按站点调试本机程序
1.配置host host地址:c:\windows\system32\drivers\etc 配置本机域名: # localhost name resolution is handled withi ...