/// <summary> /// Creates a user with the given profile data. /// </summary> /// <param name="customerName">The name of the customer for which to get the users.</param> /// <param name="userFromService">The data for the new account.</param> public void Create(string customerName, UserFromService userFromService) { var validationErrors = ValidateCreateRequest(userFromService); if (!string.IsNullOrEmpty(validationErrors)) { throw new RequestValidationException(validationErrors); } // Apply transformations var userFromRepository = Mapper.Map <UserFromService, UserFromRepository>(userFromService); // Create user this.UserRepository.Create(customerName, userFromRepository); }
/// <summary> /// Validates the supplied request body for creating a user. /// ----- /// NOTE: Extensive validation is usually handled by a business rule service or a validation extension of some kind. /// In the interest of keeping this project relatively lean, we forgoe the implementation of such a service. /// ----- /// </summary> /// <param name="request">The request body.</param> /// <returns>True, if the request body is valid. Otherwise false.</returns> private static string ValidateCreateRequest(UserFromService request) { var errors = string.Empty; if (string.IsNullOrEmpty(request.SamAccountName) | string.IsNullOrEmpty(request.DisplayName) | string.IsNullOrEmpty(request.SurName) | string.IsNullOrEmpty(request.Password)) { errors += "The request body must contain \"SamAccountName\", \"Password\", \"SurName\" and \"DisplayName\"! \n"; } // The maximum length of a SamAccountName can only ever be 20 characters. // Anything longer will result in a fatal - and unbelievably non-descript - ActiveDirectory Error! // See: https://msdn.microsoft.com/en-us/library/ms679635(v=vs.85).aspx if (request.SamAccountName == null || request.SamAccountName.Length > 20) { errors += $"SamAccountName {request.SamAccountName} is too long! It must be no longer than 20 characters! \n"; } return(errors); }
/// <summary> /// Updates a single users profile by his samAccountName. /// </summary> /// <param name="customerName">The name of the customer for which to get the users.</param> /// <param name="samAccountName">The samAccountName of the user to update.</param> /// <param name="userFromService">The updated user data.</param> public void UpdateBySamAccountName(string customerName, string samAccountName, UserFromService userFromService) { var userFromRepository = Mapper.Map <UserFromService, UserFromRepository>(userFromService); this.UserRepository.UpdateBySamAccountName(customerName, samAccountName, userFromRepository); }