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 <ListResultDto <OrganizationUnitDto> > GetAuthorizedAsync()
        {
            List <OrganizationUnit> list;

            if (await AuthorizationService.IsGrantedAsync(OrganizationUnitPermissions.OrganizationUnits.SuperAuthorization))
            {
                list = await OrganizationUnitRepository.GetChildrenAsync(null, includeDetails : false);
            }
            else
            {
                list = await UserRepository.GetOrganizationUnitsAsync(CurrentUser.Id.Value, false);

                list = list.Where(ou => !list.Any(y => ou.Code.StartsWith(y.Code) && y.Id != ou.Id))
                       .ToList();
            }


            var dtoList = ObjectMapper.Map <List <OrganizationUnit>, List <OrganizationUnitDto> >(list);

            foreach (var dto in dtoList)
            {
                dto.ChildrenCount = (await OrganizationUnitRepository.GetChildrenAsync(dto.Id, false)).Count;
            }

            return(new ListResultDto <OrganizationUnitDto>(
                       dtoList.OrderBy(ou => ou.Code)
                       .ToList()
                       ));
        }
        //[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);
        }
Esempio n. 4
0
        public async Task <List <OrganizationUnit> > FindChildrenAsync(Guid?parentId, bool recursive = false)
        {
            if (!recursive)
            {
                return(await OrganizationUnitRepository.GetChildrenAsync(parentId, includeDetails : true));
            }

            if (!parentId.HasValue)
            {
                return(await OrganizationUnitRepository.GetListAsync(includeDetails : true));
            }

            var code = await GetCodeOrDefaultAsync(parentId.Value);

            return(await OrganizationUnitRepository.GetAllChildrenWithParentCodeAsync(code, parentId, includeDetails : true));
        }
        //[Authorize(OrganizationUnitPermissions.OrganizationUnitLookup.Default)]
        public virtual async Task <ListResultDto <OrganizationUnitDto> > GetChildrenAsync(Guid?parentId, bool recursive = false)
        {
            List <OrganizationUnit> organizationUnits;

            if (recursive)
            {
                if (!parentId.HasValue)
                {
                    organizationUnits = await GetAllListAsync();
                }
                else
                {
                    var code = await OrganizationUnitManager.GetCodeOrDefaultAsync(parentId.Value);

                    organizationUnits = await OrganizationUnitRepository.GetAllChildrenWithParentCodeAsync(code, parentId, includeDetails : false);
                }
            }
            else
            {
                organizationUnits = await OrganizationUnitRepository.GetChildrenAsync(parentId, includeDetails : false);
            }

            var dtoList = ObjectMapper.Map <List <OrganizationUnit>, List <OrganizationUnitDto> >(organizationUnits);

            if (recursive)
            {
                foreach (var organizationUnit in dtoList)
                {
                    organizationUnit.ChildrenCount = dtoList.Count(ou => ou.ParentId == organizationUnit.Id);
                }
            }
            else
            {
                foreach (var organizationUnit in dtoList)
                {
                    organizationUnit.ChildrenCount = (await OrganizationUnitRepository.GetChildrenAsync(organizationUnit.Id, false)).Count;
                }
            }

            return(new ListResultDto <OrganizationUnitDto>(
                       dtoList.OrderBy(ou => ou.Position)
                       .ThenBy(ou => ou.Code)
                       .ToList()
                       ));
        }
        public virtual async Task <OrganizationUnitDto> FindByCodeAsync(string code)
        {
            var allOrganizationUnits = await OrganizationUnitRepository.GetAllChildrenWithParentCodeAsync(code, Guid.NewGuid(), false);

            var organizationUnit = allOrganizationUnits.FirstOrDefault(ou => ou.Code == code);

            if (organizationUnit == null)
            {
                return(null);
            }
            else
            {
                var dto = ObjectMapper.Map <OrganizationUnit, OrganizationUnitDto>(organizationUnit);
                dto.ChildrenCount = (await OrganizationUnitRepository.GetChildrenAsync(dto.Id, false)).Count;

                return(dto);
            }
        }
        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));
        }
Esempio n. 8
0
        public virtual async Task <OrganizationUnit> GetLastChildOrNullAsync(Guid?parentId)
        {
            var children = await OrganizationUnitRepository.GetChildrenAsync(parentId);

            return(children.OrderBy(c => c.Code).LastOrDefault());
        }