summaryrefslogtreecommitdiff
path: root/import_members.pl
blob: 223c06720b41146fe937cf0b0e9ab6d4ccbe0967 (plain)
  1. #!/usr/bin/perl
  2. #####################################################################
  3. # Data import script to migrate from version 1.1's flat file based
  4. # users to version 1.2's central database.
  5. #
  6. # NOTE: This script will die if the user it tries to import is
  7. # tied to a bogus or non-existing dataset/database. The fix
  8. # is to edit the members file and either manually set the right
  9. # database configuration for that user, or delete the user entirely.
  10. #
  11. # If the script makes it through all users it will print:
  12. #
  13. # 'SUCCESS! It seems that every user in the members file was imported!'
  14. #
  15. # If you don't get that output at the end of the script, something
  16. # went wrong in the import.
  17. #
  18. # For help/troubleshooting please join the LedgerSMB users mailing
  19. # list. Info on how to subscribe can be found here:
  20. #
  21. # http://lists.sourceforge.net/mailman/listinfo/ledger-smb-users
  22. #
  23. # Other info on how to get help, including commercial support
  24. # can be found at:
  25. #
  26. # http://www.ledgersmb.org/help/
  27. #
  28. use LedgerSMB::User;
  29. use LedgerSMB::Form;
  30. use LedgerSMB::Sysconfig;
  31. if ( $ENV{HTTP_HOST} ) {
  32. print "Content-type: text/html\n\n";
  33. print "<html>\n";
  34. print
  35. "<strong>This script cannot be executed via http. You must run it via the command line.</strong>\n";
  36. print "</html>\n";
  37. exit;
  38. }
  39. my $membersfile = $ARGV[0];
  40. if ( length($membersfile) < 2 ) {
  41. print "\nUsage: import_members.pl path/to/members\n\n";
  42. print "You must supply the path to the members file. Default location\n";
  43. print "is users/members. In this case do this:\n\n";
  44. print " ./import_members.pl users/members\n\n";
  45. exit;
  46. }
  47. my @users = ();
  48. open( FH, '<', "$membersfile" ) || die("Couldn't open members file!");
  49. while (<FH>) {
  50. chop;
  51. if (/^\[.*\]/) {
  52. $login = $_;
  53. $login =~ s/(\[|\])//g;
  54. if ( $login eq 'admin' ) {
  55. print "\nIMPORT FAILED: User 'admin' was found.\n\n";
  56. print
  57. "Please change this user's name to something else. In LedgerSMB version 1.2, \n";
  58. print
  59. "'admin' is a reserved user for the administration of the entire system.\n";
  60. print
  61. "To change the user's name, find the line in the members file that looks \n";
  62. print
  63. "like [admin] and change 'admin' to something else (keep the '[' and ']').\n";
  64. print "Save the file and run this script again.\n\n";
  65. exit;
  66. }
  67. elsif ( $login ne 'root login' ) {
  68. push @users, $login;
  69. $member{$login}{'login'} = $login;
  70. }
  71. next;
  72. }
  73. if ( $login ne 'root login' ) {
  74. if ( ( $key, $value ) = split /=/, $_, 2 ) {
  75. if ( $key eq 'dbpasswd' ) {
  76. $member{$login}{$key} = unpack 'u', $value;
  77. }
  78. elsif ( $key eq 'password' ) {
  79. $member{$login}{'crypted_password'} = $value;
  80. }
  81. else {
  82. $member{$login}{$key} = $value;
  83. }
  84. }
  85. }
  86. }
  87. close(FH);
  88. print "\n\nParsing members file completed. Now trying to import user data.\n\n";
  89. foreach (@users) {
  90. $myUser = $member{$_};
  91. &save_member($myUser);
  92. ${LedgerSMB::Sysconfig::GLOBALDBH}->commit;
  93. print "Import of user '$_' seems to have succeeded.\n";
  94. }
  95. print
  96. "\nSUCCESS! It seems that every user in the members file was imported!\n\n";
  97. sub save_member {
  98. # a slightly modified version of LegerSBM::User::save_member
  99. # with special handling of the password -> crypted_password
  100. my ($self) = @_;
  101. # replace \r\n with \n
  102. for (qw(address signature)) { $self->{$_} =~ s/\r?\n/\\n/g }
  103. # use central db
  104. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  105. #check to see if the user exists already
  106. my $userCheck = $dbh->prepare("SELECT id FROM users WHERE username = ?");
  107. $userCheck->execute( $self->{login} );
  108. my ($userID) = $userCheck->fetchrow_array;
  109. if ($userID) {
  110. #got an id, check to see if it's in the users_conf table
  111. my $userConfCheck =
  112. $dbh->prepare("SELECT count(*) FROM users_conf WHERE id = ?");
  113. $userConfCheck->execute($userID);
  114. ($userConfExists) = $userConfCheck->fetchrow_array;
  115. }
  116. else {
  117. my $userConfAdd = $dbh->prepare("SELECT create_user(?);");
  118. $userConfAdd->execute( $self->{login} );
  119. ($userID) = $userConfAdd->fetchrow_array;
  120. }
  121. if ($userConfExists) {
  122. my $userConfUpdate = $dbh->prepare(
  123. "UPDATE users_conf
  124. SET acs = ?, address = ?, businessnumber = ?,
  125. company = ?, countrycode = ?, currency = ?,
  126. dateformat = ?, dbdriver = ?,
  127. dbhost = ?, dbname = ?, dboptions = ?,
  128. dbpasswd = ?, dbport = ?, dbuser = ?,
  129. email = ?, fax = ?, menuwidth = ?,
  130. name = ?, numberformat = ?, crypted_password = ?,
  131. print = ?, printer = ?, role = ?,
  132. sid = ?, signature = ?, stylesheet = ?,
  133. tel = ?, templates = ?, timeout = ?,
  134. vclimit = ?
  135. WHERE id = ?;"
  136. );
  137. $userConfUpdate->execute(
  138. $self->{acs}, $self->{address},
  139. $self->{businessnumber}, $self->{company},
  140. $self->{countrycode}, $self->{currency},
  141. $self->{dateformat}, $self->{dbdriver},
  142. $self->{dbhost}, $self->{dbname},
  143. $self->{dboptions}, $self->{dbpasswd},
  144. $self->{dbport}, $self->{dbuser},
  145. $self->{email}, $self->{fax},
  146. $self->{menuwidth}, $self->{name},
  147. $self->{numberformat}, $self->{crypted_password},
  148. $self->{print}, $self->{printer},
  149. $self->{role}, $self->{sid},
  150. $self->{signature}, $self->{stylesheet},
  151. $self->{tel}, $self->{templates},
  152. $self->{timeout}, $self->{vclimit},
  153. $userID
  154. );
  155. }
  156. else {
  157. my $userConfInsert = $dbh->prepare(
  158. "INSERT INTO users_conf(acs, address, businessnumber,
  159. company, countrycode, currency,
  160. dateformat, dbdriver,
  161. dbhost, dbname, dboptions, dbpasswd,
  162. dbport, dbuser, email, fax, menuwidth,
  163. name, numberformat, print, printer, role,
  164. sid, signature, stylesheet, tel, templates,
  165. timeout, vclimit, id, crypted_password)
  166. VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
  167. ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
  168. ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"
  169. );
  170. $userConfInsert->execute(
  171. $self->{acs}, $self->{address},
  172. $self->{businessnumber}, $self->{company},
  173. $self->{countrycode}, $self->{currency},
  174. $self->{dateformat}, $self->{dbdriver},
  175. $self->{dbhost}, $self->{dbname},
  176. $self->{dboptions}, $self->{dbpasswd},
  177. $self->{dbport}, $self->{dbuser},
  178. $self->{email}, $self->{fax},
  179. $self->{menuwidth}, $self->{name},
  180. $self->{numberformat}, $self->{print},
  181. $self->{printer}, $self->{role},
  182. $self->{sid}, $self->{signature},
  183. $self->{stylesheet}, $self->{tel},
  184. $self->{templates}, $self->{timeout},
  185. $self->{vclimit}, $userID,
  186. $self->{crypted_password}
  187. );
  188. }
  189. $dbh->commit;
  190. }