Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. public class CheckIfOwnedByPool : BasePlugin
  2. {
  3. public CheckIfOwnedByPool(string unsecure, string secure)
  4. : base(unsecure, secure)
  5. {
  6.  
  7. }
  8.  
  9.  
  10. public override void ExecutePluginLogic(XrmObjects xrmObjects)
  11. {
  12.  
  13. if (xrmObjects.PluginContext.MessageName.ToLower() == "update" && xrmObjects.PluginContext.PreEntityImages.Contains("entity"))
  14. {
  15.  
  16. Guid PoolId = Guid.Empty;
  17. List<Guid> allowedRoles = new List<Guid>();
  18. var entity = xrmObjects.PluginContext.PreEntityImages["entity"];
  19.  
  20. //retrieve id of "POOL" team and allowed roles GUIDs from plugin step XML config. GUID varies from environment to environment...
  21. try
  22. {
  23. PoolId = new Guid(GetAllNodes("ownerconfig")[0].Attributes["value"].InnerText);
  24. GetAllNodes("roles")[0].Attributes["value"].InnerText.Split(';').ToList().ForEach(x => allowedRoles.Add(new Guid(x)));
  25. }
  26. catch (Exception ex)
  27. {
  28. throw new InvalidPluginExecutionException(string.Format("Error deserializing plugin step XML config: {0}", ex.Message));
  29. }
  30.  
  31. if (PoolId == Guid.Empty || allowedRoles.Count == 0)
  32. return;
  33.  
  34. //if entity is owned by team called "POOL"
  35. if (((EntityReference)entity["ownerid"]).Id == PoolId)
  36. {
  37. using (XrmServiceContext ctx = new XrmServiceContext(xrmObjects.Service))
  38. {
  39. //retrieve current user roles
  40. var userRoles = ctx.SystemUserRolesSet.Where(x => x.SystemUserId == xrmObjects.PluginContext.InitiatingUserId).ToList();
  41.  
  42. //check if current user has one of required roles
  43. if (!userRoles.Any(x => allowedRoles.Any(y => y == x.RoleId.Value)))
  44. throw new InvalidPluginExecutionException("You do not have access to modify this record. Please see with your supervisor");
  45.  
  46. }
  47.  
  48. }
  49.  
  50. }
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement