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

Last change on this file since 2108 was 2074, checked in by nanardon, 7 years ago

All reset department at unumployment

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