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.
 
 
 
 

201 lines
6.6 KiB

using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
using Sevomin.Models;
using Sevomin.Models.Helpers;
using System;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Sevomin.Models.Repositories;
namespace Sevomin.WebFrontend.Controllers
{
public class AccountController : BaseController
{
public AccountController()
: this(new SevominUserManager(new UserStore<User>(SevominDbContext.Current)))
{
}
public AccountController(SevominUserManager userManager)
{
UserManager = userManager;
UserManager.UserValidator = new Sevomin.Models.Helpers.SevominUserValidator();
}
public SevominUserManager UserManager { get; private set; }
private IAuthenticationManager AuthenticationManager
{
get
{
return HttpContext.GetOwinContext().Authentication;
}
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Signup(SignupViewModel model)
{
User user;
if (model.IsAvalin)
{
user = new Avalin(model.Email, model.DisplayName);
user.SignUpDate = DateTime.UtcNow;
}
else
{
int spaceIndex = model.DisplayName.IndexOf(' ');
user = new Dovomin(model.Email, model.DisplayName, string.Empty);
user.SignUpDate = DateTime.UtcNow;
}
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInAsync(user, isPersistent: false);
return RedirectToAction("Index", "Home");
}
else
{
AddErrors(result);
}
return View("Intro", model);
}
public async Task<ActionResult> CheckUsername(string Email)
{
bool result = (await UserManager.FindByNameAsync(Email)) == null;
if(result)
return Json(true, JsonRequestBehavior.AllowGet);
return Json("این ایمیل قبلا در سایت استفاده شده. کلمه عبور خود را فراموش کرده اید؟", JsonRequestBehavior.AllowGet);
}
public ActionResult Login(string returnUrl)
{
if(Request.IsAuthenticated)
return RedirectToAction("Index", "Home");
ViewBag.ReturnUrl = returnUrl;
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindAsync(model.Username, model.Password);
if (user != null)
{
await SignInAsync(user, true);
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("", "نام کاربری و یا کلمه عبور وارد شده صحیح نمی باشد.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
public ActionResult Logout()
{
AuthenticationManager.SignOut();
return RedirectToAction("Index", "Home");
}
private async Task SignInAsync(User user, bool isPersistent)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}
private ActionResult RedirectToLocal(string returnUrl)
{
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
private void AddErrors(IdentityResult result)
{
foreach (var error in result.Errors)
{
ModelState.AddModelError("", error);
}
}
[Authorize]
public async Task<ActionResult> MyProfile()
{
User u = await UserManager.FindByNameAsync(User.Identity.Name);
if (u is Avalin)
{
return View("ProfileAvalin", u as Avalin);
}
else
{
Dovomin dovomin = (Dovomin)u;
DovominViewModel dvm = new DovominViewModel(dovomin);
return View("ProfileDovomin", dvm);
}
}
[Authorize]
[HttpPost]
public async Task<ActionResult> ProfileDovomin(string JalaliBirthDate, string FirstName,
string LastName, string ContactPersonEMail, bool IsFullTime,
bool IsPartTime, string Description, bool ShowCompanyLogo, FormCollection form)
{
User u = await UserManager.FindByNameAsync(User.Identity.Name);
if (u == null || !(u is Dovomin))
throw new InvalidOperationException("نوع کاربر صحیح نیست");
try
{
Dovomin dovomin = (Dovomin)u;
dovomin.BirthDate = DateAssist.ToMiladi(JalaliBirthDate);
dovomin.Description = Description;
dovomin.FirstName = FirstName;
dovomin.LastName = LastName;
dovomin.IsFulltime = IsFullTime;
dovomin.IsPartTime = IsPartTime;
ParameterRepository.Current.AddParametersToDovomin(dovomin);
foreach (var jp in dovomin.DovominParameters)
{
string value = form[string.Format("value-{0}", jp.Parameter.Id)];
byte moscow = 0;
byte.TryParse(form[string.Format("moscow-{0}", jp.Parameter.Id)], out moscow);
jp.SetValue(value);
jp.Moscow = moscow;
}
SevominDbContext.Current.SaveChanges();
}
catch (Exception)
{
throw;
}
DovominViewModel dvm = new DovominViewModel(u as Dovomin);
return View("ProfileDovomin", dvm);
}
}
}