Esempio n. 1
0
        public Task <IViewComponentResult> InvokeAsync()
        {
            // Create a dictionary with all roles for injecting as json into grant permission page
            Dictionary <string, IdentityRole <string> > rolesList = new Dictionary <string, IdentityRole <string> >();

            // Create a dictionary with role id and name, since we will use role name in GrantViewModel
            // and we have role id in RolePermission table.
            Dictionary <string, string> roleNameByRoleId = new Dictionary <string, string>();

            foreach (var role in _roleManager.Roles)
            {
                roleNameByRoleId.Add(role.Id, role.Name);
                rolesList.Add(role.Id, role);
            }

            ViewBag.RolesList = rolesList;

            GrantViewModel model = ReadGrants.ReadAll(_roleManager, Storage, roleNameByRoleId);

            return(Task.FromResult <IViewComponentResult>(View("_List_Roles_Extensions", model)));
        }
Esempio n. 2
0
        public IActionResult Index()
        {
            // Create a dictionary with all roles for injecting as json into grant permission page
            Dictionary <string, IdentityRole <string> > rolesList = new Dictionary <string, IdentityRole <string> >();

            // Create a dictionary with role id and name, since we will use role name in GrantViewModel
            // and we have role id in RolePermission table.
            Dictionary <string, string> roleNameByRoleId = new Dictionary <string, string>();

            foreach (var role in _roleManager.Roles)
            {
                roleNameByRoleId.Add(role.Id, role.Name);
                rolesList.Add(role.Id, role);
            }

            ViewBag.RolesList = rolesList;

            var model = ReadGrants.ReadAll(_roleManager, Storage, roleNameByRoleId);

            return(View(model));
        }
Esempio n. 3
0
        public async Task ReadAllAsync()
        {
            var repo     = DatabaseFixture.Storage.GetRepository <IRolePermissionRepository>();
            var permRepo = DatabaseFixture.Storage.GetRepository <IPermissionRepository>();

            try
            {
                // Arrange
                // 1. Create base roles
                await CreateBaseRolesIfNeededAsync();

                // 2. Create "Special User" role
                await CreateRoleIfNotExistingAsync("Special User");

                // 3. Read roles to get their IDs
                var adminRole = await DatabaseFixture.RoleManager.FindByNameAsync(Role.Administrator.GetRoleName());

                var userRole = await DatabaseFixture.RoleManager.FindByNameAsync(Role.User.GetRoleName());

                var anonymousRole = await DatabaseFixture.RoleManager.FindByNameAsync(Role.Anonymous.GetRoleName());

                var specialUserRole = await DatabaseFixture.RoleManager.FindByNameAsync("Special User");

                // 4. Read permissions to get their IDs
                var adminPermissionId = permRepo.All().FirstOrDefault(p_ => p_.Name == Permission.Admin.GetPermissionName())?.Id;
                var writePermissionId = permRepo.All().FirstOrDefault(p_ => p_.Name == Permission.Write.GetPermissionName())?.Id;
                var readPermissionId  = permRepo.All().FirstOrDefault(p_ => p_.Name == Permission.Read.GetPermissionName())?.Id;
                var neverPermissionId = permRepo.All().FirstOrDefault(p_ => p_.Name == Permission.Never.GetPermissionName())?.Id;
                Assert.NotNull(adminPermissionId);
                Assert.NotNull(writePermissionId);
                Assert.NotNull(readPermissionId);
                Assert.NotNull(neverPermissionId);

                // 5. Create role-extension links
                // Cleanup first
                repo.DeleteAll();
                await DatabaseFixture.Storage.SaveAsync();

                repo.Create(new RolePermission
                {
                    RoleId = adminRole.Id, Extension = Constants.SoftinuxBaseSecurity, PermissionId = adminPermissionId
                });
                repo.Create(new RolePermission
                {
                    RoleId = userRole.Id, Extension = Constants.SoftinuxBaseSecurity, PermissionId = readPermissionId
                });
                repo.Create(new RolePermission
                {
                    RoleId       = anonymousRole.Id,
                    Extension    = Constants.SoftinuxBaseSecurity,
                    PermissionId = neverPermissionId
                });
                repo.Create(new RolePermission
                {
                    RoleId       = specialUserRole.Id,
                    Extension    = Constants.SoftinuxBaseSecurity,
                    PermissionId = writePermissionId
                });

                repo.Create(new RolePermission
                {
                    RoleId = adminRole.Id, Extension = "Chinook", PermissionId = adminPermissionId
                });
                repo.Create(new RolePermission
                {
                    RoleId = userRole.Id, Extension = "Chinook", PermissionId = writePermissionId
                });

                await DatabaseFixture.Storage.SaveAsync();

                // 6. Build the dictionary that is used by the tool and created in GrantPermissionsController
                Dictionary <string, string> roleNameByRoleId = new Dictionary <string, string>();
                roleNameByRoleId.Add(adminRole.Id, adminRole.Name);
                roleNameByRoleId.Add(userRole.Id, userRole.Name);
                roleNameByRoleId.Add(anonymousRole.Id, anonymousRole.Name);
                roleNameByRoleId.Add(specialUserRole.Id, specialUserRole.Name);

                // Execute
                GrantViewModel model = ReadGrants.ReadAll(DatabaseFixture.RoleManager, DatabaseFixture.Storage, roleNameByRoleId);

                // Assert
                // 1. Number of keys: extensions
                Assert.Equal(ExtensionManager.GetInstances <IExtensionMetadata>().Count(), model.PermissionsByRoleAndExtension.Keys.Count);

                // 2. Number of roles for "Security" extension
                Assert.True(model.PermissionsByRoleAndExtension.ContainsKey(Constants.SoftinuxBaseSecurity));

                // We may have additional linked roles left by other tests...
                Assert.True(model.PermissionsByRoleAndExtension[Constants.SoftinuxBaseSecurity].Keys.Count >= 4);

                // 3. Admin role
                Assert.True(model.PermissionsByRoleAndExtension[Constants.SoftinuxBaseSecurity].ContainsKey(adminRole.Name));

                // Admin -> Admin, Write, Read, Never
                Assert.Equal(4, model.PermissionsByRoleAndExtension[Constants.SoftinuxBaseSecurity][adminRole.Name].Count);
                Assert.Contains(Permission.Admin, model.PermissionsByRoleAndExtension[Constants.SoftinuxBaseSecurity][adminRole.Name]);
                Assert.Contains(Permission.Write, model.PermissionsByRoleAndExtension[Constants.SoftinuxBaseSecurity][adminRole.Name]);
                Assert.Contains(Permission.Read, model.PermissionsByRoleAndExtension[Constants.SoftinuxBaseSecurity][adminRole.Name]);
                Assert.Contains(Permission.Never, model.PermissionsByRoleAndExtension[Constants.SoftinuxBaseSecurity][adminRole.Name]);

                // 4. Special User role
                Assert.True(model.PermissionsByRoleAndExtension[Constants.SoftinuxBaseSecurity].ContainsKey(specialUserRole.Name));

                // Write -> Write, Read, Never
                Assert.Equal(3, model.PermissionsByRoleAndExtension[Constants.SoftinuxBaseSecurity][specialUserRole.Name].Count);
                Assert.Contains(Permission.Write, model.PermissionsByRoleAndExtension[Constants.SoftinuxBaseSecurity][specialUserRole.Name]);
                Assert.Contains(Permission.Read, model.PermissionsByRoleAndExtension[Constants.SoftinuxBaseSecurity][specialUserRole.Name]);
                Assert.Contains(Permission.Never, model.PermissionsByRoleAndExtension[Constants.SoftinuxBaseSecurity][specialUserRole.Name]);

                // 5. User role
                Assert.True(model.PermissionsByRoleAndExtension[Constants.SoftinuxBaseSecurity].ContainsKey(userRole.Name));

                // Read -> Read, Never
                Assert.Equal(2, model.PermissionsByRoleAndExtension[Constants.SoftinuxBaseSecurity][userRole.Name].Count);
                Assert.Contains(Permission.Read, model.PermissionsByRoleAndExtension[Constants.SoftinuxBaseSecurity][userRole.Name]);
                Assert.Contains(Permission.Never, model.PermissionsByRoleAndExtension[Constants.SoftinuxBaseSecurity][userRole.Name]);

                // 6. Anonymous role
                Assert.True(model.PermissionsByRoleAndExtension[Constants.SoftinuxBaseSecurity].ContainsKey(anonymousRole.Name));

                // Never -> Never
                Assert.Single(model.PermissionsByRoleAndExtension[Constants.SoftinuxBaseSecurity][anonymousRole.Name]);
                Assert.Contains(Permission.Never, model.PermissionsByRoleAndExtension[Constants.SoftinuxBaseSecurity][anonymousRole.Name]);

                // 7. Number of roles for Chinook extension
                // When the dll doesn't exist on disk, this is our case, no permissions should be found
                Assert.False(model.PermissionsByRoleAndExtension.ContainsKey("Chinook"));

                // No need to check the details for this extension

                // 8. SoftinuxBase.SeedDatabase extension was found
                Assert.True(model.PermissionsByRoleAndExtension.ContainsKey("SoftinuxBase.SeedDatabase"));
            }
            finally
            {
                // Cleanup created data
                repo.DeleteAll();
                await DatabaseFixture.Storage.SaveAsync();

                var specialUserRole = await DatabaseFixture.RoleManager.FindByNameAsync("Special User");

                await DatabaseFixture.RoleManager.DeleteAsync(specialUserRole);
            }
        }
Esempio n. 4
0
        public async void Test()
        {
            var repo = DatabaseFixture.Storage.GetRepository <IRolePermissionRepository>();

            try
            {
                // Arrange
                // 1. Create base roles
                await CreateBaseRolesIfNeeded();

                // 2. Create "Special User" role
                await CreateRoleIfNotExisting("Special User");

                // 3. Read roles to get their IDs
                var adminRole = await DatabaseFixture.RoleManager.FindByNameAsync(Role.Administrator.GetRoleName());

                var userRole = await DatabaseFixture.RoleManager.FindByNameAsync(Role.User.GetRoleName());

                var anonymousRole = await DatabaseFixture.RoleManager.FindByNameAsync(Role.Anonymous.GetRoleName());

                var specialUserRole = await DatabaseFixture.RoleManager.FindByNameAsync("Special User");

                // 4. Create role-extension links
                // Cleanup first
                repo.DeleteAll();
                DatabaseFixture.Storage.Save();

                repo.Create(new RolePermission
                {
                    RoleId = adminRole.Id, Scope = "Security", PermissionId = Permission.Admin.GetPermissionName()
                });
                repo.Create(new RolePermission
                {
                    RoleId = userRole.Id, Scope = "Security", PermissionId = Permission.Read.GetPermissionName()
                });
                repo.Create(new RolePermission
                {
                    RoleId = anonymousRole.Id, Scope = "Security", PermissionId = Permission.Never.GetPermissionName()
                });
                repo.Create(new RolePermission
                {
                    RoleId = specialUserRole.Id, Scope = "Security", PermissionId = Permission.Write.GetPermissionName()
                });

                repo.Create(new RolePermission
                {
                    RoleId = adminRole.Id, Scope = "Chinook", PermissionId = Permission.Admin.GetPermissionName()
                });
                repo.Create(new RolePermission
                {
                    RoleId = userRole.Id, Scope = "Chinook", PermissionId = Permission.Write.GetPermissionName()
                });

                DatabaseFixture.Storage.Save();

                // 5. Build the dictionary that is used by the tool and created in GrantPermissionsController
                Dictionary <string, string> roleNameByRoleId = new Dictionary <string, string>();
                roleNameByRoleId.Add(adminRole.Id, adminRole.Name);
                roleNameByRoleId.Add(userRole.Id, userRole.Name);
                roleNameByRoleId.Add(anonymousRole.Id, anonymousRole.Name);
                roleNameByRoleId.Add(specialUserRole.Id, specialUserRole.Name);

                // Execute
                GrantViewModel model = ReadGrants.ReadAll(DatabaseFixture.RoleManager, DatabaseFixture.Storage,
                                                          roleNameByRoleId);

                // Assert
                // 1. Number of keys: extensions
                Assert.Equal(ExtensionManager.GetInstances <IExtensionMetadata>().Count(), model.PermissionsByRoleAndScope.Keys.Count);

                // 2. Number of roles for "Security" extension
                Assert.True((model.PermissionsByRoleAndScope.ContainsKey("Security")));
                Assert.Equal(4, model.PermissionsByRoleAndScope["Security"].Keys.Count);

                // 3. Admin role
                Assert.True(model.PermissionsByRoleAndScope["Security"].ContainsKey(adminRole.Name));
                // Admin -> Admin, Write, Read, Never
                Assert.Equal(4, model.PermissionsByRoleAndScope["Security"][adminRole.Name].Count);
                Assert.Contains(Permission.Admin, model.PermissionsByRoleAndScope["Security"][adminRole.Name]);
                Assert.Contains(Permission.Write, model.PermissionsByRoleAndScope["Security"][adminRole.Name]);
                Assert.Contains(Permission.Read, model.PermissionsByRoleAndScope["Security"][adminRole.Name]);
                Assert.Contains(Permission.Never, model.PermissionsByRoleAndScope["Security"][adminRole.Name]);

                // 4. Special User role
                Assert.True(model.PermissionsByRoleAndScope["Security"].ContainsKey(specialUserRole.Name));
                // Write -> Write, Read, Never
                Assert.Equal(3, model.PermissionsByRoleAndScope["Security"][specialUserRole.Name].Count);
                Assert.Contains(Permission.Write, model.PermissionsByRoleAndScope["Security"][specialUserRole.Name]);
                Assert.Contains(Permission.Read, model.PermissionsByRoleAndScope["Security"][specialUserRole.Name]);
                Assert.Contains(Permission.Never, model.PermissionsByRoleAndScope["Security"][specialUserRole.Name]);

                // 5. User role
                Assert.True(model.PermissionsByRoleAndScope["Security"].ContainsKey(userRole.Name));

                // Read -> Read, Never
                Assert.Equal(2, model.PermissionsByRoleAndScope["Security"][userRole.Name].Count);
                Assert.Contains(Permission.Read, model.PermissionsByRoleAndScope["Security"][userRole.Name]);
                Assert.Contains(Permission.Never, model.PermissionsByRoleAndScope["Security"][userRole.Name]);

                // 6. Anonymous role
                Assert.True(model.PermissionsByRoleAndScope["Security"].ContainsKey(anonymousRole.Name));
                // Never -> Never
                Assert.Single(model.PermissionsByRoleAndScope["Security"][anonymousRole.Name]);
                Assert.Contains(Permission.Never, model.PermissionsByRoleAndScope["Security"][anonymousRole.Name]);

                // 7. Number of roles for Chinook extension
                Assert.True((model.PermissionsByRoleAndScope.ContainsKey("Chinook")));
                Assert.Equal(2, model.PermissionsByRoleAndScope["Chinook"].Keys.Count);

                // No need to check the details for this extension

                //  8. SeedDatabase extension was found, no permissions should be found
                Assert.True((model.PermissionsByRoleAndScope.ContainsKey("SeedDatabase")));
                Assert.Equal(0, model.PermissionsByRoleAndScope["SeedDatabase"].Keys.Count);
            }
            finally
            {
                // Cleanup created data
                repo.DeleteAll();
                DatabaseFixture.Storage.Save();

                var specialUserRole = await DatabaseFixture.RoleManager.FindByNameAsync("Special User");

                await DatabaseFixture.RoleManager.DeleteAsync(specialUserRole);
            }
        }