using System;
using System.Web;
using System.Configuration;
using System.Web.Security;
using System.IO;
using System.Web.Configuration;
using System.Net.Mail;
using Yavsc.Model.RolesAndMembers;
using System.Collections.Generic;
using System.Collections.Specialized;
using Yavsc.Model.Circles;
using System.Web.UI;
using System.Linq.Expressions;
using System.Web.Profile;
namespace Yavsc.Helpers
{
///
/// Yavsc helpers.
///
public static class YavscHelpers
{
private static string siteName = null;
///
/// Gets the name of the site.
///
/// The name of the site.
public static string SiteName {
get {
if (siteName == null)
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 message.
///
/// Helper.
/// User.
public static void SendActivationMessage(this System.Web.Http.Routing.UrlHelper helper, MembershipUser user)
{
SendActivationMessage (helper.Route("~/Account/Validate/",new { id=user.UserName, key=user.ProviderUserKey.ToString() } )
, WebConfigurationManager.AppSettings ["RegistrationMessage"],
user);
}
///
/// Sends the activation message.
///
/// Helper.
/// User.
public static void SendActivationMessage(this System.Web.Mvc.UrlHelper helper, MembershipUser user)
{
SendActivationMessage (helper.Content("~/Account/Validate/"+user.UserName+"/?key="+user.ProviderUserKey.ToString())
, WebConfigurationManager.AppSettings ["RegistrationMessage"],
user);
}
///
/// Sends the activation message.
///
/// Validation URL.
/// Registration message.
/// User.
public static void SendActivationMessage(string validationUrl, 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%>", validationUrl);
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);
}
}
}
}
///
/// Validates the password reset.
///
/// Model.
/// Errors.
/// User.
public static void ValidatePasswordReset(LostPasswordModel model, out StringDictionary errors, out MembershipUser user)
{
MembershipUserCollection users = null;
errors = new StringDictionary ();
user = null;
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
foreach (MembershipUser u in users) user = u;
}
///
/// Avatars the URL.
///
/// The URL.
/// Helper.
/// Username.
public static string AvatarUrl (this System.Web.Mvc.UrlHelper helper, string username) {
ProfileBase pr = ProfileBase.Create (username);
var a = pr.GetPropertyValue("Avatar") ;
if (a == null || a is DBNull) return "/avatars/" + helper.Encode(username)+".png";
return helper.Encode ((string)a);
}
}
}