public static bool GetIsExecutionAllowed(IPluginContextBase pluginCtx) { var executeHandler = true; // ReSharper disable once SwitchStatementMissingSomeCases switch (pluginCtx.MessageName) { case MessageName.Create: case MessageName.Update: var target = pluginCtx.TargetEntityAsBaseEntity; var serviceOperationDateIsChanged = target.GetIsFieldSpecified(ServiceOperationDateFieldName); if (serviceOperationDateIsChanged) { if (pluginCtx.GetIsAdminUser() == false) { throw new PluginBusinessLogicExceptionWithSimpleLogging("Only System Administrator may initiate Service Operation."); } executeHandler = target.Attributes[ServiceOperationDateFieldName] == null; } break; case MessageName.Delete: var isServiceDeleteAction = false; var parentExecCtx = pluginCtx.ParentExecContext; if (parentExecCtx != null) { var grandParentExecCtx = parentExecCtx.ParentContext; if (grandParentExecCtx != null) { isServiceDeleteAction = ServiceDeleteActionName.Equals(grandParentExecCtx.MessageName); } if (!isServiceDeleteAction) { isServiceDeleteAction = ServiceDeleteActionName.Equals(parentExecCtx.MessageName); } } executeHandler = !isServiceDeleteAction; break; } return(executeHandler); }
/// <summary> /// Gets if the initiating user is a system administrator. /// </summary> /// <param name="pluginCtx">The plugin context.</param> /// <returns></returns> public static bool GetIsAdminUser(this IPluginContextBase pluginCtx) { return(pluginCtx.GetIsAdminUser(pluginCtx.InitiatingUserId)); }