예제 #1
0
        public IActionResult AddEnemy(Enemy enemy)
        {
            if (!IsAdmin())
            {
                return(StatusCode((int)HttpStatusCode.Unauthorized));
            }

            // add enemy and save changes
            _context.Enemies.Add(enemy);

            int count;

            try
            {
                count = _context.SaveChanges();
            }
            catch (System.Exception oops)
            {
                Console.Write("\n" + oops.ToString() + "\n\n");
                return(StatusCode(500));
            }

            // if changes occurred it worked, else something went wrong
            if (count > 0)
            {
                return(Ok());
            }

            return(StatusCode(500));
        }
        public IActionResult DeleteList(int listId)
        {
            // Get authenticated user from database
            var user = _context.Users
                       .FirstOrDefault(u => u.Username == Username());

            if (user == null)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError,
                                  "Unable to resolve user"));
            }

            // Find hitlist with ID if it belongs to the user
            var hitlist = user.Hitlists.FirstOrDefault(h => h.Id == listId);

            if (hitlist == null)
            {
                return(BadRequest("There is no list with that ID that belongs to user"));
            }

            // Remove hitlist from user
            user.Hitlists.Remove(hitlist);

            // Commit changes to database
            try
            {
                if (_context.SaveChanges() > 0)
                {
                    return(Ok());
                }
            }
            catch (System.Exception oops)
            {
                Console.Write("\n" + oops.ToString() + "\n\n");
            }

            return(StatusCode((int)HttpStatusCode.InternalServerError));
        }
예제 #3
0
        public IActionResult Register(UserAuthenticate user)
        {
            if (_context.Users.Where(u => u.Username == user.Username).Any())
            {
                return(Unauthorized("An account with that username already exists"));
            }

            User newUser = new User();

            newUser.Username = user.Username;

            byte[] salt = new byte[128 / 8];
            using (var rng = RandomNumberGenerator.Create())
            {
                rng.GetBytes(salt);
            }

            string hashedPass = Convert.ToBase64String(
                KeyDerivation.Pbkdf2(
                    user.Password,
                    salt,
                    KeyDerivationPrf.HMACSHA512,
                    10000,
                    512 / 8));

            newUser.Password = hashedPass;
            newUser.Spice    = Convert.ToBase64String(salt);
            newUser.Type     = 'g';

            _context.Users.Add(newUser);
            int count;

            try
            {
                count = _context.SaveChanges();
            }
            catch (System.Exception oops)
            {
                Console.Write("\n" + oops.ToString() + "\n\n");
                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }

            // if changes occurred it worked, else something went wrong
            if (count > 0)
            {
                return(Ok(newUser));
            }
            return(StatusCode((int)HttpStatusCode.InternalServerError));
        }