source: main/tags/2.83/gsdl/bin/script/csv-usernames-to-db.pl@ 24114

Last change on this file since 24114 was 20729, checked in by kjdon, 15 years ago

uncommented alreadyencrypted option, as it appears to be implemented. added groups to the line in csv, ie now username,password,groups,comment.

  • Property svn:executable set to *
File size: 6.4 KB
Line 
1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# csv-usernames-to-db.pl --
6# A component of the Greenstone digital library software
7# from the New Zealand Digital Library Project at the
8# University of Waikato, New Zealand.
9#
10# Copyright (C) 1999 New Zealand Digital Library Project
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25#
26###########################################################################
27
28
29# This program converts username details (password, group information etc)
30# into the format used by Greenstone, and store them in etc/users.gdb
31
32package cu2db;
33
34BEGIN {
35 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
36 die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
37 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
38 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
39 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan/perl-5.8");
40
41 if (defined $ENV{'GSDLEXTS'}) {
42 my @extensions = split(/:/,$ENV{'GSDLEXTS'});
43 foreach my $e (@extensions) {
44 my $ext_prefix = "$ENV{'GSDLHOME'}/ext/$e";
45
46 unshift (@INC, "$ext_prefix/perllib");
47 unshift (@INC, "$ext_prefix/perllib/cpan");
48 }
49 }
50}
51
52use strict;
53no strict 'refs'; # allow filehandles to be variables and vice versa
54no strict 'subs'; # allow barewords (eg STDERR) as function arguments
55
56use FileHandle;
57use util;
58use gsprintf 'gsprintf';
59use printusage;
60use parse2;
61
62
63my $arguments =
64 [ { 'name' => "fieldseparator",
65 'desc' => "{cu2db.field-separator}",
66 'type' => "string",
67 'deft' => ",",
68 'reqd' => "no" },
69 { 'name' => "alreadyencrypted",
70 'desc' => "{cu2db.already-encrypted}",
71 'type' => "flag",
72 'reqd' => "no" },
73 { 'name' => "verbosity",
74 'desc' => "{scripts.verbosity}",
75 'type' => "int",
76 'range' => "0,",
77 'deft' => "1",
78 'reqd' => "no" },
79 { 'name' => "language",
80 'desc' => "{scripts.language}",
81 'type' => "string",
82 'reqd' => "no",
83 'hiddengli' => "yes" },
84 { 'name' => "out",
85 'desc' => "{scripts.out}",
86 'type' => "string",
87 'deft' => "STDERR",
88 'reqd' => "no",
89 'hiddengli' => "yes" },
90 { 'name' => "faillog",
91 'desc' => "{import.faillog}",
92 'type' => "string",
93 'deft' => &util::filename_cat($ENV{'GSDLHOME'},"etc", "error.txt"),
94 'reqd' => "no",
95 'modegai' => "3" },
96 { 'name' => "xml",
97 'desc' => "{scripts.xml}",
98 'type' => "flag",
99 'reqd' => "no",
100 'hiddengai' => "yes" },
101 { 'name' => "gai",
102 'desc' => "{scripts.gai}",
103 'type' => "flag",
104 'reqd' => "no",
105 'hiddengai' => "yes" }];
106
107
108
109my $options = { 'name' => "csv-usernames-to-db.pl",
110 'desc' => "{cu2db.desc}",
111 'args' => $arguments };
112
113
114sub convert_csv_to_db
115{
116 my ($csv_filename,$fieldseparator,$alreadyencrypted) = @_;
117
118 my $db_filename = &util::filename_cat($ENV{'GSDLHOME'},"etc","users.gdb");
119
120 my $cmd = "txt2db -append \"$db_filename\"";
121
122 if (!open(DBOUT,"| $cmd")) {
123 print STDERR "Error: failed to run\n $cmd\n";
124 print STDERR "$!\n";
125 exit(-1);
126 }
127
128 binmode(DBOUT, ":utf8");
129
130 if (!open(FIN,"<$csv_filename")) {
131 print STDERR "Error: Unable to open file $csv_filename\n";
132 print STDERR "$!\n";
133 exit(-1);
134 }
135
136 my $line;
137 while (defined ($line = <FIN>)) {
138 chomp $line;
139 my ($username,$password,$groups,$comment) = split(/$fieldseparator/,$line);
140
141 if (!$alreadyencrypted) {
142 $password = crypt($password,"Tp");
143 }
144
145 print DBOUT "[$username]\n";
146 print DBOUT "<comment>$comment\n";
147 print DBOUT "<enabled>true\n";
148 print DBOUT "<groups>$groups\n";
149 print DBOUT "<password>$password\n";
150 print DBOUT "<username>$username\n";
151 print DBOUT "-" x 70, "\n";
152 }
153
154 close(FIN);
155 close(DBOUT);
156}
157
158
159sub main
160{
161 my ($fieldseparator,$alreadyencrypted);
162 my ($language, $out, $faillog);
163
164 my $xml = 0;
165 my $gai = 0;
166
167 my $service = "csv-usernames";
168
169 my $hashParsingResult = {};
170 # general options available to all plugins
171 my $intArgLeftinAfterParsing
172 = parse2::parse(\@ARGV,$arguments,$hashParsingResult,
173 "allow_extra_options");
174 # Parse returns -1 if something has gone wrong
175 if ($intArgLeftinAfterParsing == -1)
176 {
177 &PrintUsage::print_txt_usage($options, "{cu2db.params}");
178 die "\n";
179 }
180
181 foreach my $strVariable (keys %$hashParsingResult)
182 {
183 eval "\$$strVariable = \$hashParsingResult->{\"\$strVariable\"}";
184 }
185
186
187 # If $language has been specified, load the appropriate resource bundle
188 # (Otherwise, the default resource bundle will be loaded automatically)
189 if ($language && $language =~ /\S/) {
190 &gsprintf::load_language_specific_resource_bundle($language);
191 }
192
193 if ($xml) {
194 &PrintUsage::print_xml_usage($options);
195 print "\n";
196 return;
197 }
198
199 if ($gai) { # the gli wants strings to be in UTF-8
200 &gsprintf::output_strings_in_UTF8;
201 }
202
203 # now check that we had exactly one leftover arg, which should be
204 # the collection name. We don't want to do this earlier, cos
205 # -xml arg doesn't need a collection name
206 # Or if the user specified -h, then we output the usage also
207 if ($intArgLeftinAfterParsing != 1 || (@ARGV && $ARGV[0] =~ /^\-+h/))
208 {
209 &PrintUsage::print_txt_usage($options, "{cu2db.params}");
210 die "\n";
211 }
212
213 my $csv_filename = shift @ARGV;
214
215 my $close_out = 0;
216 if ($out !~ /^(STDERR|STDOUT)$/i) {
217 open (OUT, ">$out") ||
218 (&gsprintf(STDERR, "{common.cannot_open_output_file}: $!\n", $out) && die);
219 $out = 'cu2db::OUT';
220 $close_out = 1;
221 }
222 $out->autoflush(1);
223
224 # check that we can open the faillog
225 if ($faillog eq "") {
226 $faillog = &util::filename_cat($ENV{'GSDLHOME'}, "etc", "error.txt");
227 }
228 open (FAILLOG, ">$faillog") ||
229 (&gsprintf(STDERR, "{script.cannot_open_fail_log}\n", $faillog) && die);
230
231
232 my $faillogname = $faillog;
233 $faillog = 'cu2db::FAILLOG';
234 $faillog->autoflush(1);
235
236 convert_csv_to_db($csv_filename,$fieldseparator,$alreadyencrypted);
237
238
239 close OUT if $close_out;
240 close FAILLOG;
241}
242
243
244&main();
Note: See TracBrowser for help on using the repository browser.