public Task StartAsync(CancellationToken cancellationToken)
        {
            timerSpamProtectionCache = new Timer(_ =>
            {
                Console.WriteLine("SpamProtectionCache.RemoveExpired");
                spamProtectionCache.RemoveExpired();
            }, null, TimeSpan.Zero, TimeSpan.FromMinutes(schedulerOptions.SpamProtectionCacheClearMinutes));

            timerJwtBlackListService = new Timer(_ =>
            {
                Console.WriteLine("JwtBlackListService.RemoveExpired");
                jwtBlackListService.RemoveExpired();
            }, null, TimeSpan.Zero, TimeSpan.FromMinutes(schedulerOptions.JwtBlackListServiceClearMinutes));

            timerLongSessionsClearer = new Timer(_ =>
            {
                Console.WriteLine("LongSessionsClearer.ClearExpiredLongSessions");
                using (var db = dbFactory.CreateDb())
                {
                    LongSessionsClearer.ClearExpiredLongSessions(db);
                }
            }, null, TimeSpan.Zero, TimeSpan.FromDays(schedulerOptions.LongSessionsClearDays));

            timerExpiredRegistrationUsersCleaner = new Timer(_ =>
            {
                Console.WriteLine("OldNotRegisteredUsersClearer.CleanOldNotRegisteredUsers");
                using (var db = dbFactory.CreateDb())
                {
                    ExpiredRegistrationUsersClearer.CleanExpiredRegistrationUsers(db);
                }
            }, null, TimeSpan.Zero, TimeSpan.FromDays(schedulerOptions.ExpiredRegistrationUsersClearDays));


            return(Task.CompletedTask);
        }
Esempio n. 2
0
        public Task StartAsync(CancellationToken cancellationToken)
        {
            timerSpamProtectionCache = new Timer(_ =>
            {
                if (schedulerOptions.CurrentValue.LogJobs)
                {
                    Console.WriteLine("SpamProtectionCache.RemoveExpired");
                }
                spamProtectionCache.RemoveExpired();
            }, null, TimeSpan.Zero,
                                                 TimeSpan.FromMinutes(schedulerOptions.CurrentValue.SpamProtectionCacheClearMinutes));

            timerJwtBlackListService = new Timer(_ =>
            {
                if (schedulerOptions.CurrentValue.LogJobs)
                {
                    Console.WriteLine("JwtBlackListService.RemoveExpired");
                }
                jweBlackListService.RemoveExpired();
            }, null, TimeSpan.Zero,
                                                 TimeSpan.FromMinutes(schedulerOptions.CurrentValue.JwtBlackListServiceClearMinutes));

            timerLongSessionsClearer = new Timer(_ =>
            {
                if (schedulerOptions.CurrentValue.LogJobs)
                {
                    Console.WriteLine("LongSessionsClearer.ClearExpiredLongSessions");
                }
                using var db = dbFactory.CreateDb();
                LongSessionsClearer.ClearExpiredLongSessions(db);
            }, null, TimeSpan.Zero, TimeSpan.FromDays(schedulerOptions.CurrentValue.LongSessionsClearDays));

            timerExpiredRegistrationUsersCleaner = new Timer(_ =>
            {
                if (schedulerOptions.CurrentValue.LogJobs)
                {
                    Console.WriteLine("OldNotRegisteredUsersClearer.CleanOldNotRegisteredUsers");
                }
                using var db = dbFactory.CreateDb();
                ExpiredRegistrationUsersClearer.CleanExpiredRegistrationUsers(db);
            }, null, TimeSpan.Zero, TimeSpan.FromDays(schedulerOptions.CurrentValue.ExpiredRegistrationUsersClearDays));

            timerCountersUpload = new Timer(_ =>
            {
                if (schedulerOptions.CurrentValue.LogJobs)
                {
                    Console.WriteLine("CountersUploadToDataBase");
                }
                materialsVisitsCounterCache.UploadToDataBase();
                profilesVisitsCounterService.UploadToDataBase();
            }, null, TimeSpan.FromMinutes(schedulerOptions.CurrentValue.UploadVisitsToDataBaseMinutes),
                                            TimeSpan.FromMinutes(schedulerOptions.CurrentValue.UploadVisitsToDataBaseMinutes));

            return(Task.CompletedTask);
        }
        public async Task AddAllUserTokensToBlackListAsync(int userId)
        {
            using var db = dataBaseFactory.CreateDb();
            var sessions = await db.LongSessions.Where(x => x.UserId == userId).ToListAsync();

            DateTime exp = DateTime.UtcNow.AddMinutes(jweOptions.CurrentValue.ShortTokenLiveTimeMinutes + 5);

            foreach (var session in sessions)
            {
                await AddBlackListShortTokenAsync(session.LongToken2, exp);
            }
        }
Esempio n. 4
0
        public void Initialize()
        {
            using (var db = dataBaseFactory.CreateDb())
            {
                _allSectionTypes = db.SectionTypes
                                   .ToImmutableDictionary(x => x.Name, x => new SectionTypeCached(x));

                var categories = db.Categories.Where(x => !x.IsDeleted).Select(x => new CategoryCached(x))
                                 .ToDictionary(x => x.Id);

                PrepareCategories(categories);
            }
        }
Esempio n. 5
0
        public async Task ResetSecret(string name)
        {
            using var db = dbFactory.CreateDb();
            var newSecret = GenerateSecurityKey();

            int updated = await db.CipherSecrets.Where(x => x.Name == name).Set(x => x.Secret, newSecret)
                          .UpdateAsync();

            if (updated != 1)
            {
                throw new SunEntityNotUpdatedException(nameof(CipherSecret), name, "Name");
            }
        }
Esempio n. 6
0
        public async Task AddUserTokensAsync(int userId)
        {
            using (var db = dataBaseFactory.CreateDb())
            {
                var sessions = await db.LongSessions.Where(x => x.UserId == userId).ToListAsync();

                DateTime exp = DateTime.UtcNow.AddMinutes(jwtOptions.ShortTokenLiveTimeMinutes + 5);

                foreach (var session in sessions)
                {
                    Add(session.LongToken2, exp);
                }
            }
        }
Esempio n. 7
0
        public void Initialize()
        {
            using var db = dataBaseFactory.CreateDb();
            var categories = db.Categories.Where(x => x.DeletedDate == null).Select(x => new CategoryCached(x))
                             .ToDictionary(x => x.Id);

            foreach (var category in categories.Values)
            {
                category.Init1_ParentAndSub(categories);
            }

            RootCategory = categories.Values.FirstOrDefault(x => x.Name == Category.RootCategoryName);
            if (RootCategory == null)
            {
                throw new Exception($"Can not find category '{Category.RootCategoryName}' in data base.");
            }

            var categoriesList = RootCategory.Init2_AllSub();

            categoriesList.Insert(0, RootCategory);

            RootCategory.Init3_UrlPaths();

            RootCategory.Init4_InitSectionsRoots();

            foreach (var category in categoriesList)
            {
                category.Init5_SetListsAndFreeze();
            }

            AllCategoriesByName =
                categoriesList.ToImmutableDictionary(x => x.NameNormalized, StringComparer.OrdinalIgnoreCase);

            AllCategoriesById = AllCategoriesByName.ToImmutableDictionary(x => x.Value.Id, x => x.Value);
        }
Esempio n. 8
0
        public void Initialize()
        {
            using (var db = dataBaseFactory.CreateDb())
            {
                var roles = db.Roles.Select(x => new RoleTmp(x)).ToDictionary(x => x.Id);

                _allOperationKeys = db.OperationKeys.Select(x => new OperationKeyCached(x)).ToImmutableList();


                var categoryAccesses = db.CategoryAccess.Select(x => new CategoryAccessTmp(x))
                                       .ToDictionary(x => x.Id);

                foreach (CategoryOperationAccess categoryOperationAccess in db.CategoryOperationAccess.ToList())
                {
                    categoryAccesses[categoryOperationAccess.CategoryAccessId].CategoryOperationAccesses
                    .Add(categoryOperationAccess.OperationKeyId, categoryOperationAccess.Access);
                }

                foreach (var categoryAccess in categoryAccesses.Values)
                {
                    roles[categoryAccess.RoleId].CategoryAccesses
                    .Add(categoryAccess);
                }

                _allRoles = roles.Values.ToImmutableDictionary(x => x.Name, x => new RoleCached(x));
            }
        }
Esempio n. 9
0
        public void Initialize()
        {
            using var db = dataBaseFactory.CreateDb();
            var sections = db.Sections.ToList();

            var serverSectionsTmp = new Dictionary <string, SectionServerCached>(sections.Count);

            var clientSectionsTmp = new List <SectionClientCached>();

            foreach (var section in sections)
            {
                try
                {
                    serverSectionsTmp.Add(section.Name,
                                          new SectionServerCached(section, sectionTypes.Sections[section.Type].ServerSectionType, rolesCache));
                    clientSectionsTmp.Add(new SectionClientCached(section, sectionTypes.Sections[section.Type].ClientSectionType, rolesCache));
                }
                catch
                {
                    // ignored
                }
            }

            ServerSections = serverSectionsTmp.ToImmutableDictionary(StringComparer.OrdinalIgnoreCase);
            ClientSections = clientSectionsTmp.ToImmutableList();
        }
Esempio n. 10
0
        public void Initialize()
        {
            using var db = dataBaseFactory.CreateDb();

            var menuItems    = db.MenuItems.Where(x => !x.IsHidden).ToDictionary(x => x.Id, x => x);
            var allMenuItems = new List <MenuItemCached>(menuItems.Count);

            foreach (var menuItem in menuItems.Values)
            {
                ImmutableDictionary <int, RoleCached> roles;
                if (menuItem.Roles != null)
                {
                    roles = menuItem.Roles.Split(',')
                            .Select(x => rolesCache.GetRole(x))
                            .ToDictionary(x => x.Id, x => x)
                            .ToImmutableDictionary();
                }
                else
                {
                    roles = new Dictionary <int, RoleCached>().ToImmutableDictionary();
                }


                if (CheckIsVisible(menuItem))
                {
                    allMenuItems.Add(new MenuItemCached(menuItem, roles));
                }
            }

            AllMenuItems = allMenuItems.OrderBy(x => x.SortNumber).ToImmutableList();

            RootMenuItem = AllMenuItems.First(x => x.Id == 1);


            bool CheckIsVisible(MenuItem menuItem)
            {
                while (true)
                {
                    if (menuItem.IsHidden)
                    {
                        return(false);
                    }

                    if (!menuItem.ParentId.HasValue)
                    {
                        return(true);
                    }

                    if (!menuItems.ContainsKey(menuItem.ParentId.Value))
                    {
                        return(false);
                    }

                    menuItem = menuItems[menuItem.ParentId.Value];
                }
            }
        }
Esempio n. 11
0
        public void Initialize()
        {
            using (var db = dataBaseFactory.CreateDb())
            {
                var categories = db.Categories.Where(x => x.DeletedDate == null).Select(x => new CategoryCached(x))
                                 .ToDictionary(x => x.Id);

                PrepareCategories(categories);
            }
        }
Esempio n. 12
0
        public OperationKeysContainer(IDataBaseFactory dbFactory)
        {
            using DataBaseConnection db = dbFactory.CreateDb();
            Dictionary <string, int> dictionary = db.OperationKeys
                                                  .ToDictionary(x => x.Name, x => x.OperationKeyId);

            foreach (var propertyInfo in typeof(OperationKeysContainer).GetProperties())
            {
                propertyInfo.SetValue(this, dictionary[propertyInfo.Name]);
            }
        }
Esempio n. 13
0
        public override void Load()
        {
            Data.Clear();

            using var db = dataBaseFactory.CreateDb();

            EnsureItems(db);
            DeleteLostItems(db);

            var items = db.ConfigurationItems.ToList();

            foreach (var item in items)
            {
                Data.Add(item.Name, item.Value);
            }
        }
Esempio n. 14
0
        public void Initialize()
        {
            lock (lockObject)
            {
                using (var db = dataBaseFactory.CreateDb())
                {
                    var components = db.Components.ToList();

                    Dictionary <string, ComponentServerCached> serverComponentsTmp =
                        new Dictionary <string, ComponentServerCached>(components.Count);

                    List <ComponentClientCached> clientComponentsTmp = new List <ComponentClientCached>();

                    foreach (var component in components)
                    {
                        try
                        {
                            ImmutableDictionary <int, RoleCached> roles;
                            if (component.Roles != null)
                            {
                                roles = component.Roles.Split(',')
                                        .Select(x => rolesCache.GetRole(x))
                                        .ToDictionary(x => x.Id, x => x)
                                        .ToImmutableDictionary();
                            }
                            else
                            {
                                roles = new Dictionary <int, RoleCached>().ToImmutableDictionary();
                            }

                            serverComponentsTmp[component.Name] =
                                new ComponentServerCached(component, ComponentsDataTypes[component.Type], roles);

                            clientComponentsTmp.Add(new ComponentClientCached(component, roles));
                        }
                        catch
                        {
                            // ignored
                        }
                    }

                    serverComponents = serverComponentsTmp.ToImmutableDictionary(StringComparer.OrdinalIgnoreCase);

                    clientComponents = clientComponentsTmp.ToImmutableList();
                }
            }
        }
Esempio n. 15
0
        public CryptService(IDataBaseFactory dbFactory)
        {
            this.dbFactory = dbFactory;

            cipher = new RijndaelManaged
            {
                KeySize   = 256,
                BlockSize = 128,
                Padding   = PaddingMode.ISO10126,
                Mode      = CipherMode.CBC
            };

            using (var db = dbFactory.CreateDb())
            {
                foreach (var x in db.CipherSecrets)
                {
                    AddCipherKey(x.Name, x.Secret);
                }
            }
        }
Esempio n. 16
0
        public static void AddDbOptions(this IServiceCollection service, IDataBaseFactory dbFactory)
        {
            var dbOptionsService = new DbOptionsService(dbFactory.CreateDb());

            service.Configure <CacheOptions>(options => { options.UpdateOptions(dbOptionsService.GetCacheSettings()); });
        }