yavsc/web/Controllers/AccountController.cs

324 lines
9.8 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.Profile;
using System.Web.Security;
using Yavsc;
10 years ago
using Yavsc.Model.RolesAndMembers;
10 years ago
using Yavsc.Helpers;
using System.Web.Mvc;
10 years ago
namespace Yavsc.Controllers
{
/// <summary>
/// Account controller.
/// </summary>
10 years ago
public class AccountController : Controller
{
10 years ago
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; }
}
/// <summary>
/// Index this instance.
/// </summary>
10 years ago
public ActionResult Index ()
{
return View ();
}
10 years ago
// TODO [ValidateAntiForgeryToken]
/// <summary>
/// Does login.
/// </summary>
/// <returns>The login.</returns>
/// <param name="model">Model.</param>
/// <param name="returnUrl">Return URL.</param>
public ActionResult Login (LoginModel model, string returnUrl)
10 years ago
{
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
return View (model);
10 years ago
}
/// <summary>
/// Register the specified model and returnUrl.
/// </summary>
/// <param name="model">Model.</param>
/// <param name="returnUrl">Return URL.</param>
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:
YavscHelpers.SendActivationEmail (user);
ViewData ["username"] = user.UserName;
ViewData ["email"] = user.Email;
return View ("RegistrationPending");
10 years ago
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);
}
/// <summary>
/// Changes the password success.
/// </summary>
/// <returns>The password success.</returns>
10 years ago
public ActionResult ChangePasswordSuccess ()
{
return View ();
}
/// <summary>
/// Changes the password.
/// </summary>
/// <returns>The password.</returns>
10 years ago
[HttpGet]
[Authorize]
10 years ago
public ActionResult ChangePassword ()
10 years ago
{
10 years ago
return View ();
10 years ago
}
/// <summary>
/// Unregister the specified confirmed.
/// </summary>
/// <param name="confirmed">If set to <c>true</c> confirmed.</param>
[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");
}
/// <summary>
/// Changes the password.
/// </summary>
/// <returns>The password.</returns>
/// <param name="model">Model.</param>
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);
}
/// <summary>
/// Profile the specified user.
/// </summary>
/// <param name="user">User name.</param>
[Authorize]
[HttpGet]
public ActionResult Profile (string user)
{
ViewData ["ProfileUserName"] = user;
string logdu = Membership.GetUser ().UserName;
ViewData ["UserName"] = logdu;
if (user == null)
user = logdu;
Profile model = new Profile (ProfileBase.Create (user));
model.RememberMe = FormsAuthentication.GetAuthCookie (user, true) == null;
return View (model);
}
10 years ago
/// <summary>
/// Profile the specified user, model and AvatarFile.
/// </summary>
/// <param name="user">User.</param>
/// <param name="model">Model.</param>
/// <param name="AvatarFile">Avatar file.</param>
10 years ago
[Authorize]
[HttpPost]
public ActionResult Profile (string user, Profile model, HttpPostedFileBase AvatarFile)
10 years ago
{
// ASSERT("Membership.GetUser ().UserName is made of simple characters, no slash nor backslash"
string logdu = Membership.GetUser ().UserName;
ViewData ["UserName"] = logdu;
bool editsMyProfile = (user == logdu);
if (!editsMyProfile)
if (!Roles.IsUserInRole ("Admin"))
if (!Roles.IsUserInRole ("FrontOffice"))
throw new UnauthorizedAccessException ("Your are not authorized to modify this profile");
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, user + ".png");
10 years ago
AvatarFile.SaveAs (avpath);
model.avatar = Request.Url.Scheme + "://" + Request.Url.Authority + AvatarDir.Substring (1) + "/" + user + ".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) {
ProfileBase prf = ProfileBase .Create (model.UserName);
prf.SetPropertyValue ("BlogVisible", model.BlogVisible);
prf.SetPropertyValue ("BlogTitle", model.BlogTitle);
prf.SetPropertyValue ("avatar", model.avatar);
prf.SetPropertyValue ("Address", model.Address);
prf.SetPropertyValue ("CityAndState", model.CityAndState);
prf.SetPropertyValue ("Country", model.Country);
prf.SetPropertyValue ("ZipCode", model.ZipCode);
prf.SetPropertyValue ("WebSite", model.WebSite);
prf.SetPropertyValue ("Name", model.Name);
prf.SetPropertyValue ("Phone", model.Phone);
prf.SetPropertyValue ("Mobile", model.Mobile);
prf.SetPropertyValue ("BankCode", model.BankCode);
prf.SetPropertyValue ("IBAN", model.IBAN);
prf.SetPropertyValue ("BIC", model.BIC);
prf.SetPropertyValue ("WicketCode", model.WicketCode);
prf.SetPropertyValue ("AccountNumber", model.AccountNumber);
prf.SetPropertyValue ("BankedKey", model.BankedKey);
prf.SetPropertyValue ("gcalid", model.GoogleCalendar);
prf.Save ();
// only do the following if this profile belongs to current user
if (editsMyProfile)
FormsAuthentication.SetAuthCookie (user, model.RememberMe);
ViewData ["Message"] = "Profile enregistré"+((editsMyProfile)?", cookie modifié.":"");
10 years ago
}
return View (model);
10 years ago
}
/// <summary>
/// Logout the specified returnUrl.
/// </summary>
/// <param name="returnUrl">Return URL.</param>
10 years ago
[Authorize]
public ActionResult Logout (string returnUrl)
{
10 years ago
FormsAuthentication.SignOut ();
return Redirect (returnUrl);
10 years ago
}
/// <summary>
/// Validate the specified id and key.
/// </summary>
/// <param name="id">Identifier.</param>
/// <param name="key">Key.</param>
[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 ();
}
}
}