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.

241 lines
8.5 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. }
  23. public SevominUserManager UserManager { get; private set; }
  24. private IAuthenticationManager AuthenticationManager
  25. {
  26. get
  27. {
  28. return HttpContext.GetOwinContext().Authentication;
  29. }
  30. }
  31. [HttpPost]
  32. [ValidateAntiForgeryToken]
  33. public async Task<ActionResult> Signup(SignupViewModel model)
  34. {
  35. User user;
  36. if (model.IsAvalin)
  37. {
  38. user = new Avalin(model.Email, model.DisplayName);
  39. user.SignUpDate = DateTime.UtcNow;
  40. }
  41. else
  42. {
  43. int spaceIndex = model.DisplayName.IndexOf(' ');
  44. user = new Dovomin(model.Email, model.DisplayName, string.Empty);
  45. user.SignUpDate = DateTime.UtcNow;
  46. }
  47. user.DisplayName = model.DisplayName;
  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(bool? success)
  125. {
  126. if (success.HasValue)
  127. {
  128. ViewBag.Result = new PostResultViewModel(success.Value, success.Value ? "پروفایل شما با موفقیت ویرایش شد." : "در ویرایش پروفایل شما خطایی رخ داده.");
  129. }
  130. User u = await UserManager.FindByNameAsync(User.Identity.Name);
  131. if (u is Avalin)
  132. {
  133. return View("ProfileAvalin", u as Avalin);
  134. }
  135. else
  136. {
  137. Dovomin dovomin = (Dovomin)u;
  138. DovominViewModel dvm = new DovominViewModel(dovomin);
  139. return View("ProfileDovomin", dvm);
  140. }
  141. }
  142. [Authorize]
  143. [HttpPost]
  144. public async Task<ActionResult> ProfileDovomin(string JalaliBirthDate, string FirstName,
  145. string LastName, string ContactPersonEMail, bool IsFullTime,
  146. bool IsPartTime, string Description, HttpPostedFileBase EnglishResume, HttpPostedFileBase PersianResume, FormCollection form)
  147. {
  148. User u = await UserManager.FindByNameAsync(User.Identity.Name);
  149. if (u == null || !(u is Dovomin))
  150. throw new InvalidOperationException("نوع کاربر صحیح نیست");
  151. try
  152. {
  153. Dovomin dovomin = (Dovomin)u;
  154. dovomin.BirthDate = DateAssist.ToMiladi(JalaliBirthDate);
  155. dovomin.Description = Description;
  156. dovomin.FirstName = FirstName;
  157. dovomin.LastName = LastName;
  158. dovomin.IsFulltime = IsFullTime;
  159. dovomin.IsPartTime = IsPartTime;
  160. ParameterRepository.Current.AddParametersToDovomin(dovomin);
  161. foreach (var jp in dovomin.DovominParameters)
  162. {
  163. string value = form[string.Format("value-{0}", jp.Parameter.Id)];
  164. jp.SetValue(value);
  165. }
  166. if (EnglishResume != null)
  167. {
  168. dovomin.EnglishResume = string.Format("{0}-resume-en{2}", User.Identity.Name, "", System.IO.Path.GetExtension(EnglishResume.FileName));
  169. EnglishResume.SaveAs(System.IO.Path.Combine(Server.MapPath("~/App_Data/resumes"), dovomin.EnglishResume));
  170. }
  171. if (PersianResume != null)
  172. {
  173. dovomin.PersianResume = string.Format("{0}-resume-fa{2}", User.Identity.Name, "", System.IO.Path.GetExtension(PersianResume.FileName));
  174. PersianResume.SaveAs(System.IO.Path.Combine(Server.MapPath("~/App_Data/resumes"), dovomin.PersianResume));
  175. }
  176. SevominDbContext.Current.SaveChanges();
  177. return RedirectToAction("MyProfile", new { success = true });
  178. }
  179. catch (Exception)
  180. {
  181. throw;
  182. }
  183. }
  184. [Authorize]
  185. [HttpPost]
  186. public async Task<ActionResult> ProfileAvalin(string CompanyName, string NationalId, string RegisterId,
  187. string Address, string CompanyPhoneNumber, string EMail)
  188. {
  189. User u = await UserManager.FindByNameAsync(User.Identity.Name);
  190. try
  191. {
  192. if (u == null || !(u is Avalin))
  193. throw new InvalidOperationException("نوع کاربر صحیح نیست");
  194. Avalin avalin = (Avalin)u;
  195. avalin.CompanyName = CompanyName;
  196. avalin.NationalId = NationalId;
  197. avalin.RegisterId = RegisterId;
  198. avalin.Address = Address;
  199. avalin.CompanyPhoneNumber = CompanyPhoneNumber;
  200. avalin.Email = EMail;
  201. SevominDbContext.Current.SaveChanges();
  202. }
  203. catch (Exception)
  204. {
  205. throw;
  206. }
  207. return View("ProfileAvalin", u as Avalin);
  208. }
  209. }
  210. }