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