# HG changeset patch # User nelopauselli # Date 1312827866 10800 # Node ID 1deccd6c3cb2918e14b16e2397830c00d701f561 # Parent 33e57fd1a6c925df531738989935fc26e0af5b03 Aplicando seguridad x roles en sitio web diff -r 33e57fd1a6c9 -r 1deccd6c3cb2 Agendas/trunk/src/Agendas.Domain/IdentityContext.cs --- a/Agendas/trunk/src/Agendas.Domain/IdentityContext.cs Mon Aug 08 12:30:37 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Domain/IdentityContext.cs Mon Aug 08 15:24:26 2011 -0300 @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; using AltNetHispano.Agendas.Domain.Exceptions; using AltNetHispano.Agendas.Domain.Repositories; @@ -46,5 +48,17 @@ var cuenta = _personaRepository.GetCuenta(identification.IdentityProvider, identification.LogonName); return cuenta != null ? cuenta.Persona : null; } + + public static bool IsInRole(IEnumerable roles) + { + var persona = GetUsuario(); + return IsInRole(persona, roles); + } + + public static bool IsInRole(Persona persona, IEnumerable roles) + { + if (persona == null) return false; + return roles.Any(role => persona.Roles.Contains(role)); + } } } \ No newline at end of file diff -r 33e57fd1a6c9 -r 1deccd6c3cb2 Agendas/trunk/src/Agendas.Factories/Agendas.Factories.csproj --- a/Agendas/trunk/src/Agendas.Factories/Agendas.Factories.csproj Mon Aug 08 12:30:37 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Factories/Agendas.Factories.csproj Mon Aug 08 15:24:26 2011 -0300 @@ -45,7 +45,7 @@ - + diff -r 33e57fd1a6c9 -r 1deccd6c3cb2 Agendas/trunk/src/Agendas.Factories/AttributeFactory.cs --- a/Agendas/trunk/src/Agendas.Factories/AttributeFactory.cs Mon Aug 08 12:30:37 2011 -0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,13 +0,0 @@ -using System.Web.Mvc; -using Agendas.NHibernate; - -namespace AltNetHispano.Agendas.Factories -{ - public class AttributeFactory - { - public static ActionFilterAttribute GetNHibernateSessionPerAction() - { - return new NHibernateSessionPerActionAttribute(NhHelper.GetSessionFactory()); - } - } -} \ No newline at end of file diff -r 33e57fd1a6c9 -r 1deccd6c3cb2 Agendas/trunk/src/Agendas.Factories/NHibernateFactory.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Factories/NHibernateFactory.cs Mon Aug 08 15:24:26 2011 -0300 @@ -0,0 +1,19 @@ +using System; +using System.Web.Mvc; +using Agendas.NHibernate; + +namespace AltNetHispano.Agendas.Factories +{ + public static class NHibernateFactory + { + public static ActionFilterAttribute GetNHibernateSessionPerAction() + { + return new NHibernateSessionPerActionAttribute(NhHelper.GetSessionFactory()); + } + + public static IDisposable GetSessionScope() + { + return new SessionScope(NhHelper.GetSessionFactory()); + } + } +} \ No newline at end of file diff -r 33e57fd1a6c9 -r 1deccd6c3cb2 Agendas/trunk/src/Agendas.NHibernate/Agendas.NHibernate.csproj --- a/Agendas/trunk/src/Agendas.NHibernate/Agendas.NHibernate.csproj Mon Aug 08 12:30:37 2011 -0300 +++ b/Agendas/trunk/src/Agendas.NHibernate/Agendas.NHibernate.csproj Mon Aug 08 15:24:26 2011 -0300 @@ -58,6 +58,7 @@ + diff -r 33e57fd1a6c9 -r 1deccd6c3cb2 Agendas/trunk/src/Agendas.NHibernate/SessionScope.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.NHibernate/SessionScope.cs Mon Aug 08 15:24:26 2011 -0300 @@ -0,0 +1,27 @@ +using System; +using NHibernate; +using NHibernate.Context; + +namespace Agendas.NHibernate +{ + public class SessionScope : IDisposable + { + private readonly ISessionFactory _sessionFactory; + + public SessionScope(ISessionFactory sessionFactory) + { + _sessionFactory = sessionFactory; + + var session = _sessionFactory.OpenSession(); + CurrentSessionContext.Bind(session); + } + + public void Dispose() + { + var session = _sessionFactory.GetCurrentSession(); + + session.Flush(); + session.Close(); + } + } +} \ No newline at end of file diff -r 33e57fd1a6c9 -r 1deccd6c3cb2 Agendas/trunk/src/Agendas.Tests/Agendas.Tests.csproj --- a/Agendas/trunk/src/Agendas.Tests/Agendas.Tests.csproj Mon Aug 08 12:30:37 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Tests/Agendas.Tests.csproj Mon Aug 08 15:24:26 2011 -0300 @@ -87,6 +87,7 @@ + diff -r 33e57fd1a6c9 -r 1deccd6c3cb2 Agendas/trunk/src/Agendas.Tests/IdentityContextTests.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Tests/IdentityContextTests.cs Mon Aug 08 15:24:26 2011 -0300 @@ -0,0 +1,19 @@ +using AltNetHispano.Agendas.Domain; +using NUnit.Framework; + +namespace AltNetHispano.Agendas.Tests +{ + [TestFixture] + public class IdentityContextTests : TestBase + { + [Test] + public void IsInRole() + { + var persona = new Persona("Nelo"); + persona.Roles.Add(Roles.Usuario); + + Assert.IsTrue(IdentityContext.IsInRole(persona, new[] { Roles.Usuario })); + Assert.IsFalse(IdentityContext.IsInRole(persona, new[] { Roles.Administrador })); + } + } +} \ No newline at end of file diff -r 33e57fd1a6c9 -r 1deccd6c3cb2 Agendas/trunk/src/Agendas.Web.Tests/Agendas.Web.Tests.csproj --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Web.Tests/Agendas.Web.Tests.csproj Mon Aug 08 15:24:26 2011 -0300 @@ -0,0 +1,68 @@ + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {10FECBBD-F07F-4721-87DA-D3184CF86C90} + Library + Properties + Agendas.Web.Tests + Agendas.Web.Tests + v4.0 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\NUnit.2.5.10.11092\lib\nunit.framework.dll + + + + + + + + + + + + + + + + + {A14907DF-02E4-4FA7-BE27-4292AF50AA22} + Agendas.Domain + + + {319A8E3D-C61E-455F-A1BF-A6B1B1636BAB} + Agendas.Web + + + + + \ No newline at end of file diff -r 33e57fd1a6c9 -r 1deccd6c3cb2 Agendas/trunk/src/Agendas.Web.Tests/AutorizationsTests.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Web.Tests/AutorizationsTests.cs Mon Aug 08 15:24:26 2011 -0300 @@ -0,0 +1,141 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Web.Mvc; +using AltNetHispano.Agendas.Domain; +using AltNetHispano.Agendas.Web.Controllers; +using NUnit.Framework; + +namespace Agendas.Web.Tests +{ + [TestFixture] + public class Autorizaciones + { + private IEnumerable _methods; + + [TestFixtureSetUp] + public void ReadMethods() + { + var types = typeof (HomeController).Assembly.GetTypes().ToList(); + var controllers = types.Where(t => typeof (Controller).IsAssignableFrom(t)).ToList(); + + var methods = new List(); + foreach (var controller in controllers) + { + var temp = + controller.GetMethods(BindingFlags.Public | BindingFlags.Instance | ~BindingFlags.FlattenHierarchy).Where( + m => typeof (ActionResult).IsAssignableFrom(m.ReturnType)); + + methods.AddRange(temp); + } + + _methods = methods; + } + + [Test] + public void Acciones_publicas() + { + var acciones = new[] + { + "HomeController.Index", "HomeController.About", "EventoController.Index", "AccountController.LogOn", + "AccountController.LogOff", "AccountController.TwitterLogOn", "HistoricoController.Index", + "PersonaController.Index", "ErrorController.NoAutorizado" + }; + + #region Asserts + + bool fail = false; + foreach (var method in _methods) + { + var action = method.DeclaringType.Name + "." + method.Name; + if (acciones.Contains(action)) + { + if (method.GetCustomAttributes(typeof (CustomAuthorizeAttribute), false).Any()) + { + fail = true; + Console.WriteLine(action + " debe ser público"); + } + } + else + { + if (!method.GetCustomAttributes(typeof (CustomAuthorizeAttribute), false).Any()) + { + fail = true; + Console.WriteLine(action + " debe ser seguro"); + } + } + } + + Assert.IsFalse(fail); + + #endregion + } + + [Test] + public void Acciones_privadas() + { + var acciones = new[] + { + "PerfilController.Index", "PerfilController.AddGoogleAccount", "PerfilController.AddTwitterAccount", + "PerfilController.Remove", "PerfilController.Modificar" + }; + + VerficarAccionesSeguras(acciones, Roles.Usuario, "debe ser privado"); + } + + [Test] + public void Acciones_del_administrador() + { + var acciones = new[] + { + "EventoController.Agendar", "EventoController.Confirmar", "EventoController.Nuevo", + "EventoController.Publicar", "EventoController.Modificar", "EventoController.Proponer", + "EventoController.Cancelar", "EventoController.Descartar", "EventoController.ReAgendar", + "EventoController.ReProponer", "PersonaController.Nueva", "PersonaController.Modificar" + }; + + VerficarAccionesSeguras(acciones, Roles.Administrador, "debe ser de uso exclusivo de los administradores"); + } + + private void VerficarAccionesSeguras(IEnumerable acciones, string rol, string mensaje) + { + bool fail = false; + foreach (var method in _methods) + { + var action = method.DeclaringType.Name + "." + method.Name; + if (acciones.Contains(action)) + { + if (method.GetCustomAttributes(typeof (CustomAuthorizeAttribute), false).Any()) + { + var found = + method.GetCustomAttributesData().Any(d => d.NamedArguments.Any(a => rol.Equals(a.TypedValue.Value))); + + if (!found) + { + fail = true; + Console.WriteLine(action + " " + mensaje); + } + } + else + { + fail = true; + Console.WriteLine(action + " debe ser seguro"); + } + } + else if (method.GetCustomAttributes(typeof (CustomAuthorizeAttribute), false).Any()) + { + var found = + method.GetCustomAttributesData().Any(d => d.NamedArguments.Any(a => rol.Equals(a.TypedValue.Value))); + + if (found) + { + fail = true; + Console.WriteLine(action + " no " + mensaje); + } + } + } + Assert.IsFalse(fail); + } + } +} \ No newline at end of file diff -r 33e57fd1a6c9 -r 1deccd6c3cb2 Agendas/trunk/src/Agendas.Web.Tests/Properties/AssemblyInfo.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Web.Tests/Properties/AssemblyInfo.cs Mon Aug 08 15:24:26 2011 -0300 @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Agendas.Web.Tests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Microsoft")] +[assembly: AssemblyProduct("Agendas.Web.Tests")] +[assembly: AssemblyCopyright("Copyright © Microsoft 2011")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("8464a0e5-fb7f-4b26-a0cd-5f86655f13f1")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff -r 33e57fd1a6c9 -r 1deccd6c3cb2 Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj --- a/Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj Mon Aug 08 12:30:37 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj Mon Aug 08 15:24:26 2011 -0300 @@ -62,6 +62,8 @@ + + @@ -228,6 +230,9 @@ + + + - @Html.ActionLink("Modificar datos", "Modificar", "Persona", new {id=Model.Id}, null) + @Html.ActionLink("Modificar datos", "Modificar")
diff -r 33e57fd1a6c9 -r 1deccd6c3cb2 Agendas/trunk/src/Agendas.Web/Views/Shared/SinPermisos.cshtml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Web/Views/Shared/SinPermisos.cshtml Mon Aug 08 15:24:26 2011 -0300 @@ -0,0 +1,1 @@ +

Usted no tiene permisos para realizar la acción solicitada

\ No newline at end of file diff -r 33e57fd1a6c9 -r 1deccd6c3cb2 Agendas/trunk/src/Agendas.sln --- a/Agendas/trunk/src/Agendas.sln Mon Aug 08 12:30:37 2011 -0300 +++ b/Agendas/trunk/src/Agendas.sln Mon Aug 08 15:24:26 2011 -0300 @@ -38,6 +38,8 @@ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Agendas.Configurations.Tests", "Agendas.Configurations.Tests\Agendas.Configurations.Tests.csproj", "{BBE36765-6AAB-4689-B2F3-6D18E3F11746}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Agendas.Web.Tests", "Agendas.Web.Tests\Agendas.Web.Tests.csproj", "{10FECBBD-F07F-4721-87DA-D3184CF86C90}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -104,6 +106,10 @@ {BBE36765-6AAB-4689-B2F3-6D18E3F11746}.Debug|Any CPU.Build.0 = Debug|Any CPU {BBE36765-6AAB-4689-B2F3-6D18E3F11746}.Release|Any CPU.ActiveCfg = Release|Any CPU {BBE36765-6AAB-4689-B2F3-6D18E3F11746}.Release|Any CPU.Build.0 = Release|Any CPU + {10FECBBD-F07F-4721-87DA-D3184CF86C90}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {10FECBBD-F07F-4721-87DA-D3184CF86C90}.Debug|Any CPU.Build.0 = Debug|Any CPU + {10FECBBD-F07F-4721-87DA-D3184CF86C90}.Release|Any CPU.ActiveCfg = Release|Any CPU + {10FECBBD-F07F-4721-87DA-D3184CF86C90}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE