/// <summary> /// Updates a single users card number. /// </summary> /// <param name="updateOnlyIfBlank"></param> /// <param name="targetIdField"></param> /// <param name="targetUsername"></param> /// <param name="numberOfChars"></param> public void UpdateSingleCardNumber(bool updateOnlyIfBlank, int targetIdField, string targetUsername, int numberOfChars) { try { if (!string.IsNullOrEmpty(targetUsername) && numberOfChars >= Common.Constants.PaperCut.Constants.MinimumNumberOfCharacters && numberOfChars <= Common.Constants.PaperCut.Constants.MaximumNumberOfCharacters) { string newCardNumber = SecurityStringHelper.GenerateIdentifier(numberOfChars); if (!string.IsNullOrEmpty(newCardNumber)) { if (updateOnlyIfBlank) { string existingCardNumber = PaperCutProxyWrapper.GetCardNumber(_serverProxy, targetUsername, targetIdField); if (string.IsNullOrEmpty(existingCardNumber)) { PaperCutProxyWrapper.SetCardNumber(_serverProxy, targetUsername, newCardNumber, targetIdField); Console.WriteLine(string.Format(Common.Constants.PaperCut.Messages.UpdatedUserWithNewCardNumber, targetUsername, newCardNumber)); Console.WriteLine(Common.Constants.ConsoleSpacing.HashesWithNewLine); } else { Console.WriteLine(string.Format(Common.Constants.PaperCut.Messages.CannotUpdateUserDueToExistingCardNumber, targetUsername, existingCardNumber)); Console.WriteLine(Common.Constants.ConsoleSpacing.HashesWithNewLine); } } else { PaperCutProxyWrapper.SetCardNumber(_serverProxy, targetUsername, newCardNumber, targetIdField); Console.WriteLine(string.Format(Common.Constants.PaperCut.Messages.UpdatedUserWithNewCardNumber, targetUsername, newCardNumber)); Console.WriteLine(Common.Constants.ConsoleSpacing.HashesWithNewLine); } } } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } }
/// <summary> /// Updates the card numbers for all users. /// </summary> /// <param name="updateOnlyIfBlank"></param> /// <param name="targetIdField"></param> /// <param name="numberOfChars"></param> public void UpdateAllCardNumbers(bool updateOnlyIfBlank, int targetIdField, int numberOfChars) { try { if (numberOfChars >= Common.Constants.PaperCut.Constants.MinimumNumberOfCharacters && numberOfChars <= Common.Constants.PaperCut.Constants.MaximumNumberOfCharacters) { string[] existingUsers = PaperCutProxyWrapper.GetUserAccounts(_serverProxy); string[] newCardNumbers = new string[existingUsers.Length]; newCardNumbers = SecurityStringHelper.GenerateIdentifiers(numberOfChars, newCardNumbers.Length).ToArray(); if (updateOnlyIfBlank) { int updatedCount = 0; int skippedCount = 0; string[] existingCardNumbers; existingCardNumbers = PaperCutProxyWrapper.GetCardNumbers(_serverProxy, existingUsers, targetIdField); Console.WriteLine(Common.Constants.PaperCut.Messages.UpdatingOnlyUsersWithBlankCardNumbers); Console.WriteLine(Common.Constants.ConsoleSpacing.HashesWithNewLine); for (int i = 0; i < existingUsers.Length; i++) { if (string.IsNullOrEmpty(existingCardNumbers[i])) { PaperCutProxyWrapper.SetCardNumber(_serverProxy, existingUsers[i], newCardNumbers[i], targetIdField); updatedCount++; if (updatedCount > 0 && updatedCount % 100 == 0) { Console.WriteLine(string.Format(Common.Constants.PaperCut.Messages.UpdatedNUsersSoFar, updatedCount)); } } else { skippedCount++; if (skippedCount > 0 && skippedCount % 100 == 0) { Console.WriteLine(string.Format(Common.Constants.PaperCut.Messages.SkippedNUsersSoFar, skippedCount)); } } } Console.WriteLine(); Console.WriteLine(string.Format(Common.Constants.PaperCut.Messages.UpdatedAndSkippedNUsers, updatedCount, skippedCount)); Console.WriteLine(Common.Constants.ConsoleSpacing.Hashes); } else { PaperCutProxyWrapper.SetCardNumbers(_serverProxy, existingUsers, newCardNumbers, targetIdField); Console.WriteLine(); Console.WriteLine(string.Format(Common.Constants.PaperCut.Messages.UpdatedNUsers, existingUsers.Length)); Console.WriteLine(Common.Constants.ConsoleSpacing.HashesWithNewLine); } } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } }