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