adding freeside-addgroup
[freeside.git] / FS / bin / freeside-adduser
1 #!/usr/bin/perl -w
2
3 use strict;
4 use vars qw($opt_h $opt_b $opt_c $opt_g);
5 use Fcntl qw(:flock);
6 use Getopt::Std;
7
8 my $FREESIDE_CONF = "/usr/local/etc/freeside";
9
10 getopts("bch:g:");
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   system(@args) == 0 or die "htpasswd failed: $?";
31 }
32
33 #my $secretfile = $opt_s || 'secrets';
34 #
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 $secretfile\n";
39 #close MAPSECRETS or die "can't close $FREESIDE_CONF/mapsecrets: $!";
40
41 ###
42
43 use FS::UID qw(adminsuidsetup);
44 use FS::CurrentUser;
45 use FS::access_user;
46 use FS::access_usergroup;
47
48 $FS::CurrentUser::upgrade_hack = 1;
49 #adminsuidsetup $rootuser;
50 adminsuidsetup $user;
51
52 my $access_user = new FS::access_user {
53   'username'  => $user,
54   '_password' => 'notyet',
55   'first'     => 'Firstname', # $opt_f || 
56   'last'      => 'Lastname',  # $opt_l || 
57 };
58 my $au_error = $access_user->insert;
59 die $au_error if $au_error;
60
61 if ( $opt_g ) {
62
63   my $access_usergroup = new FS::access_usergroup {
64     'usernum'  => $access_user->usernum,
65     'groupnum' => $opt_g,
66   };
67   my $aug_error = $access_usergroup->insert;
68   die $aug_error if $aug_error;
69
70 }
71
72 ###
73
74 sub usage {
75   die "Usage:\n\n  freeside-adduser [ -h htpasswd_file [ -c ] [ -b ] ] [ -g groupnum ] username"
76 }
77
78 =head1 NAME
79
80 freeside-adduser - Command line interface to add (freeside) users.
81
82 =head1 SYNOPSIS
83
84   freeside-adduser [ -h htpasswd_file [ -c ] ] -g 1 username
85
86 =head1 DESCRIPTION
87
88 Adds a user to the Freeside billing system.  This is for adding users (internal
89 sales/tech folks) to the web interface, not for adding customer accounts.
90
91   -h: Also call htpasswd for this user with the given filename
92
93   -c: Passed to htpasswd(1)
94
95   -b: same as htpasswd(1), probably insecure, not recommended
96
97   -g: initial groupnum
98
99 =head1 SEE ALSO
100
101 L<htpasswd>(1), base Freeside documentation
102
103 =cut
104