/// <summary> /// Add DMS users /// </summary> /// <param name="documents"></param> /// <param name="users"></param> public static string Add(ICollection <string> documents, ICollection <string> users) { StringBuilder response = new StringBuilder(); try { // Initiate backend client var backendClient = new QMSBackendClient(); // Get a time limited service key ServiceKeyClientMessageInspector.ServiceKey = backendClient.GetTimeLimitedServiceKey(); // Get available QlikView Servers var serviceList = backendClient.GetServices(ServiceTypes.QlikViewServer); // Loop through available servers foreach (var server in serviceList) { // Get documents on each server var userDocuments = backendClient.GetUserDocuments(server.ID); // Loop through available documents foreach (var docNode in userDocuments) { // Continue if no matching documents if (documents.Count != 0 && !documents.Contains(docNode.Name.ToLower())) { continue; } // Get authorization metadata var metaData = backendClient.GetDocumentMetaData(docNode, DocumentMetaDataScope.Authorization); // Filter users already in DMS from the supplied list of users to avoid duplicates var uniqueUsers = users.Except(metaData.Authorization.Access.Select(user => user.UserName).ToList()); // Get number of users on each document var numberOfUsers = metaData.Authorization.Access.Count; // Add new users foreach (var user in uniqueUsers.Select(u => new DocumentAccessEntry { UserName = u, AccessMode = DocumentAccessEntryMode.Always, DayOfWeekConstraints = new List <DayOfWeek>() })) { metaData.Authorization.Access.Add(user); } // Save changes backendClient.SaveDocumentMetaData(metaData); // Get number of users AFTER modifications var addedUsers = metaData.Authorization.Access.Count - numberOfUsers; if (addedUsers <= 0) { response.AppendLine(String.Format("Nothing to add to '{0}' on {1}", docNode.Name, server.Name)); } else { response.AppendLine(String.Format("Added {0} users to '{1}' on {2}", addedUsers, docNode.Name, server.Name)); } } } } catch (Exception ex) { response.AppendLine(ex.Message); } return(response.ToString()); }
/// <summary> /// ADD CALs /// </summary> /// <param name="documents"></param> /// <param name="users"></param> public static string Add(ICollection <string> documents, ICollection <string> users) { StringBuilder response = new StringBuilder(); try { // Initiate backend client var backendClient = new QMSBackendClient(); // Get a time limited service key ServiceKeyClientMessageInspector.ServiceKey = backendClient.GetTimeLimitedServiceKey(); // Get available QlikView Servers var serviceList = backendClient.GetServices(ServiceTypes.QlikViewServer); if (documents.Count == 0) // NAMED CALS { // Loop through available servers foreach (var server in serviceList) { // Get Named CALs var config = backendClient.GetCALConfiguration(server.ID, CALConfigurationScope.NamedCALs); // Get number of users BEFORE modifications var numberOfCals = config.NamedCALs.AssignedCALs.Count; // Add CAL's (already existing CAL's seems unaffected, but how to check for them? Is it necessary?) foreach (var user in users.Select(u => new AssignedNamedCAL { UserName = u.ToUpper(), })) { config.NamedCALs.AssignedCALs.Add(user); } // Save changes backendClient.SaveCALConfiguration(config); // Get number of users AFTER modifications var addedCals = config.NamedCALs.AssignedCALs.Count - numberOfCals; if (addedCals <= 0) { response.AppendLine(String.Format("Nothing to add on {0}", server.Name)); } else { response.AppendLine(String.Format("Added {0} CALs on {1}", addedCals, server.Name)); } var inLicense = config.NamedCALs.InLicense; var assigned = config.NamedCALs.Assigned; // Warn if not enough available CAL's if (addedCals >= inLicense) { response.AppendLine(String.Format("WARNING: Attempted to assign {0} CALs on {1} but the license only allows {2} CALs.", addedCals, server.Name, inLicense)); } else if (assigned >= inLicense) { response.AppendLine("WARNING: All available CALs in the license have been assigned."); } } } // DOCUMENT CALS else { // Loop through available servers foreach (var server in serviceList) { // Get documents on each server var userDocuments = backendClient.GetUserDocuments(server.ID); // Loop through available documents foreach (var docNode in userDocuments) { // Continue if no matching documents if (!documents.Contains(docNode.Name.ToLower())) { continue; } var metaData = backendClient.GetDocumentMetaData(docNode, DocumentMetaDataScope.Licensing); // Get allocated CAL's for document var allocatedCals = metaData.Licensing.CALsAllocated; // Allocate more CAL's if necessary if (users.Count > allocatedCals) { metaData.Licensing.CALsAllocated = users.Count(); } // Add document CAL's foreach (var user in users.Select(u => new AssignedNamedCAL { UserName = u })) { metaData.Licensing.AssignedCALs.Add(user); } // Save changes backendClient.SaveDocumentMetaData(metaData); response.AppendLine(String.Format("Added {0} Document CALs to '{1}' on {2}", users.Count(), docNode.Name, server.Name)); } } } } catch (Exception ex) { response.AppendLine(ex.Message); } return(response.ToString()); }
/// <summary> /// Remove DMS users /// </summary> /// <param name="documents"></param> /// <param name="users"></param> public static string Remove(ICollection <string> documents, List <string> users) { StringBuilder response = new StringBuilder(); try { // Initiate backend client var backendClient = new QMSBackendClient(); // Get a time limited service key ServiceKeyClientMessageInspector.ServiceKey = backendClient.GetTimeLimitedServiceKey(); // Get available QlikView Servers var serviceList = backendClient.GetServices(ServiceTypes.QlikViewServer); // Convert all usernames to lowercase users = users.ConvertAll(d => d.ToLower()); // Loop through available servers foreach (var server in serviceList) { // Get documents on each server var userDocuments = backendClient.GetUserDocuments(server.ID); // Loop through available documents foreach (var docNode in userDocuments) { // Continue if no matching documents if (documents.Count != 0 && !documents.Contains(docNode.Name.ToLower())) { continue; } // Get authorization metadata var metaData = backendClient.GetDocumentMetaData(docNode, DocumentMetaDataScope.Authorization); // Get number of users BEFORE modifications var numberOfUsers = metaData.Authorization.Access.Count; if (users.Count == 0) { // Remove all users if no users were specified metaData.Authorization.Access.RemoveRange(0, numberOfUsers); } else { // Remove matching users foreach (var u in metaData.Authorization.Access.ToList().Where(u => users.Contains(u.UserName.ToLower()))) { metaData.Authorization.Access.Remove(u); } } // Save changes backendClient.SaveDocumentMetaData(metaData); // Get number of users AFTER modifications var removedUsers = numberOfUsers - metaData.Authorization.Access.Count; if (removedUsers <= 0) { response.AppendLine(String.Format("Nothing to remove from '{0}' on {1}", docNode.Name, server.Name)); } else { response.AppendLine(String.Format("Removed {0} users from '{1}' on {2}", removedUsers, docNode.Name, server.Name)); } } } } catch (Exception ex) { response.AppendLine(ex.Message); } return(response.ToString()); }
/// <summary> /// Remove CALs /// </summary> public static string Remove() { StringBuilder response = new StringBuilder(); try { // Initiate backend client var backendClient = new QMSBackendClient(); // Get a time limited service key ServiceKeyClientMessageInspector.ServiceKey = backendClient.GetTimeLimitedServiceKey(); // Get available QlikView Servers var serviceList = backendClient.GetServices(ServiceTypes.QlikViewServer); // Number of inactive days const int days = -30; // Loop through available servers foreach (var server in serviceList) { /********************** * NAMED CALS **********************/ // Get CAL configuration var config = backendClient.GetCALConfiguration(server.ID, CALConfigurationScope.NamedCALs); // Get number of users BEFORE modifications var numberOfUsers = config.NamedCALs.AssignedCALs.Count; // Iterate through all CAL's and remove the inactive ones foreach (var c in config.NamedCALs.AssignedCALs.ToList().Where(u => u.LastUsed.Year > 0001 && u.LastUsed.CompareTo(DateTime.UtcNow.AddDays(days)) == -1)) { config.NamedCALs.AssignedCALs.Remove(c); } // Save changes backendClient.SaveCALConfiguration(config); // Get number of users BEFORE modifications var removedUsers = numberOfUsers - config.NamedCALs.AssignedCALs.Count; if (removedUsers <= 0) { response.AppendLine(String.Format("No CALs to remove on {0}", server.Name)); } else { response.AppendLine(String.Format("Removed {0} CALs on {1}", removedUsers, server.Name)); } /********************** * DOCUMENT CALS **********************/ // Get Document CAL's var userDocuments = backendClient.GetUserDocuments(server.ID); foreach (var docNode in userDocuments) { // Get licensing meta data var metaData = backendClient.GetDocumentMetaData(docNode, DocumentMetaDataScope.Licensing); // Get number of users BEFORE modifications numberOfUsers = metaData.Licensing.AssignedCALs.Count; // Iterate through all CAL's and remove the inactive ones foreach (var c in metaData.Licensing.AssignedCALs.ToList().Where(u => u.LastUsed.Year > 0001 && u.LastUsed.CompareTo(DateTime.UtcNow.AddDays(days)) == -1)) { metaData.Licensing.AssignedCALs.Remove(c); } // Save changes backendClient.SaveDocumentMetaData(metaData); // Get number of users AFTER modifications removedUsers = numberOfUsers - metaData.Licensing.AssignedCALs.Count; if (removedUsers <= 0) { response.AppendLine(String.Format("No Document CALs to remove from '{0}' on {1}", docNode.Name, server.Name)); } else { response.AppendLine(String.Format("Removed {0} Document CALs from '{1}' on {2}", removedUsers, docNode.Name, server.Name)); } } } } catch (Exception ex) { response.AppendLine(ex.Message); } return(response.ToString()); }