summaryrefslogtreecommitdiff
path: root/import_members.pl
blob: ef8b27cb607ebffc76af6416ea90bc0ad2fdff6f (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 id FROM users_conf WHERE id = ?");
  102. $userConfCheck->execute($userID);
  103. if($userConfCheck->rows){
  104. my $userConfExists = 1;
  105. }
  106. }
  107. else{
  108. my $userConfAdd = $dbh->prepare("SELECT create_user(?);");
  109. $userConfAdd->execute($self->{login});
  110. ($userID) = $userConfAdd->fetchrow_array;
  111. }
  112. if($userConfExists){
  113. my $userConfUpdate = $dbh->prepare("UPDATE users_conf
  114. SET acs = ?, address = ?, businessnumber = ?,
  115. company = ?, countrycode = ?, currency = ?,
  116. dateformat = ?, dbdriver = ?,
  117. dbhost = ?, dbname = ?, dboptions = ?,
  118. dbpasswd = ?, dbport = ?, dbuser = ?,
  119. email = ?, fax = ?, menuwidth = ?,
  120. name = ?, numberformat = ?, crypted_password = ?,
  121. print = ?, printer = ?, role = ?,
  122. sid = ?, signature = ?, stylesheet = ?,
  123. tel = ?, templates = ?, timeout = ?,
  124. vclimit = ?
  125. WHERE id = ?;");
  126. $userConfUpdate->execute($self->{acs}, $self->{address}, $self->{businessnumber},
  127. $self->{company}, $self->{countrycode}, $self->{currency},
  128. $self->{dateformat}, $self->{dbdriver},
  129. $self->{dbhost}, $self->{dbname}, $self->{dboptions},
  130. $self->{dbpasswd}, $self->{dbport}, $self->{dbuser},
  131. $self->{email}, $self->{fax}, $self->{menuwidth},
  132. $self->{name}, $self->{numberformat}, $self->{crypted_password},
  133. $self->{print}, $self->{printer}, $self->{role},
  134. $self->{sid}, $self->{signature}, $self->{stylesheet},
  135. $self->{tel}, $self->{templates}, $self->{timeout},
  136. $self->{vclimit}, $userID);
  137. }
  138. else{
  139. my $userConfInsert = $dbh->prepare("INSERT INTO users_conf(acs, address, businessnumber,
  140. company, countrycode, currency,
  141. dateformat, dbdriver,
  142. dbhost, dbname, dboptions, dbpasswd,
  143. dbport, dbuser, email, fax, menuwidth,
  144. name, numberformat, print, printer, role,
  145. sid, signature, stylesheet, tel, templates,
  146. timeout, vclimit, id, crypted_password)
  147. VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
  148. ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
  149. ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
  150. $userConfInsert->execute($self->{acs}, $self->{address}, $self->{businessnumber},
  151. $self->{company}, $self->{countrycode}, $self->{currency},
  152. $self->{dateformat}, $self->{dbdriver},
  153. $self->{dbhost}, $self->{dbname}, $self->{dboptions},
  154. $self->{dbpasswd}, $self->{dbport}, $self->{dbuser},
  155. $self->{email}, $self->{fax}, $self->{menuwidth},
  156. $self->{name}, $self->{numberformat},
  157. $self->{print}, $self->{printer}, $self->{role},
  158. $self->{sid}, $self->{signature}, $self->{stylesheet},
  159. $self->{tel}, $self->{templates}, $self->{timeout},
  160. $self->{vclimit}, $userID, $self->{crypted_password});
  161. }
  162. if (! $self->{'admin'}) {
  163. $self->{dbpasswd} =~ s/\\'/'/g;
  164. $self->{dbpasswd} =~ s/\\\\/\\/g;
  165. # format dbconnect and dboptions string
  166. LedgerSMB::User::dbconnect_vars($self, $self->{dbname});
  167. # check if login is in database
  168. my $dbh = DBI->connect(
  169. $self->{dbconnect}, $self->{dbuser}, $self->{dbpasswd},
  170. {AutoCommit => 0})
  171. or $self->error($DBI::errstr);
  172. # add login to employee table if it does not exist
  173. my $login = $self->{login};
  174. $login =~ s/@.*//;
  175. my $query = qq|SELECT id FROM employee WHERE login = '$login'|;
  176. my $sth = $dbh->prepare($query);
  177. $sth->execute;
  178. my ($id) = $sth->fetchrow_array;
  179. $sth->finish;
  180. my $employeenumber;
  181. my @values;
  182. if ($id) {
  183. $query = qq|UPDATE employee SET
  184. role = ?,
  185. email = ?,
  186. name = ?
  187. WHERE login = ?|;
  188. @values = ($self->{role}, $self->{email}, $self->{name}, $login);
  189. } else {
  190. my ($employeenumber) = Form::update_defaults(
  191. "", \%$self, "employeenumber", $dbh);
  192. $query = qq|
  193. INSERT INTO employee
  194. (login, employeenumber, name,
  195. workphone, role, email, sales)
  196. VALUES (?, ?, ?, ?, ?, ?, '1')|;
  197. @values = ($login, $employeenumber, $self->{name}, $self->{tel},
  198. $self->{role}, $self->{email})
  199. }
  200. $sth = $dbh->prepare($query);
  201. $sth->execute(@values);
  202. $dbh->commit;
  203. $dbh->disconnect;
  204. }
  205. }