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

Last change on this file since 2626 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
RevLine 
[78]1#!/bin/env perl
2
3use strict;
4use warnings;
5use LATMOS::Accounts;
6use Getopt::Long;
7use Pod::Usage;
[339]8use 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]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
[339]25=cut
26
[78]27GetOptions(
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]46Use this configuration directory instead of the default one.
[339]47
48=item -b|--base basename
49
[594]50Query this specific base instead of the default one.
[339]51
52=item -o|object object_type
53
[594]54Query will be performed on this object. Default is the 'User' object.
[339]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)
[595]67If this option is not present, you'll have a file opened in your prefered editor to edit.
[339]68
[2383]69=item -e
70
71Calling vi with an empty text file (instead having a list of attribute)
72
[339]73=back
74
75=cut
76
[457]77my $LA = LATMOS::Accounts->new($config, noacl => 1);
[1044]78my $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]83my $objname = $ARGV[0];
[78]84
[339]85sub 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]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,
[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}
Note: See TracBrowser for help on using the repository browser.