source: trunk/LATMOS-Accounts/bin/la-create

Last change on this file was 2385, checked in by nanardon, 4 years ago

Add OCHelper for Address and Employment

  • Property svn:executable set to *
File size: 3.7 KB
Line 
1#!/bin/env perl
2
3use strict;
4use warnings;
5use LATMOS::Accounts;
6use Getopt::Long;
7use Pod::Usage;
8use LATMOS::Accounts::Utils;
9
10=head1 NAME
11
12    la-create -- Tools to create object in LATMOS::Account system
13
14=head1 USAGE
15
16    la-create [options] [obj_id]
17
18C<la-create> create objects inside database.
19
20If no input file is given C<$EDITOR> program is launch with an empty template.
21
22The object name must given except when creating C<user> object in SQL database
23for which a try will be done from C<sn> and C<givenName>.
24
25=cut
26
27GetOptions(
28    'c|config=s' => \my $config,
29    'b|base=s'   => \my $base,
30    'o|object=s' => \my $otype,
31    'f=s'        => \my $inputfile,
32    'ro'         => \my $with_ro,
33    'e'          => \my $empty_file,
34    'u|update'   => \my $allow_update,
35    'help'       => sub { pod2usage(0) },
36) or pod2usage();
37
38$otype ||= 'user';
39
40=head1 OPTIONS
41
42=over 4
43
44=item -c|--config configdir
45
46Use this configuration directory instead of the default one.
47
48=item -b|--base basename
49
50Query this specific base instead of the default one.
51
52=item -o|object object_type
53
54Query will be performed on this object. Default is the 'User' object.
55
56=item --ro
57
58Include also read-only attributes as comment
59
60=item -u|--update
61
62If the object already exists, then updating it
63
64=item -f FILE
65
66Use this file to get attributes (- use stdin)
67If this option is not present, you'll have a file opened in your prefered editor to edit.
68
69=item -e
70
71Calling vi with an empty text file (instead having a list of attribute)
72
73=back
74
75=cut
76
77my $LA = LATMOS::Accounts->new($config, noacl => 1);
78my $labase = $LA->base($base);
79$labase && $labase->load or die "Cannot load base";
80$labase->wexported(1);
81
82$labase->is_supported_object($otype) or die "$otype object unsupported\n";
83my $objname = $ARGV[0];
84
85sub input_from_handle {
86    my ($fh) = @_;
87    my %attr = LATMOS::Accounts::Utils::parse_obj_file($fh);
88    if ($objname && (my $obj = $labase->get_object($otype, $objname))) {
89        if ($allow_update) {
90            my $res = $obj->set_c_fields(%attr);
91            if ($res) {
92                print "Changes applied\n";
93                $labase->commit;
94                $LA->call_batch_sync;
95            }
96            return $res;
97        } else {
98            die "Object $otype $objname already exists, aborting\n";
99        }
100    } else {
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
121                die "Cannot create object:" . LATMOS::Accounts::Log::lastmessage() . "\n";
122
123            $labase->commit;
124            $LA->call_batch_sync;
125
126            return 1;
127        } 
128    }
129}
130
131if ($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,
149                }
150            ) unless($empty_file);
151        },
152        sub {
153            my ($fh) = @_;
154            return input_from_handle($fh);
155        }
156    );
157}
Note: See TracBrowser for help on using the repository browser.