<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MSCRM Blogger &#187; security</title>
	<atom:link href="http://mscrmblogger.com/tag/security/feed/" rel="self" type="application/rss+xml" />
	<link>http://mscrmblogger.com</link>
	<description>Achieving it all with Microsoft Dynamics CRM™</description>
	<lastBuildDate>Wed, 30 Nov 2011 00:34:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Get Current User&#8217;s Business Unit for Hiding Fields</title>
		<link>http://mscrmblogger.com/2009/09/30/get-current-users-business-unit-for-hiding-fields/</link>
		<comments>http://mscrmblogger.com/2009/09/30/get-current-users-business-unit-for-hiding-fields/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 14:34:15 +0000</pubDate>
		<dc:creator>Carlton Colter</dc:creator>
				<category><![CDATA[Extensions]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[businessunit]]></category>
		<category><![CDATA[crm]]></category>
		<category><![CDATA[crm 4]]></category>
		<category><![CDATA[crm4]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jscript]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[microsoft dynamics CRM 4]]></category>
		<category><![CDATA[onload event]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/?p=136</guid>
		<description><![CDATA[In my <a href="http://mscrmblogger.com/2009/09/30/hide-fields-or-tabs-by-role-with-javascript/">previous post about hiding fields and tabs using roles</a>, I outlined how use roles to hide fields, etc.  Here is a modification of the method to hide sections by business unit.]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://mscrmblogger.com/2009/09/30/hide-fields-or-tabs-by-role-with-javascript/">previous post about hiding fields and tabs using roles</a>, I outlined how use roles to hide fields, etc.  Here is a modification of the method to hide sections by business unit.</p>
<p><b>The syntax is UserHasBusinessUnit(['BU1','BU2','BU3']);</b></p>
<pre name="code" class="javascript">
function UserHasBusinessUnit(businessUnits)
{
  var mybu = GetMyBusinessUnit();

  for (j = 0; j &lt; businessUnits.length; j++)
  {
	// If there is a match, return true, found
	if (mybu == businessUnits[j]) return true;
  }  

  //otherwise return false
  return false;
}
function GetMyBusinessUnit() {
    var xml = &quot;&quot; +
	&quot;&lt;?xml version='1.0' encoding='utf-8'?&gt;&quot; +
	&quot;&lt;soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'&quot; +
	&quot; xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'&quot; +
	&quot; xmlns:xsd='http://www.w3.org/2001/XMLSchema'&gt;&quot; +
	GenerateAuthenticationHeader() +
	&quot;&lt;soap:Body&gt;&quot; +
	&quot;&lt;Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'&gt;&quot; +
	&quot;&lt;fetchXml&gt;&quot; +
	&quot; &amp;lt;fetch mapping='logical' count='1'&amp;gt;&quot; +
	&quot; &amp;lt;entity name='businessunit'&amp;gt;&quot; +
	&quot; &amp;lt;attribute name='name' /&amp;gt;&quot; +
	&quot; &amp;lt;filter&amp;gt;&quot; +
	&quot; &amp;lt;condition attribute='businessunitid' operator='eq-businessid' /&amp;gt;&quot; +
	&quot; &amp;lt;/filter&amp;gt;&quot; +
	&quot; &amp;lt;/entity&amp;gt;&quot; +
	&quot; &amp;lt;/fetch&amp;gt;&quot; +
	&quot;&lt;/fetchXml&gt;&quot; +
	&quot;&lt;/Fetch&gt;&quot; +
	&quot;&lt;/soap:Body&gt;&quot; +
	&quot;&lt;/soap:Envelope&gt;&quot;;

    var xmlHttpRequest = new ActiveXObject(&quot;Msxml2.XMLHTTP&quot;);
    xmlHttpRequest.Open(&quot;POST&quot;, &quot;/mscrmservices/2007/CrmService.asmx&quot;, false);
    xmlHttpRequest.setRequestHeader(&quot;SOAPAction&quot;, &quot;http://schemas.microsoft.com/crm/2007/WebServices/Fetch&quot;);
    xmlHttpRequest.setRequestHeader(&quot;Content-Type&quot;, &quot;text/xml; charset=utf-8&quot;);
    xmlHttpRequest.setRequestHeader(&quot;Content-Length&quot;, xml.length);
    xmlHttpRequest.send(xml);

    var resultXml = xmlHttpRequest.responseXML;

	var resultSet = resultXml.text;
    resultSet.replace('&amp;lt;', '&lt; ');
    resultSet.replace('&amp;gt;', '&gt;');

    var oXmlDoc = new ActiveXObject(&quot;Microsoft.XMLDOM&quot;);
    oXmlDoc.async = false;
    oXmlDoc.loadXML(resultSet);

	var result = oXmlDoc.getElementsByTagName('name');  

	return result[0].text;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2009/09/30/get-current-users-business-unit-for-hiding-fields/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>CRM4: Hide Fields or Tabs by Role with Javascript</title>
		<link>http://mscrmblogger.com/2009/09/30/hide-fields-or-tabs-by-role-with-javascript/</link>
		<comments>http://mscrmblogger.com/2009/09/30/hide-fields-or-tabs-by-role-with-javascript/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 13:13:32 +0000</pubDate>
		<dc:creator>Carlton Colter</dc:creator>
				<category><![CDATA[Extensions]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[crm]]></category>
		<category><![CDATA[crm4]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jscript]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[microsoft dynamics CRM 4]]></category>
		<category><![CDATA[onload event]]></category>
		<category><![CDATA[roles]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://mscrmblogger.com/?p=131</guid>
		<description><![CDATA[Do you need to hide fields or tabs based on security roles.  Here is an easy way to add the functionality to your CRM, use my code example.]]></description>
			<content:encoded><![CDATA[<p>Here are a set of methods to assist in hiding tabs or sections based on role, it is a modified version of <a href="http://jianwang.blogspot.com/2008/01/crm-40-check-current-users-security.html" target="_blank">Jimmy Wang&#8217;s version</a>.</p>
<p>First, we need a set of functions to facilitate our our process.  We need to get the roles of the current user, <b><i>GetCurrentUserRoles</i></b>.  Then we need to see if the user has the role, <b><i>UserHasRole</i></b>.  Finally we can hide the fields (<b><i>HideFieldByRole</i></b>) or tabs (<b><i>HideTabByRole</i></b>).</p>
<p>
<b>GetCurrentUserRoles</b><br />
The first thing we need to do is get a list of roles according to the current user.  There are a couple of different ways to do this.  You could use the RemoteCommand to get the current user and then their roles, a blog post by <a href="http://www.crowehorwath.com/cs/blogs/crm/archive/2008/05/08/hide-show-fields-in-crm-4-0-based-on-security-role.aspx" target="_blank">Zahara Hirani on Hide Show Fields in CRM 4 based on security role</a> outlines this.  However, I do not want to make multiple request for the same information.  I want to get the roles for the current user in one line.  Below shows the GetCurrentUserRoles that implements a RetrieveMultiple query to get the names of the roles that a user has in one request.</p>
<pre name="code" class="javascript">
function GetCurrentUserRoles()
{
  var xml = &quot;&quot; +
    &quot;&lt;?xml version=\&quot;1.0\&quot; encoding=\&quot;utf-8\&quot;?&gt;&quot; +
    &quot;&lt;soap:Envelope xmlns:soap=\&quot;&quot; +
    &quot;http://schemas.xmlsoap.org/soap/envelope/&quot; +
    &quot;\&quot; xmlns:xsi=\&quot;http://www.w3.org/2001/XMLSchema-instance\&quot;&quot; +
    &quot; xmlns:xsd=\&quot;http://www.w3.org/2001/XMLSchema\&quot;&gt;&quot; +
    GenerateAuthenticationHeader() +
    &quot; &lt;soap:Body&gt;&quot; +
    &quot; &lt;RetrieveMultiple xmlns=\&quot;&quot; +
    &quot;http://schemas.microsoft.com/crm/2007/WebServices\&quot;&gt;&quot; +
    &quot; &lt;query xmlns:q1=\&quot;&quot; +
    &quot;http://schemas.microsoft.com/crm/2006/Query&quot; +
    &quot;\&quot; xsi:type=\&quot;q1:QueryExpression\&quot;&gt;&quot; +
    &quot; &lt;q1:EntityName&gt;role&lt;/q1:EntityName&gt;&quot; +
    &quot; &lt;q1:ColumnSet xsi:type=\&quot;q1:ColumnSet\&quot;&gt;&quot; +
    &quot; &lt;q1:Attributes&gt;&quot; +
    &quot; &lt;q1:Attribute&gt;name&lt;/q1:Attribute&gt;&quot; +
    &quot; &lt;/q1:Attributes&gt;&quot; +
    &quot; &lt;/q1:ColumnSet&gt;&quot; +
    &quot; &lt;q1:Distinct&gt;false&lt;/q1:Distinct&gt;&quot; +
    &quot; &lt;q1:LinkEntities&gt;&quot; +
    &quot; &lt;q1:LinkEntity&gt;&quot; +
    &quot; &lt;q1:LinkFromAttributeName&gt;roleid&lt;/q1:LinkFromAttributeName&gt;&quot; +
    &quot; &lt;q1:LinkFromEntityName&gt;role&lt;/q1:LinkFromEntityName&gt;&quot; +
    &quot; &lt;q1:LinkToEntityName&gt;systemuserroles&lt;/q1:LinkToEntityName&gt;&quot; +
    &quot; &lt;q1:LinkToAttributeName&gt;roleid&lt;/q1:LinkToAttributeName&gt;&quot; +
    &quot; &lt;q1:JoinOperator&gt;Inner&lt;/q1:JoinOperator&gt;&quot; +
    &quot; &lt;q1:LinkEntities&gt;&quot; +
    &quot; &lt;q1:LinkEntity&gt;&quot; +
    &quot; &lt;q1:LinkFromAttributeName&gt;systemuserid&lt;/q1:LinkFromAttributeName&gt;&quot; +
    &quot; &lt;q1:LinkFromEntityName&gt;systemuserroles&lt;/q1:LinkFromEntityName&gt;&quot; +
    &quot; &lt;q1:LinkToEntityName&gt;systemuser&lt;/q1:LinkToEntityName&gt;&quot; +
    &quot; &lt;q1:LinkToAttributeName&gt;systemuserid&lt;/q1:LinkToAttributeName&gt;&quot; +
    &quot; &lt;q1:JoinOperator&gt;Inner&lt;/q1:JoinOperator&gt;&quot; +
    &quot; &lt;q1:LinkCriteria&gt;&quot; +
    &quot; &lt;q1:FilterOperator&gt;And&lt;/q1:FilterOperator&gt;&quot; +
    &quot; &lt;q1:Conditions&gt;&quot; +
    &quot; &lt;q1:Condition&gt;&quot; +
    &quot; &lt;q1:AttributeName&gt;systemuserid&lt;/q1:AttributeName&gt;&quot; +
    &quot; &lt;q1:Operator&gt;EqualUserId&lt;/q1:Operator&gt;&quot; +
    &quot; &lt;/q1:Condition&gt;&quot; +
    &quot; &lt;/q1:Conditions&gt;&quot; +
    &quot; &lt;/q1:LinkCriteria&gt;&quot; +
    &quot; &lt;/q1:LinkEntity&gt;&quot; +
    &quot; &lt;/q1:LinkEntities&gt;&quot; +
    &quot; &lt;/q1:LinkEntity&gt;&quot; +
    &quot; &lt;/q1:LinkEntities&gt;&quot; +
    &quot; &lt;/query&gt;&quot; +
    &quot; &lt;/RetrieveMultiple&gt;&quot; +
    &quot; &lt;/soap:Body&gt;&quot; +
    &quot;&lt;/soap:Envelope&gt;&quot; +
    &quot;&quot;;  

  var xmlHttpRequest = new ActiveXObject(&quot;Msxml2.XMLHTTP&quot;);  

  xmlHttpRequest.Open(&quot;POST&quot;, &quot;/mscrmservices/2007/CrmService.asmx&quot;, false);
  xmlHttpRequest.setRequestHeader(&quot;SOAPAction&quot;,
    &quot; http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple&quot;);
  xmlHttpRequest.setRequestHeader(&quot;Content-Type&quot;, &quot;text/xml; charset=utf-8&quot;);
  xmlHttpRequest.setRequestHeader(&quot;Content-Length&quot;, xml.length);
  xmlHttpRequest.send(xml);  

  var resultXml = xmlHttpRequest.responseXML;
  return(resultXml);
}
</pre>
<p>The next method is a simple method for searching the XML result from GetCurrentUserRoles.  By passing it the roles you are looking for and the roles the user has, it will determine if there is a match.  The roles it is passed is separated by a pipe (|).</p>
<pre name="code" class="javascript">
function UserHasRole(roleNames, rolesXML)
{
  // split roleNames on pipe
  var matchon = roleNames.split('|');

  if(rolesXML != null)
  {
    //select the node text
    var roles = rolesXML.selectNodes("//BusinessEntity/q1:name");
    if(roles != null)
    {
      for( i = 0; i < roles.length; i++)
      {
        for (j = 0; j < matchon.length; j++)
	{
	  // If there is a match, return true, found
	  if (roles[i].text == matchon[j]) return true;
        }
      }
    }
  }
  //otherwise return false
  return false;
}
</pre>
<p>Finally, we can now implement helper methods to allow the hiding of a tab or a field if a user DOES NOT have a specific role.</p>
</pre>
<pre name="code" class="javascript">
function HideTabByRole(role, roles, tabnumber)
// Tab number starts on 0
{
  var tab = document.getElementById('tab'+tabnumber+'Tab');
  var usrRole = UserHasRole(role, roles);
  if(!usrRole)
  {
    tab.style.display = "none";
  }
}

function HideFieldByRole(role, roles, field,cfield,dfield)
{
  var usrRole = UserHasRole(role, roles);
  if(!usrRole)
  {
    field.style.visibility = 'hidden';
    field.style.position = 'absolute';
    cfield.style.visibility = 'hidden';
    cfield.style.position = 'absolute';
    dfield.visibility = 'hidden';
    dfield.style.position = 'absolute';
  }
}
</pre>
<p>To implement this process, all you have to do is put every piece of code in this article inside your onload with the following customized for your situation.</p>
<pre name="code" class="javascript">
var UserRoles = GetCurrentUserRoles();

HideTabByRole('Account Managers|SystemAdministrators', UserRoles, 3);

HideFieldByRole('Account Managers', UserRoles,
                crmForm.all.parentcustomerid,
                crmForm.all.parentcustomerid_c,
                crmForm.all.parentcustomerid_d);
</pre>
<p>If you have any questions of problems implementing this in your environment, please let me know, and I&#8217;ll do my best to help you get it working</p>
]]></content:encoded>
			<wfw:commentRss>http://mscrmblogger.com/2009/09/30/hide-fields-or-tabs-by-role-with-javascript/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
	</channel>
</rss>

