示例#1
0
        public IActionResult Post([FromBody] UserDto newUserDto)
        {
            var currentUser = _loginService.GetCurrentUser();

            if (currentUser == null && newUserDto.Role != UserRole.User)
            {
                return(BadRequest("One does not simply summon a king out of thin air, one must join the ranks as a peon first."));
            }
            else if (currentUser != null && newUserDto.Role > currentUser.Role)
            {
                return(BadRequest("Cannot bestow a user with a more magnificent role than current user."));
            }
            else if (emailRegex.IsMatch(newUserDto.Email) == false)
            {
                return(BadRequest("Invalid email address."));
            }
            else if (_context.Users.FindByEmail(newUserDto.Email) != null)
            {
                return(BadRequest("A user already exists with that email address."));
            }
            else
            {
                var newUser = new User()
                {
                    Email     = newUserDto.Email,
                    FirstName = newUserDto.FirstName,
                    LastName  = newUserDto.LastName,
                    Role      = newUserDto.Role,
                };

                newUser.SetPassword(newUserDto.Password);
                _context.Add(newUser);
                _context.SaveChanges();

                _emailNotifier.SendUserdDetails(newUser.Email, newUser.FirstName, newUser.LastName, newUserDto.Password, newUser.Role.ToString());

                return(Ok());
            }
        }