Invitation.pm 11.8 KB
Newer Older
1
2
3
4
5
6
7
# vim:set sw=4 ts=4 sts=4 ft=perl expandtab:
package Lufi::Controller::Invitation;
use Mojo::Base 'Mojolicious::Controller';
use Mojo::Collection 'c';
use Mojo::File;
use Mojo::JSON qw(true false decode_json encode_json);
use Mojo::URL;
8
use Mojo::Util qw(decode encode);
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use Email::Valid;
use Lufi::DB::File;
use Lufi::DB::Invitation;
use Date::Format;

sub new_invite {
    my $c = shift;

    # The `if (defined($c->config('ldap')))` is at the router level in lib/Lufi.pm
    if ($c->is_user_authenticated) {
        my $mail_attr            = $c->config('invitations')->{'mail_attr'} // 'mail';
        my $max_expire_at        = $c->config('invitations')->{'max_invitation_expiration_delay'} // 30;
        my $send_with_user_email = defined $c->config('invitations')->{'send_invitation_with_ldap_user_mail'};
        $c->render(
            template             => 'invitations/invite',
            max_expire_at        => $max_expire_at,
            send_with_user_email => $send_with_user_email,
            user_mail            => ($send_with_user_email) ? $c->current_user->{$mail_attr} : '',
            fails                => [],
            success              => []
        );
    } else {
        $c->redirect_to($c->url_for('login')->query(redirect => 'invite'));
    }
}

sub send_invite {
    my $c          = shift;
    my $guest_mail = $c->param('guest_mail');
    my $expire_at  = $c->param('expire_at');

    my $mail_attr            = $c->config('invitations')->{'mail_attr'} // 'mail';
    my $max_expire_at        = $c->config('invitations')->{'max_invitation_expiration_delay'} // 30;
    my $send_with_user_email = defined $c->config('invitations')->{'send_invitation_with_ldap_user_mail'};

    # The `if (defined($c->config('ldap')))` is at the router level in lib/Lufi.pm
    if ($c->is_user_authenticated) {
        my @fails   = ();
        my @success = ();
        unless (Email::Valid->address($guest_mail)) {
            push @fails, $c->l('The guest email address (%1) is unvalid.', $guest_mail);
        }
        unless ($expire_at >= 1 && $expire_at <= $max_expire_at) {
            push @fails, $c->l('The expiration delay (%1) is not between 1 and %2 days.', $expire_at, $max_expire_at);
        }

        unless (scalar(@fails)) {
            my $invitation       = Lufi::DB::Invitation->new(app => $c->app);
            my $mail_attr        = $c->config('invitations')->{'mail_attr'} // 'mail';
58
            my $expend_expire_at = $c->config('invitations')->{'max_additional_period'} // 10;
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

            my $token;
            do {
                $token = $c->create_invitation_token;
            } while ($invitation->is_token_used($token));

            $invitation = $invitation->from_token($token);
            $invitation->ldap_user($c->current_user->{username});
            $invitation->ldap_user_mail($c->current_user->{$mail_attr});
            $invitation->created_at(time);
            $invitation->guest_mail($guest_mail);
            $invitation->expire_at($invitation->created_at + 86400 * $expire_at);
            $invitation->expend_expire_at($expend_expire_at);
            $invitation->show_in_list(1);
            $invitation = $invitation->write;

            my $from = ($c->config('invitations')->{'send_invitation_with_ldap_user_mail'}) ? $invitation->ldap_user_mail : $c->config('mail_sender');
            my $url  = $c->url_for('guest', token => $invitation->token)->to_abs;
            $c->mail(
                from       => $from,
                to         => $invitation->guest_mail,
                template   => 'invitations/invite',
                format     => 'mail',
                ldap_user  => ucfirst($invitation->ldap_user),
                url        => $url,
                invitation => $invitation,
85
                expires    => $c->get_date_lang()->time2str($c->l('%A %d %B %Y at %T'), $invitation->expire_at)
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
            );

            push @success, $c->l('Invitation sent to %1.<br> URL: %2', $invitation->guest_mail, $url);
        }

        $c->render(
            template             => 'invitations/invite',
            max_expire_at        => $max_expire_at,
            send_with_user_email => $send_with_user_email,
            user_mail            => ($send_with_user_email) ? $c->current_user->{$mail_attr} : '',
            fails                => \@fails,
            success              => \@success
        );
    } else {
        $c->redirect_to('login');
    }
}

sub my_invitations {
    my $c = shift;

    # The `if (defined($c->config('ldap')))` is at the router level in lib/Lufi.pm
    if ($c->is_user_authenticated) {
        my $invitations = Lufi::DB::Invitation->new(app => $c->app)
                                              ->from_user($c->current_user->{username});
        $invitations = c() unless $invitations;
        $c->render(
            template    => 'invitations/my_invitations',
            invitations => $invitations
        );
    } else {
        $c->redirect_to($c->url_for('login')->query(redirect => 'my_invitations'));
    }
}

sub delete_invitations {
    my $c = shift;
    my @tokens = @{$c->every_param('tokens[]')};

125
126
127
128
129
130
131
132
133
134
135
136
137
138
    if ($c->is_user_authenticated) {
        my @result   = ();
        my @failures = ();
        for my $token (@tokens) {
            my $i = Lufi::DB::Invitation->new(app => $c->app)
                                        ->from_token($token);
            if ($i->ldap_user eq $c->current_user->{username}) {
                $i->deleted(1)
                  ->write;
                push @result, { msg => $c->l('The invitation %1 has been deleted.', $i->token), token => $i->token, deleted => $i->deleted };
            } else {
                push @failures, $c->l('The invitation %1 can’t be deleted: it wasn’t created by you (%2).', $i->token, $c->current_user->{username});
            }
        }
139

140
141
142
143
144
145
146
147
        $c->render(json => {
            success  => (scalar(@result) > 0) ? true : false,
            tokens   => \@result,
            failures => \@failures
        });
    } else {
        $c->redirect_to($c->url_for('login')->query(redirect => 'my_invitations'));
    }
148
149
150
151
152
153
}

sub resend_invitations {
    my $c = shift;
    my @tokens = @{$c->every_param('tokens[]')};

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    if ($c->is_user_authenticated) {
        my @success  = ();
        my @failures = ();
        for my $token (@tokens) {
            my $i = Lufi::DB::Invitation->new(app => $c->app)
                                        ->from_token($token);

            if ($i->ldap_user eq $c->current_user->{username}) {
                if ($i->files_sent_at) {
                    push @failures, $c->l('The invitation %1 can’t be resent: %2 has already sent files.<br>Please create a new invitation.', $i->token, $i->guest_mail);
                } else {
                    if ($c->config('invitations')->{'extend_invitation_expiration_on_resend'}) {
                        $i->expire_at(time + $i->expire_at - $i->created_at)
                          ->write;
                    }

                    my $from   = ($c->config('invitations')->{'send_invitation_with_ldap_user_mail'}) ? $i->ldap_user_mail : $c->config('mail_sender');
                    my $url    = $c->url_for('guest', token => $i->token)->to_abs;
                    my $expire = $c->get_date_lang()->time2str($c->l('%A %d %B %Y at %T'), $i->expire_at);
                    $c->mail(
                        from       => $from,
                        to         => $i->guest_mail,
                        template   => 'invitations/invite',
                        format     => 'mail',
                        ldap_user  => ucfirst($i->ldap_user),
                        url        => $url,
                        invitation => $i,
                        expires    => $expire
                    );

                    push @success, { msg => $c->l('Invitation resent to %1.<br> URL: %2', $i->guest_mail, $url), expires => $expire, token => $i->token };
                }
            } else {
                push @failures, $c->l('The invitation %1 can’t be resent: it wasn’t created by you (%2).', $i->token, $c->current_user->{username});
188
189
190
            }
        }

191
192
193
194
195
196
197
        $c->render(json => {
            success  => \@success,
            failures => \@failures
        });
    } else {
        $c->redirect_to($c->url_for('login')->query(redirect => 'my_invitations'));
    }
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
}

sub toggle_invitations_visibility {
    my $c = shift;
    my @tokens = @{$c->every_param('tokens[]')};

    my @result = ();
    for my $token (@tokens) {
        my $i = Lufi::DB::Invitation->new(app => $c->app)
                                    ->from_token($token)
                                    ->toggle_visibility;
        push @result, { token => $i->token, show => ($i->show_in_list) ? true : false }
    }

    $c->render(json => {
        success => true,
        tokens  => \@result
    });
}

sub guest {
    my $c = shift;
    my $token = $c->param('token');

    my $invitation = Lufi::DB::Invitation->new(app => $c->app)->from_token($token);
    if ($invitation) {
        if ($invitation->is_valid) {
            $c->session->{guest_token} = $token;
            $c->session(expires => $invitation->expire_at);
            return $c->render(
                template   => 'index',
                invitation => $invitation
            );
        } else {
            $c->stash('expired_or_deleted_invitation' => 1);
        }
    } else {
        $c->stash('invitation_not_found' => 1);
    }
    return $c->render(template => 'invitations/exception');
}

sub send_mail_to_ldap_user {
    my $c = shift;
    my $token = $c->param('token');
    my $urls  = c(@{$c->every_param('urls[]')});

    my $invitation = Lufi::DB::Invitation->new(app => $c->app)->from_token($token);
    if ($invitation) {
        my @files = ();
        if ($c->config('invitations')->{'save_files_url_in_db'} && $urls->size) {
            my $guest_files = $invitation->files;
250
            my %list_token;
251
            if ($guest_files) {
252
                $guest_files = decode_json(encode 'UTF-8', $guest_files);
253
254
255
                for my $file (@{$guest_files}) {
                    $list_token{$file->{token}} = 1;
                }
256
257
258
259
260
261
            } else {
                $guest_files = [];
            }
            push @files, @{$guest_files};
            $urls->each(sub {
                my ($e, $num) = @_;
262
                $e = decode_json(encode 'UTF-8', $e);
263
264
265
266
                if (!defined($list_token{$e->{token}})) {
                    push @{$guest_files}, $e;
                    push @files, $e;
                }
267
            });
268
            $invitation->files(decode 'UTF-8', encode_json($guest_files));
269
270
271
            $invitation->write;
        } else {
            $urls->each(sub {
272
                push @files, decode_json(encode 'UTF-8', shift);
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
            });
        }
        my $already_notified = 1;
        unless ($invitation->files_sent_at) {
            $invitation->files_sent_at(time);
            $invitation->write;
            $already_notified = 0;
        }
        $c->session(expires => $invitation->files_sent_at + 60 * $invitation->expend_expire_at);
        $c->mail(
            from             => $c->config('mail_sender'),
            to               => $invitation->ldap_user_mail,
            template         => 'invitations/notification_files_sent',
            format           => 'mail',
            files            => c(@files),
            invitation       => $invitation,
            already_notified => $already_notified
        );

        return $c->render(
            json => {
                success => true,
                msg     => $c->l('The URLs of your files have been sent by email to %1.', $invitation->ldap_user_mail)
            }
        );
    } else {
        return $c->render(
            json => {
                success => false,
                msg     => $c->l('Sorry, the invitation doesn’t exist. Are you sure you are on the right URL?')
            }
        );
    }
}

1;