public virtual async Task <ListResultDto <IdentityRoleDto> > GetMemberAssignableRolesAsync(Guid id, Guid userId)
        {
            var ou = await OrganizationUnitRepository.GetAsync(id, false);

            var availableRoles = await GetAvailableRolesAsync(ou.ParentId);

            var userRoles = ObjectMapper.Map <List <IdentityRole>, List <IdentityRoleDto> >(
                await UserRepository.GetRolesAsync(userId, false)
                );

            var assignableRoles = new List <IdentityRoleDto>();

            foreach (var ur in userRoles)
            {
                ur.IsStatic = true;
                assignableRoles.Add(ur);
            }
            foreach (var avr in availableRoles)
            {
                if (!assignableRoles.Any(asr => asr.Id == avr.Id))
                {
                    assignableRoles.Add(avr);
                }
            }

            return(new ListResultDto <IdentityRoleDto>(assignableRoles));
        }
示例#2
0
 public virtual async Task AddToOrganizationUnitAsync(Guid userId, Guid ouId)
 {
     await AddToOrganizationUnitAsync(
         await UserRepository.GetAsync(userId, cancellationToken: CancellationToken),
         await OrganizationUnitRepository.GetAsync(ouId, cancellationToken: CancellationToken)
         );
 }
        public virtual async Task MoveAsync(Guid id, OrganizationUnitMoveInput input)
        {
            var ou = await OrganizationUnitRepository.GetAsync(id, false);

            await AuthorizationService.CheckAsync(ou, CommonOperations.Update);

            if (input.ParentId != ou.ParentId)
            {
                if (input.ParentId.HasValue)
                {
                    var parentOrganizationUnit = await OrganizationUnitRepository.GetAsync(input.ParentId.Value, false);

                    await AuthorizationService.CheckAsync(parentOrganizationUnit, CommonOperations.Create);
                }
                else
                {
                    await AuthorizationService.CheckAsync(null, CommonOperations.Create);
                }
            }

            await OrganizationUnitManager.MoveAsync(ou.Id, input.ParentId);

            var children = await OrganizationUnitRepository.GetChildrenAsync(input.ParentId, false);

            await UpdatePositionAsync(ou, children, input.BeforeOrganizationUnitId);
        }
        public virtual async Task <OrganizationUnitDto> UpdateAsync(Guid id, OrganizationUnitUpdateDto input)
        {
            var ou = await OrganizationUnitRepository.GetAsync(id, true);

            await AuthorizationService.CheckAsync(ou, CommonOperations.Update);

            ou.DisplayName      = input.DisplayName;
            ou.ConcurrencyStamp = input.ConcurrencyStamp;
            ou.SetProperty(OrganizationUnitExtraPropertyNames.IsActiveName, input.IsActive);

            foreach (var roleId in ou.Roles.Select(our => our.RoleId).Except(input.RoleIds))
            {
                ou.RemoveRole(roleId);
            }
            foreach (var roleId in input.RoleIds.Except(ou.Roles.Select(our => our.RoleId)))
            {
                ou.AddRole(roleId);
            }

            await OrganizationUnitManager.UpdateAsync(ou);

            //remove cache
            await CacheOrganizationUnits.RemoveAsync(AllOrganizationUnitsListCacheName);

            return(ObjectMapper.Map <OrganizationUnit, OrganizationUnitDto>(ou));
        }
示例#5
0
 public virtual async Task RemoveRoleFromOrganizationUnitAsync(Guid roleId, Guid ouId)
 {
     await RemoveRoleFromOrganizationUnitAsync(
         await IdentityRoleRepository.GetAsync(roleId),
         await OrganizationUnitRepository.GetAsync(ouId, true)
         );
 }
示例#6
0
        public virtual async Task MoveAsync(Guid id, Guid?parentId)
        {
            var organizationUnit = await OrganizationUnitRepository.GetAsync(id);

            if (organizationUnit.ParentId == parentId)
            {
                return;
            }

            //Should find children before Code change
            var children = await FindChildrenAsync(id, true);

            //Store old code of OU
            var oldCode = organizationUnit.Code;

            //Move OU
            organizationUnit.Code = await GetNextChildCodeAsync(parentId);

            organizationUnit.ParentId = parentId;

            await ValidateOrganizationUnitAsync(organizationUnit);

            //Update Children Codes
            foreach (var child in children)
            {
                child.Code = OrganizationUnit.AppendCode(organizationUnit.Code, OrganizationUnit.GetRelativeCode(child.Code, oldCode));
            }
        }
示例#7
0
        public virtual async Task MoveAsync(long id, long?parentId)
        {
            using (var uow = UnitOfWorkManager.Begin())
            {
                var organizationUnit = await OrganizationUnitRepository.GetAsync(id);

                if (organizationUnit.ParentId == parentId)
                {
                    await uow.CompleteAsync();

                    return;
                }

                //Should find children before Code change
                var children = await FindChildrenAsync(id, true);

                //Store old code of OU
                var oldCode = organizationUnit.Code;

                //Move OU
                organizationUnit.Code = await GetNextChildCodeAsync(parentId);

                organizationUnit.ParentId = parentId;

                await ValidateOrganizationUnitAsync(organizationUnit);

                //Update Children Codes
                foreach (var child in children)
                {
                    child.Code = OrganizationUnit.AppendCode(organizationUnit.Code, OrganizationUnit.GetRelativeCode(child.Code, oldCode));
                }

                await uow.CompleteAsync();
            }
        }
        public virtual async Task DeleteAsync(Guid id)
        {
            var ou = await OrganizationUnitRepository.GetAsync(id, false);

            await AuthorizationService.CheckAsync(ou, CommonOperations.Delete);

            await OrganizationUnitManager.DeleteAsync(ou.Id);
        }
        public virtual async Task <ListResultDto <IdentityRoleDto> > GetRolesAsync(Guid id)
        {
            var ou = await OrganizationUnitRepository.GetAsync(id, false);

            return(new ListResultDto <IdentityRoleDto>(
                       ObjectMapper.Map <List <IdentityRole>, List <IdentityRoleDto> >(
                           await OrganizationUnitRepository.GetRolesAsync(ou)
                           )));
        }
        //[Authorize(OrganizationUnitPermissions.OrganizationUnitLookup.Default)]
        public virtual async Task <OrganizationUnitDto> GetAsync(Guid id)
        {
            var dto = ObjectMapper.Map <OrganizationUnit, OrganizationUnitDto>(
                await OrganizationUnitRepository.GetAsync(id, false)
                );

            dto.ChildrenCount = (await OrganizationUnitRepository.GetChildrenAsync(dto.Id, false)).Count;

            return(dto);
        }
示例#11
0
        public virtual async Task <ListResultDto <IdentityUserDto> > GetUsersAsync(OrganizationUnitGetUserDto input)
        {
            var origanizationUnit = await OrganizationUnitRepository.GetAsync(input.Id);

            // TODO: 官方库没有定义分页查询API,有可能是企业付费版本,需要自行实现
            var origanizationUnitUsers = await UserRepository.GetUsersInOrganizationUnitAsync(origanizationUnit.Id);

            return(new ListResultDto <IdentityUserDto>(
                       ObjectMapper.Map <List <IdentityUser>, List <IdentityUserDto> >(origanizationUnitUsers)));
        }
        public virtual async Task <GetOrganizationUnitForEditOutput> GetOrganizationUnitForEditAsync(Guid id)
        {
            var ou = await OrganizationUnitRepository.GetAsync(id, true);

            var output = new GetOrganizationUnitForEditOutput();

            output.OrganizationUnit = ObjectMapper.Map <OrganizationUnit, OrganizationUnitEditDto>(ou);
            output.RoleIds          = ou.Roles.Select(r => r.RoleId).ToArray();
            output.AvailableRoles   = await GetAvailableRolesAsync(ou.ParentId);

            return(output);
        }
示例#13
0
    public async Task Should_Lazy_Load_OrganizationUnit_Collections()
    {
        using (var uow = GetRequiredService <IUnitOfWorkManager>().Begin())
        {
            var ou = await OrganizationUnitRepository.GetAsync(LookupNormalizer.NormalizeName("OU111"), includeDetails : false);

            ou.Roles.ShouldNotBeNull(); //?
            ou.Roles.Any().ShouldBeTrue();

            await uow.CompleteAsync();
        }
    }
        public virtual async Task <PagedResultDto <IdentityUserDto> > GetMembersAsync(Guid id, GetOrganizationUnitMembersInput input)
        {
            var ou = await OrganizationUnitRepository.GetAsync(id, false);

            var count = await OrganizationUnitRepository.GetMembersCountAsync(ou, input.Filter);

            var members = await OrganizationUnitRepository.GetMembersAsync(ou, input.Sorting, input.MaxResultCount, input.SkipCount, input.Filter);

            return(new PagedResultDto <IdentityUserDto>(count,
                                                        ObjectMapper.Map <List <IdentityUser>, List <IdentityUserDto> >(members)
                                                        ));
        }
        public virtual async Task RemoveRoleAsync(OrganizationUnitDtoAddOrRemoveRoleDto input)
        {
            var origanizationUnit = await OrganizationUnitRepository.GetAsync(input.Id);

            if (origanizationUnit.IsInRole(input.RoleId))
            {
                origanizationUnit.RemoveRole(input.RoleId);
                await OrganizationUnitManager.UpdateAsync(origanizationUnit);

                await CurrentUnitOfWork.SaveChangesAsync();
            }
        }
示例#16
0
        public virtual async Task <PagedResultDto <IdentityRoleDto> > GetRolesAsync(OrganizationUnitGetRoleByPagedDto input)
        {
            var origanizationUnit = await OrganizationUnitRepository.GetAsync(input.Id);

            var origanizationUnitRoleCount = await OrganizationUnitRepository.GetRolesCountAsync(origanizationUnit);

            var origanizationUnitRoles = await OrganizationUnitRepository.GetRolesAsync(origanizationUnit,
                                                                                        input.Sorting, input.MaxResultCount, input.SkipCount, false);

            return(new PagedResultDto <IdentityRoleDto>(origanizationUnitRoleCount,
                                                        ObjectMapper.Map <List <IdentityRole>, List <IdentityRoleDto> >(origanizationUnitRoles)));
        }
        public virtual async Task RemoveUserAsync(OrganizationUnitDtoAddOrRemoveUserDto input)
        {
            var identityUser = await UserRepository.GetAsync(input.UserId);

            var origanizationUnit = await OrganizationUnitRepository.GetAsync(input.Id);

            if (identityUser.IsInOrganizationUnit(input.Id))
            {
                await UserManager.RemoveFromOrganizationUnitAsync(identityUser, origanizationUnit);

                await CurrentUnitOfWork.SaveChangesAsync();
            }
        }
        public virtual async Task AddMembersAsync(Guid id, OrganizationUnitAddMembersInput input)
        {
            var ou = await OrganizationUnitRepository.GetAsync(id, false);

            await AuthorizationService.CheckAsync(ou, CommonOperations.Update);

            foreach (var userId in input.UserIds)
            {
                var user = await UserManager.GetByIdAsync(userId);

                await UserManager.AddToOrganizationUnitAsync(user, ou);
            }
        }
示例#19
0
        public virtual async Task AddRolesAsync(Guid id, OrganizationUnitAddRoleDto input)
        {
            var origanizationUnit = await OrganizationUnitRepository.GetAsync(id);

            var roles = await RoleRepository.GetListByIdListAsync(input.RoleIds, includeDetails : true);

            foreach (var role in roles)
            {
                await OrganizationUnitManager.AddRoleToOrganizationUnitAsync(role, origanizationUnit);
            }

            await CurrentUnitOfWork.SaveChangesAsync();
        }
示例#20
0
        public virtual async Task <OrganizationUnitDto> UpdateAsync(Guid id, OrganizationUnitUpdateDto input)
        {
            var origanizationUnit = await OrganizationUnitRepository.GetAsync(id);

            origanizationUnit.DisplayName = input.DisplayName;
            input.MapExtraPropertiesTo(origanizationUnit);

            await OrganizationUnitManager.UpdateAsync(origanizationUnit);

            await CurrentUnitOfWork.SaveChangesAsync();

            return(ObjectMapper.Map <OrganizationUnit, OrganizationUnitDto>(origanizationUnit));
        }
示例#21
0
        public virtual async Task AddUsersAsync(Guid id, OrganizationUnitAddUserDto input)
        {
            var origanizationUnit = await OrganizationUnitRepository.GetAsync(id);

            var users = await UserRepository.GetListByIdListAsync(input.UserIds, includeDetails : true);

            // 调用内部方法设置用户组织机构
            foreach (var user in users)
            {
                await UserManager.AddToOrganizationUnitAsync(user, origanizationUnit);
            }

            await CurrentUnitOfWork.SaveChangesAsync();
        }
示例#22
0
        public virtual async Task <PagedResultDto <IdentityUserDto> > GetUnaddedUsersAsync(Guid id, OrganizationUnitGetUnaddedUserByPagedDto input)
        {
            var origanizationUnit = await OrganizationUnitRepository.GetAsync(id);

            var origanizationUnitUserCount = await OrganizationUnitRepository
                                             .GetUnaddedUsersCountAsync(origanizationUnit, input.Filter);

            var origanizationUnitUsers = await OrganizationUnitRepository
                                         .GetUnaddedUsersAsync(origanizationUnit,
                                                               input.Sorting, input.MaxResultCount,
                                                               input.SkipCount, input.Filter);

            return(new PagedResultDto <IdentityUserDto>(origanizationUnitUserCount,
                                                        ObjectMapper.Map <List <IdentityUser>, List <IdentityUserDto> >(origanizationUnitUsers)));
        }
        public virtual async Task UpdateMemberRolesAsync(Guid id, Guid userId, OrganizationUnitUpdateMemberRolesInput input)
        {
            var ou = await OrganizationUnitRepository.GetAsync(id, false);

            await AuthorizationService.CheckAsync(ou, CommonOperations.Update);

            var availableRoles = (await GetAvailableRolesAsync(ou.ParentId)).Select(r => r.Name).ToArray();

            input.RoleNames = input.RoleNames.Intersect(availableRoles).ToArray();
            var user = await UserManager.GetByIdAsync(userId);

            var userRoleNames = (await RoleRepository.GetListAsync())
                                .Where(r => user.Roles.Select(ur => ur.RoleId).Contains(r.Id))
                                .Select(r => r.Name).ToArray();

            await UserManager.SetRolesAsync(user, userRoleNames.Except(availableRoles).Union(input.RoleNames));
        }
        public virtual async Task <OrganizationUnitDto> CreateAsync(OrganizationUnitCreateDto input)
        {
            var parentOrganizationUnit = input.ParentId.HasValue ?
                                         await OrganizationUnitRepository.GetAsync(input.ParentId.Value, false)
                : null;

            if (parentOrganizationUnit != null)
            {
                await AuthorizationService.CheckAsync(parentOrganizationUnit, CommonOperations.Create);
            }
            else
            {
                await AuthorizationService.CheckAsync(OrganizationUnitPermissions.OrganizationUnits.SuperAuthorization);
            }

            var children = await OrganizationUnitRepository.GetChildrenAsync(input.ParentId, false);

            await RepairPosition(children);//

            var ou = new OrganizationUnit(
                GuidGenerator.Create(),
                input.DisplayName,
                input.ParentId,
                CurrentTenant.Id);

            ou.SetProperty(OrganizationUnitExtraPropertyNames.IsActiveName, input.IsActive);
            ou.SetProperty(OrganizationUnitExtraPropertyNames.PositionName,
                           children.Select(c => c.GetProperty <int>(OrganizationUnitExtraPropertyNames.PositionName))
                           .DefaultIfEmpty(0).Max() + 1);

            foreach (var roleId in input.RoleIds)
            {
                ou.AddRole(roleId);
            }

            await OrganizationUnitManager.CreateAsync(ou);

            //remove cache
            await CacheOrganizationUnits.RemoveAsync(AllOrganizationUnitsListCacheName);

            return(ObjectMapper.Map <OrganizationUnit, OrganizationUnitDto>(ou));
        }
示例#25
0
        public virtual async Task DeleteAsync(Guid id)
        {
            var children = await FindChildrenAsync(id, true);

            foreach (var child in children)
            {
                await OrganizationUnitRepository.RemoveAllMembersAsync(child);

                await OrganizationUnitRepository.RemoveAllRolesAsync(child);

                await OrganizationUnitRepository.DeleteAsync(child);
            }

            var organizationUnit = await OrganizationUnitRepository.GetAsync(id);

            await OrganizationUnitRepository.RemoveAllMembersAsync(organizationUnit);

            await OrganizationUnitRepository.RemoveAllRolesAsync(organizationUnit);

            await OrganizationUnitRepository.DeleteAsync(id);
        }
        private async Task <List <IdentityRole> > GetOrganizationUnitRoles(Guid organizationUnitId)
        {
            var ou = await OrganizationUnitRepository.GetAsync(organizationUnitId, false);

            var ouRoles = await OrganizationUnitRepository.GetRolesAsync(ou);

            if (ouRoles.Any())
            {
                return(ouRoles);
            }
            else
            {
                if (ou.ParentId.HasValue)
                {
                    return(await GetOrganizationUnitRoles(ou.ParentId.Value));
                }
                else
                {
                    return(ouRoles);
                }
            }
        }
示例#27
0
 public virtual async Task <string> GetCodeAsync(long id)
 {
     return((await OrganizationUnitRepository.GetAsync(id)).Code);
 }
示例#28
0
        public virtual async Task <string> GetCodeOrDefaultAsync(Guid id)
        {
            var ou = await OrganizationUnitRepository.GetAsync(id);

            return(ou?.Code);
        }