[35] | 1 | #!/usr/bin/perl |
---|
| 2 | |
---|
| 3 | use strict; |
---|
| 4 | use warnings; |
---|
| 5 | use LATMOS::Accounts; |
---|
| 6 | use Getopt::Long; |
---|
| 7 | use Pod::Usage; |
---|
[1546] | 8 | use LATMOS::Accounts::I18N; |
---|
[35] | 9 | |
---|
[313] | 10 | =head1 NAME |
---|
| 11 | |
---|
| 12 | la-query - Tools to query base in LATMOS::Accounts system |
---|
| 13 | |
---|
| 14 | =head1 SYNOPSIS |
---|
| 15 | |
---|
| 16 | la-query [options] [obj_id] |
---|
| 17 | |
---|
[985] | 18 | =head1 DESCRIPTION |
---|
| 19 | |
---|
| 20 | [obj_id] : If present, all set attributes (rw) will be displayed for that obj_id. |
---|
[592] | 21 | If none is given, all obj_ids will be printed. |
---|
| 22 | |
---|
| 23 | For the default object_type (user), obj_id = login. |
---|
| 24 | |
---|
| 25 | Example : la-query lambda |
---|
| 26 | |
---|
[313] | 27 | =cut |
---|
| 28 | |
---|
[35] | 29 | GetOptions( |
---|
[849] | 30 | 'c|config=s' => \my $config, |
---|
| 31 | 'b|base=s' => \my $base, |
---|
| 32 | 'o|object=s' => \my $otype, |
---|
| 33 | 'e|empty' => \my $empty_attr, |
---|
| 34 | 'fmt=s' => \my $fmt, |
---|
| 35 | 'filefmt=s' => \my $filefmt, |
---|
| 36 | 'ro' => \my $with_ro, |
---|
| 37 | 'no-unexp|wo-unexp' => \my $nounexp, |
---|
| 38 | 'with-unexp' => \my $unexp, |
---|
| 39 | 'help' => sub { pod2usage(0) }, |
---|
[35] | 40 | ) or pod2usage(); |
---|
| 41 | |
---|
| 42 | $otype ||= 'user'; |
---|
| 43 | |
---|
[313] | 44 | =head1 OPTIONS |
---|
| 45 | |
---|
| 46 | =over 4 |
---|
| 47 | |
---|
[861] | 48 | =item -c|--config configdir |
---|
[313] | 49 | |
---|
[861] | 50 | Use this configuration directory instead of the default one. |
---|
[313] | 51 | |
---|
| 52 | =item -b|--base basename |
---|
| 53 | |
---|
[592] | 54 | Query this specific base instead of the default one. |
---|
[313] | 55 | |
---|
| 56 | =item -o|object object_type |
---|
| 57 | |
---|
[592] | 58 | Query will be performed on this object. Default is the 'User' object. |
---|
[313] | 59 | |
---|
[849] | 60 | =item --with-unexp |
---|
[664] | 61 | |
---|
[849] | 62 | Take into account all objects (even non propagated ones, with attribute |
---|
| 63 | 'exported'=0) (default) |
---|
[664] | 64 | |
---|
[849] | 65 | =item --wo-unexp |
---|
[664] | 66 | |
---|
[849] | 67 | Take into account only propagated objects (attribute 'exported'=1) |
---|
[664] | 68 | |
---|
[334] | 69 | =item -e|--empty |
---|
[333] | 70 | |
---|
[612] | 71 | (Only if obj_id is specified) Include also unset attributes. |
---|
[333] | 72 | |
---|
[334] | 73 | =item --ro |
---|
[333] | 74 | |
---|
[612] | 75 | (Only if obj_id is specified) Include also read-only attributes as comments. |
---|
[333] | 76 | |
---|
[440] | 77 | =item --fmt format |
---|
| 78 | |
---|
[612] | 79 | Specify a format of output. |
---|
[440] | 80 | |
---|
[612] | 81 | If NOT present and "obj_id" is specified, all non-empty writable attributes will be printed (modified by -e and -ro). |
---|
| 82 | If NOT present and "obj_id" is NOT specified, only the list of "obj_id" will be printed. |
---|
| 83 | |
---|
[592] | 84 | The "format" string may query tags in form: |
---|
[440] | 85 | |
---|
| 86 | %{ATTRIBUTE:printflike} |
---|
| 87 | |
---|
[592] | 88 | Where ATTRIBUTE is the name of an attribute and the optional |
---|
| 89 | C<:printflike> is a printf like string to print the attribute content (without |
---|
| 90 | the C<%>). |
---|
[440] | 91 | |
---|
| 92 | Examples for users: |
---|
| 93 | |
---|
[592] | 94 | --fmt "Name %{sn}, First Name %{givenName}\n" |
---|
[440] | 95 | |
---|
[592] | 96 | --fmt "Name %{sn:20s}, First Name %{givenName}\n" |
---|
[440] | 97 | |
---|
[592] | 98 | =item --filefmt format-file |
---|
| 99 | |
---|
[612] | 100 | "format-file" is a text file with the same strings as "--fmt" option, WITHOUT quotes ("). |
---|
[592] | 101 | |
---|
| 102 | It can be on more than one line, but all lines will be concatenated as one. |
---|
| 103 | Don't forget the "\n" if you really want seperate lines. |
---|
| 104 | |
---|
| 105 | Example of format-file: |
---|
| 106 | |
---|
| 107 | Name: %{sn}, First Name: %{givenName}\n |
---|
| 108 | Department : %{department}\n |
---|
| 109 | |
---|
| 110 | Note : --fmt and --filefmt can be used together, and will be concatenated (the "fmt" string first, then what is in the "format-file"). |
---|
| 111 | |
---|
[313] | 112 | =back |
---|
| 113 | |
---|
| 114 | =cut |
---|
| 115 | |
---|
[457] | 116 | my $LA = LATMOS::Accounts->new($config, noacl => 1); |
---|
[1044] | 117 | my $labase = $LA->base($base); |
---|
[1549] | 118 | $labase && $labase->load or die l("Cannot load base %s\n", $base); |
---|
[35] | 119 | |
---|
[849] | 120 | $labase->unexported($nounexp ? 0 : 1); |
---|
[664] | 121 | |
---|
[1045] | 122 | unless ($labase->is_supported_object($otype)) { |
---|
| 123 | die "Unsupported object type `$otype'\n"; |
---|
| 124 | } |
---|
| 125 | |
---|
[592] | 126 | if ($filefmt){ |
---|
[827] | 127 | open(my $hfmt, '<', $filefmt) or die "Cannot open $filefmt\n"; |
---|
[593] | 128 | $fmt ||= ''; # avoid undef warning |
---|
| 129 | while (<$hfmt>) { |
---|
[592] | 130 | chomp($fmt .= $_); |
---|
| 131 | } |
---|
[593] | 132 | close $hfmt; |
---|
[592] | 133 | } |
---|
| 134 | |
---|
[729] | 135 | if (@ARGV) { |
---|
| 136 | foreach my $ouid (@ARGV) { |
---|
| 137 | my $obj = $labase->get_object($otype, $ouid) or do { |
---|
| 138 | die "Object $otype $ouid not found\n"; |
---|
| 139 | }; |
---|
| 140 | if ($fmt) { |
---|
| 141 | print $obj->queryformat($fmt); |
---|
| 142 | } else { |
---|
| 143 | $obj->text_dump(*STDOUT, |
---|
| 144 | { |
---|
| 145 | empty_attr => $empty_attr, |
---|
| 146 | only_rw => !$with_ro, |
---|
| 147 | } |
---|
| 148 | ); |
---|
| 149 | } |
---|
[611] | 150 | } |
---|
[39] | 151 | } else { |
---|
[286] | 152 | foreach (sort $labase->list_objects($otype)) { |
---|
[440] | 153 | if ($fmt) { |
---|
| 154 | my $o = $labase->get_object($otype, $_) or next; |
---|
| 155 | print $o->queryformat($fmt); |
---|
| 156 | } else { |
---|
| 157 | print "$_\n"; |
---|
| 158 | } |
---|
[39] | 159 | } |
---|
[35] | 160 | } |
---|