Пример #1
0
        public async Task <IActionResult> ModifyUserRole(string username, string role)
        {
            // Check that specified user exist
            var checkUsr = await _dbHandler.GetUserAsync(username);

            if (checkUsr == null)
            {
                return(BadRequest(new GeneralMessage()
                {
                    Message = "User does not exist"
                }));
            }

            // Validate the role
            if (role != "User" && role != "Admin")
            {
                return(BadRequest(new GeneralMessage()
                {
                    Message = "Invalid role type"
                }));
            }

            // Convert role string into an Enum
            UserRole roleType = (UserRole)Enum.Parse(typeof(UserRole), role);

            // Do the change
            var response = await _dbHandler.ChangeUserRole(checkUsr, roleType);

            if (response.MessageType == MessageType.NOK)
            {
                return(BadRequest(new GeneralMessage()
                {
                    Message = response.MessageText
                }));
            }

            return(Ok(new GeneralMessage()
            {
                Message = "Role has been changed"
            }));
        }