Add new Rochade User


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. import de.rochade.srap.Connection;
  2. import de.rochade.srap.ResultSet;
  3. import de.rochade.srap.ResultSetFactory;
  4. import de.rochade.srap.Rochade;
  5. import de.rochade.srap.SrapException;
  6. import de.rochade.srap.Startup;
  7. import de.rochade.srap.UserAdmin;
  8.  
  9. public class AddUser {
  10.  
  11. public static void main(String[] args){
  12.  
  13. Connection connection = null;
  14. try {
  15.  
  16. // Connect to the Rochade Server
  17. Rochade rochade = Startup.getInstance(null, null, null, null, null, null);
  18. connection = rochade.getConnection("localhost", 8888, "ROADM", "", null);
  19.  
  20. // Request the User Administration object for the selected database
  21. UserAdmin userAdmin = connection.getUserAdmin("AP-DATA");
  22.  
  23. // Get the authentication settings from an existing template
  24. // using the select method.
  25.  
  26. // Setup the select statement parameters
  27. String what = "authentication, operPriv, userPriv";
  28. ResultSet from = ResultSetFactory.getResultSet("type, name", null, null);
  29. ResultSet where = null;
  30.  
  31. from.add();
  32. from.updateString("type", "template"); // Identify the type as template
  33. from.updateString("name", "ROCHUSER"); // The name of the template to get
  34.  
  35. // Request the template using a select call
  36. ResultSet rs = userAdmin.select(what, from, where);
  37.  
  38. // move to the first row and get the authentication values and operator
  39. // and user privileges
  40. rs.first();
  41. ResultSet authentication = rs.getResultSet("authentication");
  42. String operPriv = rs.getString("operPriv");
  43. String userPriv = rs.getString("userPriv");
  44.  
  45. // Create a new user in the selected database with the create method.
  46.  
  47. ResultSet rsUser = ResultSetFactory.getResultSet(
  48. "type, userID, shortDesc, authentication, operPriv, userPriv",
  49. null, null);
  50. rsUser.add();
  51. rsUser.updateString("type", "user"); // Identify the type as user
  52. rsUser.updateString("userID", "JOEUSER"); // User ID
  53. rsUser.updateString("shortDesc",
  54. "Joe is a Rochade user, not a plumber"); // User description
  55.  
  56. // Add an additional column to the authentication parameters
  57. // for the users initial password.
  58. authentication.appendColumn("pwd", "String");
  59. authentication.first();
  60. authentication.updateString("pwd", "password");
  61.  
  62. // set the authentication settings and the user and operator
  63. // privileges for the new user.
  64. rsUser.updateResultSet("authentication", authentication);
  65. rsUser.updateString("operPriv", operPriv);
  66. rsUser.updateString("userPriv", userPriv);
  67.  
  68. // Create the user using the create method.
  69. rs = userAdmin.create(rsUser);
  70.  
  71. // Check the results for success.
  72. rs.first();
  73. if (!rs.getBoolean("success")){
  74. System.out.println(rs.getString("message"));
  75. System.out.println(rs.getString("exception"));
  76. }
  77. }
  78. catch (SrapException e) {
  79. e.printStackTrace();
  80. }
  81. finally {
  82. if (connection != null) {
  83. connection.dispose();
  84. }
  85. }
  86. }
  87.  

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.