/// <summary>
        /// Updates an Agent's name in it's corresponding user tables
        /// </summary>
        /// <param name="oldName"></param>
        /// <param name="newName"></param>
        public void UpdateAgentName(string oldName, string newName)
        {
            _personRepo.ForceIgnoreSecurity();
            Person person = _personRepo.Find(0, 1).Items?.Where(p => p.Name == oldName && p.IsAgent && p.IsDeleted == false)?.FirstOrDefault();

            if (person != null)
            {
                person.Name = newName;
                _personRepo.Update(person);

                _usersRepo.ForceIgnoreSecurity();
                var aspUser = _usersRepo.Find(0, 1).Items?.Where(u => u.PersonId == person.Id)?.FirstOrDefault();
                if (aspUser != null)
                {
                    var existingUser = _userManager.FindByIdAsync(aspUser.Id).Result;
                    existingUser.Name = newName;
                    var result = _userManager.UpdateAsync(existingUser).Result;
                }
                else
                {
                    throw new EntityDoesNotExistException("Could not find the corresponding asp user entity to update");
                }
                _usersRepo.ForceSecurity();
            }
            else
            {
                throw new EntityDoesNotExistException("Could not find the corresponding person entity to update");
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Put(string id, [FromBody] Agent request)
        {
            try
            {
                Guid entityId = new Guid(id);

                var existingAgent = repository.GetOne(entityId);
                if (existingAgent == null)
                {
                    return(NotFound());
                }

                var namedAgent = repository.Find(null, d => d.Name.ToLower(null) == request.Name.ToLower(null) && d.Id != entityId)?.Items?.FirstOrDefault();
                if (namedAgent != null && namedAgent.Id != entityId)
                {
                    ModelState.AddModelError("Agent", "Agent Name Already Exists");
                    return(BadRequest(ModelState));
                }

                if (existingAgent.Name != request.Name)
                {
                    personRepo.ForceIgnoreSecurity();
                    Person person = personRepo.Find(0, 1).Items?.Where(p => p.Name == existingAgent.Name && p.IsAgent && p.IsDeleted == false)?.FirstOrDefault();
                    if (person != null)
                    {
                        person.UpdatedBy = string.IsNullOrWhiteSpace(applicationUser?.Name) ? person.UpdatedBy : applicationUser?.Name;
                        person.Name      = request.Name;
                        personRepo.Update(person);

                        usersRepo.ForceIgnoreSecurity();
                        var aspUser = usersRepo.Find(0, 1).Items?.Where(u => u.PersonId == person.Id)?.FirstOrDefault();
                        if (aspUser != null)
                        {
                            var existingUser = await userManager.FindByIdAsync(aspUser.Id).ConfigureAwait(false);

                            existingUser.Name = request.Name;
                            var result = await userManager.UpdateAsync(existingUser).ConfigureAwait(true);
                        }
                        usersRepo.ForceSecurity();
                    }
                    personRepo.ForceSecurity();
                }

                existingAgent.Name               = request.Name;
                existingAgent.MachineName        = request.MachineName;
                existingAgent.MacAddresses       = request.MacAddresses;
                existingAgent.IPAddresses        = request.IPAddresses;
                existingAgent.IsEnabled          = request.IsEnabled;
                existingAgent.CredentialId       = request.CredentialId;
                existingAgent.IPOption           = request.IPOption;
                existingAgent.IsEnhancedSecurity = request.IsEnhancedSecurity;

                await webhookPublisher.PublishAsync("Agents.AgentUpdated", existingAgent.Id.ToString(), existingAgent.Name).ConfigureAwait(false);

                return(await base.PutEntity(id, existingAgent));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Agent", ex.Message);
                return(BadRequest(ModelState));
            }
        }