/doc/todo/fileupload/

y'/>
summaryrefslogtreecommitdiff
path: root/import_members.pl
blob: a6aec9de188c521edfd9df6bf1c31f7e5bfee281 (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 = ?,