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

Last change on this file since 1690 was 1690, checked in by nanardon, 8 years ago

typo

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