/// <summary> /// This method is responsible for getting a specific user from the helpdesk system /// </summary> /// <param name="id">The UserId of the specific user to be retrieved</param> /// <returns>The response that indicates if the operation was a success, /// and the details of the retrieved user if it was</returns> public GetUserResponse GetUser(int id) { s_logger.Info("Getting user..."); GetUserResponse response = new GetUserResponse(); try { var dataLayer = new UsersDataLayer(); UserDTO user = dataLayer.GetUser(id); if (user == null) { throw new NotFoundException("Unable to find user!"); } response.User = user; response.Status = HttpStatusCode.OK; } catch (NotFoundException ex) { s_logger.Error(ex, "Unable to find user!"); response.Status = HttpStatusCode.NotFound; response.StatusMessages.Add(new StatusMessage(HttpStatusCode.NotFound, "Unable to find user!")); } catch (Exception ex) { s_logger.Error(ex, "Unable to get user!"); response.Status = HttpStatusCode.InternalServerError; response.StatusMessages.Add(new StatusMessage(HttpStatusCode.InternalServerError, "Unable to get user!")); } return(response); }
/// <summary> /// This is used to check that the user that is logged in is actually a valid user in the system /// </summary> /// <param name="username">The username of the user</param> /// <param name="userId">The id of the user</param> /// <returns>An indicator of whether or not the user is valid</returns> public bool VerifyUser(string username, string userId) { bool result = false; try { var dataLayer = new UsersDataLayer(); int userID = -1; if (!int.TryParse(userId, out userID)) { throw new Exception("Invalid user id received."); } UserDTO userFromID = dataLayer.GetUser(userID); UserDTO userFromUsername = dataLayer.GetUserByUsername(username); if (!(userFromID.UserId == userFromUsername.UserId && userFromID.Username == userFromUsername.Username && (!userFromID.FirstTime))) { s_logger.Warn("Unable to verify user."); result = false; } else { result = true; } } catch (NotFoundException ex) { s_logger.Warn(ex, "Unable to find user in system."); } catch (Exception ex) { s_logger.Error(ex, "Unable to perform log in attempt."); } return(result); }
/// <summary> /// This method is responsible for handling the deletion of a user from the system /// </summary> /// <param name="id">The id of the user to be deleted</param> /// <returns>A response that indicates whether or not the deletion was successful</returns> public DeleteUserResponse DeleteUser(int id, string currentUser) { var response = new DeleteUserResponse(); try { var dataLayer = new UsersDataLayer(); UserDTO user = dataLayer.GetUser(id); if (user.Username == currentUser) { response.Status = HttpStatusCode.Forbidden; return(response); } bool result = dataLayer.DeleteUser(id); if (result) { response.Status = HttpStatusCode.OK; } } catch (NotFoundException ex) { s_logger.Warn($"Unable to find the user with id [{id}]"); response.Status = HttpStatusCode.NotFound; } catch (Exception ex) { s_logger.Error(ex, "Unable to delete the user."); response.Status = HttpStatusCode.InternalServerError; } return(response); }