vgquotes/Src/VideoGameQuotes.Web/Controllers/HomeController.cs

144 lines
4.0 KiB
C#
Raw Permalink Normal View History

2011-02-11 05:21:31 +00:00
using System;
using System.ComponentModel.DataAnnotations;
2011-02-11 05:21:31 +00:00
using System.Net.Mail;
using System.Security.Cryptography;
using System.Text;
using System.Web.Mvc;
using Portoa.Web.Controllers;
using Portoa.Web.Filters;
using Portoa.Web.Security;
2011-03-07 20:56:34 +00:00
using Portoa.Web.Util;
using VideoGameQuotes.Api;
2011-02-11 05:21:31 +00:00
using VideoGameQuotes.Web.Models;
2011-02-09 03:50:45 +00:00
namespace VideoGameQuotes.Web.Controllers {
2011-02-28 10:34:46 +00:00
public static class CaptchaUtil {
private static readonly Random random = new Random();
2011-02-11 05:21:31 +00:00
private static readonly string[] answers = new[] {
2011-02-28 10:34:46 +00:00
"I AM ERROR",
2011-02-11 05:21:31 +00:00
"shyron",
"our princess is in another castle",
"the cake is a lie",
"all your base",
"ganon not gannon",
"thunderbird",
2011-02-28 10:34:46 +00:00
"glad you came, pit",
2011-02-11 05:21:31 +00:00
"ryu huyabasa"
};
2011-02-28 10:34:46 +00:00
public static string GetRandomAnswer() {
return answers[random.Next(answers.Length)];
}
public static string Hash(string value) {
return Convert.ToBase64String(MD5.Create().ComputeHash(Encoding.ASCII.GetBytes(value ?? string.Empty)));
}
}
public class HomeController : Controller {
private readonly IAuthenticationService authenticationService;
private readonly ICurrentUserProvider<User> userProvider;
public HomeController(IAuthenticationService authenticationService, ICurrentUserProvider<User> userProvider) {
this.authenticationService = authenticationService;
this.userProvider = userProvider;
}
protected new ActionResult Json(object data) {
return this.SerializeToJson(data);
}
2011-02-09 03:50:45 +00:00
public ActionResult Index() {
return View();
}
2011-02-11 05:21:31 +00:00
public ActionResult About() {
return View();
}
public ActionResult Robots() {
const string robotsTxt = @"User-Agent: *
Disallow: /search/
";
return Content(robotsTxt, "text/plain", Encoding.UTF8);
}
2011-02-28 10:34:46 +00:00
public ActionResult Favicon() {
return File("/media/images/favicon.png", "image/png");
}
[HttpPost]
public ActionResult Login([Required]string username, [Required]string password) {
if (!ModelState.IsValid) {
return Json(this.CreateJsonErrorResponse("Invalid request"));
}
if (!authenticationService.IsValid(username, password)) {
return Json(this.CreateJsonErrorResponse("Invalid username/password"));
}
authenticationService.Login(username);
return Json(this.CreateJsonResponse());
}
[PrivateAction]
public ActionResult MainMenu() {
var model = new MainMenuModel { User = userProvider.CurrentUser };
return PartialView("MainMenu", model);
}
public ActionResult Logout(string redirectUrl) {
authenticationService.Logout();
return Redirect(redirectUrl ?? "/");
}
2011-02-11 05:21:31 +00:00
public ActionResult Contact() {
2011-02-28 10:34:46 +00:00
var randomAnswer = CaptchaUtil.GetRandomAnswer();
2011-02-11 05:21:31 +00:00
var model = new ContactModel {
UnhashedCaptchaAnswer = randomAnswer,
2011-02-28 10:34:46 +00:00
HashedCaptchaAnswer = CaptchaUtil.Hash(randomAnswer)
2011-02-11 05:21:31 +00:00
};
return View(model);
}
private static void ResetModel(ContactModel model) {
2011-02-28 10:34:46 +00:00
model.UnhashedCaptchaAnswer = CaptchaUtil.GetRandomAnswer();
model.HashedCaptchaAnswer = CaptchaUtil.Hash(model.UnhashedCaptchaAnswer);
2011-02-11 05:21:31 +00:00
model.CaptchaAnswer = null;
}
[HttpPost]
public ActionResult Contact(ContactModel model) {
2011-02-28 10:34:46 +00:00
if (CaptchaUtil.Hash(model.CaptchaAnswer) != model.HashedCaptchaAnswer) {
2011-02-11 05:21:31 +00:00
ModelState.AddModelError("CaptchaAnswer", "You are not human");
}
if (!ModelState.IsValid) {
ResetModel(model);
return View(model);
}
//send email
2011-03-05 22:52:23 +00:00
var fromAddress = new MailAddress("contact@vgquotes.com", "Contact Bot");
var subject = string.Format("[vgquotes.com] Message from {0}", model.Name);
2011-02-11 05:21:31 +00:00
var client = new SmtpClient {
Host = "localhost"
};
var message = new MailMessage(fromAddress, new MailAddress("tmont@tmont.com")) { Subject = subject, Body = model.Message };
message.ReplyToList.Add(new MailAddress(model.Email, model.Name));
try {
client.Send(message);
} catch (Exception e) {
ModelState.AddModelError("client", e.Message);
ResetModel(model);
return View(model);
}
return View("ContactSuccess");
}
2011-02-09 03:50:45 +00:00
}
}