File:  [LON-CAPA] / loncom / lti / ltilogout.pm
Revision 1.1: download - view: text, annotated - select for diffs
Thu Jul 18 18:28:46 2019 UTC (4 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, version_2_11_4_msu, HEAD
- Bug 6754. LON-CAPA as LTI Provider.
  Domain configuration to support session expiration in LON-CAPA,
  after user logs out of LTI Consumer which originally launched session,
  (if Consumer supports logoutServiceUrl; e.g. custom_logout_url in Canvas).

# The LearningOnline Network with CAPA
# LTI Provider Module to respond to a user session logout request.
#
# $Id: ltilogout.pm,v 1.1 2019/07/18 18:28:46 raeburn Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#

package Apache::ltilogout;

use strict;
use Apache::Constants qw(:common :http);
use Apache::lonnet;
use Apache::loncommon;

sub handler {
    my $r = shift;
    my ($id) = ($r->uri =~ m{^/adm/service/logout/(\w+)$});
    if ($id) {
        my $dir = $r->dir_config('ltiIDsDir');
        my $lonhost = $r->dir_config('lonHostID');
        if (($dir ne '') && (-d $dir)) {
            if (-e "$dir/$id") {
                if (open(my $fh,'<',"$dir/$id")) {
                    my $contents = <$fh>;
                    chomp($contents);
                    close($fh);
                    my ($uname,$udom,$hostid) = split(/,/,$contents);
                    if (($uname ne '') && ($udom ne '')) {
                        my $uhome = &Apache::lonnet::homeserver($uname,$udom);
                        if ($uhome ne 'no_host') {
                            my $lonhost = $r->dir_config('lonHostID');
                            if ($hostid eq $lonhost) {
                                &delete_session($udom,$uname);
                            } else {
                                my ($is_balancer,$posshost,$setcookie) =
                                    &Apache::lonnet::check_loadbalancing($uname,$udom,'login');
                                if ($is_balancer) {
                                    if ($setcookie) {
                                        my $balancedir=$r->dir_config('lonBalanceDir');
                                        if (opendir(my $dirh,$balancedir)) {
                                            while (my $filename=readdir($dirh)) {
                                                if ($filename =~/^\Q$udom\E_\Q$uname\E_/) {
                                                    if (open(my $fh,'<',"$balancedir/$filename")) {
                                                        my $lonid = <$fh>;
                                                        close($fh);
                                                        if ($lonid eq $lonhost) {
                                                            &delete_session($r,$udom,$uname);
                                                        } elsif (&Apache::lonnet::hostname($lonid) ne '') {
                                                            &Apache::lonnet::delusersession($lonid,$udom,$uname);
                                                        }
                                                    }
                                                    last;
                                                }
                                            }
                                            closedir($dirh);
                                        }
                                    } else {
                                        my $lonid = &Apache::lonnet::find_existing_session($udom,$uname);
                                        if ($lonid eq $lonhost) {
                                            &delete_session($r,$udom,$uname);
                                        } elsif (&Apache::lonnet::hostname($lonid) ne '') {
                                            &Apache::lonnet::delusersession($lonid,$udom,$uname);
                                        }
                                    }
                                } else {
                                    &delete_session($r,$udom,$uname);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return OK;
}

sub delete_session {
    my ($r,$udom,$uname) = @_;
    my $lonidsdir = $r->dir_config('lonIDsDir');
    if (-d $lonidsdir) {
        if (opendir(my $dir,$lonidsdir)) {
            while (my $filename=readdir($dir)) {
                if ($filename=~/^\Q$uname\E_\d+_\Q$udom\E_/) {
                    unlink("$dir/$filename");
                    last;
                }
            }
        }
    }
}

1;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>