* YavscHelpers.cs:

* YavscModel.csproj:
* HomeController.cs:
* AccountController.cs:
* RegisterModel.cs:
* FrontOfficeApiController.cs:
* RegisterViewModel.cs: refactoring

* Web.csproj: deploy robots.txt
This commit is contained in:
Paul Schneider
2015-03-22 14:08:15 +01:00
parent 5b396b4e07
commit d31383d83b
8 changed files with 170 additions and 79 deletions

View File

@ -22,6 +22,7 @@ using System.Web.Profile;
using System.Collections.Specialized; using System.Collections.Specialized;
using Yavsc.Model; using Yavsc.Model;
using Yavsc.Model.FrontOffice; using Yavsc.Model.FrontOffice;
using Yavsc.Helpers;
namespace Yavsc.ApiControllers namespace Yavsc.ApiControllers
{ {
@ -156,7 +157,7 @@ namespace Yavsc.ApiControllers
return new HttpResponseMessage (HttpStatusCode.OK) { Content = return new HttpResponseMessage (HttpStatusCode.OK) { Content =
new ObjectContent (typeof(string), new ObjectContent (typeof(string),
ex.Message, new ErrorHtmlFormatter (HttpStatusCode.NotAcceptable, ex.Message, new ErrorHtmlFormatter (HttpStatusCode.NotAcceptable,
LocalizedText.DocTemplateException LocalizedText.DocTemplateException
)) ))
}; };
} catch (Exception ex) { } catch (Exception ex) {
@ -179,6 +180,43 @@ namespace Yavsc.ApiControllers
new TexToPdfFormatter ()) new TexToPdfFormatter ())
}; };
} }
/// <summary>
/// Register the specified model.
/// </summary>
/// <param name="model">Model.</param>
/// <param name="isApprouved">if false, sends a registration validation e-mail.</param>
[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() ) );
}
}
} }
} }

View File

@ -20,9 +20,7 @@ namespace Yavsc.Controllers
/// </summary> /// </summary>
public class AccountController : Controller public class AccountController : Controller
{ {
private static string registrationMessage =
WebConfigurationManager.AppSettings ["RegistrationMessage"];
string avatarDir = "~/avatars"; string avatarDir = "~/avatars";
/// <summary> /// <summary>
@ -108,40 +106,10 @@ namespace Yavsc.Controllers
"déjà enregistré"); "déjà enregistré");
return View (model); return View (model);
case MembershipCreateStatus.Success: case MembershipCreateStatus.Success:
FileInfo fi = new FileInfo ( YavscHelpers.SendActivationEmail (user);
Server.MapPath (registrationMessage)); ViewData ["username"] = user.UserName;
if (!fi.Exists) { ViewData ["email"] = user.Email;
ViewData ["Error"] = return View ("RegistrationPending");
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");
}
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" +
@ -162,7 +130,8 @@ namespace Yavsc.Controllers
{ {
return View (); return View ();
} }
/// <summary> /// <summary>
/// Changes the password. /// Changes the password.
/// </summary> /// </summary>

View File

@ -15,6 +15,7 @@ using Npgsql.Web;
using ITContentProvider; using ITContentProvider;
using Yavsc; using Yavsc;
using Npgsql.Web.Blog; using Npgsql.Web.Blog;
using Yavsc.Helpers;
namespace Yavsc.Controllers namespace Yavsc.Controllers
{ {
@ -38,18 +39,7 @@ namespace Yavsc.Controllers
return name; return name;
} }
} }
// Administrator email
private static string admail =
WebConfigurationManager.AppSettings ["AdminEmail"];
/// <summary>
/// Gets the Administrator email.
/// </summary>
/// <value>The admail.</value>
public static string Admail {
get {
return admail;
}
}
/// <summary> /// <summary>
/// Lists the referenced assemblies. /// Lists the referenced assemblies.
/// </summary> /// </summary>
@ -128,7 +118,7 @@ namespace Yavsc.Controllers
using (System.Net.Mail.MailMessage msg = new MailMessage(email,OwnerEmail,"[Contact] "+reason,body)) 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()) using (System.Net.Mail.SmtpClient sc = new SmtpClient())
{ {
sc.Send (msg); sc.Send (msg);

View File

@ -1,6 +1,10 @@
using System; using System;
using System.Web; using System.Web;
using System.Configuration; using System.Configuration;
using System.Web.Security;
using System.IO;
using System.Web.Configuration;
using System.Net.Mail;
namespace Yavsc.Helpers namespace Yavsc.Helpers
{ {
@ -9,6 +13,10 @@ namespace Yavsc.Helpers
/// </summary> /// </summary>
public static class YavscHelpers public static class YavscHelpers
{ {
private static string registrationMessage =
WebConfigurationManager.AppSettings ["RegistrationMessage"];
private static string siteName = null; private static string siteName = null;
/// <summary> /// <summary>
/// Gets the name of the site. /// Gets the name of the site.
@ -17,10 +25,60 @@ namespace Yavsc.Helpers
public static string SiteName { public static string SiteName {
get { get {
if (siteName == null) if (siteName == null)
siteName = ConfigurationManager.AppSettings ["Name"]; siteName = WebConfigurationManager.AppSettings ["Name"];
return siteName; return siteName;
} }
} }
// Administrator email
private static string admail =
WebConfigurationManager.AppSettings ["AdminEmail"];
/// <summary>
/// Gets the Administrator email.
/// </summary>
/// <value>The admail.</value>
public static string Admail {
get {
return admail;
}
}
/// <summary>
/// Sends the activation email.
/// </summary>
/// <param name="user">User.</param>
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);
}
}
}
}
} }
} }

View File

@ -677,6 +677,7 @@
<Content Include="Scripts\stupidtable.js" /> <Content Include="Scripts\stupidtable.js" />
<Content Include="Scripts\jquery-ui.js" /> <Content Include="Scripts\jquery-ui.js" />
<Content Include="Views\Account\Profile.aspx" /> <Content Include="Views\Account\Profile.aspx" />
<Content Include="robots.txt" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" /> <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
@ -700,7 +701,6 @@
<None Include="packages.config" /> <None Include="packages.config" />
<None Include="Scripts\jquery-2.1.3.min.map" /> <None Include="Scripts\jquery-2.1.3.min.map" />
<None Include="Scripts\styles.json" /> <None Include="Scripts\styles.json" />
<None Include="robots.txt" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\NpgsqlMRPProviders\NpgsqlMRPProviders.csproj"> <ProjectReference Include="..\NpgsqlMRPProviders\NpgsqlMRPProviders.csproj">

View File

@ -0,0 +1,58 @@
//
// RegisterModel.cs
//
// Author:
// Paul Schneider <paulschneider@free.fr>
//
// 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 <http://www.gnu.org/licenses/>.
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace Yavsc.Model.RolesAndMembers
{
/// <summary>
/// Register view model.
/// </summary>
public class RegisterModel
{
/// <summary>
/// Gets or sets the name of the user.
/// </summary>
/// <value>The name of the user.</value>
[Localizable(true)]
[Required(ErrorMessage = "S'il vous plait, entrez un nom d'utilisateur")]
public string UserName { get; set; }
/// <summary>
/// Gets or sets the password.
/// </summary>
/// <value>The password.</value>
[DisplayName("Mot de passe")]
[Required(ErrorMessage = "S'il vous plait, entez un mot de passe")]
public string Password { get; set; }
/// <summary>
/// Gets or sets the email.
/// </summary>
/// <value>The email.</value>
[DisplayName("Adresse e-mail")]
[Required(ErrorMessage = "S'il vous plait, entrez un e-mail valide")]
public string Email { get; set; }
}
}

View File

@ -7,38 +7,15 @@ namespace Yavsc.Model.RolesAndMembers
/// <summary> /// <summary>
/// Register view model. /// Register view model.
/// </summary> /// </summary>
public class RegisterViewModel public class RegisterViewModel : RegisterModel
{ {
/// <summary>
/// Gets or sets the name of the user.
/// </summary>
/// <value>The name of the user.</value>
[Localizable(true)]
[Required(ErrorMessage = "S'il vous plait, entrez un nom d'utilisateur")]
public string UserName { get; set; }
/// <summary>
/// Gets or sets the password.
/// </summary>
/// <value>The password.</value>
[DisplayName("Mot de passe")]
[Required(ErrorMessage = "S'il vous plait, entez un mot de passe")]
public string Password { get; set; }
/// <summary> /// <summary>
/// Gets or sets the confirm password. /// Gets or sets the confirm password.
/// </summary> /// </summary>
/// <value>The confirm password.</value> /// <value>The confirm password.</value>
[DisplayName("Confirmation du mot de passe")] [DisplayName("Confirmation du mot de passe")]
public string ConfirmPassword { get; set; } public string ConfirmPassword { get; set; }
/// <summary>
/// Gets or sets the email.
/// </summary>
/// <value>The email.</value>
[DisplayName("Adresse e-mail")]
[Required(ErrorMessage = "S'il vous plait, entrez un e-mail valide")]
public string Email { get; set; }
} }
} }

View File

@ -142,6 +142,7 @@
<Compile Include="Google\CalendarEventList.cs" /> <Compile Include="Google\CalendarEventList.cs" />
<Compile Include="Google\GDate.cs" /> <Compile Include="Google\GDate.cs" />
<Compile Include="Google\Resource.cs" /> <Compile Include="Google\Resource.cs" />
<Compile Include="RolesAndMemebers\RegisterModel.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup> <ItemGroup>