yavsc/web/Helpers/YavscHelpers.cs

182 lines
5.1 KiB
C#

10 years ago
using System;
using System.Web;
using System.Configuration;
using System.Web.Security;
using System.IO;
using System.Web.Configuration;
using System.Net.Mail;
* AccountController.cs: Register and reset passord from Web API * GCMController.cs: initial creation, will host GCM calls and related procedures. * ResetPassword.aspx: Html view to reset the password * LocalizedText.resx: * LocalizedText.fr.resx: new String form circles * Web.config: * Web.csproj: * YavscModel.csproj: * LocalizedText.Designer.cs: * Profile.cs: * Profile.cs: * LocalizedText.fr.Designer.cs: * LoginModel.cs: * Publishing.cs: * CalendarController.cs: * LoginModel.cs: * GCMRegister.cs: * Publishing.cs: * GCMRegister.cs: * NewRoleModel.cs: * NewRoleModel.cs: * RegisterModel.cs: * NewAdminModel.cs: * RegisterModel.cs: * NewAdminModel.cs: * LostPasswordModel.cs: * RegisterViewModel.cs: * RegisterViewModel.cs: * ProviderPublicInfo.cs: * RegisterClientModel.cs: * ChangePasswordModel.cs: * ProviderPublicInfo.cs: * RegisterClientModel.cs: * ChangePasswordModel.cs: Fixes a typo (in the namespace :-/) * NpgsqlCircleProvider.cs: Fixes the Circle creation * Global.asax.cs: * AdminController.cs: * NpgsqlContentProvider.cs: code formatting * BlogsController.cs: * CircleController.cs: * WorkFlowController.cs: * PaypalApiController.cs: * FrontOfficeController.cs: refactoring * AccountController.cs: Adds the way to reset the password * FrontOfficeController.cs: xml doc * T.cs: Make this class an helper to translation * YavscHelpers.cs: Implements the e-mail sending * style.css: style uniformization * Circles.aspx: Implements the Html interface to Circle creation (modifications and deletions are still to implement) * Register.ascx: Allows the error display in case of lack of power of the user at registering another user. * Estimate.aspx: use the partial view to register from the Account folder. Cleans the useless reference to ~/Theme/dark/style.css, that was for using the "tablesorter.js", no used anymore. * Web.config: Trying to have all the Index pages to work...
9 years ago
using System.Web.Http.ModelBinding;
using Yavsc.Model.RolesAndMembers;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web.Mvc;
using Yavsc.Model.Circles;
using System.Web.UI;
using System.Linq.Expressions;
using System.Web.Profile;
10 years ago
namespace Yavsc.Helpers
{
/// <summary>
/// Yavsc helpers.
/// </summary>
10 years ago
public static class YavscHelpers
{
/// <summary>
/// Formats the circle.
/// </summary>
/// <returns>The circle.</returns>
/// <param name="c">C.</param>
public static HtmlString FormatCircle (Circle c)
{
if (c.Members!=null)
if (c.Members.Length > 0) {
TagBuilder i = new TagBuilder ("i");
i.SetInnerText (String.Join (", ", c.Members));
return new HtmlString (c.Title+"<br/>\n"+i.ToString());
}
return new HtmlString (c.Title);
}
/// <summary>
/// Formats the circle.
/// </summary>
/// <returns>The circle as Html string.</returns>
/// <param name="helper">Helper.</param>
/// <param name="c">C.</param>
public static HtmlString FormatCircle(this HtmlHelper helper, Circle c)
{
return FormatCircle (c);
}
10 years ago
private static string siteName = null;
/// <summary>
/// Gets the name of the site.
/// </summary>
/// <value>The name of the site.</value>
10 years ago
public static string SiteName {
get {
if (siteName == null)
siteName = WebConfigurationManager.AppSettings ["Name"];
10 years ago
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>
* AccountController.cs: Register and reset passord from Web API * GCMController.cs: initial creation, will host GCM calls and related procedures. * ResetPassword.aspx: Html view to reset the password * LocalizedText.resx: * LocalizedText.fr.resx: new String form circles * Web.config: * Web.csproj: * YavscModel.csproj: * LocalizedText.Designer.cs: * Profile.cs: * Profile.cs: * LocalizedText.fr.Designer.cs: * LoginModel.cs: * Publishing.cs: * CalendarController.cs: * LoginModel.cs: * GCMRegister.cs: * Publishing.cs: * GCMRegister.cs: * NewRoleModel.cs: * NewRoleModel.cs: * RegisterModel.cs: * NewAdminModel.cs: * RegisterModel.cs: * NewAdminModel.cs: * LostPasswordModel.cs: * RegisterViewModel.cs: * RegisterViewModel.cs: * ProviderPublicInfo.cs: * RegisterClientModel.cs: * ChangePasswordModel.cs: * ProviderPublicInfo.cs: * RegisterClientModel.cs: * ChangePasswordModel.cs: Fixes a typo (in the namespace :-/) * NpgsqlCircleProvider.cs: Fixes the Circle creation * Global.asax.cs: * AdminController.cs: * NpgsqlContentProvider.cs: code formatting * BlogsController.cs: * CircleController.cs: * WorkFlowController.cs: * PaypalApiController.cs: * FrontOfficeController.cs: refactoring * AccountController.cs: Adds the way to reset the password * FrontOfficeController.cs: xml doc * T.cs: Make this class an helper to translation * YavscHelpers.cs: Implements the e-mail sending * style.css: style uniformization * Circles.aspx: Implements the Html interface to Circle creation (modifications and deletions are still to implement) * Register.ascx: Allows the error display in case of lack of power of the user at registering another user. * Estimate.aspx: use the partial view to register from the Account folder. Cleans the useless reference to ~/Theme/dark/style.css, that was for using the "tablesorter.js", no used anymore. * Web.config: Trying to have all the Index pages to work...
9 years ago
/// Sends the activation message.
/// </summary>
/// <param name="user">User.</param>
* AccountController.cs: Register and reset passord from Web API * GCMController.cs: initial creation, will host GCM calls and related procedures. * ResetPassword.aspx: Html view to reset the password * LocalizedText.resx: * LocalizedText.fr.resx: new String form circles * Web.config: * Web.csproj: * YavscModel.csproj: * LocalizedText.Designer.cs: * Profile.cs: * Profile.cs: * LocalizedText.fr.Designer.cs: * LoginModel.cs: * Publishing.cs: * CalendarController.cs: * LoginModel.cs: * GCMRegister.cs: * Publishing.cs: * GCMRegister.cs: * NewRoleModel.cs: * NewRoleModel.cs: * RegisterModel.cs: * NewAdminModel.cs: * RegisterModel.cs: * NewAdminModel.cs: * LostPasswordModel.cs: * RegisterViewModel.cs: * RegisterViewModel.cs: * ProviderPublicInfo.cs: * RegisterClientModel.cs: * ChangePasswordModel.cs: * ProviderPublicInfo.cs: * RegisterClientModel.cs: * ChangePasswordModel.cs: Fixes a typo (in the namespace :-/) * NpgsqlCircleProvider.cs: Fixes the Circle creation * Global.asax.cs: * AdminController.cs: * NpgsqlContentProvider.cs: code formatting * BlogsController.cs: * CircleController.cs: * WorkFlowController.cs: * PaypalApiController.cs: * FrontOfficeController.cs: refactoring * AccountController.cs: Adds the way to reset the password * FrontOfficeController.cs: xml doc * T.cs: Make this class an helper to translation * YavscHelpers.cs: Implements the e-mail sending * style.css: style uniformization * Circles.aspx: Implements the Html interface to Circle creation (modifications and deletions are still to implement) * Register.ascx: Allows the error display in case of lack of power of the user at registering another user. * Estimate.aspx: use the partial view to register from the Account folder. Cleans the useless reference to ~/Theme/dark/style.css, that was for using the "tablesorter.js", no used anymore. * Web.config: Trying to have all the Index pages to work...
9 years ago
public static void SendActivationMessage(MembershipUser user)
{
SendEmail (WebConfigurationManager.AppSettings ["RegistrationMessage"],
user);
}
/// <summary>
/// Sends the email.
/// </summary>
/// <param name="registrationMessage">Registration message.</param>
/// <param name="user">User.</param>
public static void SendEmail(string registrationMessage, 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);
}
}
}
}
* AccountController.cs: Register and reset passord from Web API * GCMController.cs: initial creation, will host GCM calls and related procedures. * ResetPassword.aspx: Html view to reset the password * LocalizedText.resx: * LocalizedText.fr.resx: new String form circles * Web.config: * Web.csproj: * YavscModel.csproj: * LocalizedText.Designer.cs: * Profile.cs: * Profile.cs: * LocalizedText.fr.Designer.cs: * LoginModel.cs: * Publishing.cs: * CalendarController.cs: * LoginModel.cs: * GCMRegister.cs: * Publishing.cs: * GCMRegister.cs: * NewRoleModel.cs: * NewRoleModel.cs: * RegisterModel.cs: * NewAdminModel.cs: * RegisterModel.cs: * NewAdminModel.cs: * LostPasswordModel.cs: * RegisterViewModel.cs: * RegisterViewModel.cs: * ProviderPublicInfo.cs: * RegisterClientModel.cs: * ChangePasswordModel.cs: * ProviderPublicInfo.cs: * RegisterClientModel.cs: * ChangePasswordModel.cs: Fixes a typo (in the namespace :-/) * NpgsqlCircleProvider.cs: Fixes the Circle creation * Global.asax.cs: * AdminController.cs: * NpgsqlContentProvider.cs: code formatting * BlogsController.cs: * CircleController.cs: * WorkFlowController.cs: * PaypalApiController.cs: * FrontOfficeController.cs: refactoring * AccountController.cs: Adds the way to reset the password * FrontOfficeController.cs: xml doc * T.cs: Make this class an helper to translation * YavscHelpers.cs: Implements the e-mail sending * style.css: style uniformization * Circles.aspx: Implements the Html interface to Circle creation (modifications and deletions are still to implement) * Register.ascx: Allows the error display in case of lack of power of the user at registering another user. * Estimate.aspx: use the partial view to register from the Account folder. Cleans the useless reference to ~/Theme/dark/style.css, that was for using the "tablesorter.js", no used anymore. * Web.config: Trying to have all the Index pages to work...
9 years ago
/// <summary>
/// Resets the password.
/// </summary>
/// <param name="model">Model.</param>
/// <param name="errors">Errors.</param>
* AccountController.cs: Register and reset passord from Web API * GCMController.cs: initial creation, will host GCM calls and related procedures. * ResetPassword.aspx: Html view to reset the password * LocalizedText.resx: * LocalizedText.fr.resx: new String form circles * Web.config: * Web.csproj: * YavscModel.csproj: * LocalizedText.Designer.cs: * Profile.cs: * Profile.cs: * LocalizedText.fr.Designer.cs: * LoginModel.cs: * Publishing.cs: * CalendarController.cs: * LoginModel.cs: * GCMRegister.cs: * Publishing.cs: * GCMRegister.cs: * NewRoleModel.cs: * NewRoleModel.cs: * RegisterModel.cs: * NewAdminModel.cs: * RegisterModel.cs: * NewAdminModel.cs: * LostPasswordModel.cs: * RegisterViewModel.cs: * RegisterViewModel.cs: * ProviderPublicInfo.cs: * RegisterClientModel.cs: * ChangePasswordModel.cs: * ProviderPublicInfo.cs: * RegisterClientModel.cs: * ChangePasswordModel.cs: Fixes a typo (in the namespace :-/) * NpgsqlCircleProvider.cs: Fixes the Circle creation * Global.asax.cs: * AdminController.cs: * NpgsqlContentProvider.cs: code formatting * BlogsController.cs: * CircleController.cs: * WorkFlowController.cs: * PaypalApiController.cs: * FrontOfficeController.cs: refactoring * AccountController.cs: Adds the way to reset the password * FrontOfficeController.cs: xml doc * T.cs: Make this class an helper to translation * YavscHelpers.cs: Implements the e-mail sending * style.css: style uniformization * Circles.aspx: Implements the Html interface to Circle creation (modifications and deletions are still to implement) * Register.ascx: Allows the error display in case of lack of power of the user at registering another user. * Estimate.aspx: use the partial view to register from the Account folder. Cleans the useless reference to ~/Theme/dark/style.css, that was for using the "tablesorter.js", no used anymore. * Web.config: Trying to have all the Index pages to work...
9 years ago
public static void ResetPassword(LostPasswordModel model, out StringDictionary errors)
{
MembershipUserCollection users = null;
errors = new StringDictionary ();
if (!string.IsNullOrEmpty (model.UserName)) {
users =
Membership.FindUsersByName (model.UserName);
if (users.Count < 1) {
errors.Add ("UserName", "User name not found");
return ;
}
if (users.Count != 1) {
errors.Add ("UserName", "Found more than one user!(sic)");
return ;
}
}
if (!string.IsNullOrEmpty (model.Email)) {
users =
Membership.FindUsersByEmail (model.Email);
if (users.Count < 1) {
errors.Add ( "Email", "Email not found");
return ;
}
if (users.Count != 1) {
errors.Add ("Email", "Found more than one user!(sic)");
return ;
}
}
if (users==null)
return;
// Assert users.Count == 1
if (users.Count != 1)
throw new InvalidProgramException ("Emails and user's names are uniques, and we find more than one result here, aborting.");
foreach (MembershipUser u in users)
YavscHelpers.SendActivationMessage (u);
}
/// <summary>
/// Avatars the URL.
/// </summary>
/// <returns>The URL.</returns>
/// <param name="helper">Helper.</param>
/// <param name="username">Username.</param>
public static string AvatarUrl (this HtmlHelper helper, string username) {
ProfileBase pr = ProfileBase.Create (username);
var avpath = pr.GetPropertyValue("Avatar") ;
if (avpath != null)
return helper.Encode (avpath);
return "/avatars/" + helper.Encode(username)+".png";
}
10 years ago
}
}