changeset 112:0bca45e1e664

Pantalla de Perfil donde se puede asociar la cuenta de twitter al usuario existente
author Nelo@Kenia.neluz.int
date Sat, 11 Jun 2011 01:20:59 -0300
parents 90ce239cfa6d
children 0eac9a1c1a6c
files Agendas/trunk/src/Agendas.Domain/IdentityContext.cs Agendas/trunk/src/Agendas.Domain/Services/PersonaService.cs Agendas/trunk/src/Agendas.NHibernate/NhHelper.cs Agendas/trunk/src/Agendas.Tests/TestBase.cs Agendas/trunk/src/Agendas.Tests/TrackTests.cs Agendas/trunk/src/Agendas.Twitter/ITwitterAdapter.cs Agendas/trunk/src/Agendas.Twitter/oAuthTwitter.cs Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj Agendas/trunk/src/Agendas.Web/Controllers/AccountController.cs Agendas/trunk/src/Agendas.Web/Controllers/PerfilController.cs Agendas/trunk/src/Agendas.Web/Models/EventoModel.cs Agendas/trunk/src/Agendas.Web/Models/PerfilModel.cs Agendas/trunk/src/Agendas.Web/Views/Perfil/Index.cshtml Agendas/trunk/src/Agendas.Web/Views/Shared/_Menu.cshtml Agendas/trunk/src/Agendas.Web/favicon.ico
diffstat 15 files changed, 193 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/Agendas/trunk/src/Agendas.Domain/IdentityContext.cs	Wed Jun 08 08:40:04 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Domain/IdentityContext.cs	Sat Jun 11 01:20:59 2011 -0300
@@ -35,7 +35,9 @@
 
 		public static Persona GetUsuario()
 		{
-			var cuenta = _personaRepository.GetCuenta(IdentityProviderEnum.Twitter, GetUserName());
+			var identification = new Identification(GetUserName());
+
+			var cuenta = _personaRepository.GetCuenta(identification.IdentityProvider, identification.LogonName);
 			return cuenta != null ? cuenta.Persona : null;
 		}
 	}
--- a/Agendas/trunk/src/Agendas.Domain/Services/PersonaService.cs	Wed Jun 08 08:40:04 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Domain/Services/PersonaService.cs	Sat Jun 11 01:20:59 2011 -0300
@@ -1,4 +1,6 @@
-using AltNetHispano.Agendas.Domain.Repositories;
+using System;
+using System.Linq;
+using AltNetHispano.Agendas.Domain.Repositories;
 
 namespace AltNetHispano.Agendas.Domain.Services
 {
@@ -32,5 +34,22 @@
 			}
 			return true;
 		}
+
+		public void AddCuenta(IdentityProviderEnum identityProvider, string username)
+		{
+			var persona = IdentityContext.GetUsuario();
+			persona.AddCuenta(new Cuenta(identityProvider, username));
+
+			if (identityProvider == IdentityProviderEnum.Twitter)
+				persona.Twitter = username;
+		}
+
+		public void RemoveCuenta(IdentityProviderEnum identityProvider, string username)
+		{
+			var persona = IdentityContext.GetUsuario();
+			var cuenta = persona.Cuentas.FirstOrDefault(c => c.IdentityProvider == identityProvider && c.LogonName == username);
+			if (cuenta != null)
+				persona.RemoveCuenta(cuenta);
+		}
 	}
 }
\ No newline at end of file
--- a/Agendas/trunk/src/Agendas.NHibernate/NhHelper.cs	Wed Jun 08 08:40:04 2011 -0300
+++ b/Agendas/trunk/src/Agendas.NHibernate/NhHelper.cs	Sat Jun 11 01:20:59 2011 -0300
@@ -26,7 +26,7 @@
 				orm.Cascade<Evento, Persona>(Cascade.None);
 				orm.Cascade<Persona, Evento>(Cascade.None);
 				
-				orm.Cascade<Persona, Cuenta>(Cascade.All);
+				orm.Cascade<Persona, Cuenta>(Cascade.All | Cascade.DeleteOrphans);
 
 				var mapper = new Mapper(orm);
 
--- a/Agendas/trunk/src/Agendas.Tests/TestBase.cs	Wed Jun 08 08:40:04 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Tests/TestBase.cs	Sat Jun 11 01:20:59 2011 -0300
@@ -35,9 +35,12 @@
 		protected static void SetCurrentUser(string username, string nombre)
 		{
 			const IdentityProviderEnum identityProvider = IdentityProviderEnum.Twitter;
+			string logonName = null;
+			if (username != null)
+				logonName = Identification.Map[(int) IdentityProviderEnum.Twitter] + username;
 
 			var seguridad = new Mock<ISeguridad>();
-			seguridad.Setup(s => s.GetUserName()).Returns(username);
+			seguridad.Setup(s => s.GetUserName()).Returns(logonName);
 			
 			IPersonaRepository personaRepository = new PersonaRepository();
 			var service = new PersonaService(personaRepository);
--- a/Agendas/trunk/src/Agendas.Tests/TrackTests.cs	Wed Jun 08 08:40:04 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Tests/TrackTests.cs	Sat Jun 11 01:20:59 2011 -0300
@@ -18,7 +18,7 @@
 			var agenda = new Agenda(null, repository, DefaultPersonaRepository);
 
 			var fecha = DateTime.Now.AddDays(5);
-      agenda.Agendar("Html 5", "jjmontes", fecha, urlInvitacion, TipoEvento.Van);
+			agenda.Agendar("Html 5", "jjmontes", fecha, urlInvitacion, TipoEvento.Van);
 			var evento = repository.GetEventosConFecha().First();
 
 			Assert.AreEqual(1, evento.Tracks.Count());
--- a/Agendas/trunk/src/Agendas.Twitter/ITwitterAdapter.cs	Wed Jun 08 08:40:04 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Twitter/ITwitterAdapter.cs	Sat Jun 11 01:20:59 2011 -0300
@@ -42,6 +42,7 @@
 					return showUserResponse.Content;
 				return showUserResponse.ErrorMessage;
 			}
+			
 			return string.Empty;
 
 		}
--- a/Agendas/trunk/src/Agendas.Twitter/oAuthTwitter.cs	Wed Jun 08 08:40:04 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Twitter/oAuthTwitter.cs	Sat Jun 11 01:20:59 2011 -0300
@@ -37,5 +37,22 @@
 		{
 			return _twitterAdapter.GetAccessToken(ConsumerKey, ConsumerSecret, requestToken, verifier);
 		}
+
+		public static string GetResponseContent(string response, string tagName)
+		{
+			var tagStart = "\"" + tagName + "\":";
+			var tagEnd = ",";
+
+			var start = response.IndexOf(tagStart) + tagStart.Length;
+			if (start > tagStart.Length)
+			{
+				var end = response.IndexOf(tagEnd, start);
+				if (end > 0)
+				{
+					return response.Substring(start + 1, end - start - 2);
+				}
+			}
+			return string.Empty;
+		}
 	}
 }
--- a/Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj	Wed Jun 08 08:40:04 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj	Sat Jun 11 01:20:59 2011 -0300
@@ -64,6 +64,7 @@
     <Compile Include="Controllers\AccountController.cs" />
     <Compile Include="Controllers\EventoController.cs" />
     <Compile Include="Controllers\HomeController.cs" />
+    <Compile Include="Controllers\PerfilController.cs" />
     <Compile Include="Controllers\PropuestaController.cs" />
     <Compile Include="Global.asax.cs">
       <DependentUpon>Global.asax</DependentUpon>
@@ -76,6 +77,7 @@
     <Compile Include="Models\AccountModels.cs" />
     <Compile Include="Models\EventoModel.cs" />
     <Compile Include="Models\EventoModelHelper.cs" />
+    <Compile Include="Models\PerfilModel.cs" />
     <Compile Include="Models\PropuestaModel.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Services\AccountMembershipService.cs" />
@@ -191,6 +193,9 @@
   <ItemGroup>
     <Content Include="Views\Shared\DisplayTemplates\Tooltip.cshtml" />
   </ItemGroup>
+  <ItemGroup>
+    <Content Include="Views\Perfil\Index.cshtml" />
+  </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
--- a/Agendas/trunk/src/Agendas.Web/Controllers/AccountController.cs	Wed Jun 08 08:40:04 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Web/Controllers/AccountController.cs	Sat Jun 11 01:20:59 2011 -0300
@@ -73,8 +73,8 @@
 				var response = oAuth.AccessTokenGet(Request["oauth_token"], Request["oauth_verifier"]);
 				if (response.Length > 0)
 				{
-					var username = GetResponseContent(response, "screen_name");
-					var nombre = 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))
@@ -89,23 +89,6 @@
 			return RedirectToAction("LogOn");
 		}
 
-		private static string GetResponseContent(string response, string tagName)
-		{
-			var tagStart = "\"" + tagName + "\":";
-			var tagEnd = ",";
-
-			var start = response.IndexOf(tagStart) + tagStart.Length;
-			if (start > tagStart.Length)
-			{
-				var end = response.IndexOf(tagEnd, start);
-				if (end > 0)
-				{
-					return response.Substring(start + 1, end - start - 2);
-				}
-			}
-			return string.Empty;
-		}
-
     	// **************************************
         // URL: /Account/LogOff
         // **************************************
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Agendas/trunk/src/Agendas.Web/Controllers/PerfilController.cs	Sat Jun 11 01:20:59 2011 -0300
@@ -0,0 +1,82 @@
+using System;
+using System.Linq;
+using System.Web.Mvc;
+using AltNetHispano.Agendas.Domain;
+using AltNetHispano.Agendas.Factories;
+using AltNetHispano.Agendas.Twitter;
+using AltNetHispano.Agendas.Web.Models;
+
+namespace AltNetHispano.Agendas.Web.Controllers
+{
+    public class PerfilController : Controller
+    {
+        //
+        // GET: /Perfil/
+
+        public ActionResult Index()
+        {
+        	var persona = IdentityContext.GetUsuario();
+
+			if (persona == null)
+			{
+				ModelState.AddModelError(string.Empty, "No fue posible recuperar su perfil, vuelva a autenticarse y reintente");
+				return View();
+			}
+
+        	var model = new PerfilIndexModel
+        	            	{
+        	            		DisplayName = persona.Nombre,
+        	            		Cuentas = from c in persona.Cuentas
+        	            		          select
+        	            		          	new CuentaDto
+        	            		          		{IdentityProvider = c.IdentityProvider.ToString(), UserName = c.LogonName}
+        	            	};
+
+        	return View(model);
+        }
+
+		public ActionResult AddTwitterAccount()
+		{
+			var oAuth = new OAuthTwitter();
+
+			if (Request["oauth_token"] == null)
+			{
+				var action = Url.Action("AddTwitterAccount");
+				var url = Request.Url.Scheme + "://" + Request.Url.Host +
+						  (Request.Url.Port != 80 ? ":" + Request.Url.Port : string.Empty) + action;
+
+				Response.Redirect(oAuth.AuthorizationLinkGet(url).ToString());
+			}
+			else
+			{
+				var response = oAuth.AccessTokenGet(Request["oauth_token"], Request["oauth_verifier"]);
+				if (response.Length > 0)
+				{
+					var username =OAuthTwitter.GetResponseContent(response, "screen_name");
+
+					var personaService = AgendaFactory.GetPersonaService();
+
+					personaService.AddCuenta(IdentityProviderEnum.Twitter, username);
+					return RedirectToAction("Index");
+				}
+			}
+			return RedirectToAction("Index");
+		}
+
+		public ActionResult Remove(string identityProvider, string username)
+		{
+			var personaService = AgendaFactory.GetPersonaService();
+
+			IdentityProviderEnum ip;
+			if (!Enum.TryParse<IdentityProviderEnum>(identityProvider, out ip))
+			{
+				ModelState.AddModelError("IdentityProvider", "No se reconoce el identity provider" + identityProvider);
+				return View("Index");
+			}
+
+			personaService.RemoveCuenta(ip, username);
+
+			return RedirectToAction("Index");
+		}
+    }
+}
--- a/Agendas/trunk/src/Agendas.Web/Models/EventoModel.cs	Wed Jun 08 08:40:04 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Web/Models/EventoModel.cs	Sat Jun 11 01:20:59 2011 -0300
@@ -6,7 +6,7 @@
 
 namespace AltNetHispano.Agendas.Web.Models
 {
-  public class EventoIndexModel
+	public class EventoIndexModel
   {
     public IEnumerable<EventoDto> ProximosEventos { get; set; }
   }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Agendas/trunk/src/Agendas.Web/Models/PerfilModel.cs	Sat Jun 11 01:20:59 2011 -0300
@@ -0,0 +1,22 @@
+using System.Collections.Generic;
+
+namespace AltNetHispano.Agendas.Web.Models
+{
+	public class PerfilIndexModel
+	{
+		public PerfilIndexModel()
+		{
+			Cuentas = new CuentaDto[] {};
+		}
+
+		public string DisplayName { get; set; }
+
+		public IEnumerable<CuentaDto> Cuentas { get; set; }
+	}
+
+	public class CuentaDto
+	{
+		public string IdentityProvider { get; set; }
+		public string UserName { get; set; }
+	}
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Agendas/trunk/src/Agendas.Web/Views/Perfil/Index.cshtml	Sat Jun 11 01:20:59 2011 -0300
@@ -0,0 +1,30 @@
+@model AltNetHispano.Agendas.Web.Models.PerfilIndexModel
+<h2>Perfil</h2>
+<p>
+    Hola @Model.DisplayName, estas son tus cuentas registradas
+</p>
+<table>
+    <thead>
+        <tr>
+            <th>Proveedor</th>
+            <th>username</th>
+            <th>Acciones</th>
+        </tr>
+    </thead>
+    <tbody>
+        @foreach (var item in Model.Cuentas)
+        {
+        <tr>
+            <td>@item.IdentityProvider</td>
+            <td>@item.UserName</td>
+            <td>
+				@Html.ActionLink("Quitar", "Remove", new { identityProvider = item.IdentityProvider, userName = item.UserName })
+			</td>
+        </tr>
+        }
+    </tbody>
+</table>
+
+<p>
+@Html.ActionLink("Asociar cuenta de Twitter", "AddTwitterAccount")
+</p>
--- a/Agendas/trunk/src/Agendas.Web/Views/Shared/_Menu.cshtml	Wed Jun 08 08:40:04 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Web/Views/Shared/_Menu.cshtml	Sat Jun 11 01:20:59 2011 -0300
@@ -3,6 +3,10 @@
     <li>@Html.ActionLink("Propuestas", "Index", "Propuesta")</li>
     <li>@Html.ActionLink("Eventos", "Index", "Evento")</li>
     <li>@Html.ActionLink("Histórico", "Index", "Histórico")</li>
+@if (Request.IsAuthenticated)
+{
+    <li>@Html.ActionLink("Perfil", "Index", "Perfil")</li>
+}
     <li>@Html.ActionLink("About", "About", "Home")</li>
 </ul>
 
Binary file Agendas/trunk/src/Agendas.Web/favicon.ico has changed