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

Last change on this file since 1990 was 1329, checked in by nanardon, 9 years ago

Merge branch 'jquery'

File size: 5.2 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    );
132}
133
134sub check_ug_validity : Local {
135    my ( $self, $c ) = @_;
136    my $val = $c->req->param('val');
137    $c->stash->{validity} = LATMOS::Accounts::Utils::check_ug_validity($val);
138}
139
140sub select_freeip_select : Local {
141    my ($self, $c) = @_;
142
143}
144
145sub user_to_group_form : Local {
146    my ($self, $c, $user) = @_;
147   
148    my $base = $c->model('Accounts')->db;
149
150    $c->stash->{user} = $base->get_object('user', $user) or do {
151        return;
152    };
153
154    my %ingroups =
155        map { $_ => 1 } 
156        @{ $c->stash->{user}->get_c_field('memberOf') || []};
157
158   $c->stash->{othergroups} = [ grep { ! $ingroups{$_} }
159       $base->search_objects('group', 'cn~' . ($c->req->param('gfilter') || '*')) ]; 
160}
161
162sub goto : Local {
163    my ($self, $c) = @_;
164
165    my $objurl = $c->req->param('otype');
166    $objurl .= 's' unless ($objurl =~ /s$/);
167
168    $c->res->redirect(
169        $c->uri_for('/' . $objurl, $c->req->param('goto'),
170        $c->req->param('subform'))
171    );
172    $c->stash->{template} = 'no_object.tt';
173}
174
175sub set_attrs : Local {
176    my ($self, $c, $otype, $name) = @_;
177
178    my $base = $c->model('Accounts')->db;
179    my $object = $base->get_object($otype, $name) or do {
180        return;
181    };
182    my $form = $c->model('AttrForms', '', $object);
183    $form->set_attrs([ $c->req->param('attrs') ]);
184
185}
186
187sub list_objects : Local {
188    my ($self, $c, $otype) = @_;
189    $c->stash->{ofilter} = $c->model('AttrFilter', $otype);
190}
191
192sub select_objects_list : Local {
193    my ($self, $c, $otype) = @_;
194    $c->stash->{ofilter} = $c->model('AttrFilter', $otype);
195}
196
197sub end : Private {
198    my ( $self, $c ) = @_;
199    $c->forward($c->view('TTpart'));
200}
201
202=head1 AUTHOR
203
204Thauvin Olivier
205
206=head1 LICENSE
207
208This library is free software, you can redistribute it and/or modify
209it under the same terms as Perl itself.
210
211=cut
212
2131;
Note: See TracBrowser for help on using the repository browser.