summaryrefslogtreecommitdiff
path: root/import_members.pl
blob: ba135d25e4a9dbace3d3565cda7059da4523ba3b (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. print "Import of user '$_' seems to have succeeded.\n";
  93. }
  94. print
  95. "\nSUCCESS! It seems that every user in the members file was imported!\n\n";
  96. sub save_member {
  97. # a slightly modified version of LegerSBM::User::save_member
  98. # with special handling of the password -> crypted_password
  99. my ($self) = @_;
  100. # replace \r\n with \n
  101. for (qw(address signature)) { $self->{$_} =~ s/\r?\n/\\n/g }
  102. # use central db
  103. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  104. #check to see if the user exists already
  105. my $userCheck = $dbh->prepare("SELECT id FROM users WHERE username = ?");
  106. $userCheck->execute( $self->{login} );
  107. my ($userID) = $userCheck->fetchrow_array;
  108. if ($userID) {
  109. #got an id, check to see if it's in the users_conf table
  110. my $userConfCheck =
  111. $dbh->prepare("SELECT count(*) FROM users_conf WHERE id = ?");
  112. $userConfCheck->execute($userID);
  113. ($userConfExists) = $userConfCheck->fetchrow_array;
  114. }
  115. else {
  116. my $userConfAdd = $dbh->prepare("SELECT create_user(?);");
  117. $userConfAdd->execute( $self->{login} );
  118. ($userID) = $userConfAdd->fetchrow_array;
  119. }
  120. if ($userConfExists) {
  121. my $userConfUpdate = $dbh->prepare(
  122. "UPDATE users_conf
  123. SET acs = ?, address = ?, businessnumber = ?,
  124. company = ?, countrycode = ?, currency = ?,
  125. dateformat = ?, dbdriver = ?,
  126. dbhost = ?, dbname = ?, dboptions = ?,
  127. dbpasswd = ?, dbport = ?, dbuser = ?,
  128. email = ?, fax = ?, menuwidth = ?,
  129. name = ?, numberformat = ?, crypted_password = ?,
  130. print = ?, printer = ?, role = ?,
  131. sid = ?, signature = ?, stylesheet = ?,
  132. tel = ?, templates = ?, timeout = ?,
  133. vclimit = ?
  134. WHERE id = ?;"
  135. );
  136. $userConfUpdate->execute(
  137. $self->{acs}, $self->{address},
  138. $self->{businessnumber}, $self->{company},
  139. $self->{countrycode}, $self->{currency},
  140. $self->{dateformat}, $self->{dbdriver},
  141. $self->{dbhost}, $self->{dbname},
  142. $self->{dboptions}, $self->{dbpasswd},
  143. $self->{dbport}, $self->{dbuser},
  144. $self->{email}, $self->{fax},
  145. $self->{menuwidth}, $self->{name},
  146. $self->{numberformat}, $self->{crypted_password},
  147. $self->{print}, $self->{printer},
  148. $self->{role}, $self->{sid},
  149. $self->{signature}, $self->{stylesheet},
  150. $self->{tel}, $self->{templates},
  151. $self->{timeout}, $self->{vclimit},
  152. $userID
  153. );
  154. }
  155. else {
  156. my $userConfInsert = $dbh->prepare(
  157. "INSERT INTO users_conf(acs, address, businessnumber,
  158. company, countrycode, currency,
  159. dateformat, dbdriver,
  160. dbhost, dbname, dboptions, dbpasswd,
  161. dbport, dbuser, email, fax, menuwidth,
  162. name, numberformat, print, printer, role,
  163. sid, signature, stylesheet, tel, templates,
  164. timeout, vclimit, id, crypted_password)
  165. VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
  166. ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
  167. ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"
  168. );
  169. $userConfInsert->execute(
  170. $self->{acs}, $self->{address},
  171. $self->{businessnumber}, $self->{company},
  172. $self->{countrycode}, $self->{currency},
  173. $self->{dateformat}, $self->{dbdriver},
  174. $self->{dbhost}, $self->{dbname},
  175. $self->{dboptions}, $self->{dbpasswd},
  176. $self->{dbport}, $self->{dbuser},
  177. $self->{email}, $self->{fax},
  178. $self->{menuwidth}, $self->{name},
  179. $self->{numberformat}, $self->{print},
  180. $self->{printer}, $self->{role},
  181. $self->{sid}, $self->{signature},
  182. $self->{stylesheet}, $self->{tel},
  183. $self->{templates}, $self->{timeout},
  184. $self->{vclimit}, $userID,
  185. $self->{crypted_password}
  186. );
  187. }
  188. if ( !$self->{'admin'} ) {
  189. $self->{dbpasswd} =~ s/\\'/'/g;
  190. $self->{dbpasswd} =~ s/\\\\/\\/g;
  191. # format dbconnect and dboptions string
  192. LedgerSMB::User::dbconnect_vars( $self, $self->{dbname} );
  193. # check if login is in database
  194. my $dbh = DBI->connect(
  195. $self->{dbconnect}, $self->{dbuser},
  196. $self->{dbpasswd}, { AutoCommit => 0 }
  197. ) or $self->error($DBI::errstr);
  198. # add login to employee table if it does not exist
  199. my $login = $self->{login};
  200. $login =~ s/@.*//;
  201. my $sth = $dbh->prepare("SELECT id FROM employee WHERE login = ?;");
  202. $sth->execute($login);
  203. my ($id) = $sth->fetchrow_array;
  204. $sth->finish;
  205. my $employeenumber;
  206. my @values;
  207. if ($id) {
  208. $query = qq|UPDATE employee SET
  209. role = ?,
  210. email = ?,
  211. name = ?
  212. WHERE login = ?|;
  213. @values = ( $self->{role}, $self->{email}, $self->{name}, $login );
  214. }
  215. else {
  216. my ($employeenumber) =
  217. Form::update_defaults( "", \%$self, "employeenumber", $dbh );
  218. $query = qq|
  219. INSERT INTO employee
  220. (login, employeenumber, name,
  221. workphone, role, email, sales)
  222. VALUES (?, ?, ?, ?, ?, ?, '1')|;
  223. @values = (
  224. $login, $employeenumber, $self->{name},
  225. $self->{tel}, $self->{role}, $self->{email}
  226. );
  227. }
  228. $sth = $dbh->prepare($query);
  229. $sth->execute(@values);
  230. $dbh->commit;
  231. $dbh->disconnect;
  232. }
  233. }