Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CheckIfOwnedByPool : BasePlugin
- {
- public CheckIfOwnedByPool(string unsecure, string secure)
- : base(unsecure, secure)
- {
- }
- public override void ExecutePluginLogic(XrmObjects xrmObjects)
- {
- if (xrmObjects.PluginContext.MessageName.ToLower() == "update" && xrmObjects.PluginContext.PreEntityImages.Contains("entity"))
- {
- Guid PoolId = Guid.Empty;
- List<Guid> allowedRoles = new List<Guid>();
- var entity = xrmObjects.PluginContext.PreEntityImages["entity"];
- //retrieve id of "POOL" team and allowed roles GUIDs from plugin step XML config. GUID varies from environment to environment...
- try
- {
- PoolId = new Guid(GetAllNodes("ownerconfig")[0].Attributes["value"].InnerText);
- GetAllNodes("roles")[0].Attributes["value"].InnerText.Split(';').ToList().ForEach(x => allowedRoles.Add(new Guid(x)));
- }
- catch (Exception ex)
- {
- throw new InvalidPluginExecutionException(string.Format("Error deserializing plugin step XML config: {0}", ex.Message));
- }
- if (PoolId == Guid.Empty || allowedRoles.Count == 0)
- return;
- //if entity is owned by team called "POOL"
- if (((EntityReference)entity["ownerid"]).Id == PoolId)
- {
- using (XrmServiceContext ctx = new XrmServiceContext(xrmObjects.Service))
- {
- //retrieve current user roles
- var userRoles = ctx.SystemUserRolesSet.Where(x => x.SystemUserId == xrmObjects.PluginContext.InitiatingUserId).ToList();
- //check if current user has one of required roles
- if (!userRoles.Any(x => allowedRoles.Any(y => y == x.RoleId.Value)))
- throw new InvalidPluginExecutionException("You do not have access to modify this record. Please see with your supervisor");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement