Skip to content
Browse files

moved standalone actions to account controller

  • Loading branch information...
1 parent 1dc8cd7 commit 1479a97d2ec9398e4131bda7b97b3f1e233e8db5 @aquiladev committed Mar 23, 2012
View
108 MetalMastery.Web.Tests/AccountControllerTests.cs
@@ -0,0 +1,108 @@
+using System.Web.Mvc;
+using AutoMapper;
+using MetalMastery.Core.Domain;
+using MetalMastery.Core.Mvc;
+using MetalMastery.Services.Interfaces;
+using MetalMastery.Web.App_LocalResources;
+using MetalMastery.Web.Controllers;
+using MetalMastery.Web.Models;
+using NUnit.Framework;
+using Rhino.Mocks;
+
+namespace MetalMastery.Web.Tests
+{
+ [TestFixture]
+ public class AccountControllerTests
+ {
+ private MockRepository _mockRepository;
+ private IAuthenticationService _authenticationService;
+ private IUserService _userService;
+ private IEmailSender _emailSender;
+
+ private AccountController _userController;
+
+ private const string EmailTest = "test@ads.we";
+ private const string PwdTest = "pwd123!";
+
+ [SetUp]
+ public void SetUp()
+ {
+ _mockRepository = new MockRepository();
+ _authenticationService = _mockRepository.DynamicMock<IAuthenticationService>();
+ _userService = _mockRepository.DynamicMock<IUserService>();
+ _emailSender = _mockRepository.DynamicMock<IEmailSender>();
+
+ _userController = new AccountController(
+ _authenticationService,
+ _userService,
+ _emailSender);
+
+ Mapper.CreateMap<SignUpModel, User>()
+ .ForMember(dest => dest.Id, opt => opt.Ignore())
+ .ForMember(dest => dest.Password, opt=>opt.Ignore())
+ .ForMember(dest => dest.IsAdmin, opt => opt.Ignore())
+ .ForMember(dest => dest.Orders, opt => opt.Ignore())
+ .ForMember(dest => dest.Articles, opt => opt.Ignore());
+ }
+
+ [Test]
+ public void SignIn_IncorectEmailOrPassword_ReturnError()
+ {
+ using (_mockRepository.Record())
+ {
+ _userService.Stub(x => x.ValidateUser(string.Empty, null))
+ .IgnoreArguments().Return(false);
+ }
+
+ var result = _userController.SignIn(new SignInModel { Email = EmailTest, Password = PwdTest });
+
+ Assert.AreEqual(((MmJsonResult)result).Success, false);
+ Assert.AreEqual(((MmJsonResult)result).Errors.Count, 1);
+ }
+
+ [Test]
+ public void SignIn_CorrectAll()
+ {
+ using (_mockRepository.Record())
+ {
+ _userService.Stub(x => x.ValidateUser(string.Empty, null))
+ .IgnoreArguments().Return(true);
+ _userService.Stub(x => x.GetUserByEmail(string.Empty))
+ .IgnoreArguments().Return(new User { Email = EmailTest });
+ }
+
+ var result = _userController.SignIn(new SignInModel { Email = EmailTest, Password = PwdTest });
+
+ Assert.AreEqual(((MmJsonResult)result).Success, true);
+ }
+
+ [Test]
+ public void SignUp_CorrectInsert()
+ {
+ using (_mockRepository.Record())
+ {
+ _userService.Stub(x => x.Insert(null))
+ .IgnoreArguments();
+ }
+
+ var result = _userController.SignUp(new SignUpModel { Email = EmailTest, Password = PwdTest });
+
+ Assert.AreEqual(((MmJsonResult)result).Success, true);
+ }
+
+ [Test]
+ public void SignUp_DublicateEmail_ReturnError()
+ {
+ using (_mockRepository.Record())
+ {
+ _userService.Stub(x => x.GetUserByEmail(string.Empty))
+ .IgnoreArguments().Return(new User { Email = EmailTest });
+ }
+
+ var result = _userController.SignUp(new SignUpModel{ Email = EmailTest, Password = PwdTest });
+
+ Assert.AreEqual(((MmJsonResult)result).Success, false);
+ Assert.AreEqual(((MmJsonResult)result).Errors.Count, 1);
+ }
+ }
+}
View
194 MetalMastery.Web.Tests/Admin/AccountControllerTests.cs
@@ -0,0 +1,194 @@
+using System;
+using System.Collections.Generic;
+using System.Web.Mvc;
+using AutoMapper;
+using MetalMastery.Core;
+using MetalMastery.Core.Domain;
+using MetalMastery.Services.Interfaces;
+using MetalMastery.Web.App_LocalResources;
+using MetalMastery.Web.Areas.Admin.Controllers;
+using MetalMastery.Web.Areas.Admin.Models;
+using NUnit.Framework;
+using Rhino.Mocks;
+
+namespace MetalMastery.Web.Tests.Admin
+{
+ [TestFixture]
+ public class AdminUserControllerTests
+ {
+ private IUserService _userService;
+ private MockRepository _mockRepository;
+ private AccountController _userController;
+
+ [SetUp]
+ public void SetUp()
+ {
+ _mockRepository = new MockRepository();
+ _userService = _mockRepository.DynamicMock<IUserService>();
+
+ _userController = new AccountController(_userService);
+ Mapper.CreateMap<User, UserModel>();
+ Mapper.CreateMap<UserModel, User>();
+ }
+
+ [Test]
+ public void Index_ReturnUsers_CorrectCount()
+ {
+ IPagedList<User> userList = new PagedList<User>(
+ new List<User>
+ {
+ new User(),
+ new User()
+ },
+ 0, 2);
+
+ using (_mockRepository.Record())
+ {
+ _userService.Stub(x => x.GetAll(0, 0)).IgnoreArguments().Return(userList);
+ }
+
+ var result = _userController.Index();
+
+ Assert.AreEqual(((List<UserModel>)result.Model).Count, 2);
+ }
+
+ [Test]
+ public void Edit_IdIsEmpty_Error()
+ {
+ var result = _userController.Edit(Guid.Empty);
+
+ Assert.AreEqual(((ViewResultBase)result).ViewBag.Error, MmResources.IdEmptyError);
+ Assert.IsNull(((ViewResultBase)result).Model);
+ }
+
+ [Test]
+ public void Edit_UserNotFound_Error()
+ {
+ var id = Guid.NewGuid();
+
+ using (_mockRepository.Record())
+ {
+ _userService.Stub(x => x.GetEntityById(id)).Return(null);
+ }
+
+ var result = _userController.Edit(id);
+
+ Assert.AreEqual(((ViewResultBase)result).ViewBag.Error, MmResources.UserNotFound);
+ Assert.IsNull(((ViewResultBase)result).Model);
+ }
+
+ [Test]
+ public void Edit_FoundedUser_CorrectView()
+ {
+ var id = Guid.NewGuid();
+
+ using (_mockRepository.Record())
+ {
+ _userService.Stub(x => x.GetEntityById(id)).Return(new User());
+ }
+
+ var result = _userController.Edit(id);
+
+ Assert.IsNotNull(((ViewResultBase)result).Model);
+ }
+
+ [Test]
+ public void EditPost_ModelIncorrect()
+ {
+ _userController.ModelState.AddModelError("Email", "err");
+
+ using (_mockRepository.Record())
+ {
+ _userService.Stub(x => x.GetEntityById(Guid.NewGuid()))
+ .IgnoreArguments()
+ .Return(new User());
+ }
+
+ var result = _userController.Edit(new UserModel());
+
+ Assert.IsNotNull(((ViewResultBase)result).Model);
+ }
+
+ [Test]
+ public void EditPost_CorrectEdit_Redirect()
+ {
+ var result = (RedirectToRouteResult)_userController.Edit(new UserModel());
+ Assert.AreEqual(result.RouteValues["action"], "Index");
+ }
+
+ [Test]
+ public void Delete_GetUser_ModelNotNull()
+ {
+ using (_mockRepository.Record())
+ {
+ _userService.Stub(x => x.GetEntityById(Guid.NewGuid()))
+ .IgnoreArguments()
+ .Return(new User());
+ }
+
+ var result = _userController.Delete(Guid.NewGuid());
+ Assert.IsNotNull(((ViewResultBase)result).Model);
+ }
+
+ [Test]
+ [ExpectedException(typeof(ArgumentNullException))]
+ public void Delete_IdIsEmpty_Exception()
+ {
+ _userController.Delete(Guid.Empty);
+ }
+
+ [Test]
+ public void Delete_UserNotFound_Error()
+ {
+ using (_mockRepository.Record())
+ {
+ _userService.Stub(x => x.GetEntityById(Guid.NewGuid()))
+ .IgnoreArguments()
+ .Return(null);
+ }
+
+ var result = _userController.Delete(Guid.NewGuid());
+
+ Assert.AreEqual(((ViewResultBase)result).ViewBag.Error, MmResources.UserNotFound);
+ Assert.IsNull(((ViewResultBase)result).Model);
+ }
+
+ [Test]
+ [ExpectedException(typeof(ArgumentNullException))]
+ public void DeleteConfirmed_IdIsEmpty_Exception()
+ {
+ _userController.DeleteConfirmed(Guid.Empty);
+ }
+
+ [Test]
+ public void DeleteConfirmed_UserNotFound_Error()
+ {
+ using (_mockRepository.Record())
+ {
+ _userService.Stub(x => x.GetEntityById(Guid.NewGuid()))
+ .IgnoreArguments()
+ .Return(null);
+ }
+
+ var result = _userController.DeleteConfirmed(Guid.NewGuid());
+
+ Assert.AreEqual(((ViewResultBase)result).ViewBag.Error, MmResources.UserNotFound);
+ Assert.IsNull(((ViewResultBase)result).Model);
+ }
+
+ [Test]
+ public void DeleteConfirmed_CorrectDelete_Redirect()
+ {
+ using (_mockRepository.Record())
+ {
+ _userService.Stub(x => x.GetEntityById(Guid.NewGuid()))
+ .IgnoreArguments()
+ .Return(new User());
+ }
+
+ var result = (RedirectToRouteResult)_userController.DeleteConfirmed(Guid.NewGuid());
+
+ Assert.AreEqual(result.RouteValues["action"], "Index");
+ }
+ }
+}
View
131 MetalMastery.Web.Tests/Standalone/AccountControllerTests.cs
@@ -0,0 +1,131 @@
+using System.Web.Mvc;
+using AutoMapper;
+using MetalMastery.Core.Domain;
+using MetalMastery.Services.Interfaces;
+using MetalMastery.Web.App_LocalResources;
+using MetalMastery.Web.Areas.Standalone.Controllers;
+using MetalMastery.Web.Models;
+using NUnit.Framework;
+using Rhino.Mocks;
+
+namespace MetalMastery.Web.Tests.Standalone
+{
+ [TestFixture]
+ public class AccountControllerTests
+ {
+ private MockRepository _mockRepository;
+ private IAuthenticationService _authenticationService;
+ private IUserService _userService;
+ private IEmailSender _emailSender;
+
+ private AccountController _userController;
+
+ private const string EmailTest = "test@ads.we";
+ private const string PwdTest = "pwd123!";
+
+ [SetUp]
+ public void SetUp()
+ {
+ _mockRepository = new MockRepository();
+ _authenticationService = _mockRepository.DynamicMock<IAuthenticationService>();
+ _userService = _mockRepository.DynamicMock<IUserService>();
+ _emailSender = _mockRepository.DynamicMock<IEmailSender>();
+
+ _userController = new AccountController(
+ _authenticationService,
+ _userService,
+ _emailSender);
+
+ Mapper.CreateMap<SignUpModel, User>()
+ .ForMember(dest => dest.Id, opt => opt.Ignore())
+ .ForMember(dest => dest.Password, opt => opt.Ignore())
+ .ForMember(dest => dest.IsAdmin, opt => opt.Ignore())
+ .ForMember(dest => dest.Orders, opt => opt.Ignore())
+ .ForMember(dest => dest.Articles, opt => opt.Ignore());
+ }
+
+ [Test]
+ public void SignIn_IncorectEmailOrPassword_ReturnError()
+ {
+ using (_mockRepository.Record())
+ {
+ _userService.Stub(x => x.ValidateUser(string.Empty, null))
+ .IgnoreArguments().Return(false);
+ }
+
+ var result = _userController.SignIn(new SignInModel { Email = EmailTest, Password = PwdTest });
+
+ Assert.AreEqual(((ViewResultBase)result).ViewBag.Error, MmResources.UserNotFound);
+ Assert.IsNull(((ViewResultBase)result).Model);
+ }
+
+
+ [Test]
+ public void SignIn_FoundedUser_RedirectToHome()
+ {
+ using (_mockRepository.Record())
+ {
+ _userService.Stub(x => x.ValidateUser(string.Empty, null))
+ .IgnoreArguments().Return(true);
+ _userService.Stub(x => x.GetUserByEmail(string.Empty))
+ .IgnoreArguments().Return(new User { Email = EmailTest });
+ }
+
+ var result = (RedirectToRouteResult)_userController.SignIn(new SignInModel { Email = EmailTest, Password = PwdTest });
+
+ Assert.AreEqual(result.RouteValues["action"], "Index");
+ Assert.AreEqual(result.RouteValues["controller"], "Home");
+ }
+
+
+ [Test]
+ public void SignIn_ModelStateError_GoToView()
+ {
+ _userController.ModelState.AddModelError("Email", "err");
+
+ var result = _userController.SignIn(new SignInModel { Email = EmailTest, Password = PwdTest });
+
+ Assert.IsNull(((ViewResultBase)result).Model);
+ }
+
+ [Test]
+ public void SignUp_ModelStateError_GoToView()
+ {
+ _userController.ModelState.AddModelError("Email", "err");
+
+ var result = _userController.SignUp(new SignUpModel { Email = EmailTest, Password = PwdTest });
+
+ Assert.IsNull(((ViewResultBase)result).Model);
+ }
+
+ [Test]
+ public void SignUp_DublicateUser_ReturnError()
+ {
+ using (_mockRepository.Record())
+ {
+ _userService.Stub(x => x.GetUserByEmail(string.Empty))
+ .IgnoreArguments().Return(new User { Email = EmailTest });
+ }
+
+ var result = _userController.SignUp(new SignUpModel { Email = EmailTest, Password = PwdTest });
+
+ Assert.AreEqual(((ViewResultBase)result).ViewBag.Error, MmResources.DublicateUser);
+ Assert.IsNull(((ViewResultBase)result).Model);
+ }
+
+ [Test]
+ public void SignUp_CorrectInsert_RedirectToHome()
+ {
+ using (_mockRepository.Record())
+ {
+ _userService.Stub(x => x.Insert(null))
+ .IgnoreArguments();
+ }
+
+ var result = (RedirectToRouteResult)_userController.SignUp(new SignUpModel { Email = EmailTest, Password = PwdTest });
+
+ Assert.AreEqual(result.RouteValues["action"], "Index");
+ Assert.AreEqual(result.RouteValues["controller"], "Home");
+ }
+ }
+}
View
93 metalmastery.web/Areas/Admin/Controllers/AccountController.cs
@@ -0,0 +1,93 @@
+using System;
+using System.Linq;
+using System.Web.Mvc;
+using MetalMastery.Services.Interfaces;
+using MetalMastery.Web.App_LocalResources;
+using MetalMastery.Web.Areas.Admin.Models;
+
+namespace MetalMastery.Web.Areas.Admin.Controllers
+{
+ public class AccountController : BaseAdminController
+ {
+ private readonly IUserService _userService;
+
+ public AccountController(IUserService userService)
+ {
+ _userService = userService;
+ }
+
+ public ViewResult Index()
+ {
+ return View(_userService.GetAll(0, 10)
+ .Select(x => x.ToModel())
+ .ToList());
+ }
+
+ public ActionResult Edit(Guid id)
+ {
+ if (id.Equals(Guid.Empty))
+ {
+ ViewBag.Error = MmResources.IdEmptyError;
+ return View();
+ }
+
+ UserModel user = _userService.GetEntityById(id).ToModel();
+
+ if (user == null)
+ {
+ ViewBag.Error = MmResources.UserNotFound;
+ return View();
+ }
+
+ return View(user);
+ }
+
+ [HttpPost]
+ public ActionResult Edit(UserModel user)
+ {
+ if (!ModelState.IsValid)
+ {
+ return View(_userService.GetEntityById(user.Id).ToModel());
+ }
+
+ _userService.Update(user);
+
+ return RedirectToAction("Index");
+ }
+
+ public ActionResult Delete(Guid id)
+ {
+ if (id.Equals(default(Guid)))
+ throw new ArgumentNullException("id");
+
+ var user = _userService.GetEntityById(id);
+
+ if (user == null)
+ {
+ ViewBag.Error = MmResources.UserNotFound;
+ return View();
+ }
+
+ return View(user.ToModel());
+ }
+
+ [HttpPost, ActionName("Delete")]
+ public ActionResult DeleteConfirmed(Guid id)
+ {
+ if (id.Equals(default(Guid)))
+ throw new ArgumentNullException("id");
+
+ var user = _userService.GetEntityById(id);
+
+ if (user == null)
+ {
+ ViewBag.Error = MmResources.UserNotFound;
+ return View();
+ }
+
+ _userService.Delete(user);
+
+ return RedirectToAction("Index");
+ }
+ }
+}
View
68 metalmastery.web/Areas/Standalone/Controllers/AccountController.cs
@@ -0,0 +1,68 @@
+using System.Web.Mvc;
+using MetalMastery.Services.Interfaces;
+using MetalMastery.Web.App_LocalResources;
+using MetalMastery.Web.Models;
+
+namespace MetalMastery.Web.Areas.Standalone.Controllers
+{
+ public class AccountController : Controller
+ {
+ private readonly IAuthenticationService _authenticationService;
+ private readonly IUserService _userService;
+ private readonly IEmailSender _emailSender;
+
+ public AccountController(
+ IAuthenticationService authenticationService,
+ IUserService userService,
+ IEmailSender emailSender)
+ {
+ _authenticationService = authenticationService;
+ _userService = userService;
+ _emailSender = emailSender;
+ }
+
+ public ActionResult SignIn(SignInModel user)
+ {
+ if (!ModelState.IsValid)
+ {
+ return View();
+ }
+
+ if (_userService.ValidateUser(user.Email, user.Password))
+ {
+ _authenticationService.SignIn(
+ _userService.GetUserByEmail(user.Email),
+ user.RememberMe);
+
+ return RedirectToAction("Index", "Home", new { area = "" });
+ }
+
+ ViewBag.Error = MmResources.UserNotFound;
+ return View();
+ }
+
+ public ActionResult SignUp(SignUpModel user)
+ {
+ if (!ModelState.IsValid)
+ {
+ return View();
+ }
+
+ if (_userService.GetUserByEmail(user.Email) != null)
+ {
+ ViewBag.Error = MmResources.DublicateUser;
+ return View();
+ }
+
+ _userService.Insert(user.ToEntity());
+
+ return RedirectToAction("Index", "Home", new { area = "" });
+ }
+
+ public ActionResult SignOut()
+ {
+ _authenticationService.SignOut();
+ return RedirectToAction("Index", "Home", new { area = "" });
+ }
+ }
+}
View
24 metalmastery.web/Areas/Standalone/StandaloneAreaRegistration.cs
@@ -0,0 +1,24 @@
+using System.Web.Mvc;
+
+namespace MetalMastery.Web.Areas.Standalone
+{
+ public class StandaloneAreaRegistration : AreaRegistration
+ {
+ public override string AreaName
+ {
+ get
+ {
+ return "Standalone";
+ }
+ }
+
+ public override void RegisterArea(AreaRegistrationContext context)
+ {
+ context.MapRoute(
+ "Standalone_default",
+ "s/{controller}/{action}/{id}",
+ new { action = "Index", id = UrlParameter.Optional }
+ );
+ }
+ }
+}
View
47 metalmastery.web/Areas/Standalone/Views/Account/SignIn.cshtml
@@ -0,0 +1,47 @@
+@using MetalMastery.Web.App_LocalResources
+@model MetalMastery.Web.Models.SignInModel
+@{
+ Layout = "~/Views/Shared/_Layout.cshtml";
+ ViewBag.Title = "SignIn";
+}
+<script type="text/javascript">
+ $(document).ready(function () {
+ if ("@ViewBag.Error".length > 0) {
+ MM.Notification.show("@ViewBag.Error", "error");
+ }
+ });
+</script>
+<form action="/s/account/signin">
+ @using (Html.BeginForm())
+ {
+ @Html.ValidationSummary(true)
+ <div id="signin">
+ <fieldset class="textbox">
+ <div class="editor-label">@MmResources.EmailLbl</div>
+ <div class="editor-field">
+ @Html.EditorFor(model => model.Email)
+ @Html.ValidationMessageFor(model => model.Email)
+ </div>
+
+ <div class="editor-label">@MmResources.PasswordLbl</div>
+ <div class="editor-field">
+ @Html.EditorFor(model => model.Password)
+ @Html.ValidationMessageFor(model => model.Password)
+ </div>
+ </fieldset>
+ <fieldset class="subchck">
+ <div class="remember">
+ <div>
+ @Html.CheckBoxFor(model => model.RememberMe)
+ </div>
+ <div>
+ @MmResources.RememberMeLbl
+ </div>
+ </div>
+ <input type="submit" class="button" value="@MmResources.EnterBtn" />
+ </fieldset>
+ @Html.ActionLink(MmResources.WantToReg, "SignUp")
+ </div>
+ }
+</form>
+
View
45 metalmastery.web/Areas/Standalone/Views/Account/SignUp.cshtml
@@ -0,0 +1,45 @@
+@using MetalMastery.Web.App_LocalResources
+@model MetalMastery.Web.Models.SignUpModel
+@{
+ Layout = "~/Views/Shared/_Layout.cshtml";
+ ViewBag.Title = "SignUp";
+}
+<script type="text/javascript">
+ $(document).ready(function () {
+ if ("@ViewBag.Error".length > 0) {
+ MM.Notification.show("@ViewBag.Error", "error");
+ }
+ });
+</script>
+<form action="/s/account/signup">
+ @using (Html.BeginForm())
+ {
+ @Html.ValidationSummary(true)
+ <div id="signup">
+ <fieldset class="textbox">
+ <div class="editor-label">@MmResources.EmailLbl</div>
+ <div class="editor-field">
+ @Html.EditorFor(model => model.Email)
+ @Html.ValidationMessageFor(model => model.Email)
+ </div>
+
+ <div class="editor-label">@MmResources.PasswordLbl</div>
+ <div class="editor-field">
+ @Html.EditorFor(model => model.Password)
+ @Html.ValidationMessageFor(model => model.Password)
+ </div>
+
+ <div class="editor-label">@MmResources.ConfirmPasswordLbl</div>
+ <div class="editor-field">
+ @Html.EditorFor(model => model.ConfirmPassword)
+ @Html.ValidationMessageFor(model => model.ConfirmPassword)
+ </div>
+
+ </fieldset>
+ <fieldset class="subchck">
+ <input type="submit" class="button" value="@MmResources.SignUpBtn" />
+ </fieldset>
+ </div>
+ }
+</form>
+
View
58 metalmastery.web/Areas/Standalone/Views/Web.config
@@ -0,0 +1,58 @@
+<?xml version="1.0"?>
+
+<configuration>
+ <configSections>
+ <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
+ <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
+ <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
+ </sectionGroup>
+ </configSections>
+
+ <system.web.webPages.razor>
+ <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
+ <pages pageBaseType="System.Web.Mvc.WebViewPage">
+ <namespaces>
+ <add namespace="System.Web.Mvc" />
+ <add namespace="System.Web.Mvc.Ajax" />
+ <add namespace="System.Web.Mvc.Html" />
+ <add namespace="System.Web.Routing" />
+ </namespaces>
+ </pages>
+ </system.web.webPages.razor>
+
+ <appSettings>
+ <add key="webpages:Enabled" value="false" />
+ </appSettings>
+
+ <system.web>
+ <httpHandlers>
+ <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
+ </httpHandlers>
+
+ <!--
+ Enabling request validation in view pages would cause validation to occur
+ after the input has already been processed by the controller. By default
+ MVC performs request validation before a controller processes the input.
+ To change this behavior apply the ValidateInputAttribute to a
+ controller or action.
+ -->
+ <pages
+ validateRequest="false"
+ pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
+ pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
+ userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
+ <controls>
+ <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
+ </controls>
+ </pages>
+ </system.web>
+
+ <system.webServer>
+ <validation validateIntegratedModeConfiguration="false" />
+
+ <handlers>
+ <remove name="BlockViewHandler"/>
+ <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
+ </handlers>
+ </system.webServer>
+</configuration>
View
89 metalmastery.web/Controllers/AccountController.cs
@@ -0,0 +1,89 @@
+using System.Collections.Generic;
+using System.Web.Mvc;
+using MetalMastery.Core.Domain;
+using MetalMastery.Core.Mvc;
+using MetalMastery.Services.Interfaces;
+using MetalMastery.Web.App_LocalResources;
+using MetalMastery.Web.Framework.Filters;
+using MetalMastery.Web.Models;
+
+namespace MetalMastery.Web.Controllers
+{
+ [SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
+ public class AccountController : Controller
+ {
+ private readonly IAuthenticationService _authenticationService;
+ private readonly IUserService _userService;
+ private readonly IEmailSender _emailSender;
+
+ public AccountController(
+ IAuthenticationService authenticationService,
+ IUserService userService,
+ IEmailSender emailSender)
+ {
+ _authenticationService = authenticationService;
+ _userService = userService;
+ _emailSender = emailSender;
+ }
+
+ [CheckModelFilter]
+ public JsonResult SignIn(SignInModel user)
+ {
+ User authUser;
+
+ if (_userService.ValidateUser(user.Email, user.Password))
+ {
+ authUser = _userService.GetUserByEmail(user.Email);
+ _authenticationService.SignIn(
+ authUser,
+ user.RememberMe);
+ }
+ else
+ {
+ return new MmJsonResult(
+ data: null,
+ success: false,
+ errors: new List<string> { MmResources.LoginPasswordInvalid });
+ }
+
+ return new MmJsonResult(authUser.Email);
+ }
+
+ [CheckModelFilter]
+ public JsonResult SignUp(SignUpModel user)
+ {
+ if (_userService.GetUserByEmail(user.Email) != null)
+ {
+ return new MmJsonResult(
+ data: null,
+ success: false,
+ errors: new List<string> { MmResources.DublicateUser });
+ }
+
+ _userService.Insert(user.ToEntity());
+
+ _emailSender.SendEmail(
+ MmResources.CongratulationSbjTemplate,
+ string.Format(MmResources.CongratulationMsgTemplate, user.Email),
+ user.Email, user.Email);
+
+ return new MmJsonResult(data: null);
+ }
+
+ public JsonResult IsAuthenticate()
+ {
+ return new MmJsonResult(new
+ {
+ User.Identity.IsAuthenticated,
+ User = User.Identity.Name,
+ IsAdmin = User.IsInRole(Roles.Administrator.ToString())
+ });
+ }
+
+ public JsonResult SignOut()
+ {
+ _authenticationService.SignOut();
+ return new MmJsonResult(data: null);
+ }
+ }
+}
View
24 metalmastery.web/Models/SignInModel.cs
@@ -0,0 +1,24 @@
+using System.ComponentModel.DataAnnotations;
+
+namespace MetalMastery.Web.Models
+{
+ [MetadataType(typeof(LogOnModelMetadata))]
+ public class SignInModel
+ {
+ public string Email { get; set; }
+
+ public string Password { get; set; }
+
+ public bool RememberMe { get; set; }
+ }
+
+ public class LogOnModelMetadata
+ {
+ [Required]
+ public string Email { get; set; }
+
+ [Required]
+ [DataType(DataType.Password)]
+ public string Password { get; set; }
+ }
+}
View
26 metalmastery.web/Models/SignUpModel.cs
@@ -0,0 +1,26 @@
+using System.ComponentModel.DataAnnotations;
+using System.Web.Mvc;
+using MetalMastery.Web.App_LocalResources;
+
+namespace MetalMastery.Web.Models
+{
+ public class SignUpModel
+ {
+ [Required]
+ [DataType(DataType.EmailAddress)]
+ [StringLength(256, ErrorMessageResourceType = typeof(MmResources), ErrorMessageResourceName = "EmailLength")]
+ [RegularExpression(@"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$", ErrorMessageResourceType = typeof(MmResources), ErrorMessageResourceName = "EmailIncorrect")]
+ public string Email { get; set; }
+
+ [Required]
+ [DataType(DataType.Password)]
+ [StringLength(32, MinimumLength = 5, ErrorMessageResourceType = typeof(MmResources), ErrorMessageResourceName = "PasswordLength")]
+ [RegularExpression(".*[!@#$%^&+=].*", ErrorMessageResourceType = typeof(MmResources), ErrorMessageResourceName = "PasswordIncorrect")]
+ public string Password { get; set; }
+
+ [Required]
+ [DataType(DataType.Password)]
+ [Compare("Password", ErrorMessageResourceType = typeof(MmResources), ErrorMessageResourceName = "ConfirmPasswordErrorMessage")]
+ public string ConfirmPassword { get; set; }
+ }
+}

0 comments on commit 1479a97

Please sign in to comment.
Something went wrong with that request. Please try again.