Ejemplo n.º 1
0
        public async Task UpdateOrgUnit_ChangeParentId_Test()
        {
            CreateOrgUnitDto createOrgUnitDto = new CreateOrgUnitDto()
            {
                ParentId    = null,
                DisplayName = "Ou Test"
            };
            var orgUnitDto = await _orgUnitAppService.CreateAsync(createOrgUnitDto);

            CreateOrgUnitDto createParentOrgUnitDto = new CreateOrgUnitDto()
            {
                ParentId    = null,
                DisplayName = "Ou Parent"
            };
            var parentOrgUnitDto = await _orgUnitAppService.CreateAsync(createParentOrgUnitDto);

            var getOrgUnitDto = await _orgUnitAppService.GetAsync(new EntityDto <long>(orgUnitDto.Id));

            getOrgUnitDto.ParentId    = parentOrgUnitDto.Id;
            getOrgUnitDto.DisplayName = "**Ou Test**";

            var updatedOrgUnitDto = await _orgUnitAppService.UpdateAsync(getOrgUnitDto);

            updatedOrgUnitDto.DisplayName.ShouldBe("**Ou Test**");
            updatedOrgUnitDto.Code.ShouldBe("00002.00001");
        }
Ejemplo n.º 2
0
        public async Task GetOrgUnit_Test()
        {
            CreateOrgUnitDto createOrgUnitDto = new CreateOrgUnitDto()
            {
                ParentId    = null,
                DisplayName = "Ou Test"
            };
            var orgUnitDto = await _orgUnitAppService.CreateAsync(createOrgUnitDto);

            CreateOrgUnitDto createOrgUnitDto1 = new CreateOrgUnitDto()
            {
                ParentId    = null,
                DisplayName = "Ou Test1"
            };
            var orgUnitDto1 = await _orgUnitAppService.CreateAsync(createOrgUnitDto1);

            await UsingDbContextAsync(async context =>
            {
                int count = await context.OrganizationUnits.CountAsync();
                count.ShouldBe(2);
                var testOu = await context.OrganizationUnits.FirstOrDefaultAsync(ou => ou.Id == orgUnitDto.Id);
                testOu.DisplayName.ShouldBe("Ou Test");
                testOu.Code.ShouldBe("00001");
            });

            var result = await _orgUnitAppService.GetAsync(new EntityDto <long>(orgUnitDto1.Id));

            result.DisplayName.ShouldBe("Ou Test1");
            result.Code.ShouldBe("00002");
        }
Ejemplo n.º 3
0
        public async Task DeleteOrgUnit_Exist_Test()
        {
            CreateOrgUnitDto createOrgUnitDto = new CreateOrgUnitDto()
            {
                ParentId    = null,
                DisplayName = "Ou Test"
            };
            var orgUnitDto = await _orgUnitAppService.CreateAsync(createOrgUnitDto);

            await UsingDbContextAsync(async context =>
            {
                var testOu      = await context.OrganizationUnits.FirstOrDefaultAsync(ou => ou.Id == orgUnitDto.Id);
                testOu.TenantId = 1;
                testOu.DisplayName.ShouldBe("Ou Test");
                testOu.Code.ShouldBe("00001");
            });

            await _orgUnitAppService.DeleteAsync(new EntityDto <long>(orgUnitDto.Id));

            await UsingDbContextAsync(async context =>
            {
                var testOu = await context.OrganizationUnits.FirstOrDefaultAsync(ou => ou.Id == orgUnitDto.Id);
                testOu.IsDeleted.ShouldBeTrue();
            });
        }
        public async Task CreateOrgUnitInTenant_Test()
        {
            CreateTenantDto createTenantDto = new CreateTenantDto()
            {
                TenancyName      = "TestTenant",
                Name             = "TestTenant",
                AdminPhoneNumber = "13851400000",
                IsActive         = true
            };
            var tenantDto = await _tenantAppService.CreateAsync(createTenantDto);

            CreateOrgUnitDto createOrgUnitDto = new CreateOrgUnitDto()
            {
                ParentId    = null,
                DisplayName = "Ou Test"
            };
            var orgUnitDto = await _omOrgUnitAppService.CreateOrgUnitInTenantAsync(tenantDto.Id, createOrgUnitDto);

            await UsingDbContextAsync(async context =>
            {
                var testOu      = await context.OrganizationUnits.FirstOrDefaultAsync(ou => ou.Id == orgUnitDto.Id);
                testOu.TenantId = tenantDto.Id;
                testOu.DisplayName.ShouldBe("Ou Test");
                testOu.Code.ShouldBe("00002");  // AdminGroup is 00001
            });
        }
Ejemplo n.º 5
0
        public async Task GetRolesInOrgUnit_Test()
        {
            CreateOrgUnitDto createOrgUnitDto = new CreateOrgUnitDto()
            {
                ParentId    = null,
                DisplayName = "Ou Test"
            };
            var orgUnitDto = await _orgUnitAppService.CreateAsync(createOrgUnitDto);

            var createRoleDto1 = new CreateRoleDto()
            {
                Name               = "RoleTest1",
                DisplayName        = "Test role1",
                Description        = "Role1 for test",
                GrantedPermissions = new List <string>()
                {
                    PermissionNames.Pages_Roles
                }
            };
            var role1Dto = await _roleAppService.CreateAsync(createRoleDto1);

            SetOrgUnitRoleDto setOrgUnitRole1Dto = new SetOrgUnitRoleDto()
            {
                OrgUnitId = orgUnitDto.Id,
                RoleId    = role1Dto.Id
            };
            await _orgUnitAppService.AddRoleToOrgUnitAsync(setOrgUnitRole1Dto);

            var createRoleDto2 = new CreateRoleDto()
            {
                Name               = "RoleTest2",
                DisplayName        = "Test role2",
                Description        = "Role2 for test",
                GrantedPermissions = new List <string>()
                {
                    PermissionNames.Pages_Users
                }
            };
            var role2Dto = await _roleAppService.CreateAsync(createRoleDto2);

            SetOrgUnitRoleDto setOrgUnitRole2Dto = new SetOrgUnitRoleDto()
            {
                OrgUnitId = orgUnitDto.Id,
                RoleId    = role2Dto.Id
            };
            await _orgUnitAppService.AddRoleToOrgUnitAsync(setOrgUnitRole2Dto);

            var getOrgUnitDto = await _orgUnitAppService.GetAsync(new EntityDto <long>(orgUnitDto.Id));

            getOrgUnitDto.AssignedRoles.Count.ShouldBe(2);
            getOrgUnitDto.GrantedPermissions.Count.ShouldBe(2);

            var roles = await _orgUnitAppService.GetRolesInOrgUnitAsync(new EntityDto <long>(orgUnitDto.Id));

            roles.Count.ShouldBe(2);
            roles[0].DisplayName.ShouldBe("Test role1");
            roles[0].GrantedPermissions.Count.ShouldBe(1);
            roles[1].DisplayName.ShouldBe("Test role2");
            roles[1].GrantedPermissions.Count.ShouldBe(1);
        }
        public async Task DeleteOrgUnitInTenant_Test()
        {
            CreateTenantDto createTenantDto = new CreateTenantDto()
            {
                TenancyName      = "TestTenant",
                Name             = "TestTenant",
                AdminPhoneNumber = "13851400000",
                IsActive         = true
            };
            var tenantDto = await _tenantAppService.CreateAsync(createTenantDto);

            CreateOrgUnitDto createOrgUnitDto = new CreateOrgUnitDto()
            {
                ParentId    = null,
                DisplayName = "Ou1"
            };
            var orgUnitDto = await _omOrgUnitAppService.CreateOrgUnitInTenantAsync(tenantDto.Id, createOrgUnitDto);

            await _omOrgUnitAppService.DeleteOrgUnitInTenantAsync(tenantDto.Id, new EntityDto <long>(orgUnitDto.Id));

            await UsingDbContextAsync(async context =>
            {
                var testOu = await context.OrganizationUnits.FirstOrDefaultAsync(ou => ou.Id == orgUnitDto.Id);
                testOu.TenantId.ShouldBe(tenantDto.Id);
                testOu.IsDeleted.ShouldBeTrue();
            });
        }
Ejemplo n.º 7
0
        public async Task AddUserToOrgUnit_Test()
        {
            CreateOrgUnitDto createOrgUnitDto = new CreateOrgUnitDto()
            {
                ParentId    = null,
                DisplayName = "Ou Test"
            };
            var orgUnitDto = await _orgUnitAppService.CreateAsync(createOrgUnitDto);

            CreateUserDto createUserDto = new CreateUserDto()
            {
                UserName    = "******",
                Password    = User.DefaultPassword,
                Name        = "John",
                Surname     = "Smith",
                PhoneNumber = "13851400000",
                IsActive    = true
            };
            var userDto = await _userAppService.CreateAsync(createUserDto);

            var getUser1Dto = await _userAppService.GetAsync(new EntityDto <long>(userDto.Id));

            getUser1Dto.OrgUnitNames.Length.ShouldBe(0);

            SetOrgUnitUserDto setOrgUnitUserDto = new SetOrgUnitUserDto()
            {
                UserId    = userDto.Id,
                OrgUnitId = orgUnitDto.Id
            };
            await _orgUnitAppService.AddUserToOrgUnitAsync(setOrgUnitUserDto);

            var getUser2Dto = await _userAppService.GetAsync(new EntityDto <long>(userDto.Id));

            getUser2Dto.OrgUnitNames.Length.ShouldBe(1);
        }
        public async Task GetAllOrgUnitInTenant_Test()
        {
            CreateTenantDto createTenantDto = new CreateTenantDto()
            {
                TenancyName      = "TestTenant",
                Name             = "TestTenant",
                AdminPhoneNumber = "13851400000",
                IsActive         = true
            };
            var tenantDto = await _tenantAppService.CreateAsync(createTenantDto);

            CreateOrgUnitDto createOrgUnit1Dto = new CreateOrgUnitDto()
            {
                ParentId    = null,
                DisplayName = "Ou1"
            };
            var orgUnit1Dto = await _omOrgUnitAppService.CreateOrgUnitInTenantAsync(tenantDto.Id, createOrgUnit1Dto);

            CreateOrgUnitDto createOrgUnit2Dto = new CreateOrgUnitDto()
            {
                ParentId    = null,
                DisplayName = "Ou2"
            };
            var orgUnit2Dto = await _omOrgUnitAppService.CreateOrgUnitInTenantAsync(tenantDto.Id, createOrgUnit2Dto);

            GetOrgUnitsInput getOrgUnitsInput = new GetOrgUnitsInput()
            {
                Role = string.Empty
            };
            var orgUnits = await _omOrgUnitAppService.GetAllOrgUnitInTenantAsync(tenantDto.Id, getOrgUnitsInput);

            orgUnits.Items.Count.ShouldBe(3);  // 2 new created + 1 admin
        }
Ejemplo n.º 9
0
        public async Task GetAllOrgUnit_Test()
        {
            int orgUnitCount = 50;

            for (int i = 0; i < orgUnitCount; i++)
            {
                CreateOrgUnitDto createOrgUnitDto = new CreateOrgUnitDto()
                {
                    ParentId    = null,
                    DisplayName = $"Ou Test{i,2}"
                };
                var orgUnitDto = await _orgUnitAppService.CreateAsync(createOrgUnitDto);
            }

            PagedResultRequestDto pagedRoleDto = new PagedResultRequestDto()
            {
                SkipCount      = 17,
                MaxResultCount = 8
            };

            var orgUnits = await _orgUnitAppService.GetAllAsync(pagedRoleDto);

            orgUnits.Items.Count.ShouldBe(8);
            orgUnits.Items[0].DisplayName.ShouldBe("Ou Test17");
        }
        public async Task RemoveRoleFromOrgUnitInTenant_Test()
        {
            CreateTenantDto createTenantDto = new CreateTenantDto()
            {
                TenancyName      = "TestTenant",
                Name             = "TestTenant",
                AdminPhoneNumber = "13851400000",
                IsActive         = true
            };
            var tenantDto = await _tenantAppService.CreateAsync(createTenantDto);

            var createRoleDto = new CreateRoleDto()
            {
                Name               = "RoleTest",
                DisplayName        = "Test role",
                Description        = "Role for test",
                GrantedPermissions = new List <string>()
                {
                    PermissionNames.Pages_Roles
                }
            };
            var roleDto = await _omRoleAppService.CreateRoleInTenantAsync(tenantDto.Id, createRoleDto);

            CreateOrgUnitDto createOrgUnitDto = new CreateOrgUnitDto()
            {
                ParentId    = null,
                DisplayName = "Ou Test"
            };
            var orgUnitDto = await _omOrgUnitAppService.CreateOrgUnitInTenantAsync(tenantDto.Id, createOrgUnitDto);

            SetOrgUnitRoleDto setOrgUnitRoleDto = new SetOrgUnitRoleDto()
            {
                OrgUnitId = orgUnitDto.Id,
                RoleId    = roleDto.Id
            };
            await _omOrgUnitAppService.AddRoleToOrgUnitInTenantAsync(tenantDto.Id, setOrgUnitRoleDto);

            await UsingDbContextAsync(async context =>
            {
                var testOus = await context.OrganizationUnitRoles
                              .Where(our => our.OrganizationUnitId == orgUnitDto.Id).ToListAsync();
                testOus.Count.ShouldBe(1);
                testOus[0].RoleId.ShouldBe(roleDto.Id);
            });

            await _omOrgUnitAppService.RemoveRoleFromOrgUnitInTenantAsync(tenantDto.Id, setOrgUnitRoleDto);

            await UsingDbContextAsync(async context =>
            {
                var testOus = await context.OrganizationUnitRoles
                              .Where(our => our.OrganizationUnitId == orgUnitDto.Id).ToListAsync();
                testOus.Count.ShouldBe(1);
                testOus[0].RoleId.ShouldBe(roleDto.Id);
                testOus[0].IsDeleted.ShouldBeTrue();
            });
        }
        public async Task UpdateOrgUnitInTenant_Test()
        {
            CreateTenantDto createTenantDto = new CreateTenantDto()
            {
                TenancyName      = "TestTenant",
                Name             = "TestTenant",
                AdminPhoneNumber = "13851400000",
                IsActive         = true
            };
            var tenantDto = await _tenantAppService.CreateAsync(createTenantDto);

            // Prepare roles
            var createRoleDto1 = new CreateRoleDto()
            {
                Name               = "Role1",
                DisplayName        = "Test role1",
                Description        = "Role1 for test",
                GrantedPermissions = new List <string>()
                {
                    PermissionNames.Pages_Roles
                }
            };
            var role1Dto = await _omRoleAppService.CreateRoleInTenantAsync(tenantDto.Id, createRoleDto1);

            var createRoleDto2 = new CreateRoleDto()
            {
                Name               = "Role2",
                DisplayName        = "Test role2",
                Description        = "Role2 for test",
                GrantedPermissions = new List <string>()
                {
                    PermissionNames.Pages_Users
                }
            };
            var role2Dto = await _omRoleAppService.CreateRoleInTenantAsync(tenantDto.Id, createRoleDto2);

            // Prepare organization units
            CreateOrgUnitDto createOrgUnit1Dto = new CreateOrgUnitDto()
            {
                ParentId    = null,
                DisplayName = "Ou1"
            };
            var orgUnitDto = await _omOrgUnitAppService.CreateOrgUnitInTenantAsync(tenantDto.Id, createOrgUnit1Dto);

            await _omOrgUnitAppService.AddRoleToOrgUnitInTenantAsync(tenantDto.Id, new SetOrgUnitRoleDto()
            {
                OrgUnitId = orgUnitDto.Id,
                RoleId    = role1Dto.Id
            });

            // Update
            orgUnitDto.DisplayName = "**Ou1**";
            var updatedOrgUnitDto = await _omOrgUnitAppService.UpdateOrgUnitInTenantAsync(tenantDto.Id, orgUnitDto);

            updatedOrgUnitDto.DisplayName.ShouldBe("**Ou1**");
        }
Ejemplo n.º 12
0
        /// <summary>
        /// 创建当前租户下的组织
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task <OrgUnitDto> CreateAsync(CreateOrgUnitDto input)
        {
            var orgUnit = ObjectMapper.Map <OrganizationUnit>(input);

            orgUnit.TenantId = GetCurrentTenantId();

            await _orgUnitManager.CreateAsync(orgUnit);

            await CurrentUnitOfWork.SaveChangesAsync();

            return(await GenerateOrgUnitDtoAsync(orgUnit));
        }
        public async Task GetUsersInOrgUnitInTenant_Test()
        {
            CreateTenantDto createTenantDto = new CreateTenantDto()
            {
                TenancyName      = "TestTenant",
                Name             = "TestTenant",
                AdminPhoneNumber = "13851400000",
                IsActive         = true
            };
            var tenantDto = await _tenantAppService.CreateAsync(createTenantDto);

            CreateOrgUnitDto createOrgUnitDto = new CreateOrgUnitDto()
            {
                ParentId    = null,
                DisplayName = "Ou Test"
            };
            var orgUnitDto = await _omOrgUnitAppService.CreateOrgUnitInTenantAsync(tenantDto.Id, createOrgUnitDto);

            CreateUserDto createUser1Dto = new CreateUserDto()
            {
                UserName    = "******",
                Password    = User.DefaultPassword,
                Name        = "John",
                Surname     = "Smith",
                PhoneNumber = "13851400001",
                IsActive    = true
            };
            var user1Dto = await _omUserAppService.CreateUserInTenantAsync(tenantDto.Id, createUser1Dto);

            SetOrgUnitUserDto setOrgUnitUserDto = new SetOrgUnitUserDto()
            {
                UserId    = user1Dto.Id,
                OrgUnitId = orgUnitDto.Id
            };
            await _omOrgUnitAppService.AddUserToOrgUnitInTenantAsync(tenantDto.Id, setOrgUnitUserDto);

            CreateUserDto createUser2Dto = new CreateUserDto()
            {
                UserName     = "******",
                Password     = User.DefaultPassword,
                Name         = "Mike",
                Surname      = "Smith",
                PhoneNumber  = "13851400002",
                IsActive     = true,
                OrgUnitNames = new[] { "Ou Test" }
            };
            var user2Dto = await _omUserAppService.CreateUserInTenantAsync(tenantDto.Id, createUser2Dto);

            var orgUnitUsers = await _omOrgUnitAppService.GetUsersInOrgUnitInTenantAsync(tenantDto.Id, new EntityDto <long>(orgUnitDto.Id));

            orgUnitUsers.Count.ShouldBe(2);
        }
        public async Task RemoveUserFromOrgUnitInTenant_Test()
        {
            CreateTenantDto createTenantDto = new CreateTenantDto()
            {
                TenancyName      = "TestTenant",
                Name             = "TestTenant",
                AdminPhoneNumber = "13851400000",
                IsActive         = true
            };
            var tenantDto = await _tenantAppService.CreateAsync(createTenantDto);

            CreateOrgUnitDto createOrgUnitDto = new CreateOrgUnitDto()
            {
                ParentId    = null,
                DisplayName = "Ou Test"
            };
            var orgUnitDto = await _omOrgUnitAppService.CreateOrgUnitInTenantAsync(tenantDto.Id, createOrgUnitDto);

            CreateUserDto createUserDto = new CreateUserDto()
            {
                UserName    = "******",
                Password    = User.DefaultPassword,
                Name        = "John",
                Surname     = "Smith",
                PhoneNumber = "13851400001",
                IsActive    = true
            };
            var userDto = await _omUserAppService.CreateUserInTenantAsync(tenantDto.Id, createUserDto);

            var getUser1Dto = await _omUserAppService.GetUserInTenantAsync(tenantDto.Id, new EntityDto <long>(userDto.Id));

            getUser1Dto.OrgUnitNames.Length.ShouldBe(0);

            SetOrgUnitUserDto setOrgUnitUserDto = new SetOrgUnitUserDto()
            {
                UserId    = userDto.Id,
                OrgUnitId = orgUnitDto.Id
            };
            await _omOrgUnitAppService.AddUserToOrgUnitInTenantAsync(tenantDto.Id, setOrgUnitUserDto);

            var getUser2Dto = await _omUserAppService.GetUserInTenantAsync(tenantDto.Id, new EntityDto <long>(userDto.Id));

            getUser2Dto.OrgUnitNames.Length.ShouldBe(1);

            // Remove from organization unit
            await _omOrgUnitAppService.RemoveUserFromOrgUnitInTenantAsync(tenantDto.Id, setOrgUnitUserDto);

            var getUser3Dto = await _omUserAppService.GetUserInTenantAsync(tenantDto.Id, new EntityDto <long>(userDto.Id));

            getUser3Dto.OrgUnitNames.Length.ShouldBe(0);
        }
Ejemplo n.º 15
0
        /// <summary>
        /// 运维专用:创建特定租户下的组织
        /// </summary>
        /// <param name="tenantId"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task <OrgUnitDto> CreateOrgUnitInTenantAsync(int tenantId, CreateOrgUnitDto input)
        {
            using (CurrentUnitOfWork.SetTenantId(tenantId))
            {
                var orgUnit = ObjectMapper.Map <OrganizationUnit>(input);
                orgUnit.TenantId = tenantId;

                await _orgUnitManager.CreateAsync(orgUnit);

                //await CurrentUnitOfWork.SaveChangesAsync();

                return(await GenerateOrgUnitDtoAsync(orgUnit));
            }
        }
Ejemplo n.º 16
0
        public async Task CreateOrgUnit_Hierarchy_Test()
        {
            CreateOrgUnitDto createParentOrgUnitDto = new CreateOrgUnitDto()
            {
                ParentId    = null,
                DisplayName = "Ou Parent"
            };
            var parentOrgUnitDto = await _orgUnitAppService.CreateAsync(createParentOrgUnitDto);

            await UsingDbContextAsync(async context =>
            {
                var testOu = await context.OrganizationUnits.FirstOrDefaultAsync(
                    ou => ou.Id == parentOrgUnitDto.Id);
                testOu.TenantId = 1;
                testOu.DisplayName.ShouldBe("Ou Parent");
                testOu.Code.ShouldBe("00001");
            });

            CreateOrgUnitDto createChild1OrgUnitDto = new CreateOrgUnitDto()
            {
                ParentId    = parentOrgUnitDto.Id,
                DisplayName = "Ou Child1"
            };
            var child1OrgUnitDto = await _orgUnitAppService.CreateAsync(createChild1OrgUnitDto);

            await UsingDbContextAsync(async context =>
            {
                var testOu = await context.OrganizationUnits.FirstOrDefaultAsync(
                    ou => ou.Id == child1OrgUnitDto.Id);
                testOu.TenantId = 1;
                testOu.DisplayName.ShouldBe("Ou Child1");
                testOu.Code.ShouldBe("00001.00001");
            });

            CreateOrgUnitDto createChild2OrgUnitDto = new CreateOrgUnitDto()
            {
                ParentId    = parentOrgUnitDto.Id,
                DisplayName = "Ou Child2"
            };
            var child2OrgUnitDto = await _orgUnitAppService.CreateAsync(createChild2OrgUnitDto);

            await UsingDbContextAsync(async context =>
            {
                var testOu = await context.OrganizationUnits.FirstOrDefaultAsync(
                    ou => ou.Id == child2OrgUnitDto.Id);
                testOu.TenantId = 1;
                testOu.DisplayName.ShouldBe("Ou Child2");
                testOu.Code.ShouldBe("00001.00002");
            });
        }
Ejemplo n.º 17
0
        public async Task AddRoleToOrgUnit_Test()
        {
            var createRoleDto = new CreateRoleDto()
            {
                Name               = "RoleTest",
                DisplayName        = "Test role",
                Description        = "Role for test",
                GrantedPermissions = new List <string>()
                {
                    PermissionNames.Pages_Roles
                }
            };
            var roleDto = await _roleAppService.CreateAsync(createRoleDto);

            CreateOrgUnitDto createOrgUnitDto = new CreateOrgUnitDto()
            {
                ParentId    = null,
                DisplayName = "Ou Test"
            };
            var orgUnitDto = await _orgUnitAppService.CreateAsync(createOrgUnitDto);

            SetOrgUnitRoleDto setOrgUnitRoleDto = new SetOrgUnitRoleDto()
            {
                OrgUnitId = orgUnitDto.Id,
                RoleId    = roleDto.Id
            };
            await _orgUnitAppService.AddRoleToOrgUnitAsync(setOrgUnitRoleDto);

            await UsingDbContextAsync(async context =>
            {
                var testOus = await context.OrganizationUnitRoles
                              .Where(our => our.OrganizationUnitId == orgUnitDto.Id).ToListAsync();
                testOus.Count.ShouldBe(1);
                testOus[0].RoleId.ShouldBe(roleDto.Id);
            });
        }
        public async Task GetOrgUnitInTenant_Test()
        {
            CreateTenantDto createTenantDto = new CreateTenantDto()
            {
                TenancyName      = "TestTenant",
                Name             = "TestTenant",
                AdminPhoneNumber = "13851400000",
                IsActive         = true
            };
            var tenantDto = await _tenantAppService.CreateAsync(createTenantDto);

            CreateOrgUnitDto createOrgUnitDto = new CreateOrgUnitDto()
            {
                ParentId    = null,
                DisplayName = "Ou Test"
            };
            var orgUnitDto = await _omOrgUnitAppService.CreateOrgUnitInTenantAsync(tenantDto.Id, createOrgUnitDto);

            var getOrgUnitDto =
                await _omOrgUnitAppService.GetOrgUnitInTenantAsync(tenantDto.Id, new EntityDto <long>(orgUnitDto.Id));

            getOrgUnitDto.DisplayName.ShouldBe("Ou Test");
            getOrgUnitDto.Code.ShouldBe("00002");  // AdminGroup is 00001
        }
        public async Task CreateOrgUnitInTenant_WithRole_Test()
        {
            CreateTenantDto createTenantDto = new CreateTenantDto()
            {
                TenancyName      = "TestTenant",
                Name             = "TestTenant",
                AdminPhoneNumber = "13851400000",
                IsActive         = true
            };
            var tenantDto = await _tenantAppService.CreateAsync(createTenantDto);

            var createRoleDto1 = new CreateRoleDto()
            {
                Name               = "Role1",
                DisplayName        = "Test role1",
                Description        = "Role1 for test",
                GrantedPermissions = new List <string>()
                {
                    PermissionNames.Pages_Roles
                }
            };
            var role1Dto = await _omRoleAppService.CreateRoleInTenantAsync(tenantDto.Id, createRoleDto1);

            var createRoleDto2 = new CreateRoleDto()
            {
                Name               = "Role2",
                DisplayName        = "Test role2",
                Description        = "Role2 for test",
                GrantedPermissions = new List <string>()
                {
                    PermissionNames.Pages_Users
                }
            };
            var role2Dto = await _omRoleAppService.CreateRoleInTenantAsync(tenantDto.Id, createRoleDto2);

            CreateOrgUnitDto createOrgUnitDto = new CreateOrgUnitDto()
            {
                ParentId    = null,
                DisplayName = "Ou Test"
            };
            var orgUnitDto = await _omOrgUnitAppService.CreateOrgUnitInTenantAsync(tenantDto.Id, createOrgUnitDto);

            await _omOrgUnitAppService.AddRoleToOrgUnitInTenantAsync(tenantDto.Id, new SetOrgUnitRoleDto()
            {
                OrgUnitId = orgUnitDto.Id,
                RoleId    = role1Dto.Id
            });

            await _omOrgUnitAppService.AddRoleToOrgUnitInTenantAsync(tenantDto.Id, new SetOrgUnitRoleDto()
            {
                OrgUnitId = orgUnitDto.Id,
                RoleId    = role2Dto.Id
            });

            var getOrgUnitDto = await _omOrgUnitAppService.GetOrgUnitInTenantAsync(tenantDto.Id, new EntityDto <long>(orgUnitDto.Id));

            getOrgUnitDto.AssignedRoles.Count.ShouldBe(2);
            getOrgUnitDto.GrantedPermissions.Count.ShouldBe(2);

            await UsingDbContextAsync(async context =>
            {
                var testOu      = await context.OrganizationUnits.FirstOrDefaultAsync(ou => ou.Id == orgUnitDto.Id);
                testOu.TenantId = tenantDto.Id;
                testOu.DisplayName.ShouldBe("Ou Test");
                testOu.Code.ShouldBe("00002");  // AdminGroup is 00001
            });
        }
        public async Task GetAllOrgUnitInTenant_FilterWithRole_Test()
        {
            CreateTenantDto createTenantDto = new CreateTenantDto()
            {
                TenancyName      = "TestTenant",
                Name             = "TestTenant",
                AdminPhoneNumber = "13851400000",
                IsActive         = true
            };
            var tenantDto = await _tenantAppService.CreateAsync(createTenantDto);

            // Prepare roles
            var createRoleDto1 = new CreateRoleDto()
            {
                Name               = "Role1",
                DisplayName        = "Test role1",
                Description        = "Role1 for test",
                GrantedPermissions = new List <string>()
                {
                    PermissionNames.Pages_Roles
                }
            };
            var role1Dto = await _omRoleAppService.CreateRoleInTenantAsync(tenantDto.Id, createRoleDto1);

            var createRoleDto2 = new CreateRoleDto()
            {
                Name               = "Role2",
                DisplayName        = "Test role2",
                Description        = "Role2 for test",
                GrantedPermissions = new List <string>()
                {
                    PermissionNames.Pages_Users
                }
            };
            var role2Dto = await _omRoleAppService.CreateRoleInTenantAsync(tenantDto.Id, createRoleDto2);

            // Prepare organization units
            CreateOrgUnitDto createOrgUnit1Dto = new CreateOrgUnitDto()
            {
                ParentId    = null,
                DisplayName = "Ou1"
            };
            var orgUnit1Dto = await _omOrgUnitAppService.CreateOrgUnitInTenantAsync(tenantDto.Id, createOrgUnit1Dto);

            await _omOrgUnitAppService.AddRoleToOrgUnitInTenantAsync(tenantDto.Id, new SetOrgUnitRoleDto()
            {
                OrgUnitId = orgUnit1Dto.Id,
                RoleId    = role1Dto.Id
            });

            CreateOrgUnitDto createOrgUnit2Dto = new CreateOrgUnitDto()
            {
                ParentId    = null,
                DisplayName = "Ou2"
            };
            var orgUnit2Dto = await _omOrgUnitAppService.CreateOrgUnitInTenantAsync(tenantDto.Id, createOrgUnit2Dto);

            await _omOrgUnitAppService.AddRoleToOrgUnitInTenantAsync(tenantDto.Id, new SetOrgUnitRoleDto()
            {
                OrgUnitId = orgUnit2Dto.Id,
                RoleId    = role2Dto.Id
            });

            // Query
            GetOrgUnitsInput getOrgUnitsInput = new GetOrgUnitsInput()
            {
                Role = "Role2"
            };
            var orgUnits = await _omOrgUnitAppService.GetAllOrgUnitInTenantAsync(tenantDto.Id, getOrgUnitsInput);

            orgUnits.Items.Count.ShouldBe(2);  // 1 new created + 1 admin
        }
        public async Task GetRolesInOrgUnitInTenant_Test()
        {
            CreateTenantDto createTenantDto = new CreateTenantDto()
            {
                TenancyName      = "TestTenant",
                Name             = "TestTenant",
                AdminPhoneNumber = "13851400000",
                IsActive         = true
            };
            var tenantDto = await _tenantAppService.CreateAsync(createTenantDto);

            CreateOrgUnitDto createOrgUnitDto = new CreateOrgUnitDto()
            {
                ParentId    = null,
                DisplayName = "Ou Test"
            };
            var orgUnitDto = await _omOrgUnitAppService.CreateOrgUnitInTenantAsync(tenantDto.Id, createOrgUnitDto);

            var createRoleDto1 = new CreateRoleDto()
            {
                Name               = "RoleTest1",
                DisplayName        = "Test role1",
                Description        = "Role1 for test",
                GrantedPermissions = new List <string>()
                {
                    PermissionNames.Pages_Roles
                }
            };
            var role1Dto = await _omRoleAppService.CreateRoleInTenantAsync(tenantDto.Id, createRoleDto1);

            SetOrgUnitRoleDto setOrgUnitRole1Dto = new SetOrgUnitRoleDto()
            {
                OrgUnitId = orgUnitDto.Id,
                RoleId    = role1Dto.Id
            };
            await _omOrgUnitAppService.AddRoleToOrgUnitInTenantAsync(tenantDto.Id, setOrgUnitRole1Dto);

            var createRoleDto2 = new CreateRoleDto()
            {
                Name               = "RoleTest2",
                DisplayName        = "Test role2",
                Description        = "Role2 for test",
                GrantedPermissions = new List <string>()
                {
                    PermissionNames.Pages_Users
                }
            };
            var role2Dto = await _omRoleAppService.CreateRoleInTenantAsync(tenantDto.Id, createRoleDto2);

            SetOrgUnitRoleDto setOrgUnitRole2Dto = new SetOrgUnitRoleDto()
            {
                OrgUnitId = orgUnitDto.Id,
                RoleId    = role2Dto.Id
            };
            await _omOrgUnitAppService.AddRoleToOrgUnitInTenantAsync(tenantDto.Id, setOrgUnitRole2Dto);

            var getOrgUnitDto = await _omOrgUnitAppService.GetOrgUnitInTenantAsync(tenantDto.Id, new EntityDto <long>(orgUnitDto.Id));

            getOrgUnitDto.AssignedRoles.Count.ShouldBe(2);
            getOrgUnitDto.GrantedPermissions.Count.ShouldBe(2);

            var roles = await _omOrgUnitAppService.GetRolesInOrgUnitInTenantAsync(tenantDto.Id, new EntityDto <long>(orgUnitDto.Id));

            roles.Count.ShouldBe(2);
            roles[0].DisplayName.ShouldBe("Test role1");
            roles[0].GrantedPermissions.Count.ShouldBe(1);
            roles[1].DisplayName.ShouldBe("Test role2");
            roles[1].GrantedPermissions.Count.ShouldBe(1);
        }
Ejemplo n.º 22
0
        private async Task CreateComplexRoleAndOrgUnit()
        {
            // Role 1
            var createRole1Dto = new CreateRoleDto()
            {
                Name               = "Role1",
                DisplayName        = "Role1",
                Description        = "Role1 for test",
                GrantedPermissions = new List <string>()
                {
                    PermissionNames.Pages_Roles
                }
            };
            var role1Dto = await _roleAppService.CreateAsync(createRole1Dto);

            // Role 2
            var createRole2Dto = new CreateRoleDto()
            {
                Name               = "Role2",
                DisplayName        = "Role2",
                Description        = "Role2 for test",
                GrantedPermissions = new List <string>()
                {
                    PermissionNames.Pages_Users, PermissionNames.Pages_Roles
                }
            };
            var role2Dto = await _roleAppService.CreateAsync(createRole2Dto);

            // Role 2
            var createRole3Dto = new CreateRoleDto()
            {
                Name               = "Role3",
                DisplayName        = "Role3",
                Description        = "Role3 for test",
                GrantedPermissions = new List <string>()
                {
                    PermissionNames.Pages_Roles
                }
            };
            var role3Dto = await _roleAppService.CreateAsync(createRole3Dto);

            // OrgUnit with Role1 and Role2
            CreateOrgUnitDto createOrgUnitDto = new CreateOrgUnitDto()
            {
                ParentId    = null,
                DisplayName = "Ou Test"
            };
            var orgUnitDto = await _orgUnitAppService.CreateAsync(createOrgUnitDto);

            SetOrgUnitRoleDto setOrgUnitRole1Dto = new SetOrgUnitRoleDto()
            {
                OrgUnitId = orgUnitDto.Id,
                RoleId    = role1Dto.Id
            };
            await _orgUnitAppService.AddRoleToOrgUnitAsync(setOrgUnitRole1Dto);

            SetOrgUnitRoleDto setOrgUnitRole2Dto = new SetOrgUnitRoleDto()
            {
                OrgUnitId = orgUnitDto.Id,
                RoleId    = role2Dto.Id
            };
            await _orgUnitAppService.AddRoleToOrgUnitAsync(setOrgUnitRole2Dto);
        }