Mercurial > altnet-hispano
changeset 93:65f0b3d70c18
Reordenando clases e interfaces
author | Nelo@Kenia.neluz.int |
---|---|
date | Sat, 04 Jun 2011 00:07:20 -0300 |
parents | 7027cda13de3 |
children | db4b1e2cae49 |
files | Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj Agendas/trunk/src/Agendas.Web/Controllers/AccountController.cs Agendas/trunk/src/Agendas.Web/Models/AccountModels.cs Agendas/trunk/src/Agendas.Web/Services/AccountMembershipService.cs Agendas/trunk/src/Agendas.Web/Services/FormsAuthenticationService.cs Agendas/trunk/src/Agendas.Web/Services/IFormsAuthenticationService.cs Agendas/trunk/src/Agendas.Web/Services/IMembershipService.cs |
diffstat | 7 files changed, 118 insertions(+), 104 deletions(-) [+] |
line wrap: on
line diff
--- a/Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj Fri Jun 03 23:58:03 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj Sat Jun 04 00:07:20 2011 -0300 @@ -76,6 +76,10 @@ <Compile Include="Models\EventoModel.cs" /> <Compile Include="Models\PropuestaModel.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> + <Compile Include="Services\AccountMembershipService.cs" /> + <Compile Include="Services\FormsAuthenticationService.cs" /> + <Compile Include="Services\IFormsAuthenticationService.cs" /> + <Compile Include="Services\IMembershipService.cs" /> </ItemGroup> <ItemGroup> <Content Include="Content\altnetlogo.png" />
--- a/Agendas/trunk/src/Agendas.Web/Controllers/AccountController.cs Fri Jun 03 23:58:03 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Web/Controllers/AccountController.cs Sat Jun 04 00:07:20 2011 -0300 @@ -4,6 +4,7 @@ using System.Web.Security; using AltNetHispano.Agendas.Twitter; using AltNetHispano.Agendas.Web.Models; +using AltNetHispano.Agendas.Web.Services; namespace AltNetHispano.Agendas.Web.Controllers { @@ -98,7 +99,7 @@ return RedirectToAction("LogOn"); } - private string GetXmlContent(string xml, string tagName) + private static string GetXmlContent(string xml, string tagName) { var tagStart = "<" + tagName + ">"; var tagEnd = "</" + tagName + ">";
--- a/Agendas/trunk/src/Agendas.Web/Models/AccountModels.cs Fri Jun 03 23:58:03 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Web/Models/AccountModels.cs Sat Jun 04 00:07:20 2011 -0300 @@ -69,109 +69,7 @@ } #endregion - #region Services - // The FormsAuthentication type is sealed and contains static members, so it is difficult to - // unit test code that calls its members. The interface and helper class below demonstrate - // how to create an abstract wrapper around such a type in order to make the AccountController - // code unit testable. - - public interface IMembershipService - { - int MinPasswordLength { get; } - - bool ValidateUser(string userName, string password); - MembershipCreateStatus CreateUser(string userName, string password, string email); - bool ChangePassword(string userName, string oldPassword, string newPassword); - } - - public class AccountMembershipService : IMembershipService - { - private readonly MembershipProvider _provider; - - public AccountMembershipService() - : this(null) - { - } - - public AccountMembershipService(MembershipProvider provider) - { - _provider = provider ?? Membership.Provider; - } - - public int MinPasswordLength - { - get - { - return _provider.MinRequiredPasswordLength; - } - } - - public bool ValidateUser(string userName, string password) - { - if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName"); - if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password"); - - return _provider.ValidateUser(userName, password); - } - - public MembershipCreateStatus CreateUser(string userName, string password, string email) - { - if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName"); - if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password"); - if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email"); - - MembershipCreateStatus status; - _provider.CreateUser(userName, password, email, null, null, true, null, out status); - return status; - } - - public bool ChangePassword(string userName, string oldPassword, string newPassword) - { - if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName"); - if (String.IsNullOrEmpty(oldPassword)) throw new ArgumentException("Value cannot be null or empty.", "oldPassword"); - if (String.IsNullOrEmpty(newPassword)) throw new ArgumentException("Value cannot be null or empty.", "newPassword"); - - // The underlying ChangePassword() will throw an exception rather - // than return false in certain failure scenarios. - try - { - MembershipUser currentUser = _provider.GetUser(userName, true /* userIsOnline */); - return currentUser.ChangePassword(oldPassword, newPassword); - } - catch (ArgumentException) - { - return false; - } - catch (MembershipPasswordException) - { - return false; - } - } - } - - public interface IFormsAuthenticationService - { - void SignIn(string userName, bool createPersistentCookie); - void SignOut(); - } - - public class FormsAuthenticationService : IFormsAuthenticationService - { - public void SignIn(string userName, bool createPersistentCookie) - { - if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName"); - - FormsAuthentication.SetAuthCookie(userName, createPersistentCookie); - } - - public void SignOut() - { - FormsAuthentication.SignOut(); - } - } - #endregion - - #region Validation + #region Validation public static class AccountValidation { public static string ErrorCodeToString(MembershipCreateStatus createStatus)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Web/Services/AccountMembershipService.cs Sat Jun 04 00:07:20 2011 -0300 @@ -0,0 +1,70 @@ +using System; +using System.Web.Security; + +namespace AltNetHispano.Agendas.Web.Services +{ + public class AccountMembershipService : IMembershipService + { + private readonly MembershipProvider _provider; + + public AccountMembershipService() + : this(null) + { + } + + public AccountMembershipService(MembershipProvider provider) + { + _provider = provider ?? Membership.Provider; + } + + public int MinPasswordLength + { + get + { + return _provider.MinRequiredPasswordLength; + } + } + + public bool ValidateUser(string userName, string password) + { + if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName"); + if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password"); + + return _provider.ValidateUser(userName, password); + } + + public MembershipCreateStatus CreateUser(string userName, string password, string email) + { + if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName"); + if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password"); + if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email"); + + MembershipCreateStatus status; + _provider.CreateUser(userName, password, email, null, null, true, null, out status); + return status; + } + + public bool ChangePassword(string userName, string oldPassword, string newPassword) + { + if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName"); + if (String.IsNullOrEmpty(oldPassword)) throw new ArgumentException("Value cannot be null or empty.", "oldPassword"); + if (String.IsNullOrEmpty(newPassword)) throw new ArgumentException("Value cannot be null or empty.", "newPassword"); + + // The underlying ChangePassword() will throw an exception rather + // than return false in certain failure scenarios. + try + { + MembershipUser currentUser = _provider.GetUser(userName, true /* userIsOnline */); + return currentUser.ChangePassword(oldPassword, newPassword); + } + catch (ArgumentException) + { + return false; + } + catch (MembershipPasswordException) + { + return false; + } + } + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Web/Services/FormsAuthenticationService.cs Sat Jun 04 00:07:20 2011 -0300 @@ -0,0 +1,20 @@ +using System; +using System.Web.Security; + +namespace AltNetHispano.Agendas.Web.Services +{ + public class FormsAuthenticationService : IFormsAuthenticationService + { + public void SignIn(string userName, bool createPersistentCookie) + { + if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName"); + + FormsAuthentication.SetAuthCookie(userName, createPersistentCookie); + } + + public void SignOut() + { + FormsAuthentication.SignOut(); + } + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Web/Services/IFormsAuthenticationService.cs Sat Jun 04 00:07:20 2011 -0300 @@ -0,0 +1,8 @@ +namespace AltNetHispano.Agendas.Web.Services +{ + public interface IFormsAuthenticationService + { + void SignIn(string userName, bool createPersistentCookie); + void SignOut(); + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Web/Services/IMembershipService.cs Sat Jun 04 00:07:20 2011 -0300 @@ -0,0 +1,13 @@ +using System.Web.Security; + +namespace AltNetHispano.Agendas.Web.Services +{ + public interface IMembershipService + { + int MinPasswordLength { get; } + + bool ValidateUser(string userName, string password); + MembershipCreateStatus CreateUser(string userName, string password, string email); + bool ChangePassword(string userName, string oldPassword, string newPassword); + } +} \ No newline at end of file