Exemple #1
0
        public string Create(AGSUserEntity user)
        {
            // creat the Identity User with the provided password
            EFApplicationUser appUser = new EFApplicationUser();

            MapAGSUserEntityToEFApplicationUser(user, appUser);
            appUser.Id = CommonConstant.GenerateId();


            // set the default password for the newly created user
            _ = _userManager.CreateAsync(appUser).Result;

            _ = _userManager.AddClaimsAsync(appUser, new Claim[] {
                new Claim(JwtClaimTypes.Name, user.Username),
                new Claim(JwtClaimTypes.Email, user.Email)
            }).Result;

            // update the associated groups
            if (user.GroupIds != null)
            {
                foreach (var groupId in user.GroupIds)
                {
                    Console.WriteLine($"Adding group:{groupId}");
                    this.AddUserToGroup(appUser, groupId);
                }
            }

            return(appUser.Id);
        }
Exemple #2
0
        public void CreateUser_UsernameAdmin_ThrowException()
        {
            var repository = new Mock <IRepository>();

            var newUser = new AGSUserEntity()
            {
                Username = CommonConstant.AGSAdminName
            };
            var usersHelper = new UsersHelper(repository.Object, _configuration);

            Assert.Throws <ArgumentException>(() => usersHelper.CreateUser(newUser));
        }
Exemple #3
0
        public void CreateUser_IdNotEmpty_ThrowException()
        {
            var repository = new Mock <IRepository>();
            var newUser    = new AGSUserEntity()
            {
                Id = CommonConstant.GenerateId()
            };

            var usersHelper = new UsersHelper(repository.Object, _configuration);

            Assert.Throws <ArgumentException>(() => usersHelper.CreateUser(newUser));
        }
Exemple #4
0
 public void MapAGSUserEntityToEFApplicationUser(AGSUserEntity userEntity, EFApplicationUser efApplicationUser)
 {
     efApplicationUser.Id                 = userEntity.Id;
     efApplicationUser.UserName           = userEntity.Username;
     efApplicationUser.NormalizedUserName = userEntity.Username;
     efApplicationUser.Email              = userEntity.Email;
     efApplicationUser.NormalizedEmail    = userEntity.Email;
     efApplicationUser.SecurityStamp      = userEntity.Id;
     efApplicationUser.First_Name         = userEntity.First_Name;
     efApplicationUser.Last_Name          = userEntity.Last_Name;
     efApplicationUser.Title              = userEntity.Title;
 }
Exemple #5
0
        public void UpdateUser_EmptyOrNUllId_ThrowException(string id)
        {
            var repository = new Mock <IRepository>();

            var usersHelper = new UsersHelper(repository.Object, _configuration);
            var updateUser  = new AGSUserEntity()
            {
                Id = id
            };

            Assert.Throws <ArgumentException>(() => usersHelper.UpdateUser(updateUser));
        }
Exemple #6
0
        public int Update(AGSUserEntity user)
        {
            var selected = _userManager.FindByIdAsync(user.Id).Result;

            if (selected == null)
            {
                return(0);
            }

            MapAGSUserEntityToEFApplicationUser(user, selected);

            _ = _userManager.UpdateAsync(selected).Result;

            // remove all existing claims associated with user
            var existingClaims = _userManager.GetClaimsAsync(selected).Result;

            if (existingClaims != null)
            {
                _ = _userManager.RemoveClaimsAsync(selected, existingClaims).Result;
            }

            _ = _userManager.AddClaimsAsync(selected, new Claim[] {
                new Claim(JwtClaimTypes.Name, user.Username),
                new Claim(JwtClaimTypes.Email, user.Email)
            }).Result;


            // remove all the existing associated groups
            var existingGroupIds = this.GetGroupIdsByUser(selected.Id);

            if (existingGroupIds != null)
            {
                foreach (var existingGroupId in existingGroupIds)
                {
                    this.RemoveUserFromGroup(selected.Id, existingGroupId);
                }
            }


            // add the new associated groups
            if (user.GroupIds != null)
            {
                foreach (var groupId in user.GroupIds)
                {
                    this.AddUserToGroup(selected.Id, groupId);
                }
            }

            return(1);
        }
Exemple #7
0
        public void UpdateUser_InvalidId_ThrowException(string id)
        {
            var repository = new Mock <IRepository>();

            repository.Setup(_ => _.UsersRepository.Get(It.IsAny <string>())).Returns((AGSUserEntity)null);

            var usersHelper = new UsersHelper(repository.Object, _configuration);
            var updateUser  = new AGSUserEntity()
            {
                Id = id
            };

            Assert.Throws <AGSException>(() => usersHelper.UpdateUser(updateUser));
        }
Exemple #8
0
 public IActionResult Put([FromBody] AGSUserEntity user)
 {
     try
     {
         var result = _usersHelper.UpdateUser(user);
         return(AGSResponseFactory.GetAGSResponseJsonResult());
     }catch (AGSException ex)
     {
         return(AGSResponseFactory.GetAGSExceptionJsonResult(ex));
     }catch (Exception ex)
     {
         return(StatusCode(500));
     }
 }
Exemple #9
0
        public void CreateUser_DuplicateUsername_ThrowException()
        {
            var repository = new Mock <IRepository>();

            repository.Setup(_ => _.UsersRepository.GetByUsername(It.IsAny <string>())).Returns(new AGSUserEntity());

            var newUser = new AGSUserEntity()
            {
                Username = "******"
            };

            var usersHelper = new UsersHelper(repository.Object, _configuration);

            Assert.Throws <AGSException>(() => usersHelper.CreateUser(newUser));
        }
Exemple #10
0
        public void CreateUser_EmptyId_Success()
        {
            var repository = new Mock <IRepository>();

            repository.Setup(_ => _.UsersRepository.Create(It.IsAny <AGSUserEntity>())).Returns(CommonConstant.GenerateId());

            var newUser = new AGSUserEntity()
            {
                Username = "******"
            };

            var usersHelper = new UsersHelper(repository.Object, _configuration);
            var result      = usersHelper.CreateUser(newUser);

            Assert.NotNull(result);
        }
Exemple #11
0
        public int UpdateUser(AGSUserEntity model)
        {
            if (model == null)
            {
                throw new ArgumentNullException();
            }

            if (string.IsNullOrEmpty(model.Id))
            {
                throw new ArgumentException();
            }

            var selected = _repository.UsersRepository.Get(model.Id);

            if (selected == null)
            {
                throw new AGSException(AGSResponse.ResponseCodeEnum.ModelNotFound);
            }

            var selectedUserByName = _repository.UsersRepository.GetByUsername(model.Username);

            // Not allow to change the username to admin
            if (selected.Username != CommonConstant.AGSAdminName && model.Username == CommonConstant.AGSAdminName)
            {
                throw new ArgumentException();
            }

            // Not allow to change admin username
            if (selected.Username == CommonConstant.AGSAdminName && model.Username != CommonConstant.AGSAdminName)
            {
                throw new ArgumentException();
            }

            // not allow duplicate username
            if (selectedUserByName != null && selectedUserByName.Id != model.Id)
            {
                throw new AGSException(AGSResponse.ResponseCodeEnum.UsernameDuplicate);
            }



            var result = _repository.UsersRepository.Update(model);

            _repository.Save();
            return(result);
        }
Exemple #12
0
        public void UpdateUser_ChangeAdminUsername_ThrowException()
        {
            var adminUser = MockData.users.FirstOrDefault(x => x.Username == CommonConstant.AGSAdminName);

            var repository = new Mock <IRepository>();

            repository.Setup(_ => _.UsersRepository.Get(adminUser.Id)).Returns(adminUser);
            repository.Setup(_ => _.UsersRepository.GetByUsername(adminUser.Username)).Returns(adminUser);

            var usersHelper = new UsersHelper(repository.Object, _configuration);
            var updateUser  = new AGSUserEntity()
            {
                Id       = adminUser.Id,
                Username = adminUser.Username + "1"
            };

            Assert.Throws <ArgumentException>(() => usersHelper.UpdateUser(updateUser));
        }
Exemple #13
0
        public void UpdateUser_DuplicateUsername_ThrowException()
        {
            var updateUser = MockData.users.FirstOrDefault(x => x.Username != CommonConstant.AGSAdminName);

            var repository = new Mock <IRepository>();

            repository.Setup(_ => _.UsersRepository.Get(updateUser.Id)).Returns(updateUser);
            repository.Setup(_ => _.UsersRepository.GetByUsername(updateUser.Username)).Returns(updateUser);

            var usersHelper = new UsersHelper(repository.Object, _configuration);
            var updateUser2 = new AGSUserEntity()
            {
                Id       = MockData.users.FirstOrDefault(x => x.Id != updateUser.Id && x.Username != CommonConstant.AGSAdminName).Id,
                Username = updateUser.Username
            };

            Assert.Throws <AGSException>(() => usersHelper.UpdateUser(updateUser2));
        }
Exemple #14
0
        public void CreateUser_EmptyOrNullUsername_ThrowException(string username)
        {
            var repository = new Mock <IRepository>();

            foreach (var user in MockData.users)
            {
                repository.Setup(_ => _.UsersRepository.GetByUsername(user.Username)).Returns(user);
            }

            var newUser = new AGSUserEntity()
            {
                Username = username
            };

            var usersHelper = new UsersHelper(repository.Object, _configuration);

            Assert.Throws <ArgumentException>(() => usersHelper.CreateUser(newUser));
        }
Exemple #15
0
        public void UpdateUser_ChangeUsernameToAdmin_ThrowException(string id)
        {
            var repository = new Mock <IRepository>();

            foreach (var user in MockData.users)
            {
                repository.Setup(_ => _.UsersRepository.Get(user.Id)).Returns(user);
                repository.Setup(_ => _.UsersRepository.GetByUsername(user.Username)).Returns(user);
            }

            var usersHelper = new UsersHelper(repository.Object, _configuration);
            var updateUser  = new AGSUserEntity()
            {
                Id       = id,
                Username = CommonConstant.AGSAdminName
            };

            Assert.Throws <ArgumentException>(() => usersHelper.UpdateUser(updateUser));
        }
Exemple #16
0
        public AGSUserEntity GetAGSUserEntityFromEFApplicationUser(EFApplicationUser user)
        {
            var result = new AGSUserEntity()
            {
                Id         = user.Id,
                Email      = user.Email,
                Username   = user.UserName,
                First_Name = user.First_Name,
                Last_Name  = user.Last_Name,
                Title      = user.Title,
                GroupIds   = new List <string>()
            };

            // get the associated groups
            var groupIds = this.GetGroupIdsByUser(user.Id);

            if (groupIds != null)
            {
                result.GroupIds = groupIds;
            }

            return(result);
        }
Exemple #17
0
        public string CreateUser(AGSUserEntity model)
        {
            if (model == null)
            {
                throw new ArgumentNullException();
            }

            if (!string.IsNullOrEmpty(model.Id))
            {
                throw new ArgumentException();
            }

            if (string.IsNullOrEmpty(model.Username))
            {
                throw new ArgumentException();
            }

            // Not allow to create admin
            if (model.Username == CommonConstant.AGSAdminName)
            {
                throw new ArgumentException();
            }

            var selectedUserByName = _repository.UsersRepository.GetByUsername(model.Username);

            if (selectedUserByName != null)
            {
                throw new AGSException(AGSResponse.ResponseCodeEnum.UsernameDuplicate);
            }

            string result = _repository.UsersRepository.Create(model);

            // set the default password
            _repository.UsersRepository.ResetPassword(result, _configuration["default_user_password"]);
            _repository.Save();
            return(result);
        }