changeset 115:7a2eeb9e9bf9

Crear cuenta interna asociada
author Nelo@Kenia.neluz.int
date Sun, 12 Jun 2011 01:40:09 -0300
parents 1ce71844ffa4
children 53bcd338542b
files Agendas/trunk/src/Agendas.Domain/Cuenta.cs Agendas/trunk/src/Agendas.Domain/IdentityContext.cs Agendas/trunk/src/Agendas.Domain/Services/PersonaService.cs Agendas/trunk/src/Agendas.Tests/PersonaServiceTests.cs Agendas/trunk/src/Agendas.Tests/TestBase.cs Agendas/trunk/src/Agendas.Web/Controllers/AccountController.cs Agendas/trunk/src/Agendas.Web/Controllers/PerfilController.cs Agendas/trunk/src/Agendas.Web/Views/Perfil/Index.cshtml
diffstat 8 files changed, 72 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/Agendas/trunk/src/Agendas.Domain/Cuenta.cs	Sat Jun 11 12:50:44 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Domain/Cuenta.cs	Sun Jun 12 01:40:09 2011 -0300
@@ -15,5 +15,28 @@
 			LogonName = logonName;
 			IdentityProvider = identityProvider;
 		}
+
+		public override bool Equals(object obj)
+		{
+			if (ReferenceEquals(null, obj)) return false;
+			if (ReferenceEquals(this, obj)) return true;
+			if (obj.GetType() != typeof (Cuenta)) return false;
+			return Equals((Cuenta) obj);
+		}
+
+		public virtual bool Equals(Cuenta other)
+		{
+			if (ReferenceEquals(null, other)) return false;
+			if (ReferenceEquals(this, other)) return true;
+			return Equals(other.LogonName, LogonName) && Equals(other.IdentityProvider, IdentityProvider);
+		}
+
+		public override int GetHashCode()
+		{
+			unchecked
+			{
+				return ((LogonName != null ? LogonName.GetHashCode() : 0)*397) ^ IdentityProvider.GetHashCode();
+			}
+		}
 	}
 }
\ No newline at end of file
--- a/Agendas/trunk/src/Agendas.Domain/IdentityContext.cs	Sat Jun 11 12:50:44 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Domain/IdentityContext.cs	Sun Jun 12 01:40:09 2011 -0300
@@ -27,15 +27,21 @@
 
 		public static string GetUserName()
 		{
-			string username = Current.GetUserName();
-			if (string.IsNullOrWhiteSpace(username))
-				throw new UsuarioNoAutenticadoException();
-			return username;
+			return Current.GetUserName();
+		}
+
+		public static bool IsAuthenticated()
+		{
+			return !string.IsNullOrWhiteSpace(GetUserName());
 		}
 
 		public static Persona GetUsuario()
 		{
-			var identification = new Identification(GetUserName());
+			var userName = GetUserName();
+			if (string.IsNullOrWhiteSpace(userName))
+				throw new UsuarioNoAutenticadoException();
+
+			var identification = new Identification(userName);
 
 			var cuenta = _personaRepository.GetCuenta(identification.IdentityProvider, identification.LogonName);
 			return cuenta != null ? cuenta.Persona : null;
--- a/Agendas/trunk/src/Agendas.Domain/Services/PersonaService.cs	Sat Jun 11 12:50:44 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Domain/Services/PersonaService.cs	Sun Jun 12 01:40:09 2011 -0300
@@ -19,12 +19,12 @@
 			return cuenta != null;
 		}
 
-		public bool Validate(IdentityProviderEnum identityProvider, string username, string nombre)
+		public bool CreateIfNotExist(IdentityProviderEnum identityProvider, string username, string nombre)
 		{
 			var cuenta = _personaRepository.GetCuenta(identityProvider, username);
 			if (cuenta==null)
 			{
-				var persona=new Persona(nombre);
+				var persona = IdentityContext.IsAuthenticated() ? IdentityContext.GetUsuario() : new Persona(nombre);
 				persona.AddCuenta(new Cuenta(identityProvider, username));
 
 				if (identityProvider == IdentityProviderEnum.Twitter)
--- a/Agendas/trunk/src/Agendas.Tests/PersonaServiceTests.cs	Sat Jun 11 12:50:44 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Tests/PersonaServiceTests.cs	Sun Jun 12 01:40:09 2011 -0300
@@ -19,7 +19,7 @@
 
 			var personaService = new PersonaService(personaRepository.Object);
 			
-			Assert.IsTrue(personaService.Validate(IdentityProviderEnum.Twitter, "nelopauselli", "Nelo Pauselli"));
+			Assert.IsTrue(personaService.CreateIfNotExist(IdentityProviderEnum.Twitter, "nelopauselli", "Nelo Pauselli"));
 			personaRepository.Verify(r=>r.Save(It.IsAny<Persona>()), Times.Once());
 			Assert.IsNotNull(persona);
 			Assert.AreEqual(1, persona.Cuentas.Count());
@@ -32,7 +32,7 @@
 			personaRepository.Setup(r => r.GetCuenta(IdentityProviderEnum.Twitter, "nelopauselli")).Returns(cuenta);
 
 			Assert.AreEqual(1, persona.Cuentas.Count());
-			Assert.IsTrue(personaService.Validate(IdentityProviderEnum.Twitter, "nelopauselli", "Nelo Pauselli"));
+			Assert.IsTrue(personaService.CreateIfNotExist(IdentityProviderEnum.Twitter, "nelopauselli", "Nelo Pauselli"));
 			Assert.AreEqual(1, persona.Cuentas.Count());
 		}
 
--- a/Agendas/trunk/src/Agendas.Tests/TestBase.cs	Sat Jun 11 12:50:44 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Tests/TestBase.cs	Sun Jun 12 01:40:09 2011 -0300
@@ -43,8 +43,11 @@
 			seguridad.Setup(s => s.GetUserName()).Returns(logonName);
 			
 			IPersonaRepository personaRepository = new PersonaRepository();
-			var service = new PersonaService(personaRepository);
-			service.Validate(identityProvider, username, nombre);
+			var persona = new Persona(nombre);
+			persona.AddCuenta(new Cuenta(identityProvider, username));
+			persona.Twitter = username;
+
+			personaRepository.Save(persona);
 
 			IdentityContext.Init(seguridad.Object, personaRepository);
 
--- a/Agendas/trunk/src/Agendas.Web/Controllers/AccountController.cs	Sat Jun 11 12:50:44 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Web/Controllers/AccountController.cs	Sun Jun 12 01:40:09 2011 -0300
@@ -1,5 +1,4 @@
-using System;
-using System.Web.Mvc;
+using System.Web.Mvc;
 using System.Web.Routing;
 using System.Web.Security;
 using AltNetHispano.Agendas.Domain;
@@ -66,26 +65,24 @@
 				var url = Request.Url.Scheme + "://" + Request.Url.Host +
 				          (Request.Url.Port != 80 ? ":" + Request.Url.Port : string.Empty) + action;
 
-				Response.Redirect(oAuth.AuthorizationLinkGet(url).ToString());
+				return Redirect(oAuth.AuthorizationLinkGet(url).ToString());
 			}
-			else
+			
+			var response = oAuth.AccessTokenGet(Request["oauth_token"], Request["oauth_verifier"]);
+			if (response.Length > 0)
 			{
-				var response = oAuth.AccessTokenGet(Request["oauth_token"], Request["oauth_verifier"]);
-				if (response.Length > 0)
-				{
-					var username = OAuthTwitter.GetResponseContent(response, "screen_name");
-					var nombre = OAuthTwitter.GetResponseContent(response, "name");
+				var username = OAuthTwitter.GetResponseContent(response, "screen_name");
+				var nombre = OAuthTwitter.GetResponseContent(response, "name");
 
-					var personaService = AgendaFactory.GetPersonaService();
-					if (personaService.Validate(IdentityProviderEnum.Twitter, username, nombre))
-					{
-						FormsService.SignIn(Identification.Map[(int)IdentityProviderEnum.Twitter]+username, false);
-						return RedirectToAction("Index", "Home");
-					}
-					ModelState.AddModelError("", "The user name or password provided is incorrect.");
-				}
+				var personaService = AgendaFactory.GetPersonaService();
+				personaService.CreateIfNotExist(IdentityProviderEnum.Twitter, username, nombre);
+
+				FormsService.SignIn(Identification.Map[(int)IdentityProviderEnum.Twitter]+username, false);
+				return RedirectToAction("Index", "Home");
 			}
 
+			ModelState.AddModelError("", "The user name or password provided is incorrect.");
+
 			return RedirectToAction("LogOn");
 		}
 
@@ -111,7 +108,7 @@
         }
 
         [HttpPost]
-        public ActionResult Register(RegisterModel model)
+        public ActionResult Register(RegisterModel model, string returnUrl)
         {
             if (ModelState.IsValid)
             {
@@ -121,9 +118,11 @@
                 if (createStatus == MembershipCreateStatus.Success)
                 {
 					var personaService = AgendaFactory.GetPersonaService();
-					if (personaService.Validate(IdentityProviderEnum.BuiltIn, model.UserName, model.Nombre))
+					if (personaService.CreateIfNotExist(IdentityProviderEnum.BuiltIn, model.UserName, model.Nombre))
 					{
 						FormsService.SignIn(Identification.Map[(int)IdentityProviderEnum.BuiltIn] + model.UserName, false);
+						if (Url.IsLocalUrl(returnUrl))
+							return Redirect(returnUrl); 
 						return RedirectToAction("Index", "Home");
 					}
                 }
--- a/Agendas/trunk/src/Agendas.Web/Controllers/PerfilController.cs	Sat Jun 11 12:50:44 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Web/Controllers/PerfilController.cs	Sun Jun 12 01:40:09 2011 -0300
@@ -58,20 +58,18 @@
 				var url = Request.Url.Scheme + "://" + Request.Url.Host +
 						  (Request.Url.Port != 80 ? ":" + Request.Url.Port : string.Empty) + action;
 
-				Response.Redirect(oAuth.AuthorizationLinkGet(url).ToString());
+				return Redirect(oAuth.AuthorizationLinkGet(url).ToString());
 			}
-			else
+			
+			var response = oAuth.AccessTokenGet(Request["oauth_token"], Request["oauth_verifier"]);
+			if (response.Length > 0)
 			{
-				var response = oAuth.AccessTokenGet(Request["oauth_token"], Request["oauth_verifier"]);
-				if (response.Length > 0)
-				{
-					var username =OAuthTwitter.GetResponseContent(response, "screen_name");
+				var username = OAuthTwitter.GetResponseContent(response, "screen_name");
 
-					var personaService = AgendaFactory.GetPersonaService();
+				var personaService = AgendaFactory.GetPersonaService();
 
-					personaService.AddCuenta(IdentityProviderEnum.Twitter, username);
-					return RedirectToAction("Index");
-				}
+				personaService.AddCuenta(IdentityProviderEnum.Twitter, username);
+				return RedirectToAction("Index");
 			}
 			return RedirectToAction("Index");
 		}
--- a/Agendas/trunk/src/Agendas.Web/Views/Perfil/Index.cshtml	Sat Jun 11 12:50:44 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Web/Views/Perfil/Index.cshtml	Sun Jun 12 01:40:09 2011 -0300
@@ -28,5 +28,7 @@
 <p>
 @Html.ActionLink("Asociar cuenta de Twitter", "AddTwitterAccount")
 <br/>
-@Html.ActionLink("Asociar cuenta interna", "AddBuiltInAccount")
+@Html.ActionLink("Asociar cuenta de interna", "AddBuiltInAccount")
+<br/>
+@Html.ActionLink("Crear cuenta interna asociada", "Register", "Account", new { returnUrl = Url.Action("Index") }, null)
 </p>