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

Last change on this file since 2468 was 2468, checked in by nanardon, 3 years ago

Ensure anonymous request are anonymous

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