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