members.pm 16.1 KB
Newer Older
Florian Vessaz's avatar
Florian Vessaz committed
1 2
package members;

Florian Vessaz's avatar
Florian Vessaz committed
3 4 5
use strict;
use warnings 'FATAL' => 'all';

Florian Vessaz's avatar
Florian Vessaz committed
6 7
our $VERSION = '0.1';

Florian Vessaz's avatar
Florian Vessaz committed
8
use Dancer ':syntax';
9
use Email::Valid;
10
use PasswordToken;
11 12
use Data::Dumper;

13
use EPFL::People;
14
use GnuGeneration::Member;
Florian Vessaz's avatar
Florian Vessaz committed
15
use GnuGeneration::Group;
16 17
use PasswordToken;

18 19
use mail;

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
my $hosts = {
    "rainbowdash" => {
        name => "rainbowdash.gnugen.ch",
        description => "This is the server accessible to all members."},
    "morgarten" => {
        name => "morgarten.gnugen.ch",
        description => "This servers hosts the web applications."},
    "gnunas" => {
        name => "nas.gnugen.ch",
        description => "The NAS in the clubroom."},
};

my $mailing_lists = {
    'membres@lists.gnugen.ch' => "The list of GNU Generation members",
    'bbq@lists.gnugen.ch' => "Used to organize BBQs on campus",
};

my $shells = [ "/bin/bash", "/bin/fish", "/bin/zsh" ];
38

Florian Vessaz's avatar
Florian Vessaz committed
39 40 41 42 43 44 45 46 47 48 49 50
package members {
    sub setup {
        my ($ldap_dn, $ldap_password, $ldap_url, $dbfile) = @_;
        GnuGeneration::LDAP->credentials(
            dn => $ldap_dn,
            password => $ldap_password,
        );
        GnuGeneration::Member->ldap_url($ldap_url);
        PasswordToken->init(dbfile => $dbfile);
    }
}

51
sub is_admin() {
52 53 54 55 56 57 58 59 60 61 62 63
    my $is_admin = session 'admin';
    return $is_admin if (defined $is_admin);
    my $g = GnuGeneration::Group->new('comite');
    if ($g) {
        if ($g->contains(session 'username')) {
            session 'admin' => 1;
        } else {
            # Only cache definitive negative lookup: group was found and
            # user is not in it
            session 'admin' => 0;
        }
    }
64 65 66 67 68 69 70 71 72
}

sub logged_as($) {
    my $wanted = shift;
    my $username = session 'username';
    return 0 unless defined $username;
    return $wanted eq session 'username';
}

73 74 75 76 77 78 79 80
sub members_sort_func {
    my $ret = $a->{firstName} cmp $b->{firstName};
    if ($ret == 0) {
        $ret = $a->{lastName} cmp $b->{lastName};
    }
    return $ret;
}

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
# Temporary: we should make a request to our mlmmj-web app instead.
sub mlmmj_subscribe_member {
	my ($mail) = @_;
	my $prog = "/usr/local/bin/www-members-mlmmj-sub";
	if (not -x $prog) {
		warning "Cannot add member to mailing list: $prog is not executable";
		return;
	}
	my $child = open(my $fh, "|-", "sudo", $prog, $mail);
	unless ($child) {
		warning "Couldn't run $prog: $!";
		return;
	}
	close $fh;
	if ($? != 0) {
		warning "$prog exited with status $?";
	}
}

100
hook 'before' => sub {
101 102 103
    unless (request->path_info =~
            m{^/login$|^/session_expired$|^/member/[^/]+/password$})
    {
104
        unless (session 'username') {
105
            info "Redirect to /login from " . request->path_info;
106
            session 'redirect' => request->path_info;
107 108 109
            return redirect "/login";
        } elsif (not defined session 'timestamp'
                 or (session 'timestamp') < time - config->{session_expire}) {
Florian Vessaz's avatar
Florian Vessaz committed
110
            info "Session of " . session('username') . " has expired.";
111
            session 'redirect' => request->path_info;
112 113 114 115 116 117 118
            return redirect "/session_expired";
        } else {
            session 'timestamp' => time;
        }
    }
};

Florian Vessaz's avatar
Florian Vessaz committed
119
get '/' => sub {
120 121 122 123
    unless (is_admin) {
        redirect "/member/" . session('username');
    }
    template 'admin_home';
Florian Vessaz's avatar
Florian Vessaz committed
124 125
};

126 127 128 129 130 131 132 133 134 135 136 137 138
get '/login' => sub {
    template 'login';
};

post '/login' => sub {
    my $username = params->{username};
    my $password = params->{password};

    if (defined $username and defined $password) {
        my $member = GnuGeneration::Member->new(
            username => $username,
            password => $password);
        if (defined $member) {
139
            info "$username logged in.";
140 141
            session 'username' => $username;
            session 'timestamp' => time;
142 143
            my $redirect = (session 'redirect') // "/member/$username";
            session 'redirect' => undef;
Florian Vessaz's avatar
Florian Vessaz committed
144
            $redirect = '/' if is_admin;
145
            return redirect $redirect;
146 147 148 149 150 151
        }
    }

    redirect 'login';
};

Florian Vessaz's avatar
Florian Vessaz committed
152
sub logout {
153
    session 'username' => undef;
Florian Vessaz's avatar
Florian Vessaz committed
154
    session 'admin'    => undef;
155
    session->destroy;
Florian Vessaz's avatar
Florian Vessaz committed
156 157 158 159 160
}

any '/session_expired' => sub {
    info session('username') . "logged out due to expired session.";
    logout;
161 162 163 164 165
    template 'session_expired';
};


get '/logout' => sub {
Florian Vessaz's avatar
Florian Vessaz committed
166 167
    info session('username') . " logs out.";
    logout;
168 169 170
    redirect '/';
};

171 172 173 174 175 176 177 178 179 180 181 182 183 184
get '/member/:username' => sub {
    unless (is_admin or logged_as params->{username}) {
        status '403';
        return "Unauthorized";
    }

    my $member = GnuGeneration::Member->new(username => params->{username});
    unless (defined $member) {
        status '404';
        return 'Member not found';
    }

    template 'member', {
        'member' => $member,
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
        'hosts' => $hosts,
        'mailing_lists' => $mailing_lists,
        'shells' => $shells,
    };
};

post '/member/:username' => sub {
    unless (is_admin or logged_as params->{username}) {
        status '403';
        return "Unauthorized";
    }

    my $member = GnuGeneration::Member->new(username => params->{username});
    unless (defined $member) {
        status '404';
        return 'Member not found';
    }

    my $message;

    # Handle shell modification
206
    if (defined params->{shell} and not params->{shell} eq $member->shell) {
207 208 209 210 211 212 213 214 215 216 217 218 219 220
        if (grep {$_ eq params->{shell}} @$shells) {
            $member->shell(params->{shell});
            $message .= "Your shell has been set to " . $member->shell
                      . "<br/>";
        } else {
            $message .= "Invalid shell specified.<br/>";
        }
    }

    # Handle password modification
    if (params->{password} and params->{password_new}
        and params->{password_new_check})
    {
        if (params->{password_new} eq params->{password_new_check}) {
221 222 223 224 225 226 227 228 229
            my $m = GnuGeneration::Member->new(
                username => params->{username},
                password => params->{password});
            if (defined $m) {
                $m->password(params->{password_new});
                $message .= "Password changed.<br/>";
            } else {
                $message .= "Couldn't change password: old one is wrong.<br/>";
            }
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
        } else {
            $message .= "New passwords don't match.<br/>";
        }
    }

    # Handle ToS acceptation
    if (params->{tos_gnu} and params->{tos_epfl}) {
        unless ($member->tos_accepted) {
            $member->tos_accepted(1);
            $message .= "You accepted the term of service.<br/>";
        }
    } else {
        $member->tos_accepted(0);
        $message .= "You didn't accept the term of service.<br/>";
    }

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
    # Handle mobile modification
    if (params->{mobile} and not params->{mobile} eq $member->mobile) {
        if (params->{mobile} =~ m/^\+/) {
            $member->mobile(params->{mobile});
            $message .= "Your mobile phone number has been modified.<br/>";
        } else {
            $message .= "The mobile phone you specifed is malformed.<br/>";
        }
    }

    # Handle mail modification
    if (params->{mail}) {
        my $mail = Email::Valid->address(params->{mail});
        if ($mail) {
            if (not $mail eq $member->mail) {
                $member->mail(params->{mail});
                $message .= "Your mail address has been modified.<br/>";
263
                mlmmj_subscribe_member(params->{mail});
264 265 266 267 268 269
            }
        } else {
            $message .= "The mail address you specified is invalid.<br/>";
        }
    }

270 271 272 273 274 275
    template 'member', {
        'member' => $member,
        'hosts' => $hosts,
        'mailing_lists' => $mailing_lists,
        'shells' => $shells,
        'message' => $message,
276 277 278
    };
};

279
any ['get', 'post'] => '/subscriptions' => sub {
280 281 282 283
    unless (is_admin) {
        status '403';
        return "Unauthorized";
    }
284

285
    my $message;
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
    my @members = sort {
            ($b->subscription_expire <=> $a->subscription_expire)
            or ($b->subscription <=> $a->subscription)
            or ($a->username cmp $b->username)
        } values(%{GnuGeneration::Member->all_members});
    my %active_members_by_year;
    my %inactive_members_by_year;
    foreach my $member (@members) {
        my $year = POSIX::strftime("%Y", localtime $member->subscription_expire);
        if ($member->subscription_active) {
            push @{$active_members_by_year{$year}}, $member;
        } else {
            push @{$inactive_members_by_year{$year}}, $member;
        }
    }
301

302 303 304 305 306 307 308 309
    if (request->is_post) {
        foreach my $member (@members) {
            my $username = $member->username;
            my $pay_now = params->{$username . '-now'};
            my $date = params->{$username . '-date'};
            my $ts;
            if ($pay_now) {
                $ts = time;
310
            } else {
311 312 313 314 315 316 317
                $date =~ m/^(\d\d\d\d)-(\d\d?)-(\d\d?)/;
                if ($1 <= 1900 or $2 < 1 or $2 > 12 or $3 < 1 or $3 > 31) {
                    $message .= "Malformed date for $username.<br/>";
                    last;
                } else {
                    $ts = POSIX::mktime(0, 0, 0, $3, $2 - 1, $1 - 1900);
                }
318
            }
319

320 321 322 323 324 325 326 327 328
            if (defined $ts) {
                my $old_date = POSIX::strftime("%Y-%m-%d", localtime $member->subscription);
                my $new_date = POSIX::strftime("%Y-%m-%d", localtime $ts);
                unless ($old_date eq $new_date) {
                    $member->subscription($ts);
                    $member->shadow_expire_update;
                    my $expire = $member->subscription_expire_as_string;
                    $message .= "Updated subscription date for $username, expiration date is now $expire.<br/>";
                }
329 330 331
            }
        }
    }
332

333 334
    template 'subscriptions', {
        'message' => $message,
335 336 337
        'members' => \@members,
        'active_members_by_year' => \%active_members_by_year,
        'inactive_members_by_year' => \%inactive_members_by_year,
338
    };
339 340
};

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
get '/list/active' => sub {
    unless (is_admin) {
        status '403';
        return 'Unauthorized';
    }

    my $members = GnuGeneration::Member->all_members;
    my @members;
    foreach my $username (keys %$members) {
        if ($members->{$username}->active) {
            push @members, $members->{$username};
        }
    }
    @members = sort members_sort_func @members;

    template 'member_list', {
        'members' => \@members,
        'title' => 'Active member list',
    };
};

get '/list/all' => sub {
    unless (is_admin) {
        status '403';
        return 'Unauthorized';
    }

    my $members = GnuGeneration::Member->all_members;
    my @members = sort members_sort_func values %$members;

    template 'member_list', {
        'members' => \@members,
        'title' => 'All members (even old ones)',
    };
};

get '/list/inactive' => sub {
    unless (is_admin) {
        status '403';
        return 'Unauthorized';
    }

    my $members = GnuGeneration::Member->all_members;
    my @members;
    foreach my $username (keys %$members) {
        unless ($members->{$username}->active) {
            push @members, $members->{$username};
        }
    }
    @members = sort members_sort_func @members;

    template 'member_list', {
        'members' => \@members,
        'title' => 'Active member list',
    };
};
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413

get '/new_member' => sub {
    unless (is_admin) {
        status '403';
        return 'Unauthorized';
    }
    template 'new_member', {
        'shells' => $shells,
    };
};

post '/new_member' => sub {
    unless (is_admin) {
        status '403';
        return 'Unauthorized';
    }

414

415
    my $message;
416 417 418 419 420 421 422 423
    my %variables = (
        username   => params->{username},
        firstName  => params->{firstName},
        lastName   => params->{lastName},
        commonName => params->{commonName},
        mail       => params->{mail},
        mobile     => params->{mobile},
        shell      => params->{shell},
424
        membership => params->{paid},
425 426 427
    );

    if (defined params->{gaspar}) {
Florian Vessaz's avatar
Florian Vessaz committed
428
        # Query information on ldap.epfl.ch
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
        my $e = EPFL::People->new(gaspar => params->{gaspar});
        %variables = ( %variables,
            username   => $e->gaspar,
            firstName  => $e->firstName,
            lastName   => $e->lastName,
            commonName => $e->name,
            mail       => $e->email,
        );
    } else {
        # Create the new member
        my @required = qw( commonName firstName lastName mail username shell );
        if (grep(!defined, map {params->{$_}} @required)) {
            $message = "Some information is missing.";
        } else {
            my $m = GnuGeneration::Member->create(
                cn => params->{commonName},
                firstName => params->{firstName},
                lastName => params->{lastName},
                mail => params->{mail},
                mobile => params->{mobile},
                username => params->{username},
                password => params->{password},
                shell => params->{shell}
            );
            if ($m) {
454
                if (params->{paid}) {
455 456 457 458 459 460
                    $m->subscription(time);
                }
                my $reset_link = password_reset_link($m->username);
                mail::send_welcome_mail(
                    member => $m,
                    password_link => $reset_link);
461
                mlmmj_subscribe_member(params->{mail});
462 463 464 465 466 467 468 469 470 471 472 473
                return redirect "/member/" . $m->username;
            } else {
                $message = "Couldn't create new member.";
            }
        }
    }

    template 'new_member', {
        shells => $shells,
        message => $message,
        %variables,
    };
474 475 476 477 478 479 480 481 482 483 484 485 486 487
};

any '/passwords' => sub {
    unless (is_admin) {
        status '403';
        return 'Unauthorized';
    }

    my $message;
    if (defined params->{delete_for}) {
        PasswordToken->delete_all(params->{delete_for});
        $message .= "Tokens for " . params->{delete_for} . " were deleted.";
    }

488 489 490 491 492 493
    my $create_for = params->{create_for};
    if (defined $create_for) {
        my $member = GnuGeneration::Member->new(username => $create_for);
        if ($member) {
            my $link = password_reset_link(params->{create_for});
            mail::send_password_reset(member => $member, password_link => $link);
Florian Vessaz's avatar
Florian Vessaz committed
494 495
            $message .= "Password reset link for "
                     .  $member->username . " sent to " . $member->mail;
496 497 498
        } else {
            $message .= "No such user";
        }
499 500 501 502
    }

    template 'passwords', {
        'message' => $message,
503
        'token_map' => PasswordToken->count_by_user,
504 505 506
    };
};

507 508 509 510 511 512 513 514

# Create a password reset link for the given username.
sub password_reset_link {
    my $username = shift;
    my $token = PasswordToken->create($username);
    return "/member/" . $username . "/password?token=" . $token;
};

515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
# Return true if the token is valid for the given username.
sub check_password_token {
    my ($username, $token) = @_;
    return unless defined $username;
    my $owner = PasswordToken->check($token);
    return (defined $owner and $username eq $owner);
};

get '/member/:username/password' => sub {
    my $username = params->{username};
    my $token = params->{token};
    unless (defined $token) {
        status '403';
        return "Unauthorized (no token)";
    }

    unless (check_password_token($username, $token)) {
        status '403';
        return "Unauthorized (no such token)";
    }

    template 'password_token', {
        'username' => $username,
        'token' => $token,
    };
};

post '/member/:username/password' => sub {
    my $token = params->{token};
    my $username = params->{username};
    unless (defined $token) {
        status '403';
        return "Unauthorized (no token)";
    }

    unless (check_password_token($username, $token)) {
        status '403';
        return "Unauthorized (no such token)";
    }

    my $message;
    if (params->{password_new} and params->{password_new_check}) {
        if (params->{password_new} eq params->{password_new_check}) {
            my $member = GnuGeneration::Member->new(username => $username);
            $member->password(params->{password_new});
            PasswordToken->delete_all($username);
            return redirect "/login";
        } else {
            $message .= "Passwords don't match.";
        }
    }

    template 'password_token', {
        'username' => $username,
        'token' => $token,
        'message' => $message,
    };
};

Florian Vessaz's avatar
Florian Vessaz committed
574
true;