Exemplo n.º 1
0
 private static DataPortalClient.IDataPortalProxy GetDataPortalProxy(bool forceLocal)
 {
     if (forceLocal)
     {
         if (_localPortal == null)
         {
             _localPortal = new DataPortalClient.LocalProxy();
         }
         return(_localPortal);
     }
     else
     {
         if (_portal == null)
         {
             string proxyTypeName = ApplicationContext.DataPortalProxy;
             if (proxyTypeName == "Local")
             {
                 _portal = new DataPortalClient.LocalProxy();
             }
             else
             {
                 string typeName =
                     proxyTypeName.Substring(0, proxyTypeName.IndexOf(",")).Trim();
                 string assemblyName =
                     proxyTypeName.Substring(proxyTypeName.IndexOf(",") + 1).Trim();
                 _portal = (DataPortalClient.IDataPortalProxy)
                           Activator.CreateInstance(assemblyName, typeName).Unwrap();
             }
         }
         return(_portal);
     }
 }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Creates an instance of the type
 /// </summary>
 /// <param name="applicationContext">ApplicationContext</param>
 /// <param name="proxy"></param>
 public DataPortal(ApplicationContext applicationContext, DataPortalClient.IDataPortalProxy proxy)
 {
     ApplicationContext = applicationContext;
     DataPortalProxy    = proxy;
 }