source: trunk/LATMOS-Accounts-Web/lib/LATMOS/Accounts/Web/Controller/Root.pm @ 1622

Last change on this file since 1622 was 1609, checked in by nanardon, 9 years ago

Fix redirect when login

  • Property svn:keywords set to Id Rev
File size: 3.5 KB
Line 
1package LATMOS::Accounts::Web::Controller::Root;
2
3use strict;
4use warnings;
5use base 'Catalyst::Controller';
6no utf8;
7use POSIX;
8
9#
10# Sets the actions in this controller to be registered with no prefix
11# so they function identically to actions created in MyApp.pm
12#
13__PACKAGE__->config->{namespace} = '';
14
15=head1 NAME
16
17LATMOS::Accounts::Web::Controller::Root - Root Controller for LATMOS::Accounts::Web
18
19=head1 DESCRIPTION
20
21[enter your description here]
22
23=head1 METHODS
24
25=cut
26
27sub component_disabled : Private {
28    my ( $self, $c ) = @_;
29    $c->res->status(403);
30}
31
32sub begin : Private {
33    my ( $self, $c ) = @_;
34
35    my ($locale) = split(',', $c->req->header('Accept-Language') || '');
36    $locale =~ s/(\s\s)/$1/;
37    if (my $plocale = ($c->req->param('locale')|| $c->session->{'locale'})) {
38        $c->session->{'locale'} = $locale = $plocale;
39    }
40    POSIX::setlocale(POSIX::LC_ALL, $locale);
41    $c->languages($locale ? [ $locale ] : '');
42
43    if ($c->user_exists) {
44        # Set login user:
45        $c->model('Accounts')->db->{_user} = $c->user->{username};
46    } else {
47        # No need to login for About section
48        $c->forward('needlogin');
49        return;
50    }
51}
52
53=head2 default
54
55=cut
56
57sub default : Private {
58    my ( $self, $c ) = @_;
59    $c->res->redirect($c->uri_for('/users', $c->user->{username})) if($c->user);
60}
61
62sub needlogin : Private {
63    my ( $self, $c ) = @_;
64
65    $c->session->{redirurl} ||= $c->req->uri;
66    $c->session->{redirparam} ||= $c->req->params;
67
68    my $sslid = $c->config->{ssl}->{SSL_ID} || 'HTTP_SSL_CLIENT_S_DN_EMAIL';
69    $c->stash->{sslid} = $ENV{$sslid};
70
71    $c->stash->{template} = 'login.tt';
72}
73
74sub login : Local {
75    my ( $self, $c ) = @_;
76
77    my $sslid = $c->config->{ssl}->{SSL_ID} || 'HTTP_SSL_CLIENT_S_DN_EMAIL';
78    $c->stash->{sslid} = $ENV{$sslid};
79
80    $c->stash->{template} = 'login.tt';
81    if ($c->req->param('username')) {
82        if ($c->authenticate({
83                username => $c->req->param('username') || '',
84                password => $c->req->param('password') || '',
85                ssl      => $c->req->param('use_ssl')  || '',
86        })) {
87            my $redirurl = $c->session->{redirurl};
88            $redirurl->query_form(%{ $c->session->{redirparam} || {}});
89            $c->session->{redirurl} = undef;
90            $c->session->{redirparam} = undef;
91            $c->res->redirect($redirurl || $c->uri_for('/'), );
92        } else {
93            # invalid login...
94        }
95    }
96}
97
98sub no_object : Local {
99    my ($self, $c) = @_;
100    $c->stash->{template} = 'no_object.tt';
101}
102
103sub obj_to_uri : Private {
104    my ($self, $c, $ref) = @_;
105    my $uri_part = {
106        user => 'users',
107        group => 'groups',
108        nethost => 'nethosts',
109        netzone => 'netzones',
110        site => 'sites',
111        aliases => 'aliases',
112        service => 'services',
113    }->{$ref} || $ref;
114}
115
116sub obj_to_label : Private {
117    my ($self, $c, $ref) = @_;
118    my $uri_part = {
119        user => 'Utilisateurs',
120        group => 'Groupes',
121        nethost => 'HÃŽtes réseaux',
122        netzone => 'Zones réseau',
123        site => 'Sites',
124        aliases => 'Alias',
125        service => 'Services',
126    }->{$ref} || $ref;
127}
128
129=head2 end
130
131Attempt to render a view, if needed.
132
133=cut
134
135sub end : ActionClass('RenderView') {
136    my ($self, $c) = @_;
137    $c->forward($c->view('TT')) unless($c->res->body);
138    $c->model('Accounts')->db->rollback;
139    $c->model('Accounts')->call_batch_sync;
140}
141
142=head1 AUTHOR
143
144Thauvin Olivier
145
146=head1 LICENSE
147
148This library is free software, you can redistribute it and/or modify
149it under the same terms as Perl itself.
150
151=cut
152
1531;
Note: See TracBrowser for help on using the repository browser.