Try adding the following code to main Dynamics CRM home pages. In order to invoke the code automatically, add a button to the home page ribbon and attach the code as an 'Enable Rule' to that button.
Note: the following JavaScript code was verified on CRM 2011. It needs to be updated for later CRM versions (CRM 2013/2015/2016).
// ---- constants ---------
var ONLINE_URL = "https://organization.crm4.dynamics.com/XRMServices/2011/Organization.svc/web"
var ONLINE = "organization.crm4.dynamics.com";
var EMPTY_STRING = "";
verifyUser();
function verifyUser()
{
var url = _getServerUrl();
var online_env = (url == ONLINE_URL); //
var deployment = online_env ? "Online" : "On-Premise";
var OFFICE_ADDRESS = "<your office IP address>"; // i.e. "182.79.45.43";
var SYSTEM_ADMINISTRATOR = "System Administrator";
var SALES_MANAGER = "Sales Manager";
var NO_WORK_FROM_HOME = "No Work From Home";
// get user role
var admin = userRole(SYSTEM_ADMINISTRATOR);
var sales_mgr = userRole(SALES_MANAGER);
var noworkfromhome = userRole(NO_WORK_FROM_HOME);
// abort if not NO_WORK_FROM_HOME
if (!noworkfromhome)
return;
// ----------------------------------------
// safty
if (admin)
return;
// ----------------------------------------
// if not permitted - check ip
var ip = myIP();
// ignore if no ip
if (ip == null || ip == false)
return;
if (ip != OFFICE_ADDRESS)
{
parent.window.location = "http://www.google.com/";
alert("You are not allowed to use Microsoft Dynamics CRM from this location.");
}
} // verifyUser
function myIP()
{
var url = _getServerUrl();
if (window.XMLHttpRequest)
xmlhttp = new XMLHttpRequest();
else
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
try
{
if (online_env)
xmlhttp.open("GET", "https://<ssl service which returns user ip address>", false); // online
else
xmlhttp.open("GET", "http://api.hostip.info/get_html.php", false); // on-premise
xmlhttp.send();
hostipInfo = xmlhttp.responseText.split("\n");
if (online_env)
{
var hostIpAddress = hostipInfo.toString();
hostIpAddress = hostIpAddress.replace(/^\s+|\s+$/g, '');
return hostIpAddress;
}
else if (hostipInfo.length > 0)
{
for (i = 0; hostipInfo.length >= i; i++)
{
ipAddress = hostipInfo[i].split(":");
if (ipAddress[0] == "IP")
{
ipAddress[1] = ipAddress[1].replace(/^\s+|\s+$/g, '');
return ipAddress[1];
}
} // for
}
}
catch (e)
{
var error_message = e.message.replace(/^\s+|\s+$/g, '');
alert(error_message);
if (error_message == "Access is denied.")
{
alert("Your browser settings do not allow identifying your location.\n\n" +
"Use Tools\\Windows Options 'Security' tab.\n\n" +
"1. Verify your default security is 'Trusted Sites'\n\n" +
"2. Click the 'Custom Level' button to change\n" +
" the following settings:\n" +
" - Set 'Access data sources across domains' to 'Enabled'.");
return true;
}
}
return false;
} // verifyUser
function userRole(roleId)
{
var debug = false;
var id = roleId
if (debug) alert("Role Id '" + id + "'");
var currentUserRoles = Xrm.Page.context.getUserRoles();
for (var i = 0; i < currentUserRoles.length; i++)
{
var userRole = currentUserRoles[i];
if (GuidsAreEqual(userRole, id))
{
return true;
} //if
} //for
return false;
} // userRole
function GuidsAreEqual(guid1, guid2)
{
var isEqual = false;
if (guid1 == null || guid2 == null)
{
isEqual = false;
}
else
{
isEqual = guid1.replace(/[{}]/g, EMPTY_STRING).toLowerCase() == guid2.replace(/[{}]/g, EMPTY_STRING).toLowerCase();
}
return isEqual;
} // GuidsAreEqual
function _getServerUrl()
{
///<summary>
/// Returns the URL for the SOAP endpoint using the context information available in the form
/// or HTML Web resource.
///</summary>
var OrgServicePath = "/XRMServices/2011/Organization.svc/web";
var serverUrl = "";
if (typeof GetGlobalContext == "function")
{
var context = GetGlobalContext();
serverUrl = context.getServerUrl();
}
else
{
if (typeof Xrm.Page.context == "object")
{
serverUrl = Xrm.Page.context.getServerUrl();
}
else
{ throw new Error("Unable to access the server URL"); }
}
if (serverUrl.match(/\/$/))
{
serverUrl = serverUrl.substring(0, serverUrl.length - 1);
}
return serverUrl + OrgServicePath;
} // _getServerUrl