/// <summary> /// Deletes users from the LMS. /// </summary> /// <param name="userIds">The IDs of the user to delete.</param> /// <returns>The results of the users deletion. Including any errors and warnings from the LMS.</returns> public UserActionResults DeleteUsers(string[] userIds) { if (userIds.Length == 0) { return(null); } UserActionResults result = null; StringBuilder usersCsvBuilder = new StringBuilder(); usersCsvBuilder.Append("Actions,UserID"); foreach (string userId in userIds) { if (!string.IsNullOrWhiteSpace(userId)) { usersCsvBuilder.AppendFormat("\n{0},{1}", NetDimensionsConstants.UserActions.Delete, userId); } } string serviceApiUrl = string.Format(NetDimensionsConstants.ApiUsersCsv, this.LmsUrl); using (WebClient apiWebClient = new WebClient()) { apiWebClient.Headers[HttpRequestHeader.ContentType] = "text/plain"; apiWebClient.Credentials = new NetworkCredential(this.UserAuthUser, this.SysAuthKey); string csvResponse = Encoding.UTF8.GetString( apiWebClient.UploadData( serviceApiUrl, "POST", Encoding.ASCII.GetBytes(usersCsvBuilder.ToString()))); result = new UserActionResults(csvResponse); } return(result); }
/// <summary> /// Creates users in the LMS. Also adds their related organizations if needed, and assigns the users to them. /// </summary> /// <param name="users">The users to create.</param> /// <returns>The results of the users creation. Including any errors and warnings from the LMS.</returns> public UserActionResults CreateUsers(NetDimensionsUser[] users) { if (users.Length == 0) { return(null); } if ((users.Any(u => string.IsNullOrWhiteSpace(u.UserID))) || (users.Any(u => string.IsNullOrWhiteSpace(u.LastName)) || (users.Any(u => string.IsNullOrWhiteSpace(u.FirstName))) || (users.Any(u => string.IsNullOrWhiteSpace(u.Password))) || (users.Any(u => string.IsNullOrWhiteSpace(u.Email))))) { throw new ArgumentException("To create a user, the following fields are mandatory: UserID, LastName, FirstName, Password, Email"); } UserActionResults result = null; string serviceApiUrl = string.Format(NetDimensionsConstants.ApiUsersCsv, this.LmsUrl); string usersCsv = NetDimensionsUser.GetCsv(NetDimensionsConstants.UserActions.AddOrUpdate, users); using (WebClient apiWebClient = new WebClient()) { apiWebClient.Headers[HttpRequestHeader.ContentType] = "text/plain"; apiWebClient.Credentials = new NetworkCredential(this.UserAuthUser, this.SysAuthKey); string csvResponse = Encoding.UTF8.GetString(apiWebClient.UploadData( serviceApiUrl, "POST", Encoding.ASCII.GetBytes(usersCsv))); result = new UserActionResults(csvResponse); } Dictionary <NetDimensionsOrganization, List <string> > organizationUsers = new Dictionary <NetDimensionsOrganization, List <string> >(); foreach (NetDimensionsUser user in users) { if (user.Organization != null) { NetDimensionsOrganization organizationInList = organizationUsers.Keys.FirstOrDefault(o => o.Code == user.Organization.Code); if (organizationInList == null) { organizationUsers.Add(user.Organization, new List <string>()); } organizationInList = organizationUsers.Keys.FirstOrDefault(o => o.Code == user.Organization.Code); if (string.IsNullOrWhiteSpace(organizationInList.Description)) { organizationInList.Description = user.Organization.Description; } if (!organizationUsers[organizationInList].Contains(user.UserID)) { organizationUsers[organizationInList].Add(user.UserID); } } } foreach (KeyValuePair <NetDimensionsOrganization, List <string> > organization in organizationUsers) { this.CreateOrganization(organization.Key.Code, organization.Key.Description); this.AddUsersToOrganization(organization.Value.ToArray(), organization.Key.Code); } return(result); }