get rid of too-verbose debugging
[freeside.git] / FS / bin / freeside-adduser
1 #!/usr/bin/perl -w
2
3 use strict;
4 use vars qw($opt_s $opt_h $opt_b $opt_c $opt_g $opt_n);
5 use Fcntl qw(:flock);
6 use Getopt::Std;
7
8 my $FREESIDE_CONF = "/usr/local/etc/freeside";
9
10 getopts("s:bch:g:n");
11 die &usage if $opt_c && ! $opt_h;
12 my $user = shift or die &usage;
13
14 #if ( -e "$FREESIDE_CONF/mapsecrets" ) {
15 #  open(MAPSECRETS,"<$FREESIDE_CONF/mapsecrets")
16 #    or die "can't open $FREESIDE_CONF/mapsecrets: $!";
17 #  while (<MAPSECRETS>) {
18 #    /^(\S+) / or die "unparsable line in mapsecrets: $_";
19 #    die "user $user already exists\n" if $user eq $1;
20 #  }
21 #  close MAPSECRETS;
22 #}
23
24 if ( $opt_h ) {
25   my @args = ( 'htpasswd' );
26   push @args, '-b' if $opt_b;
27   push @args, '-c' if $opt_c;
28   push @args, $opt_h, $user;
29   push @args, shift if $opt_b;
30   #warn join(', ', @args)."\n";
31   system(@args) == 0 or die "htpasswd failed: $?";
32 }
33
34 if ( $opt_s ) {
35   open(MAPSECRETS,">>$FREESIDE_CONF/mapsecrets")
36     and flock(MAPSECRETS,LOCK_EX)
37       or die "can't open $FREESIDE_CONF/mapsecrets: $!";
38   print MAPSECRETS "$user $opt_s\n";
39   close MAPSECRETS or die "can't close $FREESIDE_CONF/mapsecrets: $!";
40 }
41
42 ###
43
44 exit if $opt_n;
45
46 ###
47
48 use FS::UID qw(adminsuidsetup);
49 use FS::CurrentUser;
50 use FS::access_user;
51 use FS::access_usergroup;
52
53 $FS::CurrentUser::upgrade_hack = 1;
54 #adminsuidsetup $rootuser;
55 adminsuidsetup $user;
56
57 my $access_user = new FS::access_user {
58   'username'  => $user,
59   '_password' => 'notyet',
60   'first'     => 'Firstname', # $opt_f || 
61   'last'      => 'Lastname',  # $opt_l || 
62 };
63 my $au_error = $access_user->insert;
64 die $au_error if $au_error;
65
66 if ( $opt_g ) {
67
68   my $access_usergroup = new FS::access_usergroup {
69     'usernum'  => $access_user->usernum,
70     'groupnum' => $opt_g,
71   };
72   my $aug_error = $access_usergroup->insert;
73   die $aug_error if $aug_error;
74
75 }
76
77 ###
78
79 sub usage {
80   die "Usage:\n\n  freeside-adduser [ -h htpasswd_file [ -c ] [ -b ] ] [ -g groupnum ] username [ password ]"
81 }
82
83 =head1 NAME
84
85 freeside-adduser - Command line interface to add (freeside) users.
86
87 =head1 SYNOPSIS
88
89   freeside-adduser [ -n ] [ -h htpasswd_file [ -c ] [ -b ] ] [ -g groupnum ] username [ password ]
90
91 =head1 DESCRIPTION
92
93 Adds a user to the Freeside billing system.  This is for adding users (internal
94 sales/tech folks) to the web interface, not for adding customer accounts.
95
96   -h: Also call htpasswd for this user with the given filename
97
98   -c: Passed to htpasswd(1)
99
100   -b: same as htpasswd(1), probably insecure, not recommended
101
102   -g: initial groupnum
103
104   Development/multi-DB options:
105
106   -s: alternate secrets file
107
108   -n: no ACL added, for bootstrapping
109
110 =head1 SEE ALSO
111
112 L<htpasswd>(1), base Freeside documentation
113
114 =cut
115