source: trunk/LATMOS-Accounts-Web/lib/LATMOS/Accounts/Web/Controller/Ajax.pm @ 1107

Last change on this file since 1107 was 1107, checked in by nanardon, 12 years ago

Add filter to object list

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