view Agendas/trunk/src/Agendas.Web/Controllers/PerfilController.cs @ 275:bf993f99cee3

Ticket #123: Patrocinadores de las vans.
author juanjose.montesdeocaarbos
date Wed, 14 Dec 2011 08:15:44 -0300
parents 222362c29416
children
line wrap: on
line source

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
    {
		[CustomAuthorize(Roles = Roles.Usuario)]
		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
        	            	{
        	            		Id = persona.Id,
        	            		DisplayName = persona.Nombre,
        	            		Email = persona.Mail,
        	            		Blog = persona.Blog,
        	            		Cuentas = from c in persona.Cuentas
        	            		          select
        	            		          	new CuentaDto
        	            		          		{IdentityProvider = c.IdentityProvider.ToString(), UserName = c.LogonName}
        	            	};

        	return View(model);
        }

		[CustomAuthorize(Roles = Roles.Usuario)]
		public ActionResult AddGoogleAccount()
        {
            throw new NotImplementedException();
        }

		[CustomAuthorize(Roles = Roles.Usuario)]
		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;

				return Redirect(oAuth.AuthorizationLinkGet(url).ToString());
			}
			
			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");
		}

		[CustomAuthorize(Roles = Roles.Usuario)]
		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");
		}

		[CustomAuthorize(Roles = Roles.Usuario)]
		public ActionResult Modificar()
		{
			var persona = IdentityContext.GetUsuario();
			if (persona == null)
			{
				this.AddError("No se encontró la persona que intenta modificar");
				return RedirectToAction("Index");
			}

			var model = new PerfilEditModel
			{
				Nombre = persona.Nombre,
				Twitter = persona.Twitter,
				EMail = persona.Mail,
				Blog = persona.Blog
			};

			return View("Defaulteditor", model);
		}

		[HttpPost]
		[CustomAuthorize(Roles = Roles.Usuario)]
		public ActionResult Modificar(PerfilEditModel model)
		{
			if (ModelState.IsValid)
			{
				var persona = IdentityContext.GetUsuario();

				var personas = AgendaFactory.GetPersonaService();
				var r = personas.Update(persona.Id, model.Nombre, model.Twitter, model.EMail, model.Blog, null);
				if (r.Succeful)
				{
					this.AddNotification("Los datos fueron guardados");
					return RedirectToAction("Index");
				}
				this.AddError(r.Message);
			}

			return View("Defaulteditor", model);
		}
	}
}