Exemplo n.º 1
0
        public IActionResult Get()
        {
            using EntityGateway db = new EntityGateway();
            int i = db.GetTable <Subject>().Count();

            Subject[] subjects = new Subject[]
            {
                new Subject {
                    Name = $"Subject {i}"
                },
                new Subject {
                    Name = $"Subject {i+1}"
                },
                new Subject {
                    Name = $"Subject {i+2}"
                },
                new Subject {
                    Name = $"Subject {i+3}"
                },
                new Subject {
                    Name = $"Subject {i+4}"
                },
            };
            db.AddRange(subjects);
            IEnumerable <Moderator> moderators = db.GetTable <Moderator>().ToList();

            if (!moderators.Any())
            {
                db.AddOrUpdate(new Moderator
                {
                    Birthday            = DateTime.Now,
                    Citizenship         = "DEFAULT",
                    DocumentNumber      = "DEFAULT DEFAULT",
                    DocumentType        = DocumentType.Passport,
                    Login               = "******",
                    Name                = "DEFAULT",
                    Passhash            = "admin",
                    PensionInsurance    = "DEFAULT",
                    Phone               = "DEFAULT",
                    RegistrationAddress = "DEFAULT DEFAULT DEFAULT"
                });
            }
            return(Ok(new { status = "ok", shit = "lol + 5 subjects", admins = moderators }));
        }
        /// <summary>
        /// Create user session for token
        /// </summary>
        /// <returns>Session Token</returns>
        public static Guid CreateSession(string login, string password)
        {
            Task.Run(() => Sessions.RemoveAll(session => !session.IsActive));
            using EntityGateway db = new EntityGateway();

            var passhash = StringHash.GetStringSha256Hash(password);
            IEnumerable <Human> enumerable = db.GetTable <Moderator>(x => x.Login == login && x.Passhash == passhash).ToList();
            var potentialUser = enumerable.FirstOrDefault();

            if (potentialUser == null)
            {
                throw new UnauthorizedAccessException("Wrong credentials");
            }

            var newSession = new Session()
            {
                User = potentialUser, LastOperation = DateTime.Now, Token = Guid.NewGuid()
            };

            Sessions.Add(newSession);
            return(newSession.Token);
        }