yavsc/web/Controllers/AccountController.cs

305 lines
9.4 KiB
C#

10 years ago
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.Configuration;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Web.Profile;
using System.Web.Security;
using Yavsc;
10 years ago
using Yavsc.Model.RolesAndMembers;
10 years ago
using Yavsc.Helpers;
namespace Yavsc.Controllers
{
public class AccountController : Controller
{
private static string registrationMessage =
WebConfigurationManager.AppSettings ["RegistrationMessage"];
string avatarDir = "~/avatars";
10 years ago
/// <summary>
/// Gets or sets the avatar dir.
/// This value is past to <c>Server.MapPath</c>,
/// it should start with </c>~/</c>, and we assume it
/// to be relative to the application path.
/// </summary>
/// <value>The avatar dir.</value>
10 years ago
public string AvatarDir {
get { return avatarDir; }
set { avatarDir = value; }
}
public ActionResult Index ()
{
return View ();
}
public ActionResult Login (string returnUrl)
{
ViewData ["returnUrl"] = returnUrl;
return View ();
}
public static Profile GetProfile (string user)
{
10 years ago
return new Profile (ProfileBase.Create (user));
}
10 years ago
// TODO [ValidateAntiForgeryToken]
public ActionResult DoLogin (LoginModel model, string returnUrl)
{
if (ModelState.IsValid) {
if (Membership.ValidateUser (model.UserName, model.Password)) {
FormsAuthentication.SetAuthCookie (model.UserName, model.RememberMe);
if (returnUrl != null)
return Redirect (returnUrl);
10 years ago
else
return View ("Index");
10 years ago
} else {
ModelState.AddModelError ("UserName", "The user name or password provided is incorrect.");
}
}
ViewData ["returnUrl"] = returnUrl;
// If we got this far, something failed, redisplay form
10 years ago
return View ("Login", model);
10 years ago
}
public ActionResult Register (RegisterViewModel model, string returnUrl)
{
10 years ago
ViewData ["returnUrl"] = returnUrl;
10 years ago
if (Request.RequestType == "GET") {
foreach (string k in ModelState.Keys)
ModelState [k].Errors.Clear ();
return View (model);
}
if (ModelState.IsValid) {
10 years ago
if (model.ConfirmPassword != model.Password) {
ModelState.AddModelError ("ConfirmPassword", "Veuillez confirmer votre mot de passe");
10 years ago
return View (model);
}
MembershipCreateStatus mcs;
var user = Membership.CreateUser (
10 years ago
model.UserName,
model.Password,
model.Email,
null,
null,
false,
out mcs);
10 years ago
switch (mcs) {
case MembershipCreateStatus.DuplicateEmail:
10 years ago
ModelState.AddModelError ("Email", "Cette adresse e-mail correspond " +
"à un compte utilisateur existant");
10 years ago
return View (model);
case MembershipCreateStatus.DuplicateUserName:
10 years ago
ModelState.AddModelError ("UserName", "Ce nom d'utilisateur est " +
"déjà enregistré");
10 years ago
return View (model);
case MembershipCreateStatus.Success:
FileInfo fi = new FileInfo (
Server.MapPath (registrationMessage));
10 years ago
if (!fi.Exists) {
ViewData ["Error"] =
string.Format (
"Erreur inattendue (pas de corps de message " +
"à envoyer pour le message de confirmation ({0}))",
registrationMessage);
10 years ago
return View (model);
}
10 years ago
using (StreamReader sr = fi.OpenText ()) {
string body = sr.ReadToEnd ();
body = body.Replace ("<%SiteName%>", YavscHelpers.SiteName);
body = body.Replace ("<%UserName%>", user.UserName);
body = body.Replace ("<%UserActivatonUrl%>",
string.Format ("<{0}://{1}/Account/Validate/{2}?key={3}",
Request.Url.Scheme,
Request.Url.Authority,
user.UserName,
user.ProviderUserKey.ToString ()));
using (MailMessage msg = new MailMessage (
HomeController.Admail, user.Email,
string.Format ("Validation de votre compte {0}", YavscHelpers.SiteName),
body)) {
using (SmtpClient sc = new SmtpClient ()) {
10 years ago
sc.Send (msg);
}
}
ViewData ["username"] = user.UserName;
ViewData ["email"] = user.Email;
return View ("RegistrationPending");
}
default:
10 years ago
ViewData ["Error"] = "Une erreur inattendue s'est produite" +
"a l'enregistrement de votre compte utilisateur" +
string.Format ("({0}).", mcs.ToString ()) +
"Veuillez pardonner la gêne" +
"occasionnée";
10 years ago
return View (model);
}
}
return View (model);
}
public ActionResult ChangePasswordSuccess ()
{
return View ();
}
[HttpGet]
[Authorize]
10 years ago
public ActionResult ChangePassword ()
10 years ago
{
10 years ago
return View ();
10 years ago
}
[Authorize]
10 years ago
public ActionResult Unregister (bool confirmed = false)
{
if (!confirmed)
return View ();
Membership.DeleteUser (
Membership.GetUser ().UserName);
10 years ago
return RedirectToAction ("Index", "Home");
}
10 years ago
[Authorize]
[HttpPost]
public ActionResult ChangePassword (ChangePasswordModel model)
{
if (ModelState.IsValid) {
// ChangePassword will throw an exception rather
// than return false in certain failure scenarios.
10 years ago
bool changePasswordSucceeded = false;
10 years ago
try {
var users = Membership.FindUsersByName (model.Username);
if (users.Count > 0) {
10 years ago
MembershipUser user = Membership.GetUser (model.Username, true);
10 years ago
changePasswordSucceeded = user.ChangePassword (model.OldPassword, model.NewPassword);
} else {
changePasswordSucceeded = false;
ModelState.AddModelError ("Username", "The user name not found.");
10 years ago
}
} catch (Exception ex) {
ViewData ["Error"] = ex.ToString ();
10 years ago
}
if (changePasswordSucceeded) {
return RedirectToAction ("ChangePasswordSuccess");
} else {
ModelState.AddModelError ("Password", "The current password is incorrect or the new password is invalid.");
}
}
// If we got this far, something failed, redisplay form
return View (model);
}
[Authorize]
[HttpGet]
10 years ago
public ActionResult Profile (Profile model)
{
10 years ago
string username = Membership.GetUser ().UserName;
ViewData ["UserName"] = username;
model = GetProfile (username);
10 years ago
model.RememberMe = FormsAuthentication.GetAuthCookie (username, true) == null;
return View (model);
}
10 years ago
[Authorize]
[HttpPost]
//public ActionResult UpdateProfile(HttpPostedFileBase Avatar, string Address, string CityAndState, string ZipCode, string Country, string WebSite)
10 years ago
public ActionResult Profile (Profile model, HttpPostedFileBase AvatarFile)
10 years ago
{
string username = Membership.GetUser ().UserName;
ViewData ["UserName"] = username;
if (AvatarFile != null) {
// if said valid, move as avatar file
// else invalidate the model
10 years ago
if (AvatarFile.ContentType == "image/png") {
10 years ago
string avdir = Server.MapPath (AvatarDir);
string avpath = Path.Combine (avdir, username + ".png");
10 years ago
AvatarFile.SaveAs (avpath);
model.avatar =
Path.Combine(AvatarDir.Substring(1),username)+".png";
} else
10 years ago
ModelState.AddModelError ("Avatar",
string.Format ("Image type {0} is not supported (suported formats : {1})",
AvatarFile.ContentType, "image/png"));
10 years ago
}
/* Sync the property in the Profile model to display :
* string cAvat = HttpContext.Profile.GetPropertyValue ("avatar") as string;
if (cAvat != null) if (model.avatar == null) model.avatar = cAvat;
*/
10 years ago
if (ModelState.IsValid) {
if (model.avatar != null)
HttpContext.Profile.SetPropertyValue ("avatar", model.avatar);
HttpContext.Profile.SetPropertyValue ("Address", model.Address);
HttpContext.Profile.SetPropertyValue ("BlogTitle", model.BlogTitle);
HttpContext.Profile.SetPropertyValue ("BlogVisible", model.BlogVisible);
HttpContext.Profile.SetPropertyValue ("CityAndState", model.CityAndState);
HttpContext.Profile.SetPropertyValue ("ZipCode", model.ZipCode);
HttpContext.Profile.SetPropertyValue ("Country", model.Country);
HttpContext.Profile.SetPropertyValue ("WebSite", model.WebSite);
HttpContext.Profile.SetPropertyValue ("Name", model.Name);
HttpContext.Profile.SetPropertyValue ("Phone", model.Phone);
HttpContext.Profile.SetPropertyValue ("Mobile", model.Mobile);
HttpContext.Profile.SetPropertyValue ("BankCode", model.BankCode);
HttpContext.Profile.SetPropertyValue ("WicketCode", model.WicketCode);
HttpContext.Profile.SetPropertyValue ("AccountNumber", model.AccountNumber);
HttpContext.Profile.SetPropertyValue ("BankedKey", model.BankedKey);
HttpContext.Profile.SetPropertyValue ("BIC", model.BIC);
HttpContext.Profile.SetPropertyValue ("IBAN", model.IBAN);
HttpContext.Profile.Save ();
10 years ago
FormsAuthentication.SetAuthCookie (username, model.RememberMe);
ViewData ["Message"] = "Profile enregistré, cookie modifié.";
10 years ago
}
return View (model);
10 years ago
}
[Authorize]
public ActionResult Logout (string returnUrl)
{
10 years ago
FormsAuthentication.SignOut ();
return Redirect (returnUrl);
10 years ago
}
[HttpGet]
10 years ago
public ActionResult Validate (string id, string key)
{
MembershipUser u = Membership.GetUser (id, false);
if (u == null) {
ViewData ["Error"] =
string.Format ("Cet utilisateur n'existe pas ({0})", id);
10 years ago
} else if (u.ProviderUserKey.ToString () == key) {
10 years ago
u.IsApproved = true;
10 years ago
Membership.UpdateUser (u);
ViewData ["Message"] =
10 years ago
string.Format ("La création de votre compte ({0}) est validée.", id);
10 years ago
} else
ViewData ["Error"] = "La clé utilisée pour valider ce compte est incorrecte";
10 years ago
return View ();
}
}
}