public class PreReplacePrivilege:IPlugin { #region IPlugin Members
public void Execute(IPluginExecutionContext context) { if (context.MessageName == "ReplacePrivileges") { if (context.InputParameters.Contains("RoleId")) { context.SharedVariables["PreRolePrivilagesValues"] = Utilities.GetRolePrivilages(context.InputParameters.Properties["RoleId"].ToString(), context.CreateCrmService(true));
context.SharedVariables["RoleId"] = context.InputParameters.Properties["RoleId"]; } } }
#endregion }
static class Utilities {
/// <summary> /// Get privilages for Roles. /// </summary> public static Dictionary<string, string> GetRolePrivilages(string roleId, ICrmService crmService) { Microsoft.Crm.Sdk.Query.AllColumns cols = new Microsoft.Crm.Sdk.Query.AllColumns();
Dictionary<Guid, string> privileges = GetAllPrivilages(crmService); //Get list of all privileges
RetrieveRolePrivilegesRoleRequest request = new RetrieveRolePrivilegesRoleRequest(); request.RoleId = new Guid(roleId);
RetrieveRolePrivilegesRoleResponse response = (RetrieveRolePrivilegesRoleResponse)crmService.Execute(request);
Dictionary<string, string> rolePrigivleges = ResolvePrivilageNames(privileges, response.RolePrivileges);
return rolePrigivleges; } /// <summary> /// Get all privilages on system /// </summary> /// <param name="crmService"></param> /// <returns></returns> private static Dictionary<Guid, string> GetAllPrivilages(ICrmService crmService) { Dictionary<Guid, string> privilages = new Dictionary<Guid, string>(); RetrievePrivilegeSetRequest requestp = new RetrievePrivilegeSetRequest(); requestp.ReturnDynamicEntities = true; RetrievePrivilegeSetResponse responsep = (RetrievePrivilegeSetResponse)crmService.Execute(requestp);
foreach (DynamicEntity p in responsep.BusinessEntityCollection.BusinessEntities) { privilages.Add( ((Key)p["privilegeid"]).Value,p["name"].ToString());
} return privilages; }
/// <summary> /// Resolving Privileges Names /// </summary> /// <param name="rolePrivilegesList"></param> /// <param name="crmService"></param> /// <returns></returns> public static Dictionary<string, string> ResolvePrivilageNames(RolePrivilege[] rolePrivilegesList, ICrmServicecrmService) { Dictionary<Guid, string> privileges = GetAllPrivilages(crmService); Dictionary<string, string> rolePrigivleges = new Dictionary<string, string>(); foreach (RolePrivilege r in rolePrivilegesList) { rolePrigivleges.Add(privileges[r.PrivilegeId], r.Depth.ToString()); }
foreach (KeyValuePair<Guid, string> pair in privileges) { if (!rolePrigivleges.ContainsKey(pair.Value)) rolePrigivleges.Add(pair.Value, "None"); } return rolePrigivleges; }
public static string GetPropertyValue(object property) { try { if (property.GetType() == typeof(CrmBoolean)) return ((CrmBoolean)property).Value.ToString(); else if (property.GetType() == typeof(CrmDateTime)) return ((CrmDateTime)property).UserTime.ToString(); else if (property.GetType() == typeof(Owner)) return ((Owner)property).Value.ToString(); else if (property.GetType() == typeof(Lookup)) return ((Lookup)property).Value.ToString(); else if (property.GetType() == typeof(Picklist)) return ((Picklist)property).Value.ToString(); else if (property.GetType() == typeof(StringProperty)) return ((StringProperty)property).Value.ToString(); else if (property.GetType() == typeof(LookupProperty)) return ((LookupProperty)property).Value.name.ToString(); else if (property.GetType() == typeof(OwnerProperty)) return ((OwnerProperty)property).Value.name.ToString(); else if (property.GetType() == typeof(PicklistProperty)) return ((PicklistProperty)property).Value.name.ToString(); else if (property.GetType() == typeof(CrmDateTimeProperty)) return ((CrmDateTimeProperty)property).Value.UserTime.ToString(); else if (property.GetType() == typeof(KeyProperty)) return ((KeyProperty)property).Value.Value.ToString(); else if (property.GetType() == typeof(CrmBooleanProperty)) return ((CrmBooleanProperty)property).Value.Value.ToString(); else if (property.GetType() == typeof(CrmDecimalProperty)) return ((CrmDecimalProperty)property).Value.Value.ToString(); else if (property.GetType() == typeof(CrmFloatProperty)) return ((CrmFloatProperty)property).Value.Value.ToString(); else if (property.GetType() == typeof(CrmMoneyProperty)) return ((CrmMoneyProperty)property).Value.Value.ToString(); else if (property.GetType() == typeof(CrmNumberProperty)) return ((CrmNumberProperty)property).Value.Value.ToString(); else if (property.GetType() == typeof(CustomerProperty)) return ((CustomerProperty)property).Value.name.ToString(); else if (property.GetType() == typeof(CustomerProperty)) return ((CustomerProperty)property).Value.name.ToString(); else if (property.GetType() == typeof(StatusProperty)) return ((StatusProperty)property).Value.name.ToString(); else if (property.GetType() == typeof(StateProperty)) return ((StateProperty)property).Value.ToString(); else if (property.GetType() == typeof(UniqueIdentifierProperty)) return ((UniqueIdentifierProperty)property).Value.Value.ToString(); else if (property == null) return ""; else return property.ToString(); } catch { return ""; }
} public static AuditDifferenceCollection AddDifferences(IMetadataService metaService, string entityName,DynamicEntity preImage, DynamicEntity postImage) { AuditDifferenceCollection col = new AuditDifferenceCollection();
System.Collections.Hashtable attributesMetadata = MetadataHelper.GetEntityAttributeMetadata(metaService, entityName, Microsoft.Crm.Sdk.Metadata.EntityItems.IncludeAttributes); //For any Message that has a Pre and/or Post Image. I will add the values to the Audit Differences. foreach (Property prop in preImage.Properties) { if(prop.GetType()!= typeof(KeyProperty) && prop.Name!="modifiedby" && prop.Name != "modifiedon" && prop.Name != "createdby" && prop.Name != "createdon") { col.Add(new AuditDifference(prop.Name,attributesMetadata[prop.Name].ToString(),Utilities.GetPropertyValue(prop), "")); } } foreach (Property prop in postImage.Properties) { if(prop.GetType()!= typeof(KeyProperty) && prop.Name!="modifiedby" && prop.Name != "modifiedon" && prop.Name != "createdby" && prop.Name != "createdon") { if (col.Contains(prop.Name)) { AuditDifference diff = col[prop.Name]; diff.CurrentValue = Utilities.GetPropertyValue(prop); col[prop.Name] = diff; } else { col.Add(new AuditDifference(prop.Name, attributesMetadata[prop.Name].ToString(),"",Utilities.GetPropertyValue(prop)));
} } }
return col; } public static AuditDifferenceCollection AddDifferences(IMetadataService metaService, string entityName,Dictionary<string, string> preValues, Dictionary<string, string> postValues) { AuditDifferenceCollection col = new AuditDifferenceCollection(); if (preValues != null && preValues.Count > 0) { foreach (KeyValuePair<string, string> entry in preValues) { col.Add(new AuditDifference(entry.Key + ":" + entry.Value, entry.Value, "")); } }
if (postValues != null & postValues.Count > 0) { foreach (KeyValuePair<string, string> entry in postValues) { if (col.Contains(entry.Key + ":" + entry.Value)) { AuditDifference diff = col[entry.Key + ":" + entry.Value]; diff.CurrentValue = entry.Value; col[entry.Key + ":" + entry.Value] = diff; } else { col.Add(new AuditDifference(entry.Key + ":" + entry.Value, "", entry.Value));
} } }
return col; } public static string AddAttributeToList(string value, string list) { if (list.IndexOf(value) == -1) { if (list == "") list = value; else |