示例#1
0
        public static void Start(SchemaBuilder sb, string?systemUserName, string?anonymousUserName)
        {
            if (sb.NotDefined(MethodInfo.GetCurrentMethod()))
            {
                SystemUserName    = systemUserName;
                AnonymousUserName = anonymousUserName;

                CultureInfoLogic.AssertStarted(sb);

                sb.Include <UserEntity>();

                sb.Include <RoleEntity>()
                .WithSave(RoleOperation.Save)
                .WithDelete(RoleOperation.Delete)
                .WithQuery(() => r => new
                {
                    Entity = r,
                    r.Id,
                    r.Name,
                });

                roles           = sb.GlobalLazy(CacheRoles, new InvalidateWith(typeof(RoleEntity)), AuthLogic.NotifyRulesChanged);
                rolesInverse    = sb.GlobalLazy(() => roles.Value.Inverse(), new InvalidateWith(typeof(RoleEntity)));
                rolesByName     = sb.GlobalLazy(() => roles.Value.ToDictionaryEx(a => a.ToString()), new InvalidateWith(typeof(RoleEntity)));
                mergeStrategies = sb.GlobalLazy(() =>
                {
                    var strategies = Database.Query <RoleEntity>().Select(r => KVP.Create(r.ToLite(), r.MergeStrategy)).ToDictionary();

                    var graph = roles.Value;

                    Dictionary <Lite <RoleEntity>, RoleData> result = new Dictionary <Lite <RoleEntity>, RoleData>();
                    foreach (var r in graph.CompilationOrder())
                    {
                        var strat = strategies.GetOrThrow(r);

                        var baseValues = graph.RelatedTo(r).Select(r2 => result[r2].DefaultAllowed);

                        result.Add(r, new RoleData
                        {
                            MergeStrategy  = strat,
                            DefaultAllowed = strat == MergeStrategy.Union ? baseValues.Any(a => a) : baseValues.All(a => a)
                        });
                    }

                    return(result);
                }, new InvalidateWith(typeof(RoleEntity)), AuthLogic.NotifyRulesChanged);

                sb.Schema.EntityEvents <RoleEntity>().Saving += Schema_Saving;

                QueryLogic.Queries.Register(RoleQuery.RolesReferedBy, () =>
                                            from r in Database.Query <RoleEntity>()
                                            from rc in r.Roles
                                            select new
                {
                    Entity = r,
                    r.Id,
                    r.Name,
                    Refered = rc,
                });
                sb.Include <UserEntity>()
                .WithQuery(() => e => new
                {
                    Entity = e,
                    e.Id,
                    e.UserName,
                    e.Email,
                    e.Role,
                    e.State,
                });

                UserGraph.Register();
            }
        }
示例#2
0
        public static void Start(SchemaBuilder sb, string?systemUserName, string?anonymousUserName)
        {
            if (sb.NotDefined(MethodInfo.GetCurrentMethod()))
            {
                SystemUserName    = systemUserName;
                AnonymousUserName = anonymousUserName;

                CultureInfoLogic.AssertStarted(sb);

                sb.Include <UserEntity>()
                .WithExpressionFrom((RoleEntity r) => r.Users())
                .WithQuery(() => e => new
                {
                    Entity = e,
                    e.Id,
                    e.UserName,
                    e.Email,
                    e.Role,
                    e.State,
                    e.CultureInfo,
                });

                sb.Include <RoleEntity>()
                .WithSave(RoleOperation.Save)
                .WithDelete(RoleOperation.Delete)
                .WithQuery(() => r => new
                {
                    Entity = r,
                    r.Id,
                    r.Name,
                });

                roles           = sb.GlobalLazy(CacheRoles, new InvalidateWith(typeof(RoleEntity)), AuthLogic.NotifyRulesChanged);
                rolesInverse    = sb.GlobalLazy(() => roles.Value.Inverse(), new InvalidateWith(typeof(RoleEntity)));
                rolesByName     = sb.GlobalLazy(() => roles.Value.ToDictionaryEx(a => a.ToString() !), new InvalidateWith(typeof(RoleEntity)));
                mergeStrategies = sb.GlobalLazy(() =>
                {
                    var strategies = Database.Query <RoleEntity>().Select(r => KeyValuePair.Create(r.ToLite(), r.MergeStrategy)).ToDictionary();

                    var graph = roles.Value;

                    Dictionary <Lite <RoleEntity>, RoleData> result = new Dictionary <Lite <RoleEntity>, RoleData>();
                    foreach (var r in graph.CompilationOrder())
                    {
                        var strat = strategies.GetOrThrow(r);

                        var baseValues = graph.RelatedTo(r).Select(r2 => result[r2].DefaultAllowed);

                        result.Add(r, new RoleData
                        {
                            MergeStrategy  = strat,
                            DefaultAllowed = strat == MergeStrategy.Union ? baseValues.Any(a => a) : baseValues.All(a => a)
                        });
                    }

                    return(result);
                }, new InvalidateWith(typeof(RoleEntity)), AuthLogic.NotifyRulesChanged);

                sb.Schema.EntityEvents <RoleEntity>().Saving += Schema_Saving;

                UserGraph.Register();

                EmailModelLogic.RegisterEmailModel <UserLockedMail>(() => new EmailTemplateEntity
                {
                    Messages = CultureInfoLogic.ForEachCulture(culture => new EmailTemplateMessageEmbedded(culture)
                    {
                        Text =
                            "<p>{0}</p>".FormatWith(AuthEmailMessage.YourAccountHasBeenLockedDueToSeveralFailedLogins.NiceToString()) +
                            "<p>{0}</p>".FormatWith(AuthEmailMessage.YouCanResetYourPasswordByFollowingTheLinkBelow.NiceToString()) +
                            "<p><a href=\"@[m:Url]\">@[m:Url]</a></p>",
                        Subject = AuthEmailMessage.YourAccountHasBeenLocked.NiceToString()
                    }).ToMList()
                });
            }
        }