Example #1
0
        public static void Start(SchemaBuilder sb)
        {
            if (sb.NotDefined(MethodInfo.GetCurrentMethod()))
            {
                sb.Include <ResetPasswordRequestEntity>()
                .WithQuery(() => e => new
                {
                    Entity = e,
                    e.Id,
                    e.RequestDate,
                    e.Code,
                    e.User,
                    e.User.Email
                });

                EmailLogic.AssertStarted(sb);

                EmailModelLogic.RegisterEmailModel <ResetPasswordRequestEmail>(() => new EmailTemplateEntity
                {
                    Messages = CultureInfoLogic.ForEachCulture(culture => new EmailTemplateMessageEmbedded(culture)
                    {
                        Text = "<p>{0}</p>".FormatWith(AuthEmailMessage.YouRecentlyRequestedANewPassword.NiceToString()) +
                               "<p>{0} @[User.UserName]</p>".FormatWith(AuthEmailMessage.YourUsernameIs.NiceToString()) +
                               "<p>{0}</p>".FormatWith(AuthEmailMessage.YouCanResetYourPasswordByFollowingTheLinkBelow.NiceToString()) +
                               "<p><a href=\"@[m:Url]\">@[m:Url]</a></p>",
                        Subject = AuthEmailMessage.ResetPasswordRequestSubject.NiceToString()
                    }).ToMList()
                });
            }
        }
        public static void Start(SchemaBuilder sb)
        {
            if (sb.NotDefined(MethodInfo.GetCurrentMethod()))
            {
                sb.Include <ResetPasswordRequestEntity>()
                .WithQuery(() => e => new
                {
                    Entity = e,
                    e.Id,
                    e.RequestDate,
                    e.Code,
                    e.User,
                    e.User.Email
                });

                EmailLogic.AssertStarted(sb);

                EmailModelLogic.RegisterEmailModel <ResetPasswordRequestEmail>(() => new EmailTemplateEntity
                {
                    DisableAuthorization = true,
                    Messages             = CultureInfoLogic.ForEachCulture(culture => new EmailTemplateMessageEmbedded(culture)
                    {
                        Text = "<p>{0}</p>".FormatWith(AuthEmailMessage.YouRecentlyRequestedANewPassword.NiceToString()) +
                               "<p>{0} @[User.UserName]</p>".FormatWith(AuthEmailMessage.YourUsernameIs.NiceToString()) +
                               "<p>{0}</p>".FormatWith(AuthEmailMessage.YouCanResetYourPasswordByFollowingTheLinkBelow.NiceToString()) +
                               "<p><a href=\"@[m:Url]\">@[m:Url]</a></p>",
                        Subject = AuthEmailMessage.ResetPasswordRequestSubject.NiceToString()
                    }).ToMList()
                });



                new Graph <ResetPasswordRequestEntity> .Execute(ResetPasswordRequestOperation.Execute)
                {
                    CanBeNew      = false,
                    CanBeModified = false,
                    CanExecute    = (e) => e.Lapsed == false ? null : AuthEmailMessage.YourResetPasswordRequestHasExpired.NiceToString(),
                    Execute       = (e, args) =>
                    {
                        string password = args.GetArg <string>();
                        e.Lapsed = true;
                        var user = e.User;

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

                        user.PasswordHash = Security.EncodePassword(password);
                        using (AuthLogic.Disable())
                            user.Execute(UserOperation.Save);
                    }
                }

                .Register();
            }
        }
Example #3
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()
            });
        }
    }
Example #4
0
        public static void RegisterAlertNotificationMail(SchemaBuilder sb)
        {
            EmailModelLogic.RegisterEmailModel <AlertNotificationMail>(() => new EmailTemplateEntity
            {
                Messages = CultureInfoLogic.ForEachCulture(culture => new EmailTemplateMessageEmbedded(culture)
                {
                    Text    = @"
<p>Hi @[m:Entity],</p>
<p>You have some pending alerts:</p>
<ul>
 @foreach[m:Alerts] as $a
    <li>
        <strong>@[$a.AlertType]:</strong><br/>
        @[m:TextFormatted]<br/>
        <small>@[$a.AlertDate] @[$a.CreatedBy]</small>
    </li>
 @endforeach
</ul>
<p>Please visit <a href=""@[g:UrlLeft]"">@[g:UrlLeft]</a></p>",
                    Subject = AlertMessage.NewUnreadNotifications.NiceToString(),
                }).ToMList()
            });

            sb.Include <SendNotificationEmailTaskEntity>()
            .WithSave(SendNotificationEmailTaskOperation.Save)
            .WithQuery(() => e => new
            {
                Entity = e,
                e.Id,
                e.SendNotificationsOlderThan,
                e.SendBehavior,
            });

            SchedulerLogic.ExecuteTask.Register((SendNotificationEmailTaskEntity task, ScheduledTaskContext ctx) =>
            {
                var limit = DateTime.Now.AddMinutes(-task.SendNotificationsOlderThan);

                var query = Database.Query <AlertEntity>()
                            .Where(a => a.State == AlertState.Saved && a.EmailNotificationsSent == 0 && a.Recipient != null && a.CreationDate < limit)
                            .Where(a => task.SendBehavior == SendAlertTypeBehavior.All ||
                                   task.SendBehavior == SendAlertTypeBehavior.Include && task.AlertTypes.Contains(a.AlertType !) ||
                                   task.SendBehavior == SendAlertTypeBehavior.Exclude && !task.AlertTypes.Contains(a.AlertType !));

                if (!query.Any())
                {
                    return(null);
                }

                var alerts = query
                             .Select(a => new { Alert = a, Recipient = a.Recipient !.Entity })
                             .ToList();

                EmailPackageEntity emailPackage = new EmailPackageEntity().Save();

                var emails = alerts.GroupBy(a => a.Recipient, a => a.Alert).SelectMany(gr => new AlertNotificationMail((UserEntity)gr.Key, gr.ToList()).CreateEmailMessage()).ToList();

                emails.ForEach(a =>
                {
                    a.State   = EmailMessageState.ReadyToSend;
                    a.Package = emailPackage.ToLite();
                });

                emails.BulkInsertQueryIds(a => a.Target !);

                query.UnsafeUpdate().Set(a => a.EmailNotificationsSent, a => 1).Execute();

                return(emailPackage.ToLite());
            });