view Agendas/trunk/src/Agendas.Domain/IdentityContext.cs @ 154:2233039ac2eb

Unificando vistas
author Nelo@Guinea.neluz.int
date Wed, 03 Aug 2011 01:02:42 -0300
parents 7a2eeb9e9bf9
children 1deccd6c3cb2
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()
		{
			return Current.GetUserName();
		}

		public static bool IsAuthenticated()
		{
			return !string.IsNullOrWhiteSpace(GetUserName());
		}

		public static Persona GetUsuario()
		{
			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;
		}
	}
}