summaryrefslogtreecommitdiff
path: root/bin/mapsecrets2access_user
blob: d632360f56a3716b3e2c674db0ac1a1f1ac3075f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/perl -w

use strict;
use File::Copy "cp";
use FS::UID qw(adminsuidsetup);
use FS::CurrentUser;
use FS::AccessRight;
use FS::Record qw(qsearchs qsearch);
use FS::access_group;
use FS::access_user;
use FS::access_usergroup;
use FS::access_right;
use FS::access_groupagent;
use FS::agent;

$FS::CurrentUser::upgrade_hack = 1;
my $user = shift or die &usage;
adminsuidsetup $user;

my $supergroup = qsearchs('access_group', { 'groupname' => 'Superuser' } );
unless ( $supergroup ) {

  $supergroup = new FS::access_group { 'groupname' => 'Superuser' };
  my $error = $supergroup->insert;
  die $error if $error;

  foreach my $rightname ( FS::AccessRight->default_superuser_rights ) {
    my $access_right = new FS::access_right {
      'righttype'   => 'FS::access_group',
      'rightobjnum' => $supergroup->groupnum,
      'rightname'   => $rightname,
    };
    my $ar_error = $access_right->insert;
    die $ar_error if $ar_error;
  }

  foreach my $agent ( qsearch('agent', {} ) ) {
    my $access_groupagent = new FS::access_groupagent {
      'groupnum' => $supergroup->groupnum,
      'agentnum' => $agent->agentnum,
    };
    my $aga_error = $access_groupagent->insert;
    die $aga_error if $aga_error;
  }

}
my $supergroupnum = $supergroup->groupnum;

my $conf = new FS::Conf;
my $dir = $conf->base_dir;
my $mapsecrets = "$dir/mapsecrets";
open(MAPSECRETS, "<$mapsecrets") or die "Can't open $mapsecrets: $!";
while (<MAPSECRETS>) {
  /([\w]+)\s+secrets\s*$/ or die "unparsable line in mapsecrets: $_";
  my $username = $1;

  next if qsearchs('access_user', { 'username' => $username } );

  my $access_user = new FS::access_user {
    'username'  => $username,
    '_password' => 'notyet',
    'first'     => 'Legacy',
    'last'      => 'User',
  };
  my $au_error = $access_user->insert;
  die $au_error if $au_error;

  my $access_usergroup = new FS::access_usergroup { 
    'usernum'  => $access_user->usernum,
    'groupnum' => $supergroupnum,
  };
  my $aug_error = $access_usergroup->insert;
  die $aug_error if $aug_error;

}
close MAPSECRETS;

# okay to clobber mapsecrets now i guess
cp $mapsecrets, "$mapsecrets.bak$$";
open(MAPSECRETS, ">$mapsecrets") or die $!;
print MAPSECRETS '* secrets'. "\n";
close MAPSECRETS or die $!;

sub usage {
  die "Usage:\n  mapsecrets2access_user user\n"; 
}