view Agendas/trunk/src/Agendas.Domain/IdentityContext.cs @ 104:c5034884c7d7

refactor para que los estados sean singletons
author jorge.rowies
date Sun, 05 Jun 2011 13:22:36 -0300
parents db4b1e2cae49
children 0bca45e1e664
line wrap: on
line source

using System;
using AltNetHispano.Agendas.Domain.Exceptions;
using AltNetHispano.Agendas.Domain.Repositories;

namespace AltNetHispano.Agendas.Domain
{
	public class IdentityContext
	{
		private static ISeguridad _current;
		private static IPersonaRepository _personaRepository;

		public static void Init(ISeguridad seguridad, IPersonaRepository personaRepository)
		{
			_current = seguridad;
			_personaRepository = personaRepository;
		}

		private static ISeguridad Current
		{
			get
			{
				if (_current == null)
					throw new IdentityContextNotConfiguredException();
				return _current;
			}
		}

		public static string GetUserName()
		{
			string username = Current.GetUserName();
			if (string.IsNullOrWhiteSpace(username))
				throw new UsuarioNoAutenticadoException();
			return username;
		}

		public static Persona GetUsuario()
		{
			var cuenta = _personaRepository.GetCuenta(IdentityProviderEnum.Twitter, GetUserName());
			return cuenta != null ? cuenta.Persona : null;
		}
	}
}