Exemplo n.º 1
0
        /// <summary>
        /// Create or Update Accounts
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task CreateorUpdateAccountAccessList(UserSecuritySettingsInputUnit input)
        {
            var tenantId = Convert.ToInt32(_customAppSession.TenantId);

            foreach (var userId in input.UserIdList)
            {
                var user = await _userManager.GetUserByIdAsync(userId);

                var organizationUnits = await _organizationExtendedUnitManager.GetExtendedOrganizationUnitsAsync(user, EntityClassification.Account);

                if (!ReferenceEquals(input.AccountAccessList, null))
                {
                    input.AccountAccessList.ForEach(u => u.UserId = userId);

                    //assign Account restrictions to User
                    foreach (var account in input.AccountAccessList)
                    {
                        var organization = await _organizationUnitRepository.FirstOrDefaultAsync(u => u.Id == account.OrganizationUnitId);

                        if (organization != null)
                        {
                            if (!await _userOrganizationUnitRepository.GetAll().AnyAsync(u => u.OrganizationUnitId == organization.Id && u.UserId == account.UserId))
                            {
                                await _userOrganizationUnitRepository.InsertAsync(new UserOrganizationUnit(tenantId, userId, organization.Id));
                            }
                        }
                        else
                        {
                            var orgUnit = new OrganizationExtended(tenantId, account.AccountNumber, entityClassificationId: EntityClassification.Account);
                            orgUnit.Code = await _organizationUnitManager.GetNextChildCodeAsync(null);

                            var orgId = await _organizationUnitRepository.InsertAndGetIdAsync(orgUnit);

                            await _userOrganizationUnitRepository.InsertAsync(new UserOrganizationUnit(tenantId, userId, orgId));

                            var accountDetails = await _accountUnitRepository.FirstOrDefaultAsync(u => u.Id == account.AccountId);

                            accountDetails.OrganizationUnitId = orgId;
                            await _accountUnitRepository.UpdateAsync(accountDetails);

                            await CurrentUnitOfWork.SaveChangesAsync();
                        }
                    }

                    //delete Account restrictions from User
                    var deleteAccountList = organizationUnits.Where(p => !input.AccountAccessList.Any(p2 => p2.OrganizationUnitId == p.Id)).ToList();
                    foreach (var UserOrg in deleteAccountList)
                    {
                        await UserManager.RemoveFromOrganizationUnitAsync(userId, UserOrg.Id);
                    }
                }  //delete Account restrictions from User
                else
                {
                    foreach (var UserOrg in organizationUnits)
                    {
                        await UserManager.RemoveFromOrganizationUnitAsync(userId, UserOrg.Id);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public async Task <string> GetNextChildCodeAsync(long?parentId)
        {
            var Ou = await _entityManager.GetNextChildCodeAsync(parentId);

            //var lastChild = await GetLastChildOrNullAsync(parentId);
            //if (lastChild == null)
            //{
            //    var parentCode = parentId != null ? await GetCodeAsync(parentId.Value) : null;
            //    return OrganizationUnit.AppendCode(parentCode, OrganizationUnit.CreateCode(1));
            //}

            //return OrganizationUnit.CalculateNextCode(lastChild.Code);
            if (Ou != null)
            {
                return(Ou);
            }
            return(null);
        }
        public async Task <OrganizationUnitDto> CreateOuInTenantAsync(CreateOrganizationUnitDto input)
        {
            using (CurrentUnitOfWork.SetTenantId(input.TenantId))
            {
                if (string.IsNullOrEmpty(input.Code))
                {
                    input.Code = await _organizationUnitManager.GetNextChildCodeAsync(input.ParentId);
                }

                OrganizationUnit entity = ObjectMapper.Map <OrganizationUnit>(input);

                //await _organizationUnitManager.CreateAsync(entity);

                long id = await _orgUnitRepository.InsertAndGetIdAsync(entity);

                entity.Id = id;

                return(ObjectMapper.Map <OrganizationUnitDto>(entity));
            }
        }