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

Last change on this file since 2469 was 2295, checked in by nanardon, 5 years ago

Fix: random password generation

File size: 5.4 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    my $user = $c->model('Accounts')->base->get_object('user', $c->req->param('user'))
129        or return;
130
131    $c->stash->{random_password} =
132    LATMOS::Accounts::Utils::genpassword(
133        %{ $c->req->params() },
134        checkpassword => sub {
135            $user->check_password(@_)
136        },
137    );
138}
139
140sub check_ug_validity : Local {
141    my ( $self, $c ) = @_;
142    my $val = $c->req->param('val');
143    $c->stash->{validity} = LATMOS::Accounts::Utils::check_ug_validity($val);
144}
145
146sub select_freeip_select : Local {
147    my ($self, $c) = @_;
148
149}
150
151sub 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
168sub goto : Local {
169    my ($self, $c) = @_;
170
171    my $objurl = $c->req->param('otype');
172    $objurl .= 's' unless ($objurl =~ /s$/);
173
174    $c->res->redirect(
175        $c->uri_for('/' . $objurl, $c->req->param('goto'),
176        $c->req->param('subform'))
177    );
178    $c->stash->{template} = 'no_object.tt';
179}
180
181sub set_attrs : Local {
182    my ($self, $c, $otype, $name) = @_;
183
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
193sub list_objects : Local {
194    my ($self, $c, $otype) = @_;
195    $c->stash->{ofilter} = $c->model('AttrFilter', $otype);
196}
197
198sub select_objects_list : Local {
199    my ($self, $c, $otype) = @_;
200    $c->stash->{ofilter} = $c->model('AttrFilter', $otype);
201}
202
203sub end : Private {
204    my ( $self, $c ) = @_;
205    $c->forward($c->view('TTpart'));
206}
207
208=head1 AUTHOR
209
210Thauvin Olivier
211
212=head1 LICENSE
213
214This library is free software, you can redistribute it and/or modify
215it under the same terms as Perl itself.
216
217=cut
218
2191;
Note: See TracBrowser for help on using the repository browser.