Beispiel #1
0
        public static User AddFromDto(UULContext context, NewUserDTO newUser)
        {
            var salt = SecHelper.CreateSalt();

            var habitant = new Habitant(newUser);

            context.Habitants.Add(habitant);

            var userToSave = new User {
                Login         = newUser.Login,
                IsActivated   = false,
                CreatedAt     = DateOperations.Now(),
                Hash          = SecHelper.SaltAndHashPwd(newUser.Pwd, salt),
                Salt          = salt,
                ApartmentCode = newUser.ApartmentCode,
                Habitants     = new List <Habitant>()
                {
                    habitant
                }
            };

            context.Users.Add(userToSave);

            return(userToSave);
        }
        Task IHostedService.StartAsync(CancellationToken cancellationToken)
        {
            // timer repeates call to CreateTimeSlots every 24 hours.
            TimeSpan interval = TimeSpan.FromHours(24);                        //.FromMinutes(1);//
                                                                               //calculate time to run the first time & delay to set the timer
                                                                               //DateTime.Today gives time of midnight 00.00
            var nextRunTime   = DateOperations.Today().AddDays(1).AddHours(7); // DateTime.Now.AddMinutes(2);//
            var curTime       = DateOperations.Now();                          // DateTime.Now;
            var firstInterval = nextRunTime.Subtract(curTime);

            void action()
            {
                var t1 = Task.Delay(firstInterval, cancellationToken);

                t1.Wait(cancellationToken);
                //create at expected time
                _logger.LogInformation("First service run");
                CreateTimeSlots(null);
                //now schedule it to be called every 24 hours for future
                // timer repeates call to CreateTimeSlots every 24 hours.
                _timer = new Timer(
                    CreateTimeSlots,
                    null,
                    TimeSpan.Zero,
                    interval
                    );
            }

            // no need to await this call here because this task is scheduled to run much much later.
            Task.Run(action, cancellationToken);
            return(Task.CompletedTask);
        }
Beispiel #3
0
        public static User CreateDefaultAdmin()
        {
            var salt = CreateSalt();

            return(new User()
            {
                ApartmentCode = AdminAppCode,
                Login = Admin,
                IsActivated = true,
                CreatedAt = DateOperations.Now(),
                Hash = SaltAndHashPwd("thecownamedlolasayshola", salt),
                Salt = salt,
            });
        }
Beispiel #4
0
        public async Task <ActionResult <UULResponse> > CreateOrUpdateNews(NewsWebDTO dto)
        {
            UULResponse response;

            try {
                var user = await UserDao.GetUserFromClaimsOrThrow(_context, HttpContext.User);

                if (!SecHelper.IsAdmin(user))
                {
                    throw new Exception("Access denied");
                }
                var news = new News(dto);
                var now  = DateOperations.Now();
                if (news.ID == null)
                {
                    news.CreatedAt = now;
                }
                else
                {
                    news.UpdatedAt = now;
                }
                string message = "News was created";
                if (news.ID == null)
                {
                    _context.News.Add(news);
                }
                else
                {
                    _context.News.Update(news);
                    message = "News was upadted";
                }
                await _context.SaveChangesAsync();

                response = new UULResponse()
                {
                    Success = true, Message = message, Data = new NewsWebDTO(news)
                };
            } catch (Exception e) {
                response = new UULResponse()
                {
                    Success = false, Message = e.Message, Data = null
                };
            }
            return(response);
        }
Beispiel #5
0
        public static string GenerateJSONWebToken(string login, string apartmentCode, IConfiguration _config)
        {
            var key         = _config["Jwt:Key"];
            var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));
            var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);

            var claims = new[] {
                new Claim(ClaimLogin, login),
                new Claim(ClaimApartmentCode, apartmentCode)
            };

            var token = new JwtSecurityToken(_config["Jwt:Issuer"],
                                             _config["Jwt:Issuer"],
                                             claims,
                                             expires: DateOperations.Now().AddDays(360),
                                             signingCredentials: credentials);

            return(new JwtSecurityTokenHandler().WriteToken(token));
        }
        private void CreateTimeSlots(object state)
        {
            var scope     = _scopeFactory.CreateScope();
            var dbContext = scope.ServiceProvider.GetRequiredService <UULContext>();
            var newSlots  = TimeSlotsFactory.CreateTodayTimeSlots(dbContext, 11);

            newSlots.Wait();
            dbContext.TimeSlots.AddRange(newSlots.Result);
            int rows = dbContext.SaveChanges();

            _logger.LogInformation("Creation func affected " + rows + " rows, run at " + DateOperations.Now().ToString());
            scope.Dispose();
        }