//获取当前人员是否含有指定角色权限
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的更多相关文章

  1. Dynamic CRM 2013学习笔记(四十六)简单审批流的实现

    前面介绍过自定义审批流: Dynamic CRM 2013学习笔记(十九)自定义审批流1 - 效果演示 Dynamic CRM 2013学习笔记(二十一)自定义审批流2 - 配置按钮 Dynamic ...

  2. 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 数据库 ...

  3. SAP CRM 性能小技巧

    导言 本页面打算收集SAP CRM实施中可以用于避免性能问题的注意事项,重要的事项会由图标标识. 如果你有其他的技巧想要说出来,别犹豫! 性能注意事项 通用 缓存读取类访问,特别是在性能关键的地方,比 ...

  4. SAP CRM 树视图(TREE VIEW)

    树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...

  5. SAP CRM 用户界面对象类型和设计对象

    在CRM中的用户界面对象类型的帮助下,我们可以做这些工作: 进行不同的视图配置 创建动态导航 从设计层控制字段标签.值帮助 控制BOL对象的属性的可视性 从导航栏访问自定义组件 一个用户界面对象类型之 ...

  6. SAP CRM 显示消息/在消息中进行导航

    向用户展示消息,在任何软件中都是十分重要的. 在SAP CRM WEB UI中展示消息,不是一项很难的任务,只需要创建消息并在之后调用方法来显示它 消息类和消息号: 我在SE91中创建了如下的消息类和 ...

  7. Dynamics CRM 2015-Data Encryption激活报错

    在CRM的日常开发中,Data Encryption经常是不得不开启的一个功能.但是有时,我们可能遇到一种情况,Organization导入之后,查看Data Encryption是已激活的状态,但是 ...

  8. SAP CRM 客户控制器与数据绑定

    当用户从视图离开时,视图将失去它的数据.解决这个问题,需要引入客户控制器(Custom Controller)(译者注:SAP CRM客户端中,不同地方的Custom Controller会翻译为“客 ...

  9. SAP CRM BOL编程基础,代码+详细注释

    网络上可以找到一些使用BOL查询.维护数据的DEMO,但几乎都是单纯的代码,缺乏说明,难以理解.本文除了代码外,还给出了详细的注释,有助于理解BOL编程中的一些基本概念. 这是一篇翻译的文章,你可能会 ...

随机推荐

  1. Problem A+B(Big Integer)

    /*======================================================================== Problem A+B(Big Integer) ...

  2. webStorm 注册码 (备用)

    webStorm : UserName:William ===== LICENSE BEGIN ===== 45550-12042010 00001SzFN0n1bPII7FnAxnt0DDOPJA  ...

  3. 由浅入深探究mysql索引结构原理、性能分析与优化 转

    第一部分:基础知识 第二部分:MYISAM和INNODB索引结构 1. 简单介绍B-tree B+ tree树 2. MyisAM索引结构 3. Annode索引结构 4. MyisAM索引与Inno ...

  4. webqq协议请求交互过程

    1.http://my.oschina.net/ij2ee/blog/191692 2.http://www.qqxieyi.com/fenxi_show.asp?id=34

  5. spring mvc 利用匿名内部类构建返回json对象

    @RequestMapping(value = "/order/findOrderByIdVague/{noId}.json", method = {RequestMethod.G ...

  6. mysql常用命令集锦

    一.DCL语句(数据控制语句) 1.授权远程访问,针对IP和用户.DB的 grant {privilege list} on {dbname}.* to '{user}'@'{ip}' identif ...

  7. maven本地仓库

    引入某一个站点的jar包 <repositories> <repository> <id>maven.seasar.org</id> <name& ...

  8. [原]在Fedora中编译Libevent测试实例

    在我的昨天的博文<[原]我在Windows环境下的首个Libevent测试实例>中介绍了在Windows环境下如何编译一个echo server例子.今天我又试了一下在Linux环境中编译 ...

  9. js之引用类型

    一.摘要: <javascript高级程序设计第三版>一书中单独有一章对js的引用类型(Object.Array.RegExp.Function:基本包装类型:Boolean.Number ...

  10. Spring实战4:面向切面编程

    主要内容 面向切面编程的基本知识 为POJO创建切面 使用@AspectJ注解 为AspectJ的aspects注入依赖关系 在南方没有暖气的冬天,太冷了,非常想念北方有暖气的冬天.为了取暖,很多朋友 ...