You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
8.2 KiB

  1. using Microsoft.AspNet.Identity;
  2. using Microsoft.AspNet.Identity.EntityFramework;
  3. using Microsoft.Owin.Security;
  4. using Sevomin.Models;
  5. using Sevomin.Models.Helpers;
  6. using System;
  7. using System.Threading.Tasks;
  8. using System.Web;
  9. using System.Web.Mvc;
  10. using Sevomin.Models.Repositories;
  11. namespace Sevomin.WebFrontend.Controllers
  12. {
  13. public class AccountController : BaseController
  14. {
  15. public AccountController()
  16. : this(new SevominUserManager(new UserStore<User>(SevominDbContext.Current)))
  17. {
  18. }
  19. public AccountController(SevominUserManager userManager)
  20. {
  21. UserManager = userManager;
  22. UserManager.UserValidator = new Sevomin.Models.Helpers.SevominUserValidator();
  23. }
  24. public SevominUserManager UserManager { get; private set; }
  25. private IAuthenticationManager AuthenticationManager
  26. {
  27. get
  28. {
  29. return HttpContext.GetOwinContext().Authentication;
  30. }
  31. }
  32. [HttpPost]
  33. [ValidateAntiForgeryToken]
  34. public async Task<ActionResult> Signup(SignupViewModel model)
  35. {
  36. User user;
  37. if (model.IsAvalin)
  38. {
  39. user = new Avalin(model.Email, model.DisplayName);
  40. user.SignUpDate = DateTime.UtcNow;
  41. }
  42. else
  43. {
  44. int spaceIndex = model.DisplayName.IndexOf(' ');
  45. user = new Dovomin(model.Email, model.DisplayName, string.Empty);
  46. user.SignUpDate = DateTime.UtcNow;
  47. }
  48. var result = await UserManager.CreateAsync(user, model.Password);
  49. if (result.Succeeded)
  50. {
  51. await SignInAsync(user, isPersistent: false);
  52. return RedirectToAction("Index", "Home");
  53. }
  54. else
  55. {
  56. AddErrors(result);
  57. }
  58. return View("Intro", model);
  59. }
  60. public async Task<ActionResult> CheckUsername(string Email)
  61. {
  62. bool result = (await UserManager.FindByNameAsync(Email)) == null;
  63. if(result)
  64. return Json(true, JsonRequestBehavior.AllowGet);
  65. return Json("این ایمیل قبلا در سایت استفاده شده. کلمه عبور خود را فراموش کرده اید؟", JsonRequestBehavior.AllowGet);
  66. }
  67. public ActionResult Login(string returnUrl)
  68. {
  69. if(Request.IsAuthenticated)
  70. return RedirectToAction("Index", "Home");
  71. ViewBag.ReturnUrl = returnUrl;
  72. return View();
  73. }
  74. [HttpPost]
  75. [ValidateAntiForgeryToken]
  76. public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
  77. {
  78. if (ModelState.IsValid)
  79. {
  80. var user = await UserManager.FindAsync(model.Username, model.Password);
  81. if (user != null)
  82. {
  83. await SignInAsync(user, true);
  84. return RedirectToLocal(returnUrl);
  85. }
  86. else
  87. {
  88. ModelState.AddModelError("", "نام کاربری و یا کلمه عبور وارد شده صحیح نمی باشد.");
  89. }
  90. }
  91. // If we got this far, something failed, redisplay form
  92. return View(model);
  93. }
  94. public ActionResult Logout()
  95. {
  96. AuthenticationManager.SignOut();
  97. return RedirectToAction("Index", "Home");
  98. }
  99. private async Task SignInAsync(User user, bool isPersistent)
  100. {
  101. AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
  102. var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
  103. AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
  104. }
  105. private ActionResult RedirectToLocal(string returnUrl)
  106. {
  107. if (Url.IsLocalUrl(returnUrl))
  108. {
  109. return Redirect(returnUrl);
  110. }
  111. else
  112. {
  113. return RedirectToAction("Index", "Home");
  114. }
  115. }
  116. private void AddErrors(IdentityResult result)
  117. {
  118. foreach (var error in result.Errors)
  119. {
  120. ModelState.AddModelError("", error);
  121. }
  122. }
  123. [Authorize]
  124. public async Task<ActionResult> MyProfile()
  125. {
  126. User u = await UserManager.FindByNameAsync(User.Identity.Name);
  127. if (u is Avalin)
  128. {
  129. return View("ProfileAvalin", u as Avalin);
  130. }
  131. else
  132. {
  133. Dovomin dovomin = (Dovomin)u;
  134. DovominViewModel dvm = new DovominViewModel(dovomin);
  135. return View("ProfileDovomin", dvm);
  136. }
  137. }
  138. [Authorize]
  139. [HttpPost]
  140. public async Task<ActionResult> ProfileDovomin(string JalaliBirthDate, string FirstName,
  141. string LastName, string ContactPersonEMail, bool IsFullTime,
  142. bool IsPartTime, string Description, HttpPostedFileBase EnglishResume, HttpPostedFileBase PersianResume, FormCollection form)
  143. {
  144. User u = await UserManager.FindByNameAsync(User.Identity.Name);
  145. if (u == null || !(u is Dovomin))
  146. throw new InvalidOperationException("نوع کاربر صحیح نیست");
  147. try
  148. {
  149. Dovomin dovomin = (Dovomin)u;
  150. dovomin.BirthDate = DateAssist.ToMiladi(JalaliBirthDate);
  151. dovomin.Description = Description;
  152. dovomin.FirstName = FirstName;
  153. dovomin.LastName = LastName;
  154. dovomin.IsFulltime = IsFullTime;
  155. dovomin.IsPartTime = IsPartTime;
  156. ParameterRepository.Current.AddParametersToDovomin(dovomin);
  157. foreach (var jp in dovomin.DovominParameters)
  158. {
  159. string value = form[string.Format("value-{0}", jp.Parameter.Id)];
  160. jp.SetValue(value);
  161. }
  162. if (EnglishResume != null)
  163. {
  164. dovomin.EnglishResume = string.Format("{0}-resume-en{2}", User.Identity.Name, "", System.IO.Path.GetExtension(EnglishResume.FileName));
  165. EnglishResume.SaveAs(System.IO.Path.Combine(Server.MapPath("~/App_Data/resumes"), dovomin.EnglishResume));
  166. }
  167. if (PersianResume != null)
  168. {
  169. dovomin.PersianResume = string.Format("{0}-resume-fa{2}", User.Identity.Name, "", System.IO.Path.GetExtension(PersianResume.FileName));
  170. PersianResume.SaveAs(System.IO.Path.Combine(Server.MapPath("~/App_Data/resumes"), dovomin.PersianResume));
  171. }
  172. SevominDbContext.Current.SaveChanges();
  173. }
  174. catch (Exception)
  175. {
  176. throw;
  177. }
  178. return RedirectToAction("MyProfile");
  179. }
  180. [Authorize]
  181. [HttpPost]
  182. public async Task<ActionResult> ProfileAvalin(string CompanyName, string NationalId, string RegisterId,
  183. string Address, string CompanyPhoneNumber, string EMail)
  184. {
  185. User u = await UserManager.FindByNameAsync(User.Identity.Name);
  186. try
  187. {
  188. if (u == null || !(u is Avalin))
  189. throw new InvalidOperationException("نوع کاربر صحیح نیست");
  190. Avalin avalin = (Avalin)u;
  191. avalin.CompanyName = CompanyName;
  192. avalin.NationalId = NationalId;
  193. avalin.RegisterId = RegisterId;
  194. avalin.Address = Address;
  195. avalin.CompanyPhoneNumber = CompanyPhoneNumber;
  196. avalin.Email = EMail;
  197. SevominDbContext.Current.SaveChanges();
  198. }
  199. catch (Exception)
  200. {
  201. throw;
  202. }
  203. return View("ProfileAvalin", u as Avalin);
  204. }
  205. }
  206. }