view Agendas/trunk/src/Agendas.Web/Controllers/PerfilController.cs @ 114:1ce71844ffa4

Ups, me habia olvidado del return.
author Carlos Peix
date Sat, 11 Jun 2011 12:50:44 -0300
parents 0eac9a1c1a6c
children 7a2eeb9e9bf9
line wrap: on
line source

using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using AltNetHispano.Agendas.Domain;
using AltNetHispano.Agendas.Factories;
using AltNetHispano.Agendas.Twitter;
using AltNetHispano.Agendas.Web.Models;
using AltNetHispano.Agendas.Web.Services;

namespace AltNetHispano.Agendas.Web.Controllers
{
    public class PerfilController : Controller
    {
		public IMembershipService MembershipService { get; set; }

		protected override void Initialize(RequestContext requestContext)
		{
			//if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
			if (MembershipService == null)
				MembershipService = new AccountMembershipService();

			base.Initialize(requestContext);
		}

		//
        // 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 AddBuiltInAccount()
		{
			return View();
		}

		[HttpPost]
		public ActionResult AddBuiltInAccount(LogOnModel model)
		{
			if (ModelState.IsValid)
			{
				if (MembershipService.ValidateUser(model.UserName, model.Password))
				{
					var personaService = AgendaFactory.GetPersonaService();
					personaService.AddCuenta(IdentityProviderEnum.BuiltIn, model.UserName);
					return RedirectToAction("Index");
				}
				ModelState.AddModelError("", "The user name or password provided is incorrect.");
			}

			// If we got this far, something failed, redisplay form
			return View(model);
		}

		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");
		}
    }
}