diff --git a/web/ApiControllers/FrontOfficeApiController.cs b/web/ApiControllers/FrontOfficeApiController.cs index 26037236..b74b9f48 100644 --- a/web/ApiControllers/FrontOfficeApiController.cs +++ b/web/ApiControllers/FrontOfficeApiController.cs @@ -22,6 +22,7 @@ using System.Web.Profile; using System.Collections.Specialized; using Yavsc.Model; using Yavsc.Model.FrontOffice; +using Yavsc.Helpers; namespace Yavsc.ApiControllers { @@ -156,7 +157,7 @@ namespace Yavsc.ApiControllers return new HttpResponseMessage (HttpStatusCode.OK) { Content = new ObjectContent (typeof(string), ex.Message, new ErrorHtmlFormatter (HttpStatusCode.NotAcceptable, - LocalizedText.DocTemplateException + LocalizedText.DocTemplateException )) }; } catch (Exception ex) { @@ -179,6 +180,43 @@ namespace Yavsc.ApiControllers new TexToPdfFormatter ()) }; } + + /// + /// Register the specified model. + /// + /// Model. + /// if false, sends a registration validation e-mail. + [Authorize(Roles="Admin")] + [ValidateAjaxAttribute] + public void Register ([FromBody] RegisterModel model, bool isApprouved=true) + { + MembershipCreateStatus mcs; + var user = Membership.CreateUser ( + model.UserName, + model.Password, + model.Email, + null, + null, + isApprouved, + out mcs); + switch (mcs) { + case MembershipCreateStatus.DuplicateEmail: + ModelState.AddModelError ("Email", "Cette adresse e-mail correspond " + + "à un compte utilisateur existant"); + return ; + case MembershipCreateStatus.DuplicateUserName: + ModelState.AddModelError ("UserName", "Ce nom d'utilisateur est " + + "déjà enregistré"); + return ; + case MembershipCreateStatus.Success: + if (!isApprouved) + Yavsc.Helpers.YavscHelpers.SendActivationEmail (user); + return ; + + default: + throw new Exception ( string.Format( "Unexpected membership creation status : {0}", mcs.ToString() ) ); + } + } } } diff --git a/web/Controllers/AccountController.cs b/web/Controllers/AccountController.cs index 923f2e1b..c99be3ff 100644 --- a/web/Controllers/AccountController.cs +++ b/web/Controllers/AccountController.cs @@ -20,9 +20,7 @@ namespace Yavsc.Controllers /// public class AccountController : Controller { - private static string registrationMessage = - WebConfigurationManager.AppSettings ["RegistrationMessage"]; - + string avatarDir = "~/avatars"; /// @@ -108,40 +106,10 @@ namespace Yavsc.Controllers "déjà enregistré"); return View (model); case MembershipCreateStatus.Success: - FileInfo fi = new FileInfo ( - Server.MapPath (registrationMessage)); - if (!fi.Exists) { - ViewData ["Error"] = - string.Format ( - "Erreur inattendue (pas de corps de message " + - "à envoyer pour le message de confirmation ({0}))", - registrationMessage); - return View (model); - } - - 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 ()) { - sc.Send (msg); - } - } - - ViewData ["username"] = user.UserName; - ViewData ["email"] = user.Email; - return View ("RegistrationPending"); - } + YavscHelpers.SendActivationEmail (user); + ViewData ["username"] = user.UserName; + ViewData ["email"] = user.Email; + return View ("RegistrationPending"); default: ViewData ["Error"] = "Une erreur inattendue s'est produite" + "a l'enregistrement de votre compte utilisateur" + @@ -162,7 +130,8 @@ namespace Yavsc.Controllers { return View (); } - + + /// /// Changes the password. /// diff --git a/web/Controllers/HomeController.cs b/web/Controllers/HomeController.cs index 4fdc85ca..9896d239 100644 --- a/web/Controllers/HomeController.cs +++ b/web/Controllers/HomeController.cs @@ -15,6 +15,7 @@ using Npgsql.Web; using ITContentProvider; using Yavsc; using Npgsql.Web.Blog; +using Yavsc.Helpers; namespace Yavsc.Controllers { @@ -38,18 +39,7 @@ namespace Yavsc.Controllers return name; } } - // Administrator email - private static string admail = - WebConfigurationManager.AppSettings ["AdminEmail"]; - /// - /// Gets the Administrator email. - /// - /// The admail. - public static string Admail { - get { - return admail; - } - } + /// /// Lists the referenced assemblies. /// @@ -128,7 +118,7 @@ namespace Yavsc.Controllers using (System.Net.Mail.MailMessage msg = new MailMessage(email,OwnerEmail,"[Contact] "+reason,body)) { - msg.CC.Add(new MailAddress(Admail)); + msg.CC.Add(new MailAddress(YavscHelpers.Admail)); using (System.Net.Mail.SmtpClient sc = new SmtpClient()) { sc.Send (msg); diff --git a/web/Helpers/YavscHelpers.cs b/web/Helpers/YavscHelpers.cs index 5da47eb9..6d3175d1 100644 --- a/web/Helpers/YavscHelpers.cs +++ b/web/Helpers/YavscHelpers.cs @@ -1,6 +1,10 @@ using System; using System.Web; using System.Configuration; +using System.Web.Security; +using System.IO; +using System.Web.Configuration; +using System.Net.Mail; namespace Yavsc.Helpers { @@ -9,6 +13,10 @@ namespace Yavsc.Helpers /// public static class YavscHelpers { + private static string registrationMessage = + WebConfigurationManager.AppSettings ["RegistrationMessage"]; + + private static string siteName = null; /// /// Gets the name of the site. @@ -17,10 +25,60 @@ namespace Yavsc.Helpers public static string SiteName { get { if (siteName == null) - siteName = ConfigurationManager.AppSettings ["Name"]; + siteName = WebConfigurationManager.AppSettings ["Name"]; return siteName; } } + // Administrator email + private static string admail = + WebConfigurationManager.AppSettings ["AdminEmail"]; + /// + /// Gets the Administrator email. + /// + /// The admail. + public static string Admail { + get { + return admail; + } + } + + /// + /// Sends the activation email. + /// + /// User. + public static void SendActivationEmail(MembershipUser user) { + FileInfo fi = new FileInfo ( + HttpContext.Current.Server.MapPath (registrationMessage)); + if (!fi.Exists) { + throw new Exception( + string.Format ( + "Erreur inattendue (pas de corps de message " + + "à envoyer pour le message de confirmation ({0}))", + registrationMessage)); + } + + 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}>", + HttpContext.Current.Request.Url.Scheme, + HttpContext.Current.Request.Url.Authority, + user.UserName, + user.ProviderUserKey.ToString ())); + using (MailMessage msg = new MailMessage ( + Admail, user.Email, + string.Format ("Validation de votre compte {0}", YavscHelpers.SiteName), + body)) { + using (SmtpClient sc = new SmtpClient ()) { + sc.Send (msg); + } + } + + } + } + } } diff --git a/web/Web.csproj b/web/Web.csproj index b7003812..a2477f76 100644 --- a/web/Web.csproj +++ b/web/Web.csproj @@ -677,6 +677,7 @@ + @@ -700,7 +701,6 @@ - diff --git a/yavscModel/RolesAndMemebers/RegisterModel.cs b/yavscModel/RolesAndMemebers/RegisterModel.cs new file mode 100644 index 00000000..02d0fb47 --- /dev/null +++ b/yavscModel/RolesAndMemebers/RegisterModel.cs @@ -0,0 +1,58 @@ +// +// RegisterModel.cs +// +// Author: +// Paul Schneider +// +// Copyright (c) 2015 Paul Schneider +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . + +using System; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; + +namespace Yavsc.Model.RolesAndMembers +{ + /// + /// Register view model. + /// + public class RegisterModel + { + /// + /// Gets or sets the name of the user. + /// + /// The name of the user. + [Localizable(true)] + [Required(ErrorMessage = "S'il vous plait, entrez un nom d'utilisateur")] + public string UserName { get; set; } + + /// + /// Gets or sets the password. + /// + /// The password. + [DisplayName("Mot de passe")] + [Required(ErrorMessage = "S'il vous plait, entez un mot de passe")] + public string Password { get; set; } + + /// + /// Gets or sets the email. + /// + /// The email. + [DisplayName("Adresse e-mail")] + [Required(ErrorMessage = "S'il vous plait, entrez un e-mail valide")] + public string Email { get; set; } + } + +} diff --git a/yavscModel/RolesAndMemebers/RegisterViewModel.cs b/yavscModel/RolesAndMemebers/RegisterViewModel.cs index db5002c4..a1f0baf7 100644 --- a/yavscModel/RolesAndMemebers/RegisterViewModel.cs +++ b/yavscModel/RolesAndMemebers/RegisterViewModel.cs @@ -7,38 +7,15 @@ namespace Yavsc.Model.RolesAndMembers /// /// Register view model. /// - public class RegisterViewModel + public class RegisterViewModel : RegisterModel { - /// - /// Gets or sets the name of the user. - /// - /// The name of the user. - [Localizable(true)] - [Required(ErrorMessage = "S'il vous plait, entrez un nom d'utilisateur")] - public string UserName { get; set; } - - /// - /// Gets or sets the password. - /// - /// The password. - [DisplayName("Mot de passe")] - [Required(ErrorMessage = "S'il vous plait, entez un mot de passe")] - public string Password { get; set; } - /// /// Gets or sets the confirm password. /// /// The confirm password. [DisplayName("Confirmation du mot de passe")] public string ConfirmPassword { get; set; } - - /// - /// Gets or sets the email. - /// - /// The email. - [DisplayName("Adresse e-mail")] - [Required(ErrorMessage = "S'il vous plait, entrez un e-mail valide")] - public string Email { get; set; } + } } diff --git a/yavscModel/YavscModel.csproj b/yavscModel/YavscModel.csproj index dbf90af6..4f7a94fa 100644 --- a/yavscModel/YavscModel.csproj +++ b/yavscModel/YavscModel.csproj @@ -142,6 +142,7 @@ +