1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use LATMOS::Accounts; |
---|
6 | use Getopt::Long; |
---|
7 | use Pod::Usage; |
---|
8 | |
---|
9 | =head1 NAME |
---|
10 | |
---|
11 | la-sql-alias - Tools to create or edit object alias |
---|
12 | |
---|
13 | =head1 SYNOPSIS |
---|
14 | |
---|
15 | la-sql-properties [options] obj_id |
---|
16 | |
---|
17 | =cut |
---|
18 | |
---|
19 | GetOptions( |
---|
20 | 'c|config=s' => \my $config, |
---|
21 | 'b|base=s' => \my $base, |
---|
22 | 'o|object=s' => \my $otype, |
---|
23 | 'r|remove' => \my $remove, |
---|
24 | 'help' => sub { pod2usage(0) }, |
---|
25 | ) or pod2usage(); |
---|
26 | |
---|
27 | $otype ||= 'user'; |
---|
28 | |
---|
29 | =head1 OPTIONS |
---|
30 | |
---|
31 | =over 4 |
---|
32 | |
---|
33 | =item -c|--config configdir |
---|
34 | |
---|
35 | Use this configuration directory instead of the default one. |
---|
36 | |
---|
37 | =item -b|--base basename |
---|
38 | |
---|
39 | Query this specific base instead of the default one. |
---|
40 | |
---|
41 | =item -o|object object_type |
---|
42 | |
---|
43 | Query will be performed on this object. Default is the 'User' object. |
---|
44 | |
---|
45 | =item -r|remove |
---|
46 | |
---|
47 | Remove the alias given in argument |
---|
48 | |
---|
49 | =back |
---|
50 | |
---|
51 | =cut |
---|
52 | |
---|
53 | my $LA = LATMOS::Accounts->new($config, noacl => 1); |
---|
54 | my $labase = $LA->base($base); |
---|
55 | $labase && $labase->load or die "Cannot load base"; |
---|
56 | $labase->wexported(1); |
---|
57 | |
---|
58 | my ($name, $for) = @ARGV; |
---|
59 | |
---|
60 | if (!$name) { |
---|
61 | foreach my $id ($labase->search_objects($otype, 'oalias=*')) { |
---|
62 | my $isfor = ''; |
---|
63 | if (my $obj = $labase->GetAlias($otype, $id)) { |
---|
64 | $isfor = $obj->get_attributes('oalias'); |
---|
65 | } |
---|
66 | print "$id -> $isfor\n"; |
---|
67 | } |
---|
68 | exit 0; |
---|
69 | } |
---|
70 | |
---|
71 | if ($remove) { |
---|
72 | if($labase->RemoveAlias($otype, $name)) { |
---|
73 | print "Alias $otype/$name removed\n"; |
---|
74 | exit(0); |
---|
75 | } else { |
---|
76 | die "Error while remove alias $otype/$name"; |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | $for or do { pod2usage(1); }; |
---|
81 | |
---|
82 | if (my $obj = $labase->GetAlias($otype, $name)) { |
---|
83 | if ($obj->set_c_fields(oalias => $for)) { |
---|
84 | $labase->commit; |
---|
85 | print "Alias $name updated\n"; |
---|
86 | } else { |
---|
87 | die "Alias $name unchanged\n"; |
---|
88 | } |
---|
89 | } else { |
---|
90 | if ($labase->CreateAlias($otype, $name, $for)) { |
---|
91 | $labase->commit; |
---|
92 | print "Alias $name created\n"; |
---|
93 | } else { |
---|
94 | die "Alias $name not created\n"; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|