fix 'Can't call method "setup" on an undefined value' error when using into rates...
[freeside.git] / FS / bin / freeside-adduser
1 #!/usr/bin/perl -w
2
3 use strict;
4 use vars qw($opt_s $opt_g $opt_n);
5 use Fcntl qw(:flock);
6 use Getopt::Std;
7
8 my $FREESIDE_CONF = "%%%FREESIDE_CONF%%%";
9
10 getopts("s:g:n");
11 my $user = shift or die &usage;
12
13 if ( $opt_s ) {
14
15   #if ( -e "$FREESIDE_CONF/mapsecrets" ) {
16   #  open(MAPSECRETS,"<$FREESIDE_CONF/mapsecrets")
17   #    or die "can't open $FREESIDE_CONF/mapsecrets: $!";
18   #  while (<MAPSECRETS>) {
19   #    /^(\S+) / or die "unparsable line in mapsecrets: $_";
20   #    die "user $user already exists\n" if $user eq $1;
21   #  }
22   #  close MAPSECRETS;
23   #}
24
25   #insert new entry before a wildcard...
26   open(MAPSECRETS,"<$FREESIDE_CONF/mapsecrets")
27     and flock(MAPSECRETS,LOCK_EX)
28       or die "can't open $FREESIDE_CONF/mapsecrets: $!";
29   open(NEW,">$FREESIDE_CONF/mapsecrets.new")
30     or die "can't open $FREESIDE_CONF/mapsecrets.new: $!";
31   while(<MAPSECRETS>) {
32     if ( /^\*\s/ ) {
33       print NEW "$user $opt_s\n";
34     }
35     print NEW $_;
36   }
37   close MAPSECRETS or die "can't close $FREESIDE_CONF/mapsecrets: $!";
38   close NEW or die "can't close $FREESIDE_CONF/mapsecrets.new: $!";
39   rename("$FREESIDE_CONF/mapsecrets.new", "$FREESIDE_CONF/mapsecrets")
40     or die "can't move mapsecrets.new into place: $!";
41
42 }
43
44 ###
45
46 exit if $opt_n;
47
48 ###
49
50 use FS::UID qw(adminsuidsetup);
51 use FS::CurrentUser;
52 use FS::access_user;
53 use FS::access_usergroup;
54
55 $FS::CurrentUser::upgrade_hack = 1;
56 #adminsuidsetup $rootuser;
57 adminsuidsetup $user;
58
59 my $access_user = new FS::access_user {
60   'username'  => $user,
61   '_password' => 'notyet',
62   'first'     => 'Firstname', # $opt_f || 
63   'last'      => 'Lastname',  # $opt_l || 
64 };
65 my $au_error = $access_user->insert;
66 die $au_error if $au_error;
67
68 if ( $opt_g ) {
69
70   my $access_usergroup = new FS::access_usergroup {
71     'usernum'  => $access_user->usernum,
72     'groupnum' => $opt_g,
73   };
74   my $aug_error = $access_usergroup->insert;
75   die $aug_error if $aug_error;
76
77 }
78
79 ###
80
81 sub usage {
82   die "Usage:\n\n  freeside-adduser [ -n ] [ -s ] [ -g groupnum ] username [ password ]"
83 }
84
85 =head1 NAME
86
87 freeside-adduser - Command line interface to add (freeside) users.
88
89 =head1 SYNOPSIS
90
91   freeside-adduser [ -n ] [ -s ] [ -g groupnum ] username [ password ]
92
93 =head1 DESCRIPTION
94
95 Adds a user to the Freeside billing system.  This is for adding users (internal
96 sales/tech folks) to the web interface, not for adding customer accounts.
97
98 This functionality is now available in the web interface as well, under
99 B<Configuration | Employees | View/Edit employees>.
100
101   -g: initial groupnum
102
103   Development/multi-DB options:
104
105   -s: alternate secrets file
106
107   -n: no ACL added, for bootstrapping
108
109 =head1 NOTE
110
111 No explicit htpasswd options are available in 1.7 - passwords are now
112 maintained automatically.
113
114 =head1 SEE ALSO
115
116 Base Freeside documentation
117
118 =cut
119