1: # The LearningOnline Network
2: # Verify user is in course
3: #
4: # $Id: loncourseuser.pm,v 1.1 2023/04/02 03:16:27 raeburn Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28:
29: package Apache::loncourseuser;
30:
31: use strict;
32: use Apache::Constants qw(:common :http);
33: use Apache::lonnet;
34: use Apache::loncommon;
35: use Apache::lonuserutils;
36: use JSON::DWIW;
37: use LONCAPA qw(:DEFAULT :match);
38:
39: sub handler {
40: my $r = shift;
41: &Apache::loncommon::content_type($r,'application/json');
42: $r->send_http_header;
43: my @userinfo;
44: if ($env{'request.course.id'}) {
45: my $canview;
46: foreach my $priv ('mgr','vgr') {
47: $canview = &Apache::lonnet::allowed($priv,$env{'request.course.id'});
48: if (!$canview && $env{'request.course.sec'} ne '') {
49: if (&Apache::lonnet::allowed($priv,"$env{'request.course.id'}/$env{'request.course.sec'}")) {
50: $canview = 'section';
51: }
52: }
53: last if ($canview);
54: }
55: if ($canview) {
56: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
57: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
58: my $crstype = &Apache::loncommon::course_type();
59: if (($cdom) && ($cnum)) {
60: my $possudom = $env{'form.udom'};
61: if (&Apache::lonnet::domain($possudom) ne '') {
62: my $possuname = $env{'form.uname'};
63: my $possuid = $env{'form.uid'};
64: if (($possuname eq '') && ($possuid ne '')) {
65: $possuname=(&Apache::lonnet::idget($possudom,[$possuid],'ids'))[1];
66: }
67: if ($possuname ne '') {
68: if (&is_course_user($possudom,$possuname,$cdom,$cnum,$canview,
69: $crstype,$env{'request.course.sec'})) {
70: @userinfo = ($possuname,$possudom);
71: }
72: }
73: }
74: }
75: }
76: }
77: $r->print(JSON::DWIW->to_json({match => \@userinfo}));
78: return OK;
79: }
80:
81: sub is_course_user {
82: my ($possudom,$possuname,$cdom,$cnum,$canview,$crstype,$section) = @_;
83: my $found;
84: my @types = ('active','future','previous');
85: my @roles = &Apache::lonuserutils::roles_by_context('course',1,$crstype);
86: my %userhash = &Apache::lonnet::get_my_roles($possuname,$possudom,'userroles',\@types,\@roles,[$cdom],1);
87: if (keys(%userhash)) {
88: foreach my $key (keys(%userhash)) {
89: next unless ($key =~ /^\Q$cnum:$cdom:\E/);
90: if ($canview eq 'section') {
91: my $usec = (split(/:/,$key))[-1];
92: if ($usec eq $section) {
93: $found = 1;
94: }
95: } else {
96: $found = 1;
97: }
98: last if ($found);
99: }
100: }
101: return $found;
102: }
103:
104: 1;
105:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>