public static void RemoveAllPushNotificationData(TdPushNotificationContext context)
        {
            context.TicketPushNotificationItems.RemoveRange(context.TicketPushNotificationItems);
            context.PushNotificationDestinations.RemoveRange(context.PushNotificationDestinations);
            context.SubscriberPushNotificationSettings.RemoveRange(context.SubscriberPushNotificationSettings);

            //context.PushNotificationItems.RemoveRange(context.PushNotificationItems);
            context.TicketDeskPushNotificationSettings = new ApplicationPushNotificationSetting();

            context.SaveChanges();

            Configuration.InitializeStockUserSettings(context);
            context.SaveChanges();
        }
示例#2
0
        public static void RemoveAllPushNotificationData(TdPushNotificationContext context)
        {
            context.TicketPushNotificationItems.RemoveRange(context.TicketPushNotificationItems);
            context.PushNotificationDestinations.RemoveRange(context.PushNotificationDestinations);
            context.SubscriberPushNotificationSettings.RemoveRange(context.SubscriberPushNotificationSettings);

            //context.PushNotificationItems.RemoveRange(context.PushNotificationItems);
            context.TicketDeskPushNotificationSettings = new ApplicationPushNotificationSetting();

            context.SaveChanges();

            Configuration.InitializeStockUserSettings(context);
            context.SaveChanges();
        }
示例#3
0
        public static void SetupDemoPushNotificationData(TdPushNotificationContext context)
        {
            if (!context.SubscriberPushNotificationSettings.Any(
                    s => s.SubscriberId == "64165817-9cb5-472f-8bfb-6a35ca54be6a"))
            {
                context.SubscriberPushNotificationSettings.Add(new SubscriberNotificationSetting()
                {
                    SubscriberId = "64165817-9cb5-472f-8bfb-6a35ca54be6a",
                    IsEnabled    = true,
                    PushNotificationDestinations = new[]
                    {
                        new PushNotificationDestination()
                        {
                            SubscriberName     = "Admin User",
                            DestinationAddress = "*****@*****.**",
                            DestinationType    = "email"
                        }
                    }
                });
            }
            if (!context.SubscriberPushNotificationSettings.Any(
                    s => s.SubscriberId == "72bdddfb-805a-4883-94b9-aa494f5f52dc"))
            {
                context.SubscriberPushNotificationSettings.Add(new SubscriberNotificationSetting()
                {
                    SubscriberId = "72bdddfb-805a-4883-94b9-aa494f5f52dc",
                    IsEnabled    = true,
                    PushNotificationDestinations = new[]
                    {
                        new PushNotificationDestination()
                        {
                            SubscriberName     = "HelpDesk User",
                            DestinationAddress = "*****@*****.**",
                            DestinationType    = "email"
                        }
                    }
                });
            }

            if (!context.SubscriberPushNotificationSettings.Any(
                    s => s.SubscriberId == "17f78f38-fa68-445f-90de-38896140db28"))
            {
                context.SubscriberPushNotificationSettings.Add(new SubscriberNotificationSetting()
                {
                    SubscriberId = "17f78f38-fa68-445f-90de-38896140db28",
                    IsEnabled    = true,
                    PushNotificationDestinations = new[]
                    {
                        new PushNotificationDestination()
                        {
                            SubscriberName     = "Regular User",
                            DestinationAddress = "*****@*****.**",
                            DestinationType    = "email"
                        }
                    }
                });
            }
            context.SaveChanges();
        }
示例#4
0
        public async Task <ActionResult> Register(UserRegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new TicketDeskUser {
                    UserName = model.UserName, Email = model.Email, DisplayName = model.DisplayName
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    //add comment 24/01/2020 by Tu
                    await UserManager.AddToRolesAsync(user.Id, DomainContext.TicketDeskSettings.SecuritySettings.DefaultNewUserRoles.ToArray());

                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    HostingEnvironment.QueueBackgroundWorkItem(ct =>
                    {
                        using (var notificationContext = new TdPushNotificationContext())
                        {
                            notificationContext.SubscriberPushNotificationSettingsManager.AddSettingsForSubscriber(
                                new SubscriberNotificationSetting
                            {
                                SubscriberId = user.Id,
                                IsEnabled    = true,
                                PushNotificationDestinations = new[]
                                {
                                    new PushNotificationDestination()
                                    {
                                        DestinationType    = "email",
                                        DestinationAddress = user.Email,
                                        SubscriberName     = user.DisplayName
                                    }
                                }
                            });
                            notificationContext.SaveChanges();
                        }
                    });

                    return(RedirectToAction("Index", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        private static Action <CancellationToken> CreateTicketEventNotifications(
            IEnumerable <TicketEventNotification> notifications)
        {
            return(ct =>
            {
                // ReSharper disable once EmptyGeneralCatchClause
                try
                {
                    var notificationIds = notifications.Select(n => n.EventId).ToArray();
                    var domainContext = DependencyResolver.Current.GetService <TdDomainContext>();
                    var multiProject = domainContext.Projects.Count() > 1;
                    //fetch these back and make sure all dependent entities we need are loaded
                    var notes = domainContext.TicketEventNotifications
                                .Include(t => t.TicketEvent)
                                .Include(t => t.TicketEvent.Ticket)
                                .Include(t => t.TicketEvent.Ticket.Project)
                                .Include(t => t.TicketSubscriber)
                                .Where(t => notificationIds.Contains(t.EventId))
                                .ToArray();

                    if (notes.Any())
                    {
                        using (var noteContext = new TdPushNotificationContext())
                        {
                            var subscriberExclude =
                                noteContext.TicketDeskPushNotificationSettings.AntiNoiseSettings
                                .ExcludeSubscriberEvents;

                            var noteEvents = notes.ToNotificationEventInfoCollection(subscriberExclude,
                                                                                     multiProject);

                            noteContext.AddNotifications(noteEvents);

                            noteContext.SaveChanges();
                        }
                    }
                }
                catch
                {
                    //TODO: Log this somewhere
                }
            });
        }
        private static Action <CancellationToken> CreateNewTicketBroadcastNotification(IEnumerable <Ticket> tickets)
        {
            return(ct =>
            {
                try
                {
                    var notificationIds = tickets.Select(t =>
                                                         t.TicketEvents.First(te => te.ForActivity == TicketActivity.Create ||
                                                                              te.ForActivity == TicketActivity.CreateOnBehalfOf).EventId)
                                          .ToArray();

                    var domainContext = DependencyResolver.Current.GetService <TdDomainContext>();
                    var multiProject = domainContext.Projects.Count() > 1;
                    var notes = domainContext.TicketEventNotifications
                                .Include(t => t.TicketEvent)
                                .Include(t => t.TicketEvent.Ticket)
                                .Include(t => t.TicketEvent.Ticket.Project)
                                .Where(t => notificationIds.Contains(t.EventId))
                                .ToArray();

                    if (notes.Any())
                    {
                        using (var noteContext = new TdPushNotificationContext())
                        {
                            var newNoteEvents = notes.ToNewTicketPushNotificationInfoCollection(multiProject);
                            noteContext.AddNotifications(newNoteEvents);

                            noteContext.SaveChanges();
                        }
                    }
                }
                catch
                {
                    //TODO: Log this somewhere
                }
            });
        }
        /// <summary>
        /// Configures the push notifications.
        /// </summary>
        public static void ConfigurePushNotifications()
        {
            var demoMode = ConfigurationManager.AppSettings["ticketdesk:DemoModeEnabled"] ?? "false";

            if (!DatabaseConfig.IsDatabaseReady || demoMode.Equals("true", StringComparison.InvariantCultureIgnoreCase))
            {
                //disable if database hasn't been created, of if running in demo mode
                return;
            }

            //configuration supplied by the IoC configuration
            var context = DependencyResolver.Current.GetService <TdPushNotificationContext>();

            if (DatabaseConfig.IsFirstRunDemoRefreshEnabled())
            {
                DemoPushNotificationDataManager.SetupDemoPushNotificationData(context);
            }

            if (context.TicketDeskPushNotificationSettings.IsEnabled)
            {
                //TODO: poor man's detection of appropriate scheduler
                var siteName       = Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME");
                var isAzureWebSite = !string.IsNullOrEmpty(siteName);
                if (!isAzureWebSite)
                {
                    InProcessPushNotificationScheduler.Start(context.TicketDeskPushNotificationSettings.DeliveryIntervalMinutes);
                }
                context.Dispose();//ensure that no one accidentally holds a reference to this in closure

                //register for static notifications created event handler
                TdDomainContext.NotificationsCreated += (sender, notifications) =>
                {
                    HostingEnvironment.QueueBackgroundWorkItem(ct =>
                    {
                        // ReSharper disable once EmptyGeneralCatchClause
                        try
                        {
                            var notificationIds = notifications.Select(n => n.EventId).ToArray();
                            var domainContext   = DependencyResolver.Current.GetService <TdDomainContext>();
                            var multiProject    = domainContext.Projects.Count() > 1;
                            //fetch these back and make sure all dependent entities we need are loaded
                            var notes = domainContext.TicketEventNotifications
                                        .Include(t => t.TicketEvent)
                                        .Include(t => t.TicketEvent.Ticket)
                                        .Include(t => t.TicketEvent.Ticket.Project)
                                        .Include(t => t.TicketSubscriber)
                                        .Where(t => notificationIds.Contains(t.EventId))
                                        .ToArray();

                            if (notes.Any())
                            {
                                using (var noteContext = new TdPushNotificationContext())
                                {
                                    var subscriberExclude =
                                        noteContext.TicketDeskPushNotificationSettings.AntiNoiseSettings
                                        .ExcludeSubscriberEvents;

                                    var noteEvents = notes.ToNotificationEventInfoCollection(subscriberExclude,
                                                                                             multiProject);

                                    noteContext.AddNotifications(noteEvents);

                                    noteContext.SaveChanges();
                                }
                            }
                        }
                        catch
                        {
                            //TODO: Log this somewhere
                        }
                    });
                };
            }
        }
        private static void EnsureBroadcastNotificaitonsConfiguration(TdPushNotificationContext context)
        {
            var broadcastUserSettings = context.SubscriberPushNotificationSettingsManager
                                        .GetSettingsForSubscriberAsync("new ticket broadcast").Result;

            var broadcastAppSettings = context.TicketDeskPushNotificationSettings.BroadcastSettings;

            if (broadcastUserSettings == null)
            {
                broadcastUserSettings = new SubscriberNotificationSetting
                {
                    SubscriberId = "new ticket broadcast"
                };
                context.SubscriberPushNotificationSettingsManager.AddSettingsForSubscriber(broadcastUserSettings);
            }
            if
            (
                broadcastAppSettings.BroadcastMode ==
                ApplicationPushNotificationSetting.PushNotificationBroadcastMode.CustomAddress
                &&
                !string.IsNullOrEmpty(broadcastAppSettings.SendToCustomEmailAddress)
            )
            {
                broadcastUserSettings.PushNotificationDestinations.Add(
                    new PushNotificationDestination
                {
                    SubscriberId       = "new ticket broadcast",
                    DestinationAddress = broadcastAppSettings.SendToCustomEmailAddress,
                    DestinationType    = "email",
                    SubscriberName     = broadcastAppSettings.SendToCustomEmailDisplayName
                }
                    );
            }
            else
            {
                var userManager          = DependencyResolver.Current.GetService <TicketDeskUserManager>();
                var roleManager          = DependencyResolver.Current.GetService <TicketDeskRoleManager>();
                var usersForNotification = roleManager.GetTdHelpDeskUsers(userManager)
                                           .Union(roleManager.GetTdTdAdministrators(userManager))
                                           .Distinct(new UniqueNameEmailDisplayUserEqualityComparer()).ToArray();


                var existingDestinations = broadcastUserSettings
                                           .PushNotificationDestinations
                                           .Where(pnd => pnd.DestinationType == "email" && pnd.SubscriberId == "new ticket broadcast")
                                           .ToArray();
                //remove users not in list anymore
                var usersToRemove = existingDestinations
                                    .Where(us => !usersForNotification
                                           .Any(un => un.Email == us.DestinationAddress && un.DisplayName == us.SubscriberName))
                                    .ToList();
                foreach (var us in usersToRemove)
                {
                    broadcastUserSettings.PushNotificationDestinations.Remove(us);
                }

                //add users in list, but not already in destinations
                foreach (var nu in usersForNotification)
                {
                    if (!existingDestinations.Any(ed => nu.Email == ed.DestinationAddress &&
                                                  nu.DisplayName == ed.SubscriberName))
                    {
                        broadcastUserSettings.PushNotificationDestinations.Add(
                            new PushNotificationDestination
                        {
                            SubscriberId       = "new ticket broadcast",
                            DestinationAddress = nu.Email,
                            DestinationType    = "email",
                            SubscriberName     = nu.DisplayName
                        }
                            );
                    }
                }
            }
            broadcastUserSettings.IsEnabled = broadcastAppSettings.IsBroadcastEnabled;
            context.SaveChanges();
        }
        public static void SetupDemoPushNotificationData(TdPushNotificationContext context)
        {
            if (!context.SubscriberPushNotificationSettings.Any(
                    s => s.SubscriberId == "64165817-9cb5-472f-8bfb-6a35ca54be6a"))
            {
                context.SubscriberPushNotificationSettings.Add(new SubscriberNotificationSetting()
                {
                    SubscriberId = "64165817-9cb5-472f-8bfb-6a35ca54be6a",
                    IsEnabled = true,
                    PushNotificationDestinations = new[]
                    {
                        new PushNotificationDestination()
                        {
                            SubscriberName = "Admin User",
                            DestinationAddress = "*****@*****.**",
                            DestinationType = "email"
                        }
                    }
                });
            }
            if (!context.SubscriberPushNotificationSettings.Any(
                s => s.SubscriberId == "72bdddfb-805a-4883-94b9-aa494f5f52dc"))
            {
                context.SubscriberPushNotificationSettings.Add(new SubscriberNotificationSetting()
                {
                    SubscriberId = "72bdddfb-805a-4883-94b9-aa494f5f52dc",
                    IsEnabled = true,
                    PushNotificationDestinations = new[]
                    {
                        new PushNotificationDestination()
                        {
                            SubscriberName = "HelpDesk User",
                            DestinationAddress = "*****@*****.**",
                            DestinationType = "email"
                        }
                    }
                });
            }

            if (!context.SubscriberPushNotificationSettings.Any(
                s => s.SubscriberId == "17f78f38-fa68-445f-90de-38896140db28"))
            {
                context.SubscriberPushNotificationSettings.Add(new SubscriberNotificationSetting()
                {
                    SubscriberId = "17f78f38-fa68-445f-90de-38896140db28",
                    IsEnabled = true,
                    PushNotificationDestinations = new[]
                    {
                        new PushNotificationDestination()
                        {
                            SubscriberName = "Regular User",
                            DestinationAddress = "*****@*****.**",
                            DestinationType = "email"
                        }
                    }
                });
            }
            context.SaveChanges();
        }
        public async Task <ActionResult> CreateDatabase(string email, string password, string displayName)
        {
            using (var ctx = new TdDomainContext(null))
            {
                Database.SetInitializer(
                    new MigrateDatabaseToLatestVersion <TdDomainContext, Configuration>(true));
                ctx.Database.Initialize(true);
            }



            var filter = GlobalFilters.Filters.FirstOrDefault(f => f.Instance is DbSetupFilter);

            if (filter != null)
            {
                GlobalFilters.Filters.Remove(filter.Instance);
            }

            Database.SetInitializer(new TdIdentityDbInitializer());
            var            existingUser = UserManager.FindByName(email);
            TicketDeskUser newUser      = null;

            if (existingUser == null)
            {
                newUser = new TicketDeskUser {
                    UserName = email, Email = email, DisplayName = displayName
                };
                await UserManager.CreateAsync(newUser, password);

                await UserManager.AddToRoleAsync(newUser.Id, "TdAdministrators");
            }
            else
            {
                //should only happen if user entered one of the demo user names when setting up the DB
                //reset the password to the one the user entered here, and set the admin role if needed
                var token = await UserManager.GeneratePasswordResetTokenAsync(existingUser.Id);

                await UserManager.ResetPasswordAsync(existingUser.Id, token, password);

                existingUser.DisplayName = displayName;
                await UserManager.UpdateAsync(existingUser);

                if (!UserManager.IsTdAdministrator(existingUser.Id))
                {
                    await UserManager.AddToRoleAsync(existingUser.Id, "TdAdministrators");
                }
            }
            Database.SetInitializer(new TdPushNotificationDbInitializer());

            Startup.ConfigurePushNotifications();
            if (newUser != null)
            {
                using (var notificationContext = new TdPushNotificationContext())
                {
                    notificationContext.SubscriberPushNotificationSettingsManager.AddSettingsForSubscriber(
                        new SubscriberNotificationSetting
                    {
                        SubscriberId = newUser.Id,
                        IsEnabled    = true,
                        PushNotificationDestinations = new[]
                        {
                            new PushNotificationDestination()
                            {
                                DestinationType    = "email",
                                DestinationAddress = newUser.Email,
                                SubscriberName     = newUser.DisplayName
                            }
                        }
                    });
                    notificationContext.SaveChanges();
                }
            }
            UpdateSearchIndex();
            return(RedirectToAction("Index"));
        }