public JsonResult PostRant(PostRequest Rant) { try { var rant = new Rant { UserId = CurrentUser.UserId(), Timestamp = DateTime.Now, Text = Rant.Text.ToUpper() }; if (Rant.Tags == null) { return(Json(new ApiResponse(false, "At least one tag is required."))); } if (Rant.Tags.Any(t => string.IsNullOrWhiteSpace(t))) { return(Json(new ApiResponse(false, "You cannot submit an empty tag."))); } if (string.IsNullOrWhiteSpace(Rant.Text)) { return(Json(new ApiResponse(false, "You cannot submit an empty rant."))); } using (var context = new EnrampageEntities()) { context.Tags.AddRange( Rant.Tags.Except(context.Tags.Where(t => !t.User.Banned).Select(t => t.Text)) .Select(t => new Tag { UserId = rant.UserId, Text = t })); context.SaveChanges(); foreach (var tag in Rant.Tags) { rant.Tags.Add(context.Tags.First(t => !t.User.Banned && t.Text == tag)); } context.Rants.Add(rant); context.SaveChanges(); } return(Json(new ApiResponse(true, "Posted rant successfully.", RantResponse.FromRant(rant, ReportState.Removable)))); } catch (Exception Ex) { LogHelper.Log(Ex); return(Json(new ApiResponse(false, "Failed to post rant."))); } }
public JsonResult RemoveTag(RemoveRequest Request) { try { using (var context = new EnrampageEntities()) { var tag = context.Tags.FirstOrDefault(t => t.Id == Request.Id); if (tag == null) { return(Json(new ApiResponse(false, "Tag not found."))); } if (tag.UserId != CurrentUser.UserId() && !CurrentUser.Admin()) { return(Json(new ApiResponse(false, "Tag not created by you."))); } tag.Rants.Clear(); context.Tags.Remove(tag); context.SaveChanges(); } return(Json(new ApiResponse(true, "Tag removed successfully."))); } catch (Exception Ex) { LogHelper.Log(Ex); return(Json(new ApiResponse(false, "Failed to remove tag."))); } }
public JsonResult RemoveRant(RemoveRequest Request) { try { using (var context = new EnrampageEntities()) { var rant = context.Rants.FirstOrDefault(r => r.Id == Request.Id); if (rant == null) { return(Json(new ApiResponse(false, "Rant not found."))); } if (rant.UserId != CurrentUser.UserId() && !CurrentUser.Admin()) { return(Json(new ApiResponse(false, "Rant not post by you."))); } rant.Tags.Clear(); context.Reports.RemoveRange(rant.Reports); context.Rants.Remove(rant); context.SaveChanges(); } return(Json(new ApiResponse(true, "Rant removed successfully."))); } catch (Exception Ex) { LogHelper.Log(Ex); return(Json(new ApiResponse(false, "Failed to remove rant."))); } }
public static void Log(Exception Ex) { using (var context = new EnrampageEntities()) { context.Logs.Add(new Log { Exception = Ex.ToString() }); context.SaveChanges(); } }
public ActionResult Callback(string ReturnUrl) { try { var loginInfo = AuthenticationManager.GetExternalLoginInfo(); if (loginInfo == null) { TempData["Error"] = "Failed to login."; return(RedirectToAction("Index", "Home")); } using (var context = new EnrampageEntities()) { var user = context.Users.FirstOrDefault(b => b.Email == loginInfo.Email); if (user == null) { user = new User { Email = loginInfo.Email, Admin = false, Banned = false }; context.Users.Add(user); context.SaveChanges(); } else if (user.Banned) { TempData["Error"] = "Your account has been banned."; return(RedirectToAction("Index", "Home")); } var claims = new Claim[] { new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()), new Claim(ClaimTypes.Role, user.Admin ? "Admin" : "User") }; var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignIn(new AuthenticationProperties() { AllowRefresh = true, IsPersistent = true, }, identity); } } catch (Exception Ex) { LogHelper.Log(Ex); TempData["Error"] = "Failed to login."; } TempData["Success"] = "Logged in successfully."; return(Redirect(ReturnUrl)); }
public ActionResult BanUser(int Id) { try { if (!CurrentUser.Admin()) { TempData["Error"] = "You are not an administrator."; return(RedirectToAction("Index", "Home")); } using (var context = new EnrampageEntities()) { var user = context.Users.FirstOrDefault(u => u.Id == Id); if (user == null) { TempData["Error"] = "User does not exist."; return(RedirectToAction("Index", "Home")); } if (user.Banned) { TempData["Error"] = "User already banned."; return(RedirectToAction("Index", "Home")); } user.Banned = true; context.SaveChanges(); } TempData["Success"] = "User banned successfully."; } catch (Exception Ex) { LogHelper.Log(Ex); TempData["Error"] = "Failed to ban user."; } return(RedirectToAction("Index", "Home")); }
public JsonResult ReportRant(ReportRequest Report) { try { var report = new Report { UserId = CurrentUser.UserId(), Timestamp = DateTime.Now, Text = Report.Text }; if (string.IsNullOrWhiteSpace(report.Text)) { return(Json(new ApiResponse(false, "You cannot sumbit an empty report."))); } using (var context = new EnrampageEntities()) { var rant = context.Rants.FirstOrDefault(r => r.Id == Report.Id); if (rant == null) { return(Json(new ApiResponse(false, "Rant not found."))); } if (rant.UserId == report.UserId) { return(Json(new ApiResponse(false, "Rant posted by you."))); } if (context.Reports.Any(r => r.UserId == report.UserId && r.RantId == rant.Id)) { return(Json(new ApiResponse(false, "You have already reported this rant."))); } report.Rant = rant; context.Reports.Add(report); context.SaveChanges(); using (var message = new MailMessage()) { message.From = new MailAddress(ConfigurationManager.AppSettings["ReportFrom"]); foreach (var email in context.Users.Where(u => u.Admin).Select(u => u.Email)) { message.Bcc.Add(email); } message.Subject = "Rant Report"; message.Body = string.Format("Rant: {0}\n\nReport: {1}\n\nRemove Rant: {2}\nBan User: {3}", report.Rant.Text, report.Text, Url.Action("Login", "Account", new { ReturnUrl = Url.Action("RemoveRant", "Rant", new { id = report.Rant.Id }) }, Request.Url.Scheme), Url.Action("Login", "Account", new { ReturnUrl = Url.Action("BanUser", "Account", new { id = report.Rant.UserId }) }, Request.Url.Scheme)); using (var smtpClient = new SmtpClient()) { smtpClient.Send(message); } } } return(Json(new ApiResponse(true, "Report submitted successfully."))); } catch (Exception Ex) { LogHelper.Log(Ex); return(Json(new ApiResponse(false, "Faild to submit report."))); } }