summaryrefslogtreecommitdiff
path: root/import_members.pl
blob: 1cd70d54690bda6cf3da0fa22d224a3e9b27e433 (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 "<strong>This script cannot be executed via http. You must run it via the command line.</strong>\n";
  35. print "</html>\n";
  36. exit;
  37. }
  38. my $membersfile = $ARGV[0];
  39. if (length($membersfile) < 2){
  40. print "\nUsage: import_members.pl path/to/members\n\n";
  41. print "You must supply the path to the members file. Default location\n";
  42. print "is users/members. In this case do this:\n\n";
  43. print " ./import_members.pl users/members\n\n";
  44. exit;
  45. }
  46. my @users = ();
  47. open(FH, "$membersfile") || die ("Couldn't open members file!");
  48. while (<FH>) {
  49. chop;
  50. if (/^\[.*\]/) {
  51. $login = $_;
  52. $login =~ s/(\[|\])//g;
  53. if($login eq 'admin'){
  54. print "\nIMPORT FAILED: User 'admin' was found.\n\n";
  55. print "Please change this user's name to something else. In LedgerSMB version 1.2, \n";
  56. print "'admin' is a reserved user for the administration of the entire system.\n";
  57. print "To change the user's name, find the line in the members file that looks \n";
  58. print "like [admin] and change 'admin' to something else (keep the '[' and ']').\n";
  59. print "Save the file and run this script again.\n\n";
  60. exit;
  61. } elsif($login ne 'root login'){
  62. push @users, $login;
  63. $member{$login}{'login'} = $login;
  64. }
  65. next;
  66. }
  67. if($login ne 'root login'){
  68. if( ($key, $value) = split /=/, $_, 2){
  69. if($key eq 'dbpasswd'){
  70. $member{$login}{$key} = unpack 'u', $value;
  71. } elsif($key eq 'password') {
  72. $member{$login}{'crypted_password'} = $value;
  73. } else {
  74. $member{$login}{$key} = $value;
  75. }
  76. }
  77. }
  78. }
  79. close(FH);
  80. print "\n\nParsing members file completed. Now trying to import user data.\n\n";
  81. foreach (@users) {
  82. $myUser = $member{$_};
  83. &save_member($myUser);
  84. print "Import of user '$_' seems to have succeeded.\n";
  85. }
  86. print "\nSUCCESS! It seems that every user in the members file was imported!\n\n";
  87. sub save_member {
  88. # a slightly modified version of LegerSBM::User::save_member
  89. # with special handling of the password -> crypted_password
  90. my ($self) = @_;
  91. # replace \r\n with \n
  92. for (qw(address signature)) { $self->{$_} =~ s/\r?\n/\\n/g }
  93. # use central db
  94. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  95. #check to see if the user exists already
  96. my $userCheck = $dbh->prepare("SELECT id FROM users WHERE username = ?");
  97. $userCheck->execute($self->{login});
  98. my ($userID) = $userCheck->fetchrow_array;
  99. if($userID){
  100. #got an id, check to see if it's in the users_conf table
  101. my $userConfCheck = $dbh->prepare("SELECT count(*) FROM users_conf WHERE id = ?");
  102. $userConfCheck->execute($userID);
  103. ($userConfExists) = $userConfCheck->fetchrow_array;
  104. }
  105. else{
  106. my $userConfAdd = $dbh->prepare("SELECT create_user(?);");
  107. $userConfAdd->execute($self->{login});
  108. ($userID) = $userConfAdd->fetchrow_array;
  109. }
  110. if($userConfExists){
  111. my $userConfUpdate = $dbh->prepare("UPDATE users_conf
  112. SET acs = ?, address = ?, businessnumber = ?,
  113. company = ?, countrycode = ?, currency = ?,
  114. dateformat = ?, dbdriver = ?,
  115. dbhost = ?, dbname = ?, dboptions = ?,
  116. dbpasswd = ?, dbport = ?, dbuser = ?,
  117. email = ?, fax = ?, menuwidth = ?,
  118. name = ?, numberformat = ?, crypted_password = ?,
  119. print = ?, printer = ?, role = ?,
  120. sid = ?, signature = ?, stylesheet = ?,
  121. tel = ?, templates = ?, timeout = ?,
  122. vclimit = ?
  123. WHERE id = ?;");
  124. $userConfUpdate->execute($self->{acs}, $self->{address}, $self->{businessnumber},
  125. $self->{company}, $self->{countrycode}, $self->{currency},
  126. $self->{dateformat}, $self->{dbdriver},
  127. $self->{dbhost}, $self->{dbname}, $self->{dboptions},
  128. $self->{dbpasswd}, $self->{dbport}, $self->{dbuser},
  129. $self->{email}, $self->{fax}, $self->{menuwidth},
  130. $self->{name}, $self->{numberformat}, $self->{crypted_password},
  131. $self->{print}, $self->{printer}, $self->{role},
  132. $self->{sid}, $self->{signature}, $self->{stylesheet},
  133. $self->{tel}, $self->{templates}, $self->{timeout},
  134. $self->{vclimit}, $userID);
  135. }
  136. else{
  137. my $userConfInsert = $dbh->prepare("INSERT INTO users_conf(acs, address, businessnumber,
  138. company, countrycode, currency,
  139. dateformat, dbdriver,
  140. dbhost, dbname, dboptions, dbpasswd,
  141. dbport, dbuser, email, fax, menuwidth,
  142. name, numberformat, print, printer, role,
  143. sid, signature, stylesheet, tel, templates,
  144. timeout, vclimit, id, crypted_password)
  145. VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
  146. ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
  147. ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
  148. $userConfInsert->execute($self->{acs}, $self->{address}, $self->{businessnumber},
  149. $self->{company}, $self->{countrycode}, $self->{currency},
  150. $self->{dateformat}, $self->{dbdriver},
  151. $self->{dbhost}, $self->{dbname}, $self->{dboptions},
  152. $self->{dbpasswd}, $self->{dbport}, $self->{dbuser},
  153. $self->{email}, $self->{fax}, $self->{menuwidth},
  154. $self->{name}, $self->{numberformat},
  155. $self->{print}, $self->{printer}, $self->{role},
  156. $self->{sid}, $self->{signature}, $self->{stylesheet},
  157. $self->{tel}, $self->{templates}, $self->{timeout},
  158. $self->{vclimit}, $userID, $self->{crypted_password});
  159. }
  160. if (! $self->{'admin'}) {
  161. $self->{dbpasswd} =~ s/\\'/'/g;
  162. $self->{dbpasswd} =~ s/\\\\/\\/g;
  163. # format dbconnect and dboptions string
  164. LedgerSMB::User::dbconnect_vars($self, $self->{dbname});
  165. # check if login is in database
  166. my $dbh = DBI->connect(
  167. $self->{dbconnect}, $self->{dbuser}, $self->{dbpasswd},
  168. {AutoCommit => 0})
  169. or $self->error($DBI::errstr);
  170. # add login to employee table if it does not exist
  171. my $login = $self->{login};
  172. $login =~ s/@.*//;
  173. my $sth = $dbh->prepare("SELECT id FROM employee WHERE login = ?;");
  174. $sth->execute($login);
  175. my ($id) = $sth->fetchrow_array;
  176. $sth->finish;
  177. my $employeenumber;
  178. my @values;
  179. if ($id) {
  180. $query = qq|UPDATE employee SET
  181. role = ?,
  182. email = ?,
  183. name = ?
  184. WHERE login = ?|;
  185. @values = ($self->{role}, $self->{email}, $self->{name}, $login);
  186. } else {
  187. my ($employeenumber) = Form::update_defaults(
  188. "", \%$self, "employeenumber", $dbh);
  189. $query = qq|
  190. INSERT INTO employee
  191. (login, employeenumber, name,
  192. workphone, role, email, sales)
  193. VALUES (?, ?, ?, ?, ?, ?, '1')|;
  194. @values = ($login, $employeenumber, $self->{name}, $self->{tel},
  195. $self->{role}, $self->{email})
  196. }
  197. $sth = $dbh->prepare($query);
  198. $sth->execute(@values);
  199. $dbh->commit;
  200. $dbh->disconnect;
  201. }
  202. }