Ejemplo n.º 1
0
        public IActionResult Update([FromForm] UserContentRequest user, string redirectOK = null, string redirectError = null)
        {
            if (!ModelState.IsValid)
            {
                if (!string.IsNullOrEmpty(redirectError))
                {
                    return(Redirect(redirectError));
                }

                return(BadRequest(ModelState));
            }

            var userId = User.Claims.Where(u => u.Type == "id").Select(u => u.Value).FirstOrDefault();

            if (string.IsNullOrEmpty(userId))
            {
                if (!string.IsNullOrEmpty(redirectError))
                {
                    return(Redirect(redirectError));
                }

                return(NotFound());
            }

            var entry = _context.Users.SingleOrDefault(m => m.ID == Convert.ToInt32(userId));

            if (entry == null)
            {
                if (!string.IsNullOrEmpty(redirectError))
                {
                    return(Redirect(redirectError));
                }

                return(NotFound());
            }

            entry.Name  = user.name;
            entry.Email = user.email;
            entry.Role  = user.role;
            entry.UpdatePassword(user.password);

            _context.Users.Update(entry);
            _context.SaveChanges();

            if (!string.IsNullOrEmpty(redirectOK))
            {
                return(Redirect(redirectOK));
            }

            return(Ok(entry));
        }
Ejemplo n.º 2
0
        public IActionResult New([FromForm] UserContentRequest user, string redirectOK = null)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (_context.Users.Count(u => u.Email == user.email) > 0)
            {
                // Don't allow same email account

                throw new Exception();
            }

            // TODO: We must copy this user to our backup server at 172.22.22.1

            var id = _context.Users.Count() == 0 ? 1 :
                     _context.Users.Max(u => u.ID) + 1;

            var entry = new User
            {
                ID    = id,
                Name  = user.name,
                Email = user.email,
                Role  = user.role
            };

            entry.UpdatePassword(user.password);

            _context.Users.Add(entry);
            _context.SaveChanges();

            if (!string.IsNullOrEmpty(redirectOK))
            {
                return(Redirect(redirectOK));
            }

            return(Ok(entry));
        }