public async Task <IActionResult> SendForgotPassword(string email) { // Checks the entered email is valid if (email == null) { return(RedirectToAction("ForgotPassword", new { error = 0 })); } // Retrieves user and returns error if none found User user = await _repository.GetUserAsync(email); if (user == null) { return(RedirectToAction("ForgotPassword", new { error = 1 })); } // Requests the password reset await _repository.RequestPasswordResetAsync(user); await _repository.SaveChangesAsync(); // Returns view MessageViewModel model = new MessageViewModel() { Title = "Email sent", MessageTitle = "Password reset email sent" }; return(View("Message", model)); }
public async Task <IActionResult> CreateThread(CreateThreadRequest request) { // Returns error if either parameter is null if (request.Title == null || request.Content == null) { return(BadRequest("Title and content cannot be null")); } // Creates and adds thread to database User user = await _repository.GetUserAsync(User); Thread thread = new Thread { Title = request.Title, Content = request.Content, DatePosted = DateTime.Now, User = user }; await _repository.AddThreadAsync(thread); await _repository.SaveChangesAsync(); // Returns JSON response return(Json(new ApiThread(thread))); }
public async Task <IActionResult> AdminUpdate(int id, UpdateThreadRequest request) { // Retrieves thread and updates information Thread thread = await _repository.GetThreadAsync(id); thread.Pinned = request.Pinned ?? thread.Pinned; thread.Locked = request.Locked ?? thread.Locked; await _repository.SaveChangesAsync(); // Returns response return(Json(new ApiThread(thread))); }
public async Task <IActionResult> GenerateTempToken() { User user = await _repository.GetUserAsync(User); TempApiToken token = await _repository.AddTempApiToken(user); await _repository.SaveChangesAsync(); return(Ok(token.Token)); }
public async Task <IActionResult> DeleteComment(int id) { // Deletes the comment and returns error if unsuccessful Result result = await _repository.DeleteUserCommentAsync(id); if (result.Failure) { return(StatusCode(result.Code, result.Error)); } // Saves changes and return await _repository.SaveChangesAsync(); return(Ok()); }
public RepositoryTest() { // Creates ApplicationDbContext from in memory database DbContextOptions <ApplicationDbContext> options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseSqlite(CreateInMemory()).Options; _connection = RelationalOptionsExtension.Extract(options).Connection; ApplicationDbContext context = new ApplicationDbContext(options); context.Database.EnsureCreated(); // Creates repository and adds data repository = new SimpleForumRepository(context, null, null, null); repository.AddUserAsync(new User() { Username = "******", Password = "******", Email = "*****@*****.**" }).Wait(); repository.SaveChangesAsync().Wait(); repository.AddThreadAsync(new Thread() { Title = "first thread", Content = "Thread content", UserID = 1 }).Wait(); repository.SaveChangesAsync().Wait(); }
// Sets up the first user of the forum public async Task <IActionResult> SendSetup(string email, string username, string password, string confirmPassword) { // Redirects if users have already signed up or input is invalid if (_context.Users.Count() != 0) { return(Redirect("/")); } if (email == null || username == null || password == null) { return(RedirectToAction("Setup", new { error = 0 })); } if (password != confirmPassword) { return(RedirectToAction("Setup", new { error = 1 })); } User user = new User() { Username = username, Email = email, Password = password, Role = "Admin", Activated = true }; User addedUser = await _repository.AddUserAsync(user); // Saves changes await _repository.SaveChangesAsync(); // Signs in user ClaimsPrincipal principal = Auth.CreateClaims(user); await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, new AuthenticationProperties { ExpiresUtc = DateTime.UtcNow.AddMonths(1), IsPersistent = true, AllowRefresh = false }); return(Redirect("/")); }
public async Task <IActionResult> PostComment(int id, PostCommentRequest request) { // Returns error if comment is empty if (request.Content == null) { return(BadRequest("Comment cannot be null")); } // Retrieves user and returns error if user is not found, deleted or locked User currentUser = await _repository.GetUserAsync(User); User profileUser = await _repository.GetUserAsync(id); if (profileUser == null || profileUser.Deleted) { return(NotFound("Requested user not found")); } if (profileUser.CommentsLocked) { return(Forbid("The user's comments are locked")); } // Creates user comment and adds it to database UserComment userComment = new UserComment() { Content = request.Content, DatePosted = DateTime.Now, UserID = currentUser.UserID, UserPageID = profileUser.UserID }; Result <UserComment> result = await _repository.PostUserCommentAsync(userComment); if (result.Failure) { return(StatusCode(result.Code, result.Error)); } await _repository.SaveChangesAsync(); // Returns JSON response return(Json(new ApiComment(userComment))); }
public async Task <IActionResult> AdminDeleteUserComment(int id, AdminDeleteRequest request) { // Returns error ir reason is null if (request.Reason == null) { return(BadRequest("Reason cannot be null")); } // Deletes comment and returns error if unauthorised Result result = await _repository.AdminDeleteUserCommentAsync(id, request.Reason); if (result.Failure) { return(StatusCode(result.Code, result.Error)); } // Saves changes and returns await _repository.SaveChangesAsync(); return(Ok()); }
public async Task <IActionResult> MarkRead(int id, UpdateNotificationRequest request) { // Retrieves notification and user id int userID = Tools.GetUserID(User); Notification notification = await _repository.GetNotificationAsync(id); // Returns error if user doesn't own notification if (userID != notification.UserID) { return(Forbid()); } // Updates notification and returns if (request.Read) { notification.Read = true; await _repository.SaveChangesAsync(); } return(Json(new ApiNotification(notification))); }
public async Task <IActionResult> RegisterIncomingToken(RegisterTokenRequest request) { // Retrieves the urls of the services of the instance, and returns error if unsuccessful Result <ServerURLs> urlResult = await SimpleForumClient.GetServerURLs(request.Address); if (urlResult.Failure) { return(StatusCode(urlResult.Code, urlResult.Error)); } // Adds the token to checks it is valid IncomingServerToken token = new IncomingServerToken() { Address = request.Address, ApiAddress = urlResult.Value.APIURL, CrossConnectionAddress = urlResult.Value.CrossConnectionURL, Token = request.Token }; Result addResult = await _repository.AddIncomingServerToken(token); if (addResult.Failure) { return(StatusCode(addResult.Code, addResult.Error)); } Result checkResult = await _crossConnectionClient.CheckToken(token.CrossConnectionAddress, request.Token); // Returns error if token is invalid, otherwise saves changes if (checkResult.Failure) { return(StatusCode(checkResult.Code, checkResult.Error)); } await _repository.SaveChangesAsync(); return(Ok()); }
public async Task <IActionResult> CreateThread(string title, string content) { // Redirects if the title or content are empty if (title == null || content == null) { return(Redirect("/Thread/Create")); } // Creates and adds thread to database DateTime time = DateTime.Now; int userID = Tools.GetUserID(User); Thread thread = new Thread() { Title = title, Content = content, DatePosted = time, UserID = userID }; await _repository.AddThreadAsync(thread); await _repository.SaveChangesAsync(); // Redirects to thread int threadID = thread.ID; return(Redirect("/Thread?id=" + threadID)); }
public async Task <IActionResult> PostUserComment(string content, int userPageID) { // Redirects if content is null if (content == null) { return(Redirect("/")); } // Creates and posts comment UserComment comment = new UserComment() { Content = content, DatePosted = DateTime.Now, UserID = Tools.GetUserID(User), UserPageID = userPageID, }; Result result = await _repository.PostUserCommentAsync(comment); // Saves changes and redirects if successful, otherwise returns 403 if (result.Success) { await _repository.SaveChangesAsync(); return(Redirect("/User?id=" + userPageID)); } return(StatusCode(result.Code)); }