internal async Task <T> DoUpdateAsync(T obj, bool isSync) { Server.DataPortalResult result = null; Server.DataPortalContext dpContext = null; DataPortalOperations operation = DataPortalOperations.Update; Type objectType = obj.GetType(); try { DataPortal.OnDataPortalInitInvoke(null); Csla.Server.DataPortalMethodInfo method = null; var factoryInfo = Csla.Server.ObjectFactoryAttribute.GetObjectFactoryAttribute(objectType); if (factoryInfo != null) { var factoryType = Csla.Server.FactoryDataPortal.FactoryLoader.GetFactoryType(factoryInfo.FactoryTypeName); if (obj is Core.ICommandObject) { if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.EditObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "execute", objectType.Name)); } if (factoryType != null) { method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, factoryInfo.ExecuteMethodName, new object[] { obj }); } } else { var bbase = obj as Core.BusinessBase; if (bbase != null) { if (bbase.IsDeleted) { if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.DeleteObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "delete", objectType.Name)); } if (factoryType != null) { method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, factoryInfo.DeleteMethodName, new object[] { obj }); } } // must check the same authorization rules as for DataPortal_XYZ methods else if (bbase.IsNew) { if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.CreateObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "create", objectType.Name)); } if (factoryType != null) { method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, factoryInfo.UpdateMethodName, new object[] { obj }); } } else { if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.EditObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "save", objectType.Name)); } if (factoryType != null) { method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, factoryInfo.UpdateMethodName, new object[] { obj }); } } } else { if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.EditObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "save", objectType.Name)); } if (factoryType != null) { method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, factoryInfo.UpdateMethodName, new object[] { obj }); } } } if (method == null) { method = new Csla.Server.DataPortalMethodInfo(); } } else { string methodName; if (obj is Core.ICommandObject) { methodName = "DataPortal_Execute"; operation = DataPortalOperations.Execute; if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.EditObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "execute", objectType.Name)); } } else { var bbase = obj as Core.BusinessBase; if (bbase != null) { if (bbase.IsDeleted) { methodName = "DataPortal_DeleteSelf"; if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.DeleteObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "delete", objectType.Name)); } } else if (bbase.IsNew) { methodName = "DataPortal_Insert"; if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.CreateObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "create", objectType.Name)); } } else { methodName = "DataPortal_Update"; if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.EditObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "save", objectType.Name)); } } } else { methodName = "DataPortal_Update"; if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.EditObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "save", objectType.Name)); } } } method = Server.DataPortalMethodCache.GetMethodInfo(obj.GetType(), methodName); } DataPortalClient.IDataPortalProxy proxy; proxy = GetDataPortalProxy(objectType, method.RunLocal); dpContext = new Server.DataPortalContext(GetPrincipal(), proxy.IsServerRemote); DataPortal.OnDataPortalInvoke(new DataPortalEventArgs(dpContext, objectType, obj, operation)); try { if (!proxy.IsServerRemote && ApplicationContext.AutoCloneOnUpdate) { // when using local data portal, automatically // clone original object before saving ICloneable cloneable = obj as ICloneable; if (cloneable != null) { obj = (T)cloneable.Clone(); } } result = await proxy.Update(obj, dpContext, isSync); } catch (AggregateException ex) { if (ex.InnerExceptions.Count > 0) { var dpe = ex.InnerExceptions[0] as Server.DataPortalException; if (dpe != null) { HandleUpdateDataPortalException(dpe, isSync, proxy); } } throw new DataPortalException( string.Format("DataPortal.Update {0}", Resources.Failed), ex, null); } catch (Server.DataPortalException ex) { HandleUpdateDataPortalException(ex, isSync, proxy); } GlobalContext = result.GlobalContext; if (proxy.IsServerRemote && isSync) { ApplicationContext.ContextManager.SetGlobalContext(GlobalContext); } DataPortal.OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext, objectType, obj, operation)); } catch (Exception ex) { DataPortal.OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext, objectType, obj, operation, ex)); throw; } return((T)result.ReturnObject); }
internal async Task <T> DoUpdateAsync(T obj, bool isSync) { Server.DataPortalResult result = null; Server.DataPortalContext dpContext = null; Type objectType = obj.GetType(); try { DataPortalClient.IDataPortalProxy proxy = null; var factoryInfo = Csla.Server.ObjectFactoryAttribute.GetObjectFactoryAttribute(objectType); if (factoryInfo != null) { Csla.Server.DataPortalMethodInfo method = null; var factoryLoader = ApplicationContext.CurrentServiceProvider.GetService(typeof(Server.IObjectFactoryLoader)) as Server.IObjectFactoryLoader; var factoryType = factoryLoader?.GetFactoryType(factoryInfo.FactoryTypeName); if (obj is Core.ICommandObject) { if (!Csla.Rules.BusinessRules.HasPermission(ApplicationContext, Rules.AuthorizationActions.EditObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "execute", objectType.Name)); } if (factoryType != null) { method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, factoryInfo.ExecuteMethodName, new object[] { obj }); } } else { if (obj is Core.BusinessBase bbase) { if (bbase.IsDeleted) { if (!Csla.Rules.BusinessRules.HasPermission(ApplicationContext, Rules.AuthorizationActions.DeleteObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "delete", objectType.Name)); } if (factoryType != null) { method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, factoryInfo.DeleteMethodName, new object[] { obj }); } } // must check the same authorization rules as for DataPortal_XYZ methods else if (bbase.IsNew) { if (!Csla.Rules.BusinessRules.HasPermission(ApplicationContext, Rules.AuthorizationActions.CreateObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "create", objectType.Name)); } if (factoryType != null) { method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, factoryInfo.UpdateMethodName, new object[] { obj }); } } else { if (!Csla.Rules.BusinessRules.HasPermission(ApplicationContext, Rules.AuthorizationActions.EditObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "save", objectType.Name)); } if (factoryType != null) { method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, factoryInfo.UpdateMethodName, new object[] { obj }); } } } else { if (!Csla.Rules.BusinessRules.HasPermission(ApplicationContext, Rules.AuthorizationActions.EditObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "save", objectType.Name)); } if (factoryType != null) { method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, factoryInfo.UpdateMethodName, new object[] { obj }); } } } if (method == null) { method = new Csla.Server.DataPortalMethodInfo(); } proxy = GetDataPortalProxy(method.RunLocal); } else { Reflection.ServiceProviderMethodInfo method; var criteria = Server.DataPortal.GetCriteriaArray(Server.EmptyCriteria.Instance); if (obj is Core.ICommandObject) { if (!Csla.Rules.BusinessRules.HasPermission(ApplicationContext, Rules.AuthorizationActions.EditObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "execute", objectType.Name)); } method = ServiceProviderMethodCaller.FindDataPortalMethod <ExecuteAttribute>(objectType, criteria, false); } else { if (obj is Core.BusinessBase bbase) { if (bbase.IsDeleted) { if (!Csla.Rules.BusinessRules.HasPermission(ApplicationContext, Rules.AuthorizationActions.DeleteObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "delete", objectType.Name)); } method = ServiceProviderMethodCaller.FindDataPortalMethod <DeleteSelfAttribute>(objectType, criteria, false); } else if (bbase.IsNew) { if (!Csla.Rules.BusinessRules.HasPermission(ApplicationContext, Rules.AuthorizationActions.CreateObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "create", objectType.Name)); } method = ServiceProviderMethodCaller.FindDataPortalMethod <InsertAttribute>(objectType, criteria, false); } else { if (!Csla.Rules.BusinessRules.HasPermission(ApplicationContext, Rules.AuthorizationActions.EditObject, obj)) { throw new Csla.Security.SecurityException(string.Format(Resources.UserNotAuthorizedException, "save", objectType.Name)); } method = ServiceProviderMethodCaller.FindDataPortalMethod <UpdateAttribute>(objectType, criteria, false); } } else { method = ServiceProviderMethodCaller.FindDataPortalMethod <UpdateAttribute>(objectType, criteria, false); } } proxy = GetDataPortalProxy(method); } dpContext = new Server.DataPortalContext(ApplicationContext, GetPrincipal(), proxy.IsServerRemote); try { if (!proxy.IsServerRemote && ApplicationContext.AutoCloneOnUpdate) { // when using local data portal, automatically // clone original object before saving if (obj is ICloneable cloneable) { obj = (T)cloneable.Clone(); } } result = await proxy.Update(obj, dpContext, isSync); } catch (AggregateException ex) { if (ex.InnerExceptions.Count > 0) { if (ex.InnerExceptions[0] is Server.DataPortalException dpe) { HandleUpdateDataPortalException(dpe, isSync, proxy); } } throw new DataPortalException( string.Format("DataPortal.Update {0}", Resources.Failed), ex, null); } catch (Server.DataPortalException ex) { HandleUpdateDataPortalException(ex, isSync, proxy); } } catch { throw; } return((T)result.ReturnObject); }