public static void Start(IApplicationBuilder app)
        {
            TypeHelpServer.Start(app);
            SignumControllerFactory.RegisterArea(MethodInfo.GetCurrentMethod());
            ReflectionServer.OverrideIsNamespaceAllowed.Add(typeof(ExchangeVersion).Namespace !, () => TypeAuthLogic.GetAllowed(typeof(EmailSenderConfigurationEntity)).MaxUI() > TypeAllowedBasic.None);
            ReflectionServer.OverrideIsNamespaceAllowed.Add(typeof(SmtpDeliveryMethod).Namespace !, () => TypeAuthLogic.GetAllowed(typeof(EmailSenderConfigurationEntity)).MaxUI() > TypeAllowedBasic.None);


            TemplatingServer.Start(app);

            EntityJsonConverter.AfterDeserilization.Register((EmailTemplateEntity et) =>
            {
                if (et.Query != null)
                {
                    var qd = QueryLogic.Queries.QueryDescription(et.Query.ToQueryName());
                    et.ParseData(qd);
                }
            });

            QueryDescriptionTS.AddExtension += qd =>
            {
                object type = QueryLogic.ToQueryName(qd.queryKey);
                if (Schema.Current.IsAllowed(typeof(EmailTemplateEntity), true) == null)
                {
                    var templates = EmailTemplateLogic.GetApplicableEmailTemplates(type, null, EmailTemplateVisibleOn.Query);

                    if (templates.HasItems())
                    {
                        qd.Extension.Add("emailTemplates", templates);
                    }
                }
            };


            if (Schema.Current.Tables.ContainsKey(typeof(EmailSenderConfigurationEntity)))
            {
                var piPassword = ReflectionTools.GetPropertyInfo((SmtpNetworkDeliveryEmbedded e) => e.Password);
                var pcs        = PropertyConverter.GetPropertyConverters(typeof(SmtpNetworkDeliveryEmbedded));
                pcs.GetOrThrow("password").CustomWriteJsonProperty = ctx => { };
                pcs.Add("newPassword", new PropertyConverter
                {
                    AvoidValidate           = true,
                    CustomWriteJsonProperty = ctx => { },
                    CustomReadJsonProperty  = ctx =>
                    {
                        EntityJsonConverter.AssertCanWrite(ctx.ParentPropertyRoute.Add(piPassword));

                        var password = (string)ctx.JsonReader.Value !;

                        ((SmtpNetworkDeliveryEmbedded)ctx.Entity).Password = EmailSenderConfigurationLogic.EncryptPassword(password);
                    }
                });
Exemple #2
0
        public static void Start(HttpConfiguration config, Func <AuthTokenConfigurationEmbedded> tokenConfig, string hashableEncryptionKey)
        {
            SignumControllerFactory.RegisterArea(MethodInfo.GetCurrentMethod());

            AuthTokenServer.Start(tokenConfig, hashableEncryptionKey);

            ReflectionServer.GetContext = () => new
            {
                Culture = ReflectionServer.GetCurrentValidCulture(),
                Role    = UserEntity.Current == null ? null : RoleEntity.Current,
            };

            AuthLogic.OnRulesChanged += () => ReflectionServer.cache.Clear();

            if (TypeAuthLogic.IsStarted)
            {
                ReflectionServer.AddTypeExtension += (ti, t) =>
                {
                    if (typeof(Entity).IsAssignableFrom(t))
                    {
                        ti.Extension.Add("typeAllowed", UserEntity.Current == null ? TypeAllowedBasic.None : TypeAuthLogic.GetAllowed(t).MaxUI());
                    }
                };
            }

            if (QueryAuthLogic.IsStarted)
            {
                ReflectionServer.AddTypeExtension += (ti, t) =>
                {
                    if (ti.QueryDefined)
                    {
                        ti.Extension.Add("queryAllowed", UserEntity.Current == null ? QueryAllowed.None : QueryAuthLogic.GetQueryAllowed(t));
                    }
                };

                ReflectionServer.AddFieldInfoExtension += (mi, fi) =>
                {
                    if (fi.DeclaringType.Name.EndsWith("Query"))
                    {
                        mi.Extension.Add("queryAllowed", UserEntity.Current == null ? QueryAllowed.None : QueryAuthLogic.GetQueryAllowed(fi.GetValue(null)));
                    }
                };
            }

            if (PropertyAuthLogic.IsStarted)
            {
                ReflectionServer.AddPropertyRouteExtension += (mi, pr) =>
                {
                    mi.Extension.Add("propertyAllowed", UserEntity.Current == null ? PropertyAllowed.None : pr.GetPropertyAllowed());
                };
            }

            if (OperationAuthLogic.IsStarted)
            {
                ReflectionServer.AddFieldInfoExtension += (mi, fi) =>
                {
                    if (fi.DeclaringType.Name.EndsWith("Operation"))
                    {
                        if (fi.GetValue(null) is IOperationSymbolContainer container)
                        {
                            mi.Extension.Add("operationAllowed",
                                             UserEntity.Current == null ? false
                                    : OperationAuthLogic.GetOperationAllowed(container.Symbol, inUserInterface: true));
                        }
                    }
                };
            }

            if (PermissionAuthLogic.IsStarted)
            {
                ReflectionServer.AddFieldInfoExtension += (mi, fi) =>
                {
                    if (fi.FieldType == typeof(PermissionSymbol))
                    {
                        mi.Extension.Add("permissionAllowed",
                                         UserEntity.Current == null
                                ? false
                                : PermissionAuthLogic.IsAuthorized((PermissionSymbol)fi.GetValue(null)));
                    }
                };
            }


            var piPasswordHash = ReflectionTools.GetPropertyInfo((UserEntity e) => e.PasswordHash);
            var pcs            = PropertyConverter.GetPropertyConverters(typeof(UserEntity));

            pcs.GetOrThrow("passwordHash").CustomWriteJsonProperty = ctx => { };
            pcs.Add("newPassword", new PropertyConverter
            {
                AvoidValidate           = true,
                CustomWriteJsonProperty = ctx => { },
                CustomReadJsonProperty  = ctx =>
                {
                    EntityJsonConverter.AssertCanWrite(ctx.ParentPropertyRoute.Add(piPasswordHash));

                    var password = (string)ctx.JsonReader.Value;

                    var error = UserEntity.OnValidatePassword(password);
                    if (error != null)
                    {
                        throw new ApplicationException(error);
                    }

                    ((UserEntity)ctx.Entity).PasswordHash = Security.EncodePassword(password);
                }
            });

            if (TypeAuthLogic.IsStarted)
            {
                Omnibox.OmniboxServer.IsNavigable += type => TypeAuthLogic.GetAllowed(type).MaxUI() >= TypeAllowedBasic.Read;
            }

            SchemaMap.GetColorProviders += GetMapColors;
        }
Exemple #3
0
        public static void Start(IApplicationBuilder app, Func <AuthTokenConfigurationEmbedded> tokenConfig, string hashableEncryptionKey)
        {
            SignumControllerFactory.RegisterArea(MethodInfo.GetCurrentMethod());

            AuthTokenServer.Start(tokenConfig, hashableEncryptionKey);

            ReflectionServer.GetContext = () => new
            {
                Culture = ReflectionServer.GetCurrentValidCulture(),
                Role    = UserEntity.Current == null ? null : RoleEntity.Current,
            };

            AuthLogic.OnRulesChanged += () => ReflectionServer.cache.Clear();

            if (TypeAuthLogic.IsStarted)
            {
                ReflectionServer.AddTypeExtension += (ti, t) =>
                {
                    if (typeof(Entity).IsAssignableFrom(t))
                    {
                        var ta = UserEntity.Current != null?TypeAuthLogic.GetAllowed(t) : null;

                        ti.Extension.Add("maxTypeAllowed", ta == null ? TypeAllowedBasic.None : ta.MaxUI());
                        ti.Extension.Add("minTypeAllowed", ta == null ? TypeAllowedBasic.None : ta.MinUI());
                        ti.RequiresEntityPack |= ta != null && ta.Conditions.Any();
                    }
                };


                EntityPackTS.AddExtension += ep =>
                {
                    var typeAllowed =
                        UserEntity.Current == null ? TypeAllowedBasic.None :
                        ep.entity.IsNew ? TypeAuthLogic.GetAllowed(ep.entity.GetType()).MaxUI() :
                        TypeAuthLogic.IsAllowedFor(ep.entity, TypeAllowedBasic.Write, true) ? TypeAllowedBasic.Write :
                        TypeAuthLogic.IsAllowedFor(ep.entity, TypeAllowedBasic.Read, true) ? TypeAllowedBasic.Read :
                        TypeAllowedBasic.None;

                    ep.extension.Add("typeAllowed", typeAllowed);
                };

                OperationController.AnyReadonly += (Lite <Entity>[] lites) =>
                {
                    return(lites.GroupBy(ap => ap.EntityType).Any(gr =>
                    {
                        var ta = TypeAuthLogic.GetAllowed(gr.Key);

                        if (ta.Min(inUserInterface: true) == TypeAllowedBasic.Write)
                        {
                            return false;
                        }

                        if (ta.Max(inUserInterface: true) <= TypeAllowedBasic.Read)
                        {
                            return true;
                        }

                        return giCountReadonly.GetInvoker(gr.Key)() > 0;
                    }));
                };
            }

            if (QueryAuthLogic.IsStarted)
            {
                ReflectionServer.AddTypeExtension += (ti, t) =>
                {
                    if (ti.QueryDefined)
                    {
                        ti.Extension.Add("queryAllowed", UserEntity.Current == null ? QueryAllowed.None : QueryAuthLogic.GetQueryAllowed(t));
                    }
                };

                ReflectionServer.AddFieldInfoExtension += (mi, fi) =>
                {
                    if (fi.DeclaringType !.Name.EndsWith("Query"))
                    {
                        mi.Extension.Add("queryAllowed", UserEntity.Current == null ? QueryAllowed.None : QueryAuthLogic.GetQueryAllowed(fi.GetValue(null) !));
                    }
                };
            }

            if (PropertyAuthLogic.IsStarted)
            {
                ReflectionServer.AddPropertyRouteExtension += (mi, pr) =>
                {
                    mi.Extension.Add("propertyAllowed", UserEntity.Current == null ? PropertyAllowed.None : pr.GetPropertyAllowed());
                };
            }

            if (OperationAuthLogic.IsStarted)
            {
                ReflectionServer.AddOperationExtension += (oits, oi, type) =>
                {
                    oits.Extension.Add("operationAllowed",
                                       UserEntity.Current == null ? false :
                                       OperationAuthLogic.GetOperationAllowed(oi.OperationSymbol, type, inUserInterface: true));
                };
            }

            if (PermissionAuthLogic.IsStarted)
            {
                ReflectionServer.AddFieldInfoExtension += (mi, fi) =>
                {
                    if (fi.FieldType == typeof(PermissionSymbol))
                    {
                        mi.Extension.Add("permissionAllowed",
                                         UserEntity.Current == null ? false :
                                         PermissionAuthLogic.IsAuthorized((PermissionSymbol)fi.GetValue(null) !));
                    }
                };
            }


            var piPasswordHash = ReflectionTools.GetPropertyInfo((UserEntity e) => e.PasswordHash);
            var pcs            = PropertyConverter.GetPropertyConverters(typeof(UserEntity));

            pcs.GetOrThrow("passwordHash").CustomWriteJsonProperty = ctx => { };
            pcs.Add("newPassword", new PropertyConverter
            {
                AvoidValidate           = true,
                CustomWriteJsonProperty = ctx => { },
                CustomReadJsonProperty  = ctx =>
                {
                    EntityJsonConverter.AssertCanWrite(ctx.ParentPropertyRoute.Add(piPasswordHash));

                    var password = (string)ctx.JsonReader.Value !;

                    var error = UserEntity.OnValidatePassword(password);
                    if (error != null)
                    {
                        throw new ApplicationException(error);
                    }

                    ((UserEntity)ctx.Entity).PasswordHash = Security.EncodePassword(password);
                }
            });
Exemple #4
0
        public static void SetSetters(ModifiableEntity entity, List <PropertySetter> setters, PropertyRoute route)
        {
            JsonSerializer serializer = JsonSerializer.Create(SignumServer.JsonSerializerSettings);

            foreach (var setter in setters)
            {
                var pr = route.Add(setter.Property);

                EntityJsonConverter.AssertCanWrite(pr, entity);

                if (pr.Type.IsMList())
                {
                    var elementPr = pr.Add("Item");
                    var mlist     = pr.GetLambdaExpression <ModifiableEntity, IMListPrivate>(false).Compile()(entity);
                    switch (setter.Operation)
                    {
                    case PropertyOperation.AddElement:
                    {
                        var item = (ModifiableEntity)Activator.CreateInstance(elementPr.Type) !;
                        SetSetters(item, setter.Setters !, elementPr);
                        ((IList)mlist).Add(item);
                    }
                    break;

                    case PropertyOperation.ChangeElements:
                    {
                        var predicate = GetPredicate(setter.Predicate !, elementPr, serializer);
                        var toChange  = ((IEnumerable <object>)mlist).Where(predicate.Compile()).ToList();
                        foreach (var item in toChange)
                        {
                            SetSetters((ModifiableEntity)item, setter.Setters !, elementPr);
                        }
                    }
                    break;

                    case PropertyOperation.RemoveElements:
                    {
                        var predicate = GetPredicate(setter.Predicate !, elementPr, serializer);
                        var toRemove  = ((IEnumerable <object>)mlist).Where(predicate.Compile()).ToList();
                        foreach (var item in toRemove)
                        {
                            ((IList)mlist).Remove(item);
                        }
                    }
                    break;

                    default:
                        break;
                    }
                }
                else if (setter.Operation == PropertyOperation.CreateNewEntiy)
                {
                    var subPr = pr.Type.IsEmbeddedEntity() ? pr : PropertyRoute.Root(TypeLogic.GetType(setter.EntityType !));
                    var item  = (ModifiableEntity)Activator.CreateInstance(subPr.Type) !;
                    SetSetters(item, setter.Setters !, subPr);
                    SetProperty(entity, pr, route, item);
                }
                else if (setter.Operation == PropertyOperation.ModifyEntity)
                {
                    var item = GetProperty(entity, pr, route);
                    if (!(item is ModifiableEntity mod))
                    {
                        throw new InvalidOperationException($"Unable to change entity in {pr}: {item}");
                    }

                    SetSetters(mod, setter.Setters !, pr);
                    SetProperty(entity, pr, route, mod);
                }
                else
                {
                    var value = ConvertObject(setter.Value, pr, serializer);
                    SetProperty(entity, pr, route, value);
                }
            }
        }