[78] | 1 | #!/bin/env perl |
---|
| 2 | |
---|
| 3 | use strict; |
---|
| 4 | use warnings; |
---|
| 5 | use LATMOS::Accounts; |
---|
| 6 | use Getopt::Long; |
---|
| 7 | use Pod::Usage; |
---|
[339] | 8 | use LATMOS::Accounts::Utils; |
---|
[78] | 9 | |
---|
[339] | 10 | =head1 NAME |
---|
| 11 | |
---|
| 12 | la-create -- Tools to create object in LATMOS::Account system |
---|
| 13 | |
---|
[1996] | 14 | =head1 USAGE |
---|
[339] | 15 | |
---|
[1996] | 16 | la-create [options] [obj_id] |
---|
[339] | 17 | |
---|
[1996] | 18 | C<la-create> create objects inside database. |
---|
| 19 | |
---|
| 20 | If no input file is given C<$EDITOR> program is launch with an empty template. |
---|
| 21 | |
---|
| 22 | The object name must given except when creating C<user> object in SQL database |
---|
| 23 | for which a try will be done from C<sn> and C<givenName>. |
---|
| 24 | |
---|
[339] | 25 | =cut |
---|
| 26 | |
---|
[78] | 27 | GetOptions( |
---|
| 28 | 'c|config=s' => \my $config, |
---|
| 29 | 'b|base=s' => \my $base, |
---|
| 30 | 'o|object=s' => \my $otype, |
---|
[339] | 31 | 'f=s' => \my $inputfile, |
---|
| 32 | 'ro' => \my $with_ro, |
---|
[2383] | 33 | 'e' => \my $empty_file, |
---|
[339] | 34 | 'u|update' => \my $allow_update, |
---|
[78] | 35 | 'help' => sub { pod2usage(0) }, |
---|
| 36 | ) or pod2usage(); |
---|
| 37 | |
---|
| 38 | $otype ||= 'user'; |
---|
| 39 | |
---|
[339] | 40 | =head1 OPTIONS |
---|
| 41 | |
---|
| 42 | =over 4 |
---|
| 43 | |
---|
[861] | 44 | =item -c|--config configdir |
---|
[339] | 45 | |
---|
[861] | 46 | Use this configuration directory instead of the default one. |
---|
[339] | 47 | |
---|
| 48 | =item -b|--base basename |
---|
| 49 | |
---|
[594] | 50 | Query this specific base instead of the default one. |
---|
[339] | 51 | |
---|
| 52 | =item -o|object object_type |
---|
| 53 | |
---|
[594] | 54 | Query will be performed on this object. Default is the 'User' object. |
---|
[339] | 55 | |
---|
| 56 | =item --ro |
---|
| 57 | |
---|
| 58 | Include also read-only attributes as comment |
---|
| 59 | |
---|
| 60 | =item -u|--update |
---|
| 61 | |
---|
| 62 | If the object already exists, then updating it |
---|
| 63 | |
---|
| 64 | =item -f FILE |
---|
| 65 | |
---|
| 66 | Use this file to get attributes (- use stdin) |
---|
[595] | 67 | If this option is not present, you'll have a file opened in your prefered editor to edit. |
---|
[339] | 68 | |
---|
[2383] | 69 | =item -e |
---|
| 70 | |
---|
| 71 | Calling vi with an empty text file (instead having a list of attribute) |
---|
| 72 | |
---|
[339] | 73 | =back |
---|
| 74 | |
---|
| 75 | =cut |
---|
| 76 | |
---|
[457] | 77 | my $LA = LATMOS::Accounts->new($config, noacl => 1); |
---|
[1044] | 78 | my $labase = $LA->base($base); |
---|
[78] | 79 | $labase && $labase->load or die "Cannot load base"; |
---|
[664] | 80 | $labase->wexported(1); |
---|
[78] | 81 | |
---|
| 82 | $labase->is_supported_object($otype) or die "$otype object unsupported\n"; |
---|
[339] | 83 | my $objname = $ARGV[0]; |
---|
[78] | 84 | |
---|
[339] | 85 | sub input_from_handle { |
---|
| 86 | my ($fh) = @_; |
---|
| 87 | my %attr = LATMOS::Accounts::Utils::parse_obj_file($fh); |
---|
[1996] | 88 | if ($objname && (my $obj = $labase->get_object($otype, $objname))) { |
---|
[339] | 89 | if ($allow_update) { |
---|
| 90 | my $res = $obj->set_c_fields(%attr); |
---|
| 91 | if ($res) { |
---|
| 92 | print "Changes applied\n"; |
---|
| 93 | $labase->commit; |
---|
[602] | 94 | $LA->call_batch_sync; |
---|
[339] | 95 | } |
---|
| 96 | return $res; |
---|
| 97 | } else { |
---|
| 98 | die "Object $otype $objname already exists, aborting\n"; |
---|
| 99 | } |
---|
[78] | 100 | } else { |
---|
[1996] | 101 | if ($objname) { |
---|
| 102 | my $res = $labase->create_c_object($otype, $objname, %attr); |
---|
| 103 | if($res) { |
---|
| 104 | print "Changes applied\n"; |
---|
| 105 | $labase->commit; |
---|
| 106 | $LA->call_batch_sync; |
---|
| 107 | return 1; |
---|
| 108 | } |
---|
| 109 | return 0; |
---|
| 110 | } else { |
---|
| 111 | my $ochelper = $labase->ochelper($otype); |
---|
| 112 | |
---|
| 113 | my $info = { |
---|
| 114 | contents => { %attr }, |
---|
| 115 | }; |
---|
| 116 | if ($attr{name}) { |
---|
| 117 | $info->{name}{content} = $attr{name}; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | $ochelper->Automate($info) or |
---|
[2385] | 121 | die "Cannot create object:" . LATMOS::Accounts::Log::lastmessage() . "\n"; |
---|
| 122 | |
---|
| 123 | $labase->commit; |
---|
| 124 | $LA->call_batch_sync; |
---|
| 125 | |
---|
| 126 | return 1; |
---|
[1996] | 127 | } |
---|
[339] | 128 | } |
---|
| 129 | } |
---|
[78] | 130 | |
---|
[339] | 131 | if ($inputfile) { |
---|
| 132 | my $handle; |
---|
| 133 | if ($inputfile eq '-') { |
---|
| 134 | $handle = *STDIN; |
---|
| 135 | } else { |
---|
| 136 | open($handle, '<', $inputfile) or die |
---|
| 137 | "Cannot open input file $@\n"; |
---|
| 138 | } |
---|
| 139 | my $res = input_from_handle($handle); |
---|
| 140 | close($handle); |
---|
| 141 | exit(!$res); |
---|
| 142 | } else { |
---|
| 143 | exit ! LATMOS::Accounts::Utils::dump_read_temp_file( |
---|
| 144 | sub { |
---|
| 145 | my ($fh) = @_; |
---|
| 146 | $labase->text_empty_dump($fh, $otype, |
---|
| 147 | { |
---|
| 148 | only_rw => !$with_ro, |
---|
[78] | 149 | } |
---|
[2383] | 150 | ) unless($empty_file); |
---|
[339] | 151 | }, |
---|
| 152 | sub { |
---|
| 153 | my ($fh) = @_; |
---|
| 154 | return input_from_handle($fh); |
---|
[78] | 155 | } |
---|
[339] | 156 | ); |
---|
[78] | 157 | } |
---|