code format

vnext
Paul Schneider 10 years ago
parent 29046a3e07
commit 1264bf2cbd
1 changed files with 62 additions and 65 deletions

@ -21,6 +21,7 @@ namespace Yavsc.Controllers
WebConfigurationManager.AppSettings ["RegistrationMessage"]; WebConfigurationManager.AppSettings ["RegistrationMessage"];
string avatarDir = "~/avatars"; string avatarDir = "~/avatars";
/// <summary> /// <summary>
/// Gets or sets the avatar dir. /// Gets or sets the avatar dir.
/// This value is past to <c>Server.MapPath</c>, /// This value is past to <c>Server.MapPath</c>,
@ -46,7 +47,7 @@ namespace Yavsc.Controllers
public static Profile GetProfile (string user) public static Profile GetProfile (string user)
{ {
return new Profile (ProfileBase.Create (user)) ; return new Profile (ProfileBase.Create (user));
} }
@ -58,7 +59,8 @@ namespace Yavsc.Controllers
FormsAuthentication.SetAuthCookie (model.UserName, model.RememberMe); FormsAuthentication.SetAuthCookie (model.UserName, model.RememberMe);
if (returnUrl != null) if (returnUrl != null)
return Redirect (returnUrl); return Redirect (returnUrl);
else return View ("Index"); else
return View ("Index");
} else { } else {
ModelState.AddModelError ("UserName", "The user name or password provided is incorrect."); ModelState.AddModelError ("UserName", "The user name or password provided is incorrect.");
} }
@ -67,41 +69,40 @@ namespace Yavsc.Controllers
ViewData ["returnUrl"] = returnUrl; ViewData ["returnUrl"] = returnUrl;
// If we got this far, something failed, redisplay form // If we got this far, something failed, redisplay form
return View ("Login",model); return View ("Login", model);
} }
public ActionResult Register (RegisterViewModel model, string returnUrl) public ActionResult Register (RegisterViewModel model, string returnUrl)
{ {
ViewData["returnUrl"] = returnUrl; ViewData ["returnUrl"] = returnUrl;
if (Request.RequestType == "GET") { if (Request.RequestType == "GET") {
foreach (string k in ModelState.Keys) foreach (string k in ModelState.Keys)
ModelState [k].Errors.Clear (); ModelState [k].Errors.Clear ();
return View (model); return View (model);
} }
if (ModelState.IsValid) { if (ModelState.IsValid) {
if (model.ConfirmPassword != model.Password) if (model.ConfirmPassword != model.Password) {
{ ModelState.AddModelError ("ConfirmPassword", "Veuillez confirmer votre mot de passe");
ModelState.AddModelError("ConfirmPassword","Veuillez confirmer votre mot de passe");
return View (model); return View (model);
} }
MembershipCreateStatus mcs; MembershipCreateStatus mcs;
var user = Membership.CreateUser ( var user = Membership.CreateUser (
model.UserName, model.UserName,
model.Password, model.Password,
model.Email, model.Email,
null, null,
null, null,
false, false,
out mcs); out mcs);
switch (mcs) { switch (mcs) {
case MembershipCreateStatus.DuplicateEmail: case MembershipCreateStatus.DuplicateEmail:
ModelState.AddModelError("Email", "Cette adresse e-mail correspond " + ModelState.AddModelError ("Email", "Cette adresse e-mail correspond " +
"à un compte utilisateur existant"); "à un compte utilisateur existant");
return View (model); return View (model);
case MembershipCreateStatus.DuplicateUserName: case MembershipCreateStatus.DuplicateUserName:
ModelState.AddModelError("UserName", "Ce nom d'utilisateur est " + ModelState.AddModelError ("UserName", "Ce nom d'utilisateur est " +
"déjà enregistré"); "déjà enregistré");
return View (model); return View (model);
case MembershipCreateStatus.Success: case MembershipCreateStatus.Success:
FileInfo fi = new FileInfo ( FileInfo fi = new FileInfo (
@ -115,23 +116,21 @@ namespace Yavsc.Controllers
return View (model); return View (model);
} }
using (StreamReader sr = fi.OpenText()) { using (StreamReader sr = fi.OpenText ()) {
string body = sr.ReadToEnd(); string body = sr.ReadToEnd ();
body = body.Replace("<%SiteName%>",YavscHelpers.SiteName); body = body.Replace ("<%SiteName%>", YavscHelpers.SiteName);
body = body.Replace("<%UserName%>",user.UserName); body = body.Replace ("<%UserName%>", user.UserName);
body = body.Replace("<%UserActivatonUrl%>", body = body.Replace ("<%UserActivatonUrl%>",
string.Format("<{0}://{1}/Account/Validate/{2}?key={3}", string.Format ("<{0}://{1}/Account/Validate/{2}?key={3}",
Request.Url.Scheme, Request.Url.Scheme,
Request.Url.Authority, Request.Url.Authority,
user.UserName, user.UserName,
user.ProviderUserKey.ToString())); user.ProviderUserKey.ToString ()));
using (MailMessage msg = new MailMessage( using (MailMessage msg = new MailMessage (
HomeController.Admail,user.Email, HomeController.Admail, user.Email,
string.Format("Validation de votre compte {0}",YavscHelpers.SiteName), string.Format ("Validation de votre compte {0}", YavscHelpers.SiteName),
body)) body)) {
{ using (SmtpClient sc = new SmtpClient ()) {
using (SmtpClient sc = new SmtpClient())
{
sc.Send (msg); sc.Send (msg);
} }
} }
@ -141,11 +140,11 @@ namespace Yavsc.Controllers
return View ("RegistrationPending"); return View ("RegistrationPending");
} }
default: default:
ViewData["Error"] = "Une erreur inattendue s'est produite" + ViewData ["Error"] = "Une erreur inattendue s'est produite" +
"a l'enregistrement de votre compte utilisateur" + "a l'enregistrement de votre compte utilisateur" +
string.Format("({0}).",mcs.ToString()) + string.Format ("({0}).", mcs.ToString ()) +
"Veuillez pardonner la gêne" + "Veuillez pardonner la gêne" +
"occasionnée"; "occasionnée";
return View (model); return View (model);
} }
@ -160,20 +159,20 @@ namespace Yavsc.Controllers
[HttpGet] [HttpGet]
[Authorize] [Authorize]
public ActionResult ChangePassword() public ActionResult ChangePassword ()
{ {
return View(); return View ();
} }
[Authorize] [Authorize]
public ActionResult Unregister(bool confirmed=false) public ActionResult Unregister (bool confirmed = false)
{ {
if (!confirmed) if (!confirmed)
return View (); return View ();
Membership.DeleteUser ( Membership.DeleteUser (
Membership.GetUser ().UserName); Membership.GetUser ().UserName);
return RedirectToAction ("Index","Home"); return RedirectToAction ("Index", "Home");
} }
[Authorize] [Authorize]
@ -184,12 +183,12 @@ namespace Yavsc.Controllers
// ChangePassword will throw an exception rather // ChangePassword will throw an exception rather
// than return false in certain failure scenarios. // than return false in certain failure scenarios.
bool changePasswordSucceeded=false; bool changePasswordSucceeded = false;
try { try {
var users = Membership.FindUsersByName (model.Username); var users = Membership.FindUsersByName (model.Username);
if (users.Count > 0) { if (users.Count > 0) {
MembershipUser user = Membership.GetUser (model.Username,true); MembershipUser user = Membership.GetUser (model.Username, true);
changePasswordSucceeded = user.ChangePassword (model.OldPassword, model.NewPassword); changePasswordSucceeded = user.ChangePassword (model.OldPassword, model.NewPassword);
} else { } else {
@ -214,34 +213,35 @@ namespace Yavsc.Controllers
[Authorize] [Authorize]
[HttpGet] [HttpGet]
public ActionResult Profile(Profile model) public ActionResult Profile (Profile model)
{ {
string username = Membership.GetUser ().UserName; string username = Membership.GetUser ().UserName;
ViewData ["UserName"] = username; ViewData ["UserName"] = username;
model = GetProfile (username); model = GetProfile (username);
model.RememberMe = FormsAuthentication.GetAuthCookie ( username, true )==null; model.RememberMe = FormsAuthentication.GetAuthCookie (username, true) == null;
return View (model); return View (model);
} }
[Authorize] [Authorize]
[HttpPost] [HttpPost]
//public ActionResult UpdateProfile(HttpPostedFileBase Avatar, string Address, string CityAndState, string ZipCode, string Country, string WebSite) //public ActionResult UpdateProfile(HttpPostedFileBase Avatar, string Address, string CityAndState, string ZipCode, string Country, string WebSite)
public ActionResult Profile(Profile model, HttpPostedFileBase AvatarFile) public ActionResult Profile (Profile model, HttpPostedFileBase AvatarFile)
{ {
string username = Membership.GetUser ().UserName; string username = Membership.GetUser ().UserName;
ViewData ["UserName"] = username; ViewData ["UserName"] = username;
if (AvatarFile != null) { if (AvatarFile != null) {
if (AvatarFile.ContentType == "image/png") { if (AvatarFile.ContentType == "image/png") {
string avdir=Server.MapPath (AvatarDir); string avdir = Server.MapPath (AvatarDir);
string avpath=Path.Combine(avdir,username+".png"); string avpath = Path.Combine (avdir, username + ".png");
AvatarFile.SaveAs (avpath); AvatarFile.SaveAs (avpath);
string avuri = avpath.Substring( string avuri = avpath.Substring (
AppDomain.CurrentDomain.BaseDirectory.Length); AppDomain.CurrentDomain.BaseDirectory.Length);
avuri = avuri.Replace (" ", "+"); avuri = avuri.Replace (" ", "+");
avuri = Request.Url.Scheme + "://" + Request.Url.Authority + "/" + avuri; avuri = Request.Url.Scheme + "://" + Request.Url.Authority + "/" + avuri;
HttpContext.Profile.SetPropertyValue ("avatar", avuri ); HttpContext.Profile.SetPropertyValue ("avatar", avuri);
HttpContext.Profile.Save (); HttpContext.Profile.Save ();
model.avatar = avuri;
} else } else
ModelState.AddModelError ("Avatar", ModelState.AddModelError ("Avatar",
string.Format ("Image type {0} is not supported (suported formats : {1})", string.Format ("Image type {0} is not supported (suported formats : {1})",
@ -284,16 +284,15 @@ namespace Yavsc.Controllers
HttpContext.Profile.Save (); HttpContext.Profile.Save ();
FormsAuthentication.SetAuthCookie (username, model.RememberMe); FormsAuthentication.SetAuthCookie (username, model.RememberMe);
ViewData ["Message"] = "Profile enregistré, cookie modifié."; ViewData ["Message"] = "Profile enregistré, cookie modifié.";
} }
// HttpContext.Profile.SetPropertyValue("Avatar",Avatar);
return View (model); return View (model);
} }
[Authorize] [Authorize]
public ActionResult Logout (string returnUrl) public ActionResult Logout (string returnUrl)
{ {
FormsAuthentication.SignOut(); FormsAuthentication.SignOut ();
return Redirect(returnUrl); return Redirect (returnUrl);
} }
@ -305,15 +304,13 @@ namespace Yavsc.Controllers
if (u == null) { if (u == null) {
ViewData ["Error"] = ViewData ["Error"] =
string.Format ("Cet utilisateur n'existe pas ({0})", id); string.Format ("Cet utilisateur n'existe pas ({0})", id);
} } else if (u.ProviderUserKey.ToString () == key) {
else
if (u.ProviderUserKey.ToString () == key) {
u.IsApproved = true; u.IsApproved = true;
Membership.UpdateUser(u); Membership.UpdateUser (u);
ViewData["Message"] = ViewData ["Message"] =
string.Format ("La création de votre compte ({0}) est validée.", id); string.Format ("La création de votre compte ({0}) est validée.", id);
} } else
else ViewData["Error"] = "La clé utilisée pour valider ce compte est incorrecte"; ViewData ["Error"] = "La clé utilisée pour valider ce compte est incorrecte";
return View (); return View ();
} }
} }

Loading…