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

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

Merge branch 'dyngroup'

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, $attr) = @_;
49    $attr ||= $c->req->param('attr');
50    my $attribute = $c->model('Accounts')->db->attribute($otype, $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->{values} = [
60        sort
61        grep { /^\Q$val\E/ } $base->attributes_summary($otype, $attr)
62    ];
63
64}
65
66sub objexist : Local {
67    my ($self, $c, $otype) = @_;
68    my $base = $c->model('Accounts')->db;
69    my $val = $c->req->param('val');
70    my $obj = $base->get_object($otype, $val);
71
72    $c->stash->{'exists'} = join(', ', $obj->get_attributes($c->req->param('attr') ||
73            'displayName') || $obj->id) if($obj);
74}
75
76sub check_new_name : Local {
77    my ($self, $c, $otype) = @_;
78    my $base = $c->model('Accounts')->db;
79    my $val = $c->req->param('val');
80
81    warn $otype;
82    if ($c->stash->{validity} = (lc($otype) eq 'user' || lc($otype) eq 'group')
83        ? LATMOS::Accounts::Utils::check_ug_validity($val)
84        : LATMOS::Accounts::Utils::check_oid_validity($val)) {
85        warn $c->stash->{validity};
86        return;
87    }
88
89    if (my $obj = $base->get_object($otype, $val)) {
90        $c->stash->{validity} = "$val exists " .
91            ($obj->get_attributes('displayName') || '')
92    }
93}
94
95sub objattrexist : Local {
96    my ($self, $c, $otype, $attr) = @_;
97    my $base = $c->model('Accounts')->db;
98    my $attribute = $base->attribute($otype, $attr) or return;
99    my $val = $attribute->input($c->req->param('val'));
100    my @obj = $val ? $base->search_objects($otype, "$attr=$val") : ();
101
102    if (my $filter = $c->req->param('exclude')) {
103        @obj = grep { $_ ne $filter } @obj;
104    }
105    $c->stash->{'exists'} = join(', ', @obj);
106}
107
108sub cracklib : Local {
109    my ( $self, $c, $login ) = @_;
110    my $base = $c->model('Accounts')->db;
111    my ($password)  = $c->req->param('passwd');
112    my ($cpassword) = $c->req->param('cpasswd');
113    if (my $obj = $base->get_object('user', $login)) {
114        if ($password) {
115            if ((my $crl = $obj->check_password($password)) ne 'ok') {
116                $c->stash->{cracklib} = 'Mauvais mot de passe: ' . $crl;
117            } elsif ($cpassword) {
118                if ($password ne $cpassword) {
119                    $c->stash->{cracklib} = "Mot de passe différents";
120                } else {
121                    $c->stash->{cracklib} = 'ok';
122                }
123            } else {
124                $c->stash->{cracklib} = 'Confirmez le mot de passe';
125            }
126        }
127    }
128}
129
130sub check_ug_validity : Local {
131    my ( $self, $c ) = @_;
132    my $val = $c->req->param('val');
133    $c->stash->{validity} = LATMOS::Accounts::Utils::check_ug_validity($val);
134}
135
136sub select_freeip_select : Local {
137    my ($self, $c) = @_;
138
139}
140
141sub user_to_group_form : Local {
142    my ($self, $c, $user) = @_;
143   
144    my $base = $c->model('Accounts')->db;
145
146    $c->stash->{user} = $base->get_object('user', $user) or do {
147        return;
148    };
149
150    my %ingroups =
151        map { $_ => 1 } 
152        @{ $c->stash->{user}->get_c_field('memberOf') || []};
153
154   $c->stash->{othergroups} = [ grep { ! $ingroups{$_} }
155       $base->search_objects('group', 'cn~' . ($c->req->param('gfilter') || '*')) ]; 
156}
157
158sub goto : Local {
159    my ($self, $c) = @_;
160
161    my $objurl = $c->req->param('otype');
162    $objurl .= 's' unless ($objurl =~ /s$/);
163
164    $c->res->redirect(
165        $c->uri_for('/' . $objurl, $c->req->param('goto'),
166        $c->req->param('subform'))
167    );
168    $c->stash->{template} = 'no_object.tt';
169}
170
171sub set_attrs : Local {
172    my ($self, $c, $otype, $name) = @_;
173
174    my $base = $c->model('Accounts')->db;
175    my $object = $base->get_object($otype, $name) or do {
176        return;
177    };
178    my $form = $c->model('AttrForms', '', $object);
179    $form->set_attrs([ $c->req->param('attrs') ]);
180
181}
182
183sub list_objects : Local {
184    my ($self, $c, $otype) = @_;
185    $c->stash->{ofilter} = $c->model('AttrFilter', $otype);
186}
187
188sub select_objects_list : Local {
189    my ($self, $c, $otype) = @_;
190    $c->stash->{ofilter} = $c->model('AttrFilter', $otype);
191}
192
193sub end : Private {
194    my ( $self, $c ) = @_;
195    $c->forward($c->view('TTpart'));
196}
197
198=head1 AUTHOR
199
200Thauvin Olivier
201
202=head1 LICENSE
203
204This library is free software, you can redistribute it and/or modify
205it under the same terms as Perl itself.
206
207=cut
208
2091;
Note: See TracBrowser for help on using the repository browser.