Activities of "jmalla.cp"

Answer

Where is this method?

Hi,

I get it, but if I log out and then log back in, the menu on the left dosen't show it. Why if I renewed the token?

Answer

Hi,

I would that the user with role A can't Login from Web, the system throw UnAuthorized, and if the same role LogIn from Web.public, they can go on

Answer

Hi,

Sorry but I think that I didn't explain myself very well

How can I only allow access from 'web.public' project, client_id="public", to users belonging to role 'A', but block these users trying to access from 'web' project, client_id="private" and throw the message Unauthorized.

Answer

Hi,

I can't acces to the CurrentPrincipal from my application services.

Can you give me some examples please?

Thanks

Answer

Hi,

My cuestion is, how can I deny access some user/role that tries to access through some client_id?

Thanks

Answer

Ok, thanks for your information

Answer

Hi,

Sorry maliming, Now it's going well, I think yesterday I was really obfuscated with this topic.

On the other hand, could you tell me if there is another option to create and maintain access tokens per user from the web interface?

Thanks

Hi,

Now Everythink works fine.

Thanks for all.

And my code is

Code

public class SyncActiveFarmsWorker : HangfireBackgroundWorkerBase
    {
        private readonly FarmImportManager _farmImportManager;
        private readonly ExtendedIdentityUserImportManager _extendedIdentityUserImportManager;
        private readonly BreedingImportManager _breedingImportManager;
        private readonly IRepository<Farm, Guid> _farmRepository;
        private readonly IdentityUserManager _identityUserManager;
        private readonly IObjectMapper _objectMapper;

        public SyncActiveFarmsWorker(
            FarmImportManager farmImportManager,
            BreedingImportManager breedingImportManager,
            IRepository<Farm, Guid> farmRepository,
            ExtendedIdentityUserImportManager extendedIdentityUserImportManager,
            IdentityUserManager identityUserManager,
            IObjectMapper objectMapper)
        {
            RecurringJobId = nameof(SyncActiveFarmsWorker);
            CronExpression = Cron.Daily(20);
            _farmImportManager = farmImportManager;
            _breedingImportManager = breedingImportManager;
            _farmRepository = farmRepository;
            _extendedIdentityUserImportManager = extendedIdentityUserImportManager;
            _identityUserManager = identityUserManager;
            _objectMapper = objectMapper;
        }

        public override async Task DoWorkAsync(CancellationToken cancellationToken = default)
        {
            Logger.LogInformation("Executed Sync Agents..!");
            // Pending to recive information from support of ABP
            await ImportAllAgentsAsync();
            Logger.LogInformation("Executed Sync farms..!");
            await _farmImportManager.ImportAllActiveFarmsAsync();
            Logger.LogInformation("Executed Sync last active breedings..!");
            await ImportLastActiveBreedingsAsync();
        }

        private async Task ImportLastActiveBreedingsAsync()
        {
            List<Farm> farms = await _farmRepository.GetListAsync();

            foreach (var farm in farms)
            {
                await _breedingImportManager.ImportLastActiveBreedingsByFarmIdAsync(farm.EkonClientCodeId);
            }
        }

        private async Task ImportAllAgentsAsync()
        {
            AgentsImportDto input = new AgentsImportDto();
            input.AgentType = AgentType.Veterinarian;
            await _extendedIdentityUserImportManager.CreateAllUsersActiveFromExternalSericeAsync(input);

            input = new AgentsImportDto();
            input.AgentType = AgentType.Visitor;

            await _extendedIdentityUserImportManager.CreateAllUsersActiveFromExternalSericeAsync(input);

            input = new AgentsImportDto();
            input.AgentType = AgentType.Haulier;

            await _extendedIdentityUserImportManager.CreateAllUsersActiveFromExternalSericeAsync(input);
        }

    }
public class ExtendedIdentityUserImportManager : DomainService
    {
        private readonly AgentExternalService _agentExternalService;
        private readonly IOptions<IdentityOptions> _identityOptions;
        private readonly IdentityUserManager _identityUserManager;
        private readonly IUnitOfWorkManager _unitOfWorkManager;
        private readonly IObjectMapper _objectMapper;
        private readonly IDistributedEventBus _distributedEventBus;

        public ExtendedIdentityUserImportManager(
            IOptions<IdentityOptions> identityOptions,
            IUnitOfWorkManager unitOfWorkManager,
            IObjectMapper objectMapper,
            IDistributedEventBus distributedEventBus,
            IdentityUserManager identityUserManager
            )
        {
            _agentExternalService = new AgentExternalService();
            _identityOptions = identityOptions;
            _unitOfWorkManager = unitOfWorkManager;
            _objectMapper = objectMapper;
            _distributedEventBus = distributedEventBus;
            _identityUserManager = identityUserManager;
        }

        public async Task<IdentityUserCreateDto> GetAgentToImport(AgentImportDto input)
        {
            AgentDto agent = new AgentDto();
            ExternalService.Contracts.Agents.AgentType agentType = (ExternalService.Contracts.Agents.AgentType)Enum.Parse(typeof(ExternalService.Contracts.Agents.AgentType), input.AgentType.ToString());

            if (!_agentExternalService.HaveService(agentType))
                throw new ServiceIsNotReady().WithData("Name", agentType.ToString());

            agent = await _agentExternalService.Services[agentType].GetAsync(input.AgentCode);

            IdentityUserCreateDto identityUserCreateDto = GetIdentityUserCreateDtoFromExternalAgent(agent, input.AgentType);

            return identityUserCreateDto;
        }

        private IdentityUserCreateDto GetIdentityUserCreateDtoFromExternalAgent(AgentDto agent, AgentType agentType)
        {
            IdentityUserCreateDto identityUserCreateDto = new IdentityUserCreateDto();
            identityUserCreateDto.Name = agent.Name;
            identityUserCreateDto.Surname = agent.Surname;
            identityUserCreateDto.SetAgentType(agentType);
            identityUserCreateDto.SetAgentErpId(agent.Code);
            identityUserCreateDto.UserName = agent.Username;
            identityUserCreateDto.Email = agent.Email;
            identityUserCreateDto.SetDefaultPassword();

            return identityUserCreateDto;
        }

        public async Task<List<IdentityUserCreateDto>> GetAllActiveAgents(AgentType agentTypeToImport)
        {
            ExternalService.Contracts.Agents.AgentType agentType = (ExternalService.Contracts.Agents.AgentType)Enum.Parse(typeof(ExternalService.Contracts.Agents.AgentType), agentTypeToImport.ToString());

            if (!_agentExternalService.HaveService(agentType))
                throw new ServiceIsNotReady().WithData("Name", agentType.ToString());

            List<AgentDto> agentsToImport = await _agentExternalService.Services[agentType].GetAllActiveAsync();

            if (agentsToImport == null || agentsToImport.Count == 0)
                return new List<IdentityUserCreateDto>();

            return agentsToImport.Select(x => GetIdentityUserCreateDtoFromExternalAgent(x, agentTypeToImport)).ToList();
        }

        public async Task<IdentityUserDto> UpdateAsync(Guid id, IdentityUserUpdateDto input)
        {
            await _identityOptions.SetAsync();

            var user = await _identityUserManager.GetByIdAsync(id);

            using (var uow = _unitOfWorkManager.Begin(
                requiresNew: true, isTransactional: false
            ))
            {
                user.SetConcurrencyStampIfNotNull(input.ConcurrencyStamp);

                (await _identityUserManager.SetUserNameAsync(user, input.UserName)).CheckErrors();
                
                await UpdateUserByInput(user, input);
                
                input.MapExtraPropertiesTo(user);
                
                (await _identityUserManager.UpdateAsync(user)).CheckErrors();

                await uow.CompleteAsync();
            }

            var userDto = _objectMapper.Map<IdentityUser, IdentityUserDto>(user);

            return userDto;
        }

        private async Task UpdateUserByInput(IdentityUser user, IdentityUserCreateOrUpdateDtoBase input)
        {
            if (!string.Equals(user.Email, input.Email, StringComparison.InvariantCultureIgnoreCase))
            {
                (await _identityUserManager.SetEmailAsync(user, input.Email)).CheckErrors();
            }

            if (!string.Equals(user.PhoneNumber, input.PhoneNumber, StringComparison.InvariantCultureIgnoreCase))
            {
                (await _identityUserManager.SetPhoneNumberAsync(user, input.PhoneNumber)).CheckErrors();
            }

            (await _identityUserManager.SetLockoutEnabledAsync(user, input.LockoutEnabled)).CheckErrors();

            user.Name = input.Name;
            user.Surname = input.Surname;
            (await _identityUserManager.UpdateAsync(user)).CheckErrors();
            user.SetIsActive(input.IsActive);
            
            (await _identityUserManager.SetRolesAsync(user, input.RoleNames)).CheckErrors();
        }

        public async Task<IdentityUserDto> CreateAsync(IdentityUserCreateDto input)
        {
            await _identityOptions.SetAsync();

            var user = new IdentityUser(
                GuidGenerator.Create(),
                input.UserName,
                input.Email,
                CurrentTenant.Id
            );

            input.MapExtraPropertiesTo(user);

            using (var uow = _unitOfWorkManager.Begin(
                requiresNew: true, isTransactional: false
            ))
            {
                (await _identityUserManager.CreateAsync(user, input.Password)).CheckErrors();
                
                await UpdateUserByInput(user, input);
                
                (await _identityUserManager.UpdateAsync(user)).CheckErrors();
                
                await uow.CompleteAsync();
            }

            await _distributedEventBus.PublishAsync(new IdentityUserCreatedEto()
            {
                Id = user.Id,
                Properties =
                {
                    { "SendConfirmationEmail", input.SendConfirmationEmail.ToString().ToUpper() },
                    { "AppName", "MVC" }
                }
            });

            var userDto = _objectMapper.Map<IdentityUser, IdentityUserDto>(user);

            return userDto;
        }

        public async Task CreateAllUsersActiveFromExternalSericeAsync(AgentsImportDto input)
        {
            List<IdentityUserCreateDto> identityUsersToImport = await GetAllActiveAgents(input.AgentType);

            foreach (var identityUserToImport in identityUsersToImport)
            {
                AssignRoleToUserFromAgentType(identityUserToImport);

                await CreateOrUpdateIdentityUser(identityUserToImport);
            }
        }

        private void AssignRoleToUserFromAgentType(IdentityUserCreateDto identityUserToImport)
        {
            if (identityUserToImport.RoleNames == null)
                identityUserToImport.RoleNames = new string[1];

            identityUserToImport.RoleNames[0] = identityUserToImport.GetAgentType().GetRoleName();
        }

        private async Task<IdentityUserDto> CreateOrUpdateIdentityUser(IdentityUserCreateDto input)
        {
            IdentityUser identityUser = await _identityUserManager.FindByNameAsync(input.UserName);

            Guid? userId = identityUser?.Id;

            if (identityUser != null && !identityUser.IsActive)
            {
                var userUpdateDto = _objectMapper.Map<IdentityUserCreateDto, IdentityUserUpdateDto>(input);

                IdentityUserDto userUpdated = await UpdateAsync(identityUser.Id, userUpdateDto);
                userId = userUpdated.Id;
            }
            else if (identityUser == null)
            {
                IdentityUserDto createdUser = await CreateAsync(input);
                userId = createdUser.Id;
            }

            return _objectMapper.Map<IdentityUser, IdentityUserDto>(await _identityUserManager.GetByIdAsync((Guid)userId));
        }
    }

Thanks for all

Showing 31 to 40 of 45 entries
Made with ❤️ on ABP v9.0.0-preview Updated on September 19, 2024, 10:13