protected override void ProcessRecord() { base.ProcessRecord(); if (Globals.authToken != null) { _serverProxy = new ServerCommandProxy(Globals.ComputerName, Globals.Port, Globals.authToken); try { _serverProxy.AddAdminAccessUser(UserName); PSObject returnAddPcutAdminAccessUser = new PSObject(); returnAddPcutAdminAccessUser.Properties.Add(new PSNoteProperty("Username", UserName)); returnAddPcutAdminAccessUser.Properties.Add(new PSNoteProperty("AdminAccess", true)); WriteObject(returnAddPcutAdminAccessUser); } catch (XmlRpcFaultException fex) { ErrorRecord errRecord = new ErrorRecord(new Exception(fex.Message, fex.InnerException), fex.FaultString, ErrorCategory.NotSpecified, fex); WriteError(errRecord); } } else { WriteObject("Please run Connect-PcutServer in order to establish connection."); } }
protected override void ProcessRecord() { base.ProcessRecord(); if (Globals.authToken != null) { _serverProxy = new ServerCommandProxy(Globals.ComputerName, Globals.Port, Globals.authToken); try { string PropertyValue = _serverProxy.GetUserProperty(UserName, PropertyName); PSObject returnPcutGetUserProperty = new PSObject(); returnPcutGetUserProperty.Properties.Add(new PSNoteProperty("Username", UserName)); returnPcutGetUserProperty.Properties.Add(new PSNoteProperty("propertyName", PropertyName)); returnPcutGetUserProperty.Properties.Add(new PSNoteProperty("propertyValue", PropertyValue)); WriteObject(returnPcutGetUserProperty); } catch (XmlRpcFaultException fex) { ErrorRecord errRecord = new ErrorRecord(new Exception(fex.Message, fex.InnerException), fex.FaultString, ErrorCategory.NotSpecified, fex); WriteError(errRecord); } } else { WriteObject("Please run Connect-PcutServer in order to establish connection."); } }
private void btnUpdate_Click(object sender, RoutedEventArgs e) { ServerCommandProxy serverProxy = null; OperationTypeEnum operation = OperationTypeEnum.Update; txtblockOutput.Inlines.Clear(); try { ValidationResult result = ValidateInput(operation); if (result.IsValid) { string ppcHostname = txtboxPpcServer.Text; string ppcAdminPw = pwdboxPpcAdminPw.Password; serverProxy = new ServerCommandProxy(ppcHostname, 9191, ppcAdminPw); if (PaperCutProxyWrapper.IsConnectionEstablished(serverProxy)) { txtblockOutput.Inlines.Add(Constants.PaperCut.Messages.PaperCutConnectionEstablished); int totalUsers = serverProxy.GetTotalUsers(); Console.WriteLine(string.Format(Constants.PaperCut.Messages.NumberOfUsersRetrievedFromPaperCut, totalUsers)); Console.WriteLine(Constants.ConsoleSpacing.HashesWithNewLine); PaperCutHelper paperCutHelper = new PaperCutHelper(serverProxy); if (paperCutHelper != null) { int targetIdField = cmboxTargetIDField.SelectedIndex; int numberOfChars = int.Parse(txtboxIDLength.Text.ToString()); bool updateOnlyIfBlank = chckboxUpdateIfBlank.IsChecked.Value; bool mustTargetSpecificUsername = chckboxTargetSpecificUser.IsChecked.Value; if (mustTargetSpecificUsername) { string specificUsername = txtboxTargetSpecificUser.Text; paperCutHelper.UpdateSingleCardNumber(updateOnlyIfBlank, targetIdField, specificUsername, numberOfChars); } else { paperCutHelper.UpdateAllCardNumbers(updateOnlyIfBlank, targetIdField, numberOfChars); } } } else { txtblockOutput.Inlines.Add(Constants.PaperCut.Messages.PaperCutConnectionNotEstablished); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
protected override void ProcessRecord() { base.ProcessRecord(); if (Globals.authToken != null) { _serverProxy = new ServerCommandProxy(Globals.ComputerName, Globals.Port, Globals.authToken); try { bool passwordChanged = _serverProxy.ChangeInternalAdminPassword(Password); PSObject returnPasswordChanged = new PSObject(); returnPasswordChanged.Properties.Add(new PSNoteProperty("PasswordChanged", passwordChanged)); Globals.authToken = null; Globals.ComputerName = null; Globals.Port = 0; WriteObject(returnPasswordChanged); } catch (XmlRpcFaultException fex) { ErrorRecord errRecord = new ErrorRecord(new Exception(fex.Message, fex.InnerException), fex.FaultString, ErrorCategory.NotSpecified, fex); WriteError(errRecord); } } else { WriteObject("Please run Connect-PcutServer in order to establish connection."); } }
protected override void ProcessRecord() { base.ProcessRecord(); if (Globals.authToken != null) { _serverProxy = new ServerCommandProxy(Globals.ComputerName, Globals.Port, Globals.authToken); try { int TotalUsers; TotalUsers = _serverProxy.GetTotalUsers(); PSObject returnTotalusers = new PSObject(); PSNoteProperty propertyTotalUsers = new PSNoteProperty("TotalUsers", TotalUsers); PSNoteProperty propertyComputerName = new PSNoteProperty("Server", Globals.ComputerName); returnTotalusers.Properties.Add(propertyTotalUsers); returnTotalusers.Properties.Add(propertyComputerName); WriteObject(returnTotalusers); } catch (XmlRpcFaultException fex) { ErrorRecord errRecord = new ErrorRecord(new Exception(fex.Message, fex.InnerException), fex.FaultString, ErrorCategory.NotSpecified, fex); WriteError(errRecord); } } else { WriteObject("Please run Connect-PcutServer in order to establish connection."); } }
/// <summary> /// Clears the selected department information for all users. /// </summary> internal static void ClearUsersDepartmentInfo(ServerCommandProxy serverProxy, int targetDepartmentField) { string[] users = PaperCutProxyWrapper.GetUserAccounts(serverProxy); string fieldToUpdate = PaperCutProxyWrapper.ResolveDepartmentField(targetDepartmentField); int clearCount = 0; try { Console.WriteLine("Clearing {0} for {1} users...", fieldToUpdate, users.Length); Console.WriteLine("########################################\r\n"); for (int i = 0; i < users.Length; i++) { if (i != 0 && i % 100 == 0) { Console.WriteLine("Cleared {0} {1} properties so far...", i, fieldToUpdate); } serverProxy.SetUserProperty(users[i], fieldToUpdate, ""); clearCount++; } Console.WriteLine("\nCleared {0}s for {1} users.", fieldToUpdate, clearCount); Console.WriteLine("########################################\r\n"); } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } }
protected override void ProcessRecord() { base.ProcessRecord(); if (Globals.authToken != null) { _serverProxy = new ServerCommandProxy(Globals.ComputerName, Globals.Port, Globals.authToken); try { _serverProxy.RenameUserAccount(currentUserName, newUserName); if (_serverProxy.UserExists(newUserName)) { PSObject objNewUser = new PSObject(); PSNoteProperty noteProp = new PSNoteProperty("NewUsername", newUserName); objNewUser.Properties.Add(noteProp); WriteObject(objNewUser); } } catch (XmlRpcFaultException fex) { ErrorRecord errRecord = new ErrorRecord(new Exception(fex.Message, fex.InnerException), fex.FaultString, ErrorCategory.NotSpecified, fex); WriteError(errRecord); } } else { WriteObject("Please run Connect-PcutServer in order to establish connection."); } }
protected override void ProcessRecord() { base.ProcessRecord(); if (Globals.authToken != null) { _serverProxy = new ServerCommandProxy(Globals.ComputerName, Globals.Port, Globals.authToken); try { string[] pcutUserGroups = _serverProxy.GetUserGroups(UserName); Collection <PSObject> returnPcutUserGroups = new Collection <PSObject>(); foreach (string pcutUserGroup in pcutUserGroups) { PSObject thisGroup = new PSObject(); thisGroup.Properties.Add(new PSNoteProperty("Name", pcutUserGroup)); returnPcutUserGroups.Add(thisGroup); } WriteObject(returnPcutUserGroups); } catch (XmlRpcFaultException fex) { ErrorRecord errRecord = new ErrorRecord(new Exception(fex.Message, fex.InnerException), fex.FaultString, ErrorCategory.NotSpecified, fex); WriteError(errRecord); } } else { WriteObject("Please run Connect-PcutServer in order to establish connection."); } }
/// <summary> /// Clears the card numbers all of users. /// </summary> internal static void ClearCardNumbers(ServerCommandProxy serverProxy, int cardFieldToUpdate) { string[] usernames = PaperCutProxyWrapper.GetUserAccounts(serverProxy); string fieldToUpdate = PaperCutProxyWrapper.ResolveCardField(cardFieldToUpdate); int clearCount = 0; try { Console.WriteLine("Clearing {0} for {0} users.)", fieldToUpdate, usernames.Length); Console.WriteLine("########################################\r\n"); for (int i = 0; i < usernames.Length; i++) { if (i != 0 && i % 100 == 0) { Console.WriteLine("Cleared {0} card numbers so far...", i); } serverProxy.SetUserProperty(usernames[i], fieldToUpdate, ""); clearCount++; } Console.WriteLine("\nCleared card numbers for {0} users.", clearCount); Console.WriteLine("########################################\r\n"); } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } }
protected override void ProcessRecord() { base.ProcessRecord(); if (Globals.authToken != null) { _serverProxy = new ServerCommandProxy(Globals.ComputerName, Globals.Port, Globals.authToken); try { string[] propertyNames = new string[] { "full-name", "email", "disabled-print", "balance", "restricted", "account-selection.mode", "department", "office", "card-number", "card-pin", "notes" }; string[] pcutUserProperties = _serverProxy.GetUserProperties(UserName, propertyNames); PSObject returnPcutUserProperties = new PSObject(); returnPcutUserProperties.Properties.Add(new PSNoteProperty("Username", UserName)); int propertyCount = 0; foreach (string propertyName in propertyNames) { returnPcutUserProperties.Properties.Add(new PSNoteProperty(propertyName, pcutUserProperties[propertyCount])); propertyCount += 1; } WriteObject(returnPcutUserProperties); } catch (XmlRpcFaultException fex) { ErrorRecord errRecord = new ErrorRecord(new Exception(fex.Message, fex.InnerException), fex.FaultString, ErrorCategory.NotSpecified, fex); WriteError(errRecord); } } else { WriteObject("Please run Connect-PcutServer in order to establish connection."); } }
/// <summary> /// Sets a single department information for all users in PaperCut. /// </summary> /// <param name="serverProxy"></param> /// <param name="ldapUsers"></param> /// <param name="targetDepartmentField"></param> public static void SetUsersSingleDepartmentInfo(ServerCommandProxy serverProxy, LdapUser[] ldapUsers, int targetDepartmentField) { try { if (serverProxy != null && ldapUsers.Any()) { string fieldToUpdate = ResolveDepartmentField(targetDepartmentField); for (int i = 0; i < ldapUsers.Length; i++) { serverProxy.SetUserProperty(ldapUsers[i].Username, fieldToUpdate, ldapUsers[i].DepartmentName); Console.WriteLine(string.Format(Common.Constants.PaperCut.Messages.UpdatedDeparmentProgress, i + 1, ldapUsers.Length, ldapUsers[i].Username, ldapUsers[i].DepartmentName)); } } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } Console.WriteLine(); Console.WriteLine(Common.Constants.ConsoleSpacing.HashesWithNewLine); }
/// <summary> /// Returns an array of strings that contains all of the existing card /// numbers for all of the users in PaperCut. /// </summary> /// <param name="serverProxy"></param> /// <param name="usernames"></param> /// <param name="cardFieldToRetrieve"></param> /// <returns></returns> public static string[] GetCardNumbers(ServerCommandProxy serverProxy, string[] usernames, int cardFieldToRetrieve) { List <string> cardNumbers = null; string fieldToRetrieve = ResolveCardField(cardFieldToRetrieve); try { if (serverProxy != null && usernames.Any()) { Console.WriteLine(Common.Constants.PaperCut.Messages.RetrievingExistingCardNumbers); Console.WriteLine(Common.Constants.ConsoleSpacing.HashesWithNewLine); for (int i = 0; i < usernames.Length; i++) { string currentCard = serverProxy.GetUserProperty(usernames[i], fieldToRetrieve); cardNumbers.Add(currentCard); if (i != 0 && i % 100 == 0) { Console.WriteLine(string.Format(Common.Constants.PaperCut.Messages.RetrievedNCardNumbersSoFar, i)); } } } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } Console.WriteLine(Common.Constants.ConsoleSpacing.HashesWithNewLine); return(cardNumbers != null?cardNumbers.ToArray() : null); }
protected override void ProcessRecord() { base.ProcessRecord(); if (Globals.authToken != null) { _serverProxy = new ServerCommandProxy(Globals.ComputerName, Globals.Port, Globals.authToken); try { double pcutUserPreviousBalance = _serverProxy.GetUserAccountBalance(UserName, null); _serverProxy.AdjustUserAccountBalance(UserName, Adjustment, Comment, null); double pcutUserNewBalance = _serverProxy.GetUserAccountBalance(UserName, null); PSObject returnUpdatePcutUserAccountBalance = new PSObject(); returnUpdatePcutUserAccountBalance.Properties.Add(new PSNoteProperty("Username", UserName)); returnUpdatePcutUserAccountBalance.Properties.Add(new PSNoteProperty("OldBalance", pcutUserPreviousBalance)); returnUpdatePcutUserAccountBalance.Properties.Add(new PSNoteProperty("Adjustment", Adjustment)); returnUpdatePcutUserAccountBalance.Properties.Add(new PSNoteProperty("Balance", pcutUserNewBalance)); WriteObject(returnUpdatePcutUserAccountBalance); } catch (XmlRpcFaultException fex) { ErrorRecord errRecord = new ErrorRecord(new Exception(fex.Message, fex.InnerException), fex.FaultString, ErrorCategory.NotSpecified, fex); WriteError(errRecord); } } else { WriteObject("Please run Connect-PcutServer in order to establish connection."); } }
/// <summary> /// Returns an array of strings that contains all of the existing email /// addresses for all of the users in PaperCut. /// </summary> /// <param name="serverProxy"></param> /// <param name="usernames"></param> /// <returns></returns> public static List <string> GetEmailAddresses(ServerCommandProxy serverProxy, List <string> usernames) { List <string> emailAddresses = null; try { if (serverProxy != null && usernames.Any()) { Console.WriteLine(Common.Constants.PaperCut.Messages.RetrievingEmailAddresses); Console.WriteLine(Common.Constants.ConsoleSpacing.HashesWithNewLine); for (int i = 0; i < usernames.Count; i++) { string currentEmail = serverProxy.GetUserProperty(usernames[i], Common.Constants.PaperCut.Properties.Email); emailAddresses.Add(currentEmail); if (i != 0 && i % 100 == 0) { Console.WriteLine(string.Format(Common.Constants.PaperCut.Messages.RetrievedNEmailAddressesSoFar, i)); } } } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } Console.WriteLine(Common.Constants.ConsoleSpacing.HashesWithNewLine); return(emailAddresses); }
public static int Main() { /* * User Configuration Area */ string authToken = "Password for Builtin User or API Key"; //Password of the internal Administrative user for papercut or the XML API Key if set string server = "localhost"; //URL of the papercut server ie. papercut.localDomain.edu or 172.2.21.5 int port = 9191; //Standard application server port is 9191 string printerName = "Virtual-Queue-on-Printserver"; //This should be the name of the Queue in papercut that controls the cost and permissions. string printerServer = "Printserver"; //This is the the name of the print server the fake print queue is sitting on to configure costs. /* * Non-User Area Below */ // Creates a Server Command Proxy, provided by the Papercut. string user = Environment.UserName; //Windows Username of the currently logged in user. string computerName = Environment.MachineName; ServerCommandProxy serverProxy = new ServerCommandProxy(server, port, authToken); try { //Test to see if User Exists if (!serverProxy.UserExists(user)) { // User does not exist! Explain and exit returning 1 Console.WriteLine("I'm sorry but this user does not exist in the chargeback system. Please ensure you are registered for printing."); return 1; } //Test to see if user printing is disabled if (Boolean.Parse(serverProxy.GetUserProperty(user, "disabled-print"))) { Console.WriteLine("I'm sorry but your print account is disabled. Please ensure you are registered for printing."); return 2; } //Test to see if user can afford the print if(Boolean.Parse(serverProxy.GetUserProperty(user, "restricted"))){ // Account is restricted, make sure that it has money for the print double printerCost = serverProxy.GetPrinterCostSimple(printerServer, printerName); // Get the user's account balance to see if they are able to afford the print if (serverProxy.AdjustUserAccountBalanceIfAvailable(user, -1 * printerCost, "Test Charge for " + printerServer + "\\" + printerName, "")) { serverProxy.AdjustUserAccountBalance(user, printerCost, "Test Charge for " + printerServer + "\\" + printerName, ""); } else { Console.WriteLine("I'm sorry but this user does not have enough money for this print on their account."); return 3; } } //Charge User for the Print Job string printjob = "user="******",server=" + server + ",printer=" + printerName + ",client-machine=" + computerName + ",server=" + printerServer; // This assembles a string of information to submit the printjob serverProxy.ProcessJob(printjob); // Sends the formatted print job return 0; // job was charged. Success! } catch (XmlRpcFaultException fex) { Console.WriteLine("Fault: {0}, {1}", fex.FaultCode, fex.FaultString); return -1; } }
/// <summary> /// Returns a boolean indicating if the connection has been established /// </summary> /// <param name="serverProxy"></param> /// <returns></returns> public static bool IsConnectionEstablished(ServerCommandProxy serverProxy) { bool isConnectionEstablished = false; if (serverProxy.GetTotalUsers() > 0) { isConnectionEstablished = true; } return(isConnectionEstablished); }
/// <summary> /// Returns an array of strings that contains all /// of the usernames for all of the users in PaperCut. /// </summary> /// <param name="serverProxy"></param> /// <returns></returns> public static string[] GetUserAccounts(ServerCommandProxy serverProxy) { string[] usersInBatch; string[] allUsers; int offset; int totalUsers; int remainingUsers; int maxUsersInBatch; int j; offset = 0; totalUsers = serverProxy.GetTotalUsers(); maxUsersInBatch = 1000; j = 0; allUsers = new string[totalUsers]; remainingUsers = totalUsers; if (totalUsers <= 1000) { allUsers = serverProxy.ListUserAccounts(offset, totalUsers); return(allUsers); } else { do { usersInBatch = serverProxy.ListUserAccounts(offset, maxUsersInBatch); foreach (string user in usersInBatch) { allUsers[j] = user.ToString(); j++; } remainingUsers = remainingUsers - usersInBatch.Length; offset += maxUsersInBatch; if (remainingUsers >= 1000) { maxUsersInBatch = 1000; } else { maxUsersInBatch = remainingUsers; } } while (remainingUsers > 0); } return(allUsers); }
/// <summary> /// Sets the ID number for a specific user. /// </summary> /// <param name="serverProxy"></param> /// <param name="username"></param> /// <param name="cardNumber"></param> /// <param name="cardFieldToUpdate"></param> public static void SetCardNumber(ServerCommandProxy serverProxy, string username, string cardNumber, int cardFieldToUpdate) { try { if (serverProxy != null && !string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(cardNumber)) { string fieldToUpdate = ResolveCardField(cardFieldToUpdate); serverProxy.SetUserProperty(username, fieldToUpdate, cardNumber); } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } }
/// <summary> /// Clears the card number for a specific user. /// </summary> internal static void ClearCardNumber(ServerCommandProxy serverProxy, string username, int cardFieldToUpdate) { string fieldToUpdate = PaperCutProxyWrapper.ResolveCardField(cardFieldToUpdate); try { Console.WriteLine("Clearing {0} for username {1}.)", fieldToUpdate, username); Console.WriteLine("########################################\r\n"); serverProxy.SetUserProperty(username, fieldToUpdate, ""); Console.WriteLine("Clear successful."); Console.WriteLine("########################################\r\n"); } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } }
/// <summary> /// Returns an array of PaperCutUser that contains all the users /// in PaperCut and their department information. /// </summary> /// <param name="serverProxy"></param> /// <returns></returns> public static PaperCutUser[] GetUsersDepartmentInfo(ServerCommandProxy serverProxy) { string[] allUsers = GetUserAccounts(serverProxy); PaperCutUser[] allUsersWithDeptInfo = new PaperCutUser[allUsers.Length]; string[] retrievedProperties = new string[2]; string[] propertiesToRetrieve = { Common.Constants.PaperCut.Properties.Department, Common.Constants.PaperCut.Properties.Office }; Console.WriteLine(string.Format(Common.Constants.PaperCut.Messages.RetrievingDepartmentInformationForNUsers, allUsers.Length)); Console.WriteLine(Common.Constants.ConsoleSpacing.HashesWithNewLine); for (int i = 0; i < allUsersWithDeptInfo.Length; i++) { try { allUsersWithDeptInfo[i] = new PaperCutUser(); allUsersWithDeptInfo[i].Username = allUsers[i]; retrievedProperties = serverProxy.GetUserProperties(allUsersWithDeptInfo[i].Username, propertiesToRetrieve); allUsersWithDeptInfo[i].Department = retrievedProperties[0]; allUsersWithDeptInfo[i].Department = retrievedProperties[1]; if (i != 0 && i % 100 == 0) { Console.WriteLine(string.Format(Common.Constants.PaperCut.Messages.RetrievedNUsersSoFar, i)); } if (i == allUsersWithDeptInfo.Length - 1) { Console.WriteLine(); Console.WriteLine(string.Format(Common.Constants.PaperCut.Messages.RetrievedNUsers, allUsersWithDeptInfo.Length)); Console.WriteLine(Common.Constants.ConsoleSpacing.HashesWithNewLine); } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } } return(allUsersWithDeptInfo); }
private void btnClearDept_Click(object sender, RoutedEventArgs e) { string ppcHostname; string ppcAdminPw; int targetDeptField; bool inputIsValid = true; ServerCommandProxy serverProxy; txtblockOutput.Inlines.Clear(); ppcHostname = txtboxPpcServer.Text; if (String.IsNullOrEmpty(ppcHostname)) { txtblockOutput.Inlines.Add("You must enter the PaperCut server hostname.\r\n"); inputIsValid = false; } ppcAdminPw = pwdboxPpcAdminPw.Password; if (String.IsNullOrEmpty(ppcAdminPw)) { txtblockOutput.Inlines.Add("You must enter the PaperCut admin password.\r\n"); inputIsValid = false; } targetDeptField = cmboxTargetDeptField.SelectedIndex; if (inputIsValid) { try { serverProxy = new ServerCommandProxy(ppcHostname, 9191, ppcAdminPw); bool ppcConnectionEstablished = PaperCutProxyWrapper.ConnectionEstablished(serverProxy); if(ppcConnectionEstablished) { PaperCutProxyWrapper.ClearUsersDepartmentInfo(serverProxy, targetDeptField); } } catch (Exception) { txtblockOutput.Inlines.Add("Connection to PaperCut cannot be established.\r\n"); return; } } }
/// <summary> /// Returns a string containing the users email address, /// or a blank string if nothing was retrieved. /// </summary> /// <param name="serverProxy"></param> /// <param name="username"></param> /// <returns></returns> public static string GetEmailAddress(ServerCommandProxy serverProxy, string username) { string emailAddress = null; try { if (!string.IsNullOrEmpty(username)) { emailAddress = serverProxy.GetUserProperty(username, Common.Constants.PaperCut.Properties.Email); } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } return(emailAddress); }
/// <summary> /// Returns a string containing the users card number, /// or a blank string if nothing was retrieved. /// </summary> /// <param name="serverProxy"></param> /// <param name="username"></param> /// <param name="cardFieldToRetrieve"></param> /// <returns></returns> public static string GetCardNumber(ServerCommandProxy serverProxy, string username, int cardFieldToRetrieve) { string cardNumber = null; string fieldToRetrieve = ResolveCardField(cardFieldToRetrieve); try { if (!string.IsNullOrEmpty(username)) { cardNumber = serverProxy.GetUserProperty(username, fieldToRetrieve); } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } return(cardNumber); }
/// <summary> /// Sets the ID numbers for all users. /// </summary> /// <param name="serverProxy"></param> /// <param name="usernames"></param> /// <param name="cardNumbers"></param> /// <param name="cardFieldToUpdate"></param> public static void SetCardNumbers(ServerCommandProxy serverProxy, string[] usernames, string[] cardNumbers, int cardFieldToUpdate) { try { if (serverProxy != null && usernames.Any() && cardNumbers.Any() && (usernames.Length == cardNumbers.Length)) { string fieldToUpdate = ResolveCardField(cardFieldToUpdate); for (int i = 0; i < usernames.Length; i++) { serverProxy.SetUserProperty(usernames[i], fieldToUpdate, cardNumbers[i]); Console.WriteLine(string.Format(Common.Constants.PaperCut.Messages.UpdatedCardNumberProgress, i + 1, usernames.Length, usernames[i], cardNumbers[i])); } } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } }
protected override void ProcessRecord() { base.ProcessRecord(); if (Globals.authToken != null) { _serverProxy = new ServerCommandProxy(Globals.ComputerName, Globals.Port, Globals.authToken); try { _serverProxy.RemoveUserFromGroup(UserName, GroupName); WriteObject(UserName + " removed from " + GroupName); } catch (XmlRpcFaultException fex) { ErrorRecord errRecord = new ErrorRecord(new Exception(fex.Message, fex.InnerException), fex.FaultString, ErrorCategory.NotSpecified, fex); WriteError(errRecord); } } else { WriteObject("Please run Connect-PcutServer in order to establish connection."); } }
/// <summary> /// Clears the card number for a specific user. /// </summary> /// <param name="serverProxy"></param> /// <param name="username"></param> /// <param name="cardFieldToUpdate"></param> public static void ClearCardNumber(ServerCommandProxy serverProxy, string username, int cardFieldToUpdate) { try { if (serverProxy != null && !string.IsNullOrEmpty(username)) { string fieldToUpdate = ResolveCardField(cardFieldToUpdate); Console.WriteLine(string.Format(Common.Constants.PaperCut.Messages.ClearingCardNumberForUsername, fieldToUpdate, username)); Console.WriteLine(Common.Constants.ConsoleSpacing.Hashes); serverProxy.SetUserProperty(username, fieldToUpdate, string.Empty); Console.WriteLine(Common.Constants.PaperCut.Messages.ClearSuccessful); Console.WriteLine(Common.Constants.ConsoleSpacing.Hashes); } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } }
protected override void ProcessRecord() { base.ProcessRecord(); if (Globals.authToken != null) { _serverProxy = new ServerCommandProxy(Globals.ComputerName, Globals.Port, Globals.authToken); try { _serverProxy.AddUserToGroup(UserName, GroupName); WriteObject(UserName + " added to " + GroupName); } catch (XmlRpcFaultException fex) { ErrorRecord errRecord = new ErrorRecord(new Exception(fex.Message, fex.InnerException), fex.FaultString, ErrorCategory.NotSpecified, fex); WriteError(errRecord); } } else { WriteObject("Please run Connect-PcutServer in order to establish connection."); } }
protected override void ProcessRecord() { base.ProcessRecord(); _serverProxy = new ServerCommandProxy(ComputerName, Port, authToken); try { int TotalUsers; TotalUsers = _serverProxy.GetTotalUsers(); if (TotalUsers >= 0) { Globals.authToken = authToken; Globals.ComputerName = ComputerName; Globals.Port = Port; WriteObject("Connected to " + Globals.ComputerName + ":" + Globals.Port); } } catch (XmlRpcFaultException fex) { ErrorRecord errRecord = new ErrorRecord(new Exception(fex.Message, fex.InnerException), fex.FaultString, ErrorCategory.NotSpecified, fex); WriteError(errRecord); } }
private void btnClearDept_Click(object sender, RoutedEventArgs e) { ServerCommandProxy serverProxy; OperationTypeEnum operation = OperationTypeEnum.ClearDepartment; txtblockOutput.Inlines.Clear(); try { ValidationResult result = ValidateInput(operation); if (result.IsValid) { string ppcHostname = txtboxPpcServer.Text; string ppcAdminPw = pwdboxPpcAdminPw.Password; serverProxy = new ServerCommandProxy(ppcHostname, 9191, ppcAdminPw); if (PaperCutProxyWrapper.IsConnectionEstablished(serverProxy)) { txtblockOutput.Inlines.Add(Constants.PaperCut.Messages.PaperCutConnectionEstablished); int targetDeptField = cmboxTargetDeptField.SelectedIndex; PaperCutProxyWrapper.ClearUsersDepartmentInfo(serverProxy, targetDeptField); } else { txtblockOutput.Inlines.Add(Constants.PaperCut.Messages.PaperCutConnectionNotEstablished); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
/// <summary> /// Clears the selected department information for all users. /// </summary> /// <param name="serverProxy"></param> /// <param name="targetDepartmentField"></param> public static void ClearUsersDepartmentInfo(ServerCommandProxy serverProxy, int targetDepartmentField) { try { if (serverProxy != null) { string[] users = GetUserAccounts(serverProxy); string fieldToUpdate = ResolveDepartmentField(targetDepartmentField); int clearCount = 0; if (users.Any()) { Console.WriteLine(string.Format(Common.Constants.PaperCut.Messages.ClearingPropertyForNUsers, fieldToUpdate, users.Length)); Console.WriteLine(Common.Constants.ConsoleSpacing.HashesWithNewLine); for (int i = 0; i < users.Length; i++) { if (i != 0 && i % 100 == 0) { Console.WriteLine(string.Format(Common.Constants.PaperCut.Messages.ClearedNPropertiesSoFar, i, fieldToUpdate)); } serverProxy.SetUserProperty(users[i], fieldToUpdate, string.Empty); clearCount++; } Console.WriteLine(); Console.WriteLine(string.Format(Common.Constants.PaperCut.Messages.ClearedPropertiesForNUsers, fieldToUpdate, clearCount)); Console.WriteLine(Common.Constants.ConsoleSpacing.HashesWithNewLine); } } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } }
/// <summary> /// Returns a users email address. /// </summary> /// /// <returns> /// A string containing the users email address, /// or a blank string if the operation was unsuccessful. /// </returns> internal static string GetEmailAddress(ServerCommandProxy serverProxy, string username) { string emailAddress = ""; try { emailAddress = serverProxy.GetUserProperty(username, "email"); return emailAddress; } catch (Exception ex) { Console.WriteLine(ex.StackTrace); return emailAddress; } }
/// <summary> /// Returns all of the existing email addresses from PaperCut. /// </summary> /// /// <returns> /// An array of strings that contains all of the existing email /// addresses for all of the users in PaperCut. /// </returns> internal static string[] GetEmailAddresses(ServerCommandProxy serverProxy, string[] usernames) { string[] emailAddresses = new string[usernames.Length]; try { Console.WriteLine("Retrieving email addresses..."); Console.WriteLine("########################################"); for (int i = 0; i < usernames.Length; i++) { string currentEmail = serverProxy.GetUserProperty(usernames[i], "email"); emailAddresses[i] = currentEmail.ToString(); if (i != 0 && i % 100 == 0) { Console.WriteLine("Retrieved {0} email addresses so far...", i); } } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } Console.WriteLine("########################################\r\n"); return emailAddresses; }
public PaperCutHelper(ServerCommandProxy serverProxy) { this._serverProxy = serverProxy; }
/// <summary> /// Sets the ID numbers for all users. /// </summary> internal static void SetCardNumbers(ServerCommandProxy serverProxy, string[] usernames, string[] cardNumbers, int cardFieldToUpdate) { string fieldToUpdate = PaperCutProxyWrapper.ResolveCardField(cardFieldToUpdate); try { for (int i = 0; i < usernames.Length; i++) { serverProxy.SetUserProperty(usernames[i], fieldToUpdate, cardNumbers[i]); Console.WriteLine("#{0}/{1} updated username: {2}, new card number: {3}", i + 1, usernames.Length, usernames[i], cardNumbers[i]); } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } }
private void btnSyncDept_Click(object sender, RoutedEventArgs e) { string ppcHostname; string ppcAdminPw; string domainName; string ldapRoot; string domainUser; string domainUserPwd; int targetDeptField; string deptNumberADField; string deptNameADField; bool inputIsValid = true; ServerCommandProxy serverProxy; txtblockOutput.Inlines.Clear(); ppcHostname = txtboxPpcServer.Text; if (String.IsNullOrEmpty(ppcHostname)) { txtblockOutput.Inlines.Add("You must enter the PaperCut server hostname.\r\n"); inputIsValid = false; } ppcAdminPw = pwdboxPpcAdminPw.Password; if (String.IsNullOrEmpty(ppcAdminPw)) { txtblockOutput.Inlines.Add("You must enter the PaperCut admin password.\r\n"); inputIsValid = false; } domainName = txtboxDomainName.Text; if (String.IsNullOrEmpty(domainName)) { txtblockOutput.Inlines.Add("You must enter the domain name.\r\n"); inputIsValid = false; } ldapRoot = txtboxLdapRoot.Text; if (String.IsNullOrEmpty(ldapRoot)) { txtblockOutput.Inlines.Add("You must enter the LDAP root path.\r\n"); inputIsValid = false; } domainUser = txtboxDomainUser.Text; if (String.IsNullOrEmpty(domainUser)) { txtblockOutput.Inlines.Add("You must enter the domain username.\r\n"); inputIsValid = false; } domainUserPwd = pwdboxDomainUserPwd.Password; if (String.IsNullOrEmpty(domainUserPwd)) { txtblockOutput.Inlines.Add("You must enter the domain user password.\r\n"); inputIsValid = false; } deptNameADField = txtboxDeptNameADField.Text; deptNumberADField = txtboxDeptNumberADField.Text; if (String.IsNullOrEmpty(deptNameADField) && String.IsNullOrEmpty(deptNumberADField)) { txtblockOutput.Inlines.Add("At least one department AD field is required.\r\n"); inputIsValid = false; } targetDeptField = cmboxTargetDeptField.SelectedIndex; if (inputIsValid) { try { serverProxy = new ServerCommandProxy(ppcHostname, 9191, ppcAdminPw); bool ppcConnectionEstablished = PaperCutProxyWrapper.ConnectionEstablished(serverProxy); LdapHelper ldapHelper = new LdapHelper(domainName, ldapRoot, domainUser, domainUserPwd); bool ldapConnectionEstablished = ldapHelper.ldapConnectionEstablished(domainUser); if (ppcConnectionEstablished) { txtblockOutput.Inlines.Add("Connection to PaperCut established.\r\n"); if (ldapConnectionEstablished) { txtblockOutput.Inlines.Add("Connection to LDAP established.\r\n"); } else { txtblockOutput.Inlines.Add("Connection to LDAP cannot be established.\r\n"); return; } PpcUser[] ppcUsers = PaperCutProxyWrapper.GetUsersDepartmentInfo(serverProxy); if (String.IsNullOrEmpty(deptNumberADField) || String.IsNullOrEmpty(deptNameADField)) { if (String.IsNullOrEmpty(deptNumberADField)) { LdapUser[] ldapUsers = ldapHelper.retrieveUserDepartment(ppcUsers, deptNameADField); PaperCutProxyWrapper.SetUsersSingleDepartmentInfo(serverProxy, ldapUsers, targetDeptField); } else { LdapUser[] ldapUsers = ldapHelper.retrieveUserDepartment(ppcUsers, deptNumberADField); PaperCutProxyWrapper.SetUsersSingleDepartmentInfo(serverProxy, ldapUsers, targetDeptField); } } else { LdapUser[] ldapUsers = ldapHelper.retrieveUserDepartments(ppcUsers, deptNumberADField, deptNameADField); PaperCutProxyWrapper.SetUsersMultipleDepartmentInfo(serverProxy, ldapUsers, targetDeptField); } } } catch (Exception) { txtblockOutput.Inlines.Add("Connection to PaperCut cannot be established.\r\n"); return; } } }
/// <summary> /// Sets a single department information for all users in papercut. /// </summary> internal static void SetUsersSingleDepartmentInfo(ServerCommandProxy serverProxy, LdapUser[] ldapUsers, int targetDepartmentField) { string fieldToUpdate = PaperCutProxyWrapper.ResolveDepartmentField(targetDepartmentField); try { for (int i = 0; i < ldapUsers.Length; i++) { serverProxy.SetUserProperty(ldapUsers[i].Username, fieldToUpdate, ldapUsers[i].DepartmentName); Console.WriteLine("#{0}/{1} updated username: {2}, new department: {3}", i + 1, ldapUsers.Length, ldapUsers[i].Username, ldapUsers[i].DepartmentName); } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } Console.WriteLine("\n########################################\r\n"); }
/// <summary> /// Returns a users card number. /// </summary> /// /// <returns> /// A string containing the users card number, /// or a blank string if the operation was unsuccessful. /// </returns> internal static string GetCardNumber(ServerCommandProxy serverProxy, string username, int cardFieldToRetrieve) { string cardNumber = ""; string fieldToRetrieve = PaperCutProxyWrapper.ResolveCardField(cardFieldToRetrieve); try { cardNumber = serverProxy.GetUserProperty(username, fieldToRetrieve); return cardNumber; } catch (Exception ex) { Console.WriteLine(ex.StackTrace); return cardNumber; } }
/// <summary> /// Returns all of the existing card numbers from PaperCut. /// </summary> /// /// <returns> /// An array of strings that contains all of the existing card /// numbers for all of the users in PaperCut. /// </returns> internal static string[] GetCardNumbers(ServerCommandProxy serverProxy, string[] usernames, int cardFieldToRetrieve) { string[] cardNumbers = new string[usernames.Length]; string fieldToRetrieve = PaperCutProxyWrapper.ResolveCardField(cardFieldToRetrieve); try { Console.WriteLine("Retrieving existing card numbers..."); Console.WriteLine("########################################\n"); for (int i = 0; i < usernames.Length; i++) { string currentCard = serverProxy.GetUserProperty(usernames[i], fieldToRetrieve); cardNumbers[i] = currentCard.ToString(); if (i != 0 && i % 100 == 0) { Console.WriteLine("Retrieved {0} card numbers so far...", i); } } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } Console.WriteLine("\n########################################\r\n"); return cardNumbers; }
private void btnSyncDept_Click(object sender, RoutedEventArgs e) { ServerCommandProxy serverProxy; OperationTypeEnum operation = OperationTypeEnum.SyncDepartment; txtblockOutput.Inlines.Clear(); try { ValidationResult result = ValidateInput(operation); if (result.IsValid) { string ppcHostname = txtboxPpcServer.Text; string ppcAdminPw = pwdboxPpcAdminPw.Password; serverProxy = new ServerCommandProxy(ppcHostname, 9191, ppcAdminPw); if (PaperCutProxyWrapper.IsConnectionEstablished(serverProxy)) { txtblockOutput.Inlines.Add(Constants.PaperCut.Messages.PaperCutConnectionEstablished); string domainName = txtboxDomainName.Text; string ldapRoot = txtboxLdapRoot.Text; string domainUser = txtboxDomainUser.Text; string domainUserPwd = pwdboxDomainUserPwd.Password; LdapHelper ldapHelper = new LdapHelper(domainName, ldapRoot, domainUser, domainUserPwd); if (ldapHelper.IsConnectionEstablished(domainUser)) { txtblockOutput.Inlines.Add(Constants.Ldap.Messages.LdapConnectionEstablished); PaperCutUser[] ppcUsers = PaperCutProxyWrapper.GetUsersDepartmentInfo(serverProxy); int targetDeptField = cmboxTargetDeptField.SelectedIndex; string departmentNameADField = txtboxDeptNameADField.Text; string departmentNumberADField = txtboxDeptNumberADField.Text; bool areBothAdFieldsEntered = !string.IsNullOrEmpty(departmentNameADField) && !string.IsNullOrEmpty(departmentNumberADField); if (areBothAdFieldsEntered) { LdapUser[] ldapUsers = ldapHelper.RetrieveDepartmentInformation(ppcUsers, departmentNameADField, departmentNumberADField); PaperCutProxyWrapper.SetUsersMultipleDepartmentInfo(serverProxy, ldapUsers, targetDeptField); } else { LdapUser[] ldapUsers = ldapHelper.RetrieveDepartmentInformation(ppcUsers, string.IsNullOrEmpty(departmentNameADField) ? departmentNumberADField : departmentNameADField); PaperCutProxyWrapper.SetUsersSingleDepartmentInfo(serverProxy, ldapUsers, targetDeptField); } } else { txtblockOutput.Inlines.Add(Constants.Ldap.Messages.LdapConnectionNotEstablished); } } else { txtblockOutput.Inlines.Add(Constants.PaperCut.Messages.PaperCutConnectionNotEstablished); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
protected override void ProcessRecord() { base.ProcessRecord(); if (Globals.authToken != null) { _serverProxy = new ServerCommandProxy(Globals.ComputerName, Globals.Port, Globals.authToken); try { string[] propertyNames = new string[] { "full-name","email","disabled-print","balance","restricted","account-selection.mode","department","office","card-number","card-pin","notes" }; string[] pcutUserProperties = _serverProxy.GetUserProperties(UserName,propertyNames); PSObject returnPcutUserProperties = new PSObject(); returnPcutUserProperties.Properties.Add(new PSNoteProperty("Username",UserName)); int propertyCount = 0; foreach (string propertyName in propertyNames) { returnPcutUserProperties.Properties.Add(new PSNoteProperty(propertyName, pcutUserProperties[propertyCount])); propertyCount += 1; } WriteObject(returnPcutUserProperties); } catch (XmlRpcFaultException fex) { ErrorRecord errRecord = new ErrorRecord(new Exception(fex.Message, fex.InnerException), fex.FaultString, ErrorCategory.NotSpecified, fex); WriteError(errRecord); } } else { WriteObject("Please run Connect-PcutServer in order to establish connection."); } }
private void btnUpdate_Click(object sender, RoutedEventArgs e) { string ppcHostname; string ppcAdminPw; int targetIdField; bool updateOnlyIfBlank; int numberOfChars; bool mustTargetSpecificUsername; string specificUsername; bool inputIsValid = true; ServerCommandProxy serverProxy; txtblockOutput.Inlines.Clear(); ppcHostname = txtboxPpcServer.Text; if (String.IsNullOrEmpty(ppcHostname)) { txtblockOutput.Inlines.Add("You must enter the PaperCut server hostname.\r\n"); inputIsValid = false; } ppcAdminPw = pwdboxPpcAdminPw.Password; if (String.IsNullOrEmpty(ppcAdminPw)) { txtblockOutput.Inlines.Add("You must enter the PaperCut admin password.\r\n"); inputIsValid = false; } targetIdField = cmboxTargetIDField.SelectedIndex; updateOnlyIfBlank = chckboxUpdateIfBlank.IsChecked.Value; mustTargetSpecificUsername = chckboxTargetSpecificUser.IsChecked.Value; specificUsername = txtboxTargetSpecificUser.Text; try { numberOfChars = Int16.Parse(txtboxIDLength.Text.ToString()); if (numberOfChars < 4 || numberOfChars > 10) { txtblockOutput.Inlines.Add("You must enter a length between 4 and 10.\r\n"); inputIsValid = false; } else { inputIsValid = true; } } catch (Exception) { txtblockOutput.Inlines.Add("You must enter a length between 4 and 10.\r\n"); return; } if (inputIsValid) { try { serverProxy = new ServerCommandProxy(ppcHostname, 9191, ppcAdminPw); if (PaperCutProxyWrapper.ConnectionEstablished(serverProxy)) { PaperCutHelper ppcUtility = new PaperCutHelper(serverProxy); txtblockOutput.Inlines.Add("Connection to PaperCut established.\r\n"); int totalUsers = serverProxy.GetTotalUsers(); Console.WriteLine(String.Format("Retrieved {0} users from PaperCut.", totalUsers)); Console.WriteLine("########################################\r\n"); if (mustTargetSpecificUsername) { ppcUtility.UpdateSingleCardNumber(updateOnlyIfBlank, targetIdField, specificUsername, numberOfChars); } else { ppcUtility.UpdateAllCardNumbers(updateOnlyIfBlank, targetIdField, numberOfChars); } } } catch (Exception) { txtblockOutput.Inlines.Add("Connection to PaperCut cannot be established.\r\n"); } } }
protected override void ProcessRecord() { base.ProcessRecord(); if (Globals.authToken != null) { _serverProxy = new ServerCommandProxy(Globals.ComputerName, Globals.Port, Globals.authToken); try { string[] pcutUserSharedAccounts = _serverProxy.ListUserSharedAccounts(UserName, Offset, Limit); Collection<PSObject> returnPcutUserSharedAccounts = new Collection<PSObject>(); foreach (string pcutUserSharedAccount in pcutUserSharedAccounts) { PSObject thisSharedAccount = new PSObject(); thisSharedAccount.Properties.Add(new PSNoteProperty("Name", pcutUserSharedAccount)); returnPcutUserSharedAccounts.Add(thisSharedAccount); } WriteObject(returnPcutUserSharedAccounts); } catch (XmlRpcFaultException fex) { ErrorRecord errRecord = new ErrorRecord(new Exception(fex.Message, fex.InnerException), fex.FaultString, ErrorCategory.NotSpecified, fex); WriteError(errRecord); } } else { WriteObject("Please run Connect-PcutServer in order to establish connection."); } }
/// <summary> /// Sets the ID number for a specific user. /// </summary> internal static void SetCardNumber(ServerCommandProxy serverProxy, string username, string cardNumber, int cardFieldToUpdate) { string fieldToUpdate = PaperCutProxyWrapper.ResolveCardField(cardFieldToUpdate); try { serverProxy.SetUserProperty(username, fieldToUpdate, cardNumber); } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } }
public PaperCutHelper(ServerCommandProxy serverProxy) { _serverProxy = serverProxy; }
/// <summary> /// Returns all of the users in PaperCut, /// as well as their departmental information. /// </summary> /// /// <returns> /// An array of PpcUser that contains all the users /// in PaperCut and their department information. /// </returns> internal static PpcUser[] GetUsersDepartmentInfo(ServerCommandProxy serverProxy) { string[] allUsers = GetUserAccounts(serverProxy); PpcUser[] allUsersWithDeptInfo = new PpcUser[allUsers.Length]; string[] propertiesToFetch = { "department", "office" }; string[] retrievedProperties = new string[2]; Console.WriteLine("Retrieving department information \nfor {0} PaperCut users...", allUsers.Length); Console.WriteLine("########################################\r\n"); for (int i = 0; i < allUsersWithDeptInfo.Length; i++) { try { allUsersWithDeptInfo[i] = new PpcUser(); allUsersWithDeptInfo[i].Username = allUsers[i]; retrievedProperties = serverProxy.GetUserProperties(allUsersWithDeptInfo[i].Username, propertiesToFetch); allUsersWithDeptInfo[i].Department = retrievedProperties[0]; allUsersWithDeptInfo[i].Department = retrievedProperties[1]; if (i != 0 && i % 100 == 0) { Console.WriteLine("Retrieved {0} users so far...", i); } if (i == allUsersWithDeptInfo.Length - 1) { Console.WriteLine("\nRetrieved {0} users.", allUsersWithDeptInfo.Length); Console.WriteLine("########################################\r\n"); } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); } } return allUsersWithDeptInfo; }
private void btnSendEmail_Click(object sender, RoutedEventArgs e) { string ppcHostname; string ppcAdminPw; int targetIdField; bool mustTargetSpecificUsername; string specificUsername; string smtpHostname; string senderAddress; bool inputIsValid = true; ServerCommandProxy serverProxy; txtblockOutput.Inlines.Clear(); ppcHostname = txtboxPpcServer.Text; if (String.IsNullOrEmpty(ppcHostname)) { txtblockOutput.Inlines.Add("You must enter the PaperCut server hostname.\r\n"); inputIsValid = false; } ppcAdminPw = pwdboxPpcAdminPw.Password; if (String.IsNullOrEmpty(ppcAdminPw)) { txtblockOutput.Inlines.Add("You must enter the PaperCut admin password.\r\n"); inputIsValid = false; } smtpHostname = txtboxSmtpServer.Text; if (String.IsNullOrEmpty(smtpHostname)) { txtblockOutput.Inlines.Add("You must enter the SMTP server hostname.\r\n"); inputIsValid = false; } senderAddress = txtboxSenderAddress.Text; if (!IsValidEmail(senderAddress)) { txtblockOutput.Inlines.Add("You must enter a valid sender address.\r\n"); inputIsValid = false; return; } targetIdField = cmboxTargetIDField.SelectedIndex; mustTargetSpecificUsername = chckboxTargetSpecificUser.IsChecked.Value; specificUsername = txtboxTargetSpecificUser.Text; if (inputIsValid) { try { serverProxy = new ServerCommandProxy(ppcHostname, 9191, ppcAdminPw); if (PaperCutProxyWrapper.ConnectionEstablished(serverProxy)) { PaperCutHelper ppcUtility = new PaperCutHelper(serverProxy); txtblockOutput.Inlines.Add("Connection to PaperCut established.\r\n"); int totalUsers = serverProxy.GetTotalUsers(); Console.WriteLine(String.Format("Retrieved {0} users from PaperCut.", totalUsers)); Console.WriteLine("########################################\r\n"); if (mustTargetSpecificUsername) { ppcUtility.SendSingleEmail(targetIdField, specificUsername, smtpHostname, senderAddress); Console.WriteLine(String.Format("Notification email sent to {0}.", specificUsername)); Console.WriteLine("\n########################################\r\n"); } else { ppcUtility.SendMultipleEmails(targetIdField, smtpHostname, senderAddress); Console.WriteLine(String.Format("Notification emails sent to {0} users.", totalUsers)); Console.WriteLine("\n########################################\r\n"); } } } catch (Exception) { txtblockOutput.Inlines.Add("Connection to PaperCut cannot be established,\r\n" + "or cannot send email."); } } }
/// <summary> /// Returns all of the users in PaperCut. /// </summary> /// /// <returns> /// An array of strings that contains all of the usernames for all of the users in PaperCut. /// </returns> internal static string[] GetUserAccounts(ServerCommandProxy serverProxy) { string[] usersInBatch; string[] allUsers; int offset; int totalUsers; int remainingUsers; int maxUsersInBatch; int j; offset = 0; totalUsers = serverProxy.GetTotalUsers(); maxUsersInBatch = 1000; j = 0; allUsers = new string[totalUsers]; remainingUsers = totalUsers; if (totalUsers <= 1000) { allUsers = serverProxy.ListUserAccounts(offset, totalUsers); return allUsers; } else { do { usersInBatch = serverProxy.ListUserAccounts(offset, maxUsersInBatch); foreach (string user in usersInBatch) { allUsers[j] = user.ToString(); j++; } remainingUsers = remainingUsers - usersInBatch.Length; offset += maxUsersInBatch; if (remainingUsers >= 1000) { maxUsersInBatch = 1000; } else { maxUsersInBatch = remainingUsers; } } while (remainingUsers > 0); } return allUsers; }
private void btnSendEmail_Click(object sender, RoutedEventArgs e) { ServerCommandProxy serverProxy = null; OperationTypeEnum operation = OperationTypeEnum.SendEmail; txtblockOutput.Inlines.Clear(); try { ValidationResult result = ValidateInput(operation); if (result.IsValid) { string ppcHostname = txtboxPpcServer.Text; string ppcAdminPw = pwdboxPpcAdminPw.Password; serverProxy = new ServerCommandProxy(ppcHostname, 9191, ppcAdminPw); if (PaperCutProxyWrapper.IsConnectionEstablished(serverProxy)) { txtblockOutput.Inlines.Add(Constants.PaperCut.Messages.PaperCutConnectionEstablished); int totalUsers = serverProxy.GetTotalUsers(); Console.WriteLine(string.Format(Constants.PaperCut.Messages.NumberOfUsersRetrievedFromPaperCut, totalUsers)); Console.WriteLine(Constants.ConsoleSpacing.HashesWithNewLine); PaperCutHelper paperCutHelper = new PaperCutHelper(serverProxy); if (paperCutHelper != null) { int targetIdField = cmboxTargetIDField.SelectedIndex; string smtpHostname = txtboxSmtpServer.Text; string senderAddress = txtboxSenderAddress.Text; bool mustTargetSpecificUsername = chckboxTargetSpecificUser.IsChecked.Value; if (mustTargetSpecificUsername) { string specificUsername = txtboxTargetSpecificUser.Text; paperCutHelper.SendSingleEmail(smtpHostname, specificUsername, senderAddress, targetIdField); Console.WriteLine(string.Format(Constants.Smtp.Messages.NotificationEmailSentToSingleUser, specificUsername)); Console.WriteLine(Constants.ConsoleSpacing.HashesWithNewLine); } else { paperCutHelper.SendEmailsToAllUsers(smtpHostname, senderAddress, targetIdField); Console.WriteLine(string.Format(Constants.Smtp.Messages.NotificationEmailsSentToMultipleUsers, totalUsers)); Console.WriteLine(Constants.ConsoleSpacing.HashesWithNewLine); } } } else { txtblockOutput.Inlines.Add(Constants.PaperCut.Messages.PaperCutConnectionNotEstablished); } } } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(Constants.Smtp.Messages.UnableToSendEmail); } }
/// <summary> /// Checks if the provided ServerCommandProxy instance is valid. /// </summary> /// /// <returns> /// True if a connection has been successfully established. /// </returns> internal static bool ConnectionEstablished(ServerCommandProxy serverProxy) { if (serverProxy.GetTotalUsers() != 0) { return true; } else { return false; } }