[272] | 1 | package LATMOS::Accounts::Web::Controller::Ajax; |
---|
| 2 | |
---|
| 3 | use strict; |
---|
| 4 | use warnings; |
---|
[884] | 5 | use base 'Catalyst::Controller'; |
---|
[1151] | 6 | use LATMOS::Accounts::Utils; |
---|
[272] | 7 | |
---|
| 8 | =head1 NAME |
---|
| 9 | |
---|
| 10 | LATMOS::Accounts::Web::Controller::Ajax - Catalyst Controller |
---|
| 11 | |
---|
| 12 | =head1 DESCRIPTION |
---|
| 13 | |
---|
| 14 | Catalyst Controller. |
---|
| 15 | |
---|
| 16 | =head1 METHODS |
---|
| 17 | |
---|
| 18 | =cut |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | =head2 index |
---|
| 22 | |
---|
| 23 | =cut |
---|
| 24 | |
---|
| 25 | sub index : Private { |
---|
| 26 | my ( $self, $c ) = @_; |
---|
| 27 | |
---|
| 28 | $c->response->body('Matched LATMOS::Accounts::Web::Controller::Ajax in Ajax.'); |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | sub rawattr : Local { |
---|
| 32 | my ($self, $c, $otype) = @_; |
---|
| 33 | |
---|
| 34 | my $base = $c->model('Accounts')->db; |
---|
| 35 | |
---|
| 36 | if (!$c->req->param('id')) { |
---|
| 37 | $c->stash->{value} = ''; |
---|
| 38 | return; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | my $obj = $base->get_object($otype, $c->req->param('id')) or do { |
---|
[755] | 42 | $c->stash->{value} = ''; |
---|
[272] | 43 | return; |
---|
| 44 | }; |
---|
[754] | 45 | $c->stash->{value} = $obj->get_c_field($c->req->param('attr')) || ''; |
---|
[272] | 46 | } |
---|
| 47 | |
---|
[861] | 48 | sub displayattr : Local { |
---|
[1135] | 49 | my ($self, $c, $otype, $attr) = @_; |
---|
| 50 | $attr ||= $c->req->param('attr'); |
---|
| 51 | my $attribute = $c->model('Accounts')->db->attribute($otype, $attr) or return; |
---|
[861] | 52 | $c->stash->{value} = $attribute->display($c->req->param('id')); |
---|
| 53 | } |
---|
| 54 | |
---|
[1193] | 55 | =head2 objexist |
---|
| 56 | |
---|
| 57 | Return HTML picture telling if object name is free or already used |
---|
| 58 | |
---|
| 59 | =cut |
---|
| 60 | |
---|
[500] | 61 | sub objexist : Local { |
---|
| 62 | my ($self, $c, $otype) = @_; |
---|
| 63 | my $base = $c->model('Accounts')->db; |
---|
[1193] | 64 | my $val = lc($c->req->param('val') || ''); |
---|
[500] | 65 | my $obj = $base->get_object($otype, $val); |
---|
| 66 | |
---|
| 67 | $c->stash->{'exists'} = join(', ', $obj->get_attributes($c->req->param('attr') || |
---|
[861] | 68 | 'displayName') || $obj->id) if($obj); |
---|
[500] | 69 | } |
---|
| 70 | |
---|
[680] | 71 | sub check_new_name : Local { |
---|
| 72 | my ($self, $c, $otype) = @_; |
---|
| 73 | my $base = $c->model('Accounts')->db; |
---|
| 74 | my $val = $c->req->param('val'); |
---|
| 75 | |
---|
| 76 | warn $otype; |
---|
| 77 | if ($c->stash->{validity} = (lc($otype) eq 'user' || lc($otype) eq 'group') |
---|
| 78 | ? LATMOS::Accounts::Utils::check_ug_validity($val) |
---|
| 79 | : LATMOS::Accounts::Utils::check_oid_validity($val)) { |
---|
| 80 | warn $c->stash->{validity}; |
---|
| 81 | return; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | if (my $obj = $base->get_object($otype, $val)) { |
---|
| 85 | $c->stash->{validity} = "$val exists " . |
---|
| 86 | ($obj->get_attributes('displayName') || '') |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | |
---|
[407] | 90 | sub objattrexist : Local { |
---|
| 91 | my ($self, $c, $otype, $attr) = @_; |
---|
| 92 | my $base = $c->model('Accounts')->db; |
---|
[861] | 93 | my $attribute = $base->attribute($otype, $attr) or return; |
---|
| 94 | my $val = $attribute->input($c->req->param('val')); |
---|
[451] | 95 | my @obj = $val ? $base->search_objects($otype, "$attr=$val") : (); |
---|
[407] | 96 | |
---|
| 97 | if (my $filter = $c->req->param('exclude')) { |
---|
| 98 | @obj = grep { $_ ne $filter } @obj; |
---|
| 99 | } |
---|
| 100 | $c->stash->{'exists'} = join(', ', @obj); |
---|
| 101 | } |
---|
| 102 | |
---|
[585] | 103 | sub cracklib : Local { |
---|
| 104 | my ( $self, $c, $login ) = @_; |
---|
| 105 | my $base = $c->model('Accounts')->db; |
---|
| 106 | my ($password) = $c->req->param('passwd'); |
---|
| 107 | my ($cpassword) = $c->req->param('cpasswd'); |
---|
[586] | 108 | if (my $obj = $base->get_object('user', $login)) { |
---|
| 109 | if ($password) { |
---|
[629] | 110 | if ((my $crl = $obj->check_password($password)) ne 'ok') { |
---|
| 111 | $c->stash->{cracklib} = 'Mauvais mot de passe: ' . $crl; |
---|
| 112 | } elsif ($cpassword) { |
---|
[586] | 113 | if ($password ne $cpassword) { |
---|
| 114 | $c->stash->{cracklib} = "Mot de passe différents"; |
---|
| 115 | } else { |
---|
[629] | 116 | $c->stash->{cracklib} = 'ok'; |
---|
[586] | 117 | } |
---|
| 118 | } else { |
---|
| 119 | $c->stash->{cracklib} = 'Confirmez le mot de passe'; |
---|
| 120 | } |
---|
| 121 | } |
---|
[585] | 122 | } |
---|
| 123 | } |
---|
| 124 | |
---|
[1151] | 125 | sub random_password : Local { |
---|
| 126 | my ($self, $c) = @_; |
---|
| 127 | |
---|
[2295] | 128 | my $user = $c->model('Accounts')->base->get_object('user', $c->req->param('user')) |
---|
| 129 | or return; |
---|
| 130 | |
---|
[1151] | 131 | $c->stash->{random_password} = |
---|
| 132 | LATMOS::Accounts::Utils::genpassword( |
---|
[2074] | 133 | %{ $c->req->params() }, |
---|
| 134 | checkpassword => sub { |
---|
[2295] | 135 | $user->check_password(@_) |
---|
[2074] | 136 | }, |
---|
[1151] | 137 | ); |
---|
| 138 | } |
---|
| 139 | |
---|
[680] | 140 | sub check_ug_validity : Local { |
---|
[679] | 141 | my ( $self, $c ) = @_; |
---|
| 142 | my $val = $c->req->param('val'); |
---|
| 143 | $c->stash->{validity} = LATMOS::Accounts::Utils::check_ug_validity($val); |
---|
| 144 | } |
---|
| 145 | |
---|
[861] | 146 | sub select_freeip_select : Local { |
---|
| 147 | my ($self, $c) = @_; |
---|
| 148 | |
---|
| 149 | } |
---|
| 150 | |
---|
[873] | 151 | sub user_to_group_form : Local { |
---|
| 152 | my ($self, $c, $user) = @_; |
---|
| 153 | |
---|
| 154 | my $base = $c->model('Accounts')->db; |
---|
| 155 | |
---|
| 156 | $c->stash->{user} = $base->get_object('user', $user) or do { |
---|
| 157 | return; |
---|
| 158 | }; |
---|
| 159 | |
---|
| 160 | my %ingroups = |
---|
| 161 | map { $_ => 1 } |
---|
| 162 | @{ $c->stash->{user}->get_c_field('memberOf') || []}; |
---|
| 163 | |
---|
| 164 | $c->stash->{othergroups} = [ grep { ! $ingroups{$_} } |
---|
| 165 | $base->search_objects('group', 'cn~' . ($c->req->param('gfilter') || '*')) ]; |
---|
| 166 | } |
---|
| 167 | |
---|
[861] | 168 | sub goto : Local { |
---|
| 169 | my ($self, $c) = @_; |
---|
| 170 | |
---|
[932] | 171 | my $objurl = $c->req->param('otype'); |
---|
| 172 | $objurl .= 's' unless ($objurl =~ /s$/); |
---|
| 173 | |
---|
[861] | 174 | $c->res->redirect( |
---|
[932] | 175 | $c->uri_for('/' . $objurl, $c->req->param('goto'), |
---|
[861] | 176 | $c->req->param('subform')) |
---|
| 177 | ); |
---|
| 178 | $c->stash->{template} = 'no_object.tt'; |
---|
| 179 | } |
---|
| 180 | |
---|
[927] | 181 | sub set_attrs : Local { |
---|
| 182 | my ($self, $c, $otype, $name) = @_; |
---|
[861] | 183 | |
---|
[927] | 184 | my $base = $c->model('Accounts')->db; |
---|
| 185 | my $object = $base->get_object($otype, $name) or do { |
---|
| 186 | return; |
---|
| 187 | }; |
---|
| 188 | my $form = $c->model('AttrForms', '', $object); |
---|
| 189 | $form->set_attrs([ $c->req->param('attrs') ]); |
---|
| 190 | |
---|
| 191 | } |
---|
| 192 | |
---|
[1081] | 193 | sub list_objects : Local { |
---|
| 194 | my ($self, $c, $otype) = @_; |
---|
| 195 | $c->stash->{ofilter} = $c->model('AttrFilter', $otype); |
---|
| 196 | } |
---|
| 197 | |
---|
[1107] | 198 | sub select_objects_list : Local { |
---|
| 199 | my ($self, $c, $otype) = @_; |
---|
| 200 | $c->stash->{ofilter} = $c->model('AttrFilter', $otype); |
---|
| 201 | } |
---|
| 202 | |
---|
[272] | 203 | sub end : Private { |
---|
| 204 | my ( $self, $c ) = @_; |
---|
| 205 | $c->forward($c->view('TTpart')); |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | =head1 AUTHOR |
---|
| 209 | |
---|
| 210 | Thauvin Olivier |
---|
| 211 | |
---|
| 212 | =head1 LICENSE |
---|
| 213 | |
---|
| 214 | This library is free software, you can redistribute it and/or modify |
---|
| 215 | it under the same terms as Perl itself. |
---|
| 216 | |
---|
| 217 | =cut |
---|
| 218 | |
---|
| 219 | 1; |
---|