Exemple #1
0
        public void GetRolesOExistingfUsersSucceeds()
        {
            var pdp = new PDP();
            
            // Admin
            Assert.That(pdp.getRolesOfUser("Ricardo").Select(role => role.RoleName), Is.EquivalentTo(new[] {"Admin", "Director", "User", "Manager", "Guest"}));

            // Director
            Assert.That(pdp.getRolesOfUser("Luís").Select(role => role.RoleName), Is.EquivalentTo(new[] { "Director", "User", "Guest" }));

            // Manager
            Assert.That(pdp.getRolesOfUser("Pedro").Select(role => role.RoleName), Is.EquivalentTo(new[] { "Manager", "User", "Guest" }));

            // Auditor
            Assert.That(pdp.getRolesOfUser("João").Select(role => role.RoleName), Is.EquivalentTo(new[] { "Auditor", "User", "Guest" }));

            // User
            var users = new[] {"Lídia", "Teresa", "Sara", "Elsa", "Mário", "Cristina"};
            foreach (var user in users)
            {
                Assert.That(pdp.getRolesOfUser(user).Select(role => role.RoleName), Is.EquivalentTo(new[] { "User", "Guest" }));
            }

            // Guest
            var guests = new[] { "Maria", "Miguel" };
            foreach (var guest in guests)
            {
                Assert.That(pdp.getRolesOfUser(guest).Select(role => role.RoleName), Is.EquivalentTo(new[] { "Guest" }));
            }
        }
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            var pdp = new PDP();

            string action = httpContext.Request.HttpMethod;
            string username = httpContext.User.Identity.Name;
            string resource = httpContext.Request.Url.AbsolutePath;



            if (!_caseSensitive)
            {
                action = action.ToLower();
                resource = resource.ToLower();
            }

            try
            {
                return pdp.IsUserAuthorized(username, action, resource);
            }
            catch (Exception e)
            {
                if (!(e is ActionNotFoundException) && !(e is ResourceNotFoundException))
                {
                    throw;
                }

                // Action or resource unknown to the policy repository. Forward to default AuthorizeAttribute implementation.
                System.Diagnostics.Debug.WriteLine(e.Message);
                return base.AuthorizeCore(httpContext);
            }

            
        }
Exemple #3
0
        public void GetResourcesOfAuthorizedUserSucceeds()
        {
            const string updateAction = "Alterar ficheiros", insertAction = "Criar ficheiros e pastas", executeAction = "Executar ficheiros";
            
            var pdp = new PDP();

            // Admin
            Assert.That(
                pdp.getResourcesOfAuthorizedUser("Ricardo", updateAction).Select(resource => resource.ResourceName),
                Is.EquivalentTo(new[]
                    {"/folder", "/folder/file1.txt", "/folder/file2.txt"}));

            Assert.That(
                pdp.getResourcesOfAuthorizedUser("Ricardo", executeAction).Select(resource => resource.ResourceName),
                Is.EquivalentTo(new[] { "/folder", "/program.exe" }));

            // Director
            Assert.That(
                pdp.getResourcesOfAuthorizedUser("Luís", executeAction).Select(resource => resource.ResourceName),
                Is.EquivalentTo(new[] { "/folder", "/program.exe" }));

            // Guest
            Assert.That(
                pdp.getResourcesOfAuthorizedUser("Maria", executeAction).Select(resource => resource.ResourceName),
                Is.EquivalentTo(new[] { "/program.exe" }));
        }
Exemple #4
0
        public ActionResult Index()
        {
            ViewBag.Message = "PEP - Policy Enforcement Point.";

            var pdp = new PDP();
            IList<User> users = pdp.GetUsers();
            var userRoles = new Dictionary<int, string>();

            foreach (var user in users)
            {
                List<Role> roles = pdp.getRolesOfUser(user.UserName);
                var sb = new StringBuilder();
                foreach (var role in roles)
                {
                    if (sb.Length != 0)
                    {
                        sb.Append(", ");
                    }
                    sb.Append(role.RoleName);
                }
                userRoles[user.UserId] = sb.ToString();
            }

            ViewBag.Users = users;
            ViewBag.UserRoles = userRoles;
            return View();
        }
Exemple #5
0
 static void isActionAllowedOfUserWithResourceTest(PDP lib)
 {
     String userName = "******";
     String resourceName = "/folder/file1.txt";
     String actionName = "Criar ficheiros e pastas";
     if (!lib.IsUserAuthorized(userName, actionName, resourceName))
         Console.WriteLine("O utilizador {0} não tem permissão {1} sobre o recurso {2}.", userName, actionName, resourceName);
     else
         Console.WriteLine("O utilizador {0} tem permissão {1} sobre o recurso {2}.", userName, actionName, resourceName);
         
 }
Exemple #6
0
        static void Main(string[] args)
        {
            PDP.ConnStringName = "Local";
            PDP lib = new PDP();

            //getRolesOfUserTest(lib);
            //getPermissionsOfUserTest(lib);
            //getActionsAllowedOfUserWithResourceTest(lib);
            isActionAllowedOfUserWithResourceTest(lib);
            Console.ReadKey();
        }
Exemple #7
0
        public ActionResult Resource(string id)
        {
            ViewBag.Username = id;
            
            // Get user page if user exists
            var users = new PDP().GetUsers();
            if (!users.Select(u => u.UserName).Contains(id))
            {
                return new HttpStatusCodeResult(HttpStatusCode.NotFound);
            }

            return View();
        }
Exemple #8
0
        static void getPermissionsOfUserTest(PDP lib)
        {
            String userName = "******";
            List<Permission> result = lib.getPermissionsOfUser(userName);

            if (result == null)
                Console.WriteLine("O utilizador {0} não tem Permissões", userName);
            else
            {
                Console.WriteLine("O utilizador {0} tem os seguintes permissões:", userName);
                foreach (Permission p in result)
                    Console.WriteLine("\t Permissão {0} sobre o recurso {1}.", p.ActionId,p.ResourceId);
            }
        }
Exemple #9
0
        static void getRolesOfUserTest(PDP lib)
        {
            String userName = "******";
            List<Role> result = lib.getRolesOfUser(userName);

            if (result == null)
                Console.WriteLine("O utilizador {0} não tem Roles", userName);
            else
            {
                Console.WriteLine("O utilizador {0} tem os seguintes Roles:", userName);
                foreach (Role r in result)
                    Console.WriteLine("\t {0}", r.RoleName);
            }
        }
Exemple #10
0
        static void getActionsAllowedOfUserWithResourceTest(PDP lib)
        {
            String userName = "******";
            String resourceName = "/folder/file1.txt";
            List<Action> result = lib.getActionsAllowedOfUserWithResource(userName,resourceName).ToList<Action>();

            if (result == null)
                Console.WriteLine("O utilizador {0} não tem Permissões", userName);
            else
            {
                Console.WriteLine("O utilizador {0} tem as seguintes acções sobre o recurso {1}:", userName, resourceName);
                foreach (Action a in result.Distinct())
                    Console.WriteLine("\t {0}", a.ActionName);
            }
        }
            public SimpleMembershipInitializer()
            {
                Database.SetInitializer<UsersContext>(null);

                try
                {
                    using (var context = new UsersContext())
                    {
                        if (!context.Database.Exists())
                        {
                            // Create the SimpleMembership database without Entity Framework migration schema
                            ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                        }
                    }

                    WebSecurity.InitializeDatabaseConnection("Local", "User", "userId", "username", autoCreateTables: true);

                    // Create accounts for all users, if they don't exist already
                    try
                    {
                        IList<User> users = new PDP().GetUsers();
                        foreach (var user in users)
                        {
                            WebSecurity.CreateAccount(user.UserName, "1234");
                        }
                    }
                    catch (System.Web.Security.MembershipCreateUserException)
                    {
                        // accounts already created
                    }


                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
                }
            }
Exemple #12
0
        public void GetResourcesForUnknownThrowsActionNotFoundException()
        {
            var pdp = new PDP();

            Assert.That(() => pdp.getResourcesOfAuthorizedUser("Unknown user", "Unknown action"), Throws.TypeOf<ActionNotFoundException>());
        }
Exemple #13
0
        public void GetActionsAllowedForExistingUserAndResourceSucceeds()
        {
            var pdp = new PDP();

            Assert.That(pdp.getActionsAllowedOfUserWithResource("João", "/folder").Select(action => action.ActionName),
                        Is.EquivalentTo(new[]
                            {
                                "Ver o conteúdo de ficheiros e listar conteúdo de pastas",
                                "Criar ficheiros e pastas",
                                "Executar ficheiros"
                            }));

            Assert.That(pdp.getActionsAllowedOfUserWithResource("Pedro", "/folder/file3.txt").Select(action => action.ActionName),
                       Is.EquivalentTo(new[] { "Ver o conteúdo de ficheiros e listar conteúdo de pastas" }));
        }
Exemple #14
0
        public void IsUserAuthorizedForUnknownActionThrowsActionNotFoundException()
        {
            var pdp = new PDP();

            Assert.That(() => pdp.IsUserAuthorized("Ricardo", "unknown action", "/folder"), Throws.TypeOf<ActionNotFoundException>());
        }
Exemple #15
0
        public void GetResourcesOfUnknownUserReturnsEmptyList()
        {
            var pdp = new PDP();

            Assert.That(pdp.getResourcesOfAuthorizedUser("Unknown", "Executar ficheiros").Select(resource => resource.ResourceName), Is.EquivalentTo(new string[] { }));
        }
Exemple #16
0
        public void GetUsersWithPermissionThrowsResourceNotFoundForUnknownResource()
        {
            var pdp = new PDP();

            Assert.That(() => pdp.getUsersWithPermission("Executar ficheiros", "Unknown resource"), Throws.TypeOf<ResourceNotFoundException>());
        }
Exemple #17
0
        public void GetRolesOfUnknownUserReturnsEmptyList()
        {
            var pdp = new PDP();

            Assert.That(pdp.getRolesOfUser("Unknown").Select(role => role.RoleName), Is.EquivalentTo(new string[] {}));
        }
Exemple #18
0
        public void GetUsersWithPermissionThrowsActionNotFoundForUnknownAction()
        {
            var pdp = new PDP();

            Assert.That(() => pdp.getUsersWithPermission("Unknown action", "/folder"), Throws.TypeOf<ActionNotFoundException>());
        }
Exemple #19
0
        public void IsUserAuthorizedForUnknownResourceThrowsResourceNotFoundException()
        {
            var pdp = new PDP();

            Assert.That(() => pdp.IsUserAuthorized("Ricardo", "Executar ficheiros", "unknown resource"), Throws.TypeOf<ResourceNotFoundException>());
        }
Exemple #20
0
        public void GetUsersForExistingRoleSucceeds()
        {
            var pdp = new PDP();

            Assert.That(pdp.getUsersWithRole("Admin").Select(user => user.UserName),
                Is.EquivalentTo(new[] { "Ricardo"}));

            Assert.That(pdp.getUsersWithRole("User").Select(user => user.UserName),
                Is.EquivalentTo(new[] { "Teresa", "Lídia", "Sara", "Elsa", "Mário", "Cristina" }));
        }
Exemple #21
0
        public void GetUsersForUnknownRoleReturnsEmptyList()
        {
            var pdp = new PDP();

            Assert.That(pdp.getUsersWithRole("Unknown role").Select(user => user.UserName),
                Is.EquivalentTo(new string[] {}));
        }
Exemple #22
0
        public void GetRolesForExistingUserSucceeds()
        {
            var pdp = new PDP();

            Assert.That(pdp.getRolesOfUser("Ricardo").Select(role => role.RoleName),
                Is.EquivalentTo(new[] {"Admin", "Manager", "Director", "User", "Guest"}));
        }
Exemple #23
0
        public void GetRolesForUnknownUserReturnsEmptyList()
        {
            var pdp = new PDP();

            Assert.That(pdp.getRolesOfUser("unknown user"), Is.EquivalentTo(new string[] { }));
        }
Exemple #24
0
        public void IsUserAuthorizedForUnknownUserReturnsFalse()
        {
            var pdp = new PDP();

            Assert.That(pdp.IsUserAuthorized("Unknown user", "Executar ficheiros", "/folder"), Is.False);
        }
Exemple #25
0
        public void GetActionsAllowedForUnknownUserReturnsEmptyList()
        {
            var pdp = new PDP();

            Assert.That(pdp.getActionsAllowedOfUserWithResource("Unknown user", "/folder"), Is.EquivalentTo(new string[] {}));
        }
Exemple #26
0
        public void GetActionsAllowedForUnknownResourceThrowsResourceNotFoundException()
        {
            var pdp = new PDP();

            Assert.That(() => pdp.getActionsAllowedOfUserWithResource("Ricardo", "unknown resource"), Throws.TypeOf<ResourceNotFoundException>());
        }