Annotation of loncom/lti/ltiauth.pm, revision 1.16
1.1 raeburn 1: # The LearningOnline Network
2: # Basic LTI Authentication Module
3: #
1.16 ! raeburn 4: # $Id: ltiauth.pm,v 1.15 2018/06/30 23:58:14 raeburn Exp $
1.1 raeburn 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::ltiauth;
30:
31: use strict;
32: use LONCAPA qw(:DEFAULT :match);
33: use Apache::Constants qw(:common :http);
34: use Net::OAuth;
35: use Apache::lonlocal;
36: use Apache::lonnet;
37: use Apache::loncommon;
38: use Apache::lonacc;
1.6 raeburn 39: use Apache::lonrequestcourse;
1.2 raeburn 40: use LONCAPA::ltiutils;
1.1 raeburn 41:
42: sub handler {
43: my $r = shift;
44: my $requri = $r->uri;
45: #
1.9 raeburn 46: # Check for existing session, and temporarily delete any form items
47: # in %env, if session exists
48: #
49: my %savedform;
50: my $handle = &Apache::lonnet::check_for_valid_session($r);
51: if ($handle ne '') {
52: foreach my $key (sort(keys(%env))) {
53: if ($key =~ /^form\.(.+)$/) {
54: $savedform{$1} = $env{$key};
55: delete($env{$key});
56: }
57: }
58: }
59: #
1.14 raeburn 60: # Retrieve data POSTed by LTI Consumer on launch
1.1 raeburn 61: #
62: &Apache::lonacc::get_posted_cgi($r);
63: my $params = {};
64: foreach my $key (sort(keys(%env))) {
65: if ($key =~ /^form\.(.+)$/) {
66: $params->{$1} = $env{$key};
67: }
68: }
1.9 raeburn 69: #
70: # Check for existing session, and restored temporarily
71: # deleted form items to %env, if session exists.
72: #
73: if ($handle ne '') {
74: if (keys(%savedform)) {
75: foreach my $key (sort(keys(%savedform))) {
76: $env{'form.'.$key} = $savedform{$key};
77: }
78: }
79: }
1.1 raeburn 80:
81: unless (keys(%{$params})) {
82: &invalid_request($r,1);
83: return OK;
84: }
85:
86: unless ($params->{'oauth_consumer_key'} &&
87: $params->{'oauth_nonce'} &&
88: $params->{'oauth_timestamp'} &&
89: $params->{'oauth_version'} &&
90: $params->{'oauth_signature'} &&
91: $params->{'oauth_signature_method'}) {
92: &invalid_request($r,2);
93: return OK;
94: }
95:
96: #
97: # Retrieve "internet domains" for all this institution's LON-CAPA
98: # nodes.
99: #
100: my ($udom,$uname,$uhome,$cdom,$cnum,$symb,$mapurl,@intdoms);
101: my $lonhost = $r->dir_config('lonHostID');
102: my $internet_names = &Apache::lonnet::get_internet_names($lonhost);
103: if (ref($internet_names) eq 'ARRAY') {
104: @intdoms = @{$internet_names};
105: }
106:
107: #
108: # For user who launched LTI in Consumer, determine user's domain in
109: # LON-CAPA.
110: #
111: # Order is:
112: #
113: # (a) from custom_userdomain item in POSTed data
114: # (b) from lis_person_sourcedid in POSTed data
115: # (c) from default "log-in" domain for node
116: # (can support multidomain servers, where specific domain is
117: # first part of hostname).
118: #
119: # Note: "internet domain" for user's domain must be one of the
120: # "internet domain(s)" for the institution's LON-CAPA servers.
121: #
122: if (exists($params->{'custom_userdomain'})) {
123: if ($params->{'custom_userdomain'} =~ /^$match_domain$/) {
124: my $uprimary_id = &Apache::lonnet::domain($params->{'custom_userdomain'},'primary');
125: if ($uprimary_id ne '') {
126: my $uintdom = &Apache::lonnet::internet_dom($uprimary_id);
127: if (($uintdom ne '') && (grep(/^\Q$uintdom\E$/,@intdoms))) {
128: $udom = $params->{'custom_userdomain'};
129: }
130: }
131: }
132: }
133: my $defdom = &Apache::lonnet::default_login_domain();
134: my ($domain,$possuname,$possudom,$possmapuser);
135: if ($env{'form.lis_person_sourcedid'} =~ /^($match_username)\:($match_domain)$/) {
136: ($possuname,$possudom) = ($1,$2);
137: if ($udom eq '') {
138: my $uintdom = &Apache::lonnet::domain($possudom,'primary');
139: if (($uintdom ne '') && (grep(/^\Q$uintdom\E$/,@intdoms))) {
140: $udom = $possudom;
141: $possmapuser = 'lis_person_sourcedid';
142: } else {
143: $udom = $defdom;
144: }
145: } elsif ($udom eq $possudom) {
146: $possmapuser = 'lis_person_sourcedid';
147: }
148: }
149: unless ($possuname) {
150: if ($env{'form.lis_person_sourcedid'} =~ /^$match_username$/) {
151: $possuname = $env{'form.lis_person_sourcedid'};
152: $possmapuser = 'lis_person_sourcedid';
153: } elsif ($env{'form.lis_person_contact_email_primary'} =~ /^$match_username$/) {
154: $possuname = $env{'form.lis_person_contact_email_primary'};
155: $possmapuser = 'lis_person_contact_email_primary';
156: }
157: unless ($udom) {
158: $udom = $defdom;
159: }
160: }
161:
162: #
163: # Determine course's domain in LON-CAPA
164: #
165: # Order is:
166: #
167: # (a) from custom_coursedomain item in POSTed data
1.9 raeburn 168: # (b) from tail of requested URL (after /adm/lti/) if it has format of a symb
1.1 raeburn 169: # (c) from tail of requested URL (after /adm/lti) if it has format of a map
170: # (d) from tail of requested URL (after /adm/lti) if it has format /domain/courseID
1.5 raeburn 171: # (e) from tail of requested URL (after /adm/lti) if it has format /tiny/domain/\w+
172: # i.e., a shortened URL (see bug #6400).
1.1 raeburn 173: # (f) same as user's domain
174: #
175: # Request invalid if custom_coursedomain is defined and is inconsistent with
176: # domain contained in requested URL.
177: #
178: # Note: "internet domain" for course's domain must be one of the
179: # internet domains for the institution's LON-CAPA servers.
180: #
181:
182: if (exists($params->{'custom_coursedomain'})) {
183: if ($params->{'custom_coursedomain'} =~ /^$match_domain$/) {
184: my $cprimary_id = &Apache::lonnet::domain($params->{'custom_coursedomain'},'primary');
185: if ($cprimary_id ne '') {
186: my $cintdom = &Apache::lonnet::internet_dom($cprimary_id);
187: if (($cintdom ne '') && (grep(/^\Q$cintdom\E$/,@intdoms))) {
188: $cdom = $params->{'custom_coursedomain'};
189: }
190: }
191: }
192: }
193:
194: my ($tail) = ($requri =~ m{^/adm/lti(|/.*)$});
195: my $urlcnum;
196: if ($tail ne '') {
197: my $urlcdom;
198: if ($tail =~ m{^/uploaded/($match_domain)/($match_courseid)/(?:default|supplemental)(?:|_\d+)\.(?:sequence|page)(|___\d+___.+)$}) {
199: ($urlcdom,$urlcnum,my $rest) = ($1,$2,$3);
200: if (($cdom ne '') && ($cdom ne $urlcdom)) {
201: &invalid_request($r,3);
202: return OK;
203: }
204: if ($rest eq '') {
205: $mapurl = $tail;
206: } else {
207: $symb = $tail;
1.16 ! raeburn 208: $symb =~ s{^/}{};
1.1 raeburn 209: }
1.9 raeburn 210: } elsif ($tail =~ m{^/res/(?:$match_domain)/(?:$match_username)/.+\.(?:sequence|page)(|___\d+___.+)$}) {
211: if ($1 eq '') {
212: $mapurl = $tail;
213: } else {
214: $symb = $tail;
1.16 ! raeburn 215: $symb =~ s{^/res/}{};
1.9 raeburn 216: }
1.1 raeburn 217: } elsif ($tail =~ m{^/($match_domain)/($match_courseid)$}) {
218: ($urlcdom,$urlcnum) = ($1,$2);
219: if (($cdom ne '') && ($cdom ne $urlcdom)) {
220: &invalid_request($r,4);
221: return OK;
222: }
1.5 raeburn 223: } elsif ($tail =~ m{^/tiny/($match_domain)/(\w+)$}) {
224: ($urlcdom,my $key) = ($1,$2);
225: if (($cdom ne '') && ($cdom ne $urlcdom)) {
226: &invalid_request($r,5);
227: return OK;
228: }
229: my $tinyurl;
230: my ($result,$cached)=&Apache::lonnet::is_cached_new('tiny',$urlcdom."\0".$key);
231: if (defined($cached)) {
232: $tinyurl = $result;
233: } else {
234: my $configuname = &Apache::lonnet::get_domainconfiguser($urlcdom);
235: my %currtiny = &Apache::lonnet::get('tiny',[$key],$urlcdom,$configuname);
236: if ($currtiny{$key} ne '') {
237: $tinyurl = $currtiny{$key};
238: &Apache::lonnet::do_cache_new('tiny',$urlcdom."\0".$key,$currtiny{$key},600);
239: }
240: }
241: if ($tinyurl ne '') {
242: $urlcnum = (split(/\&/,$tinyurl))[0];
243: }
1.1 raeburn 244: }
245: if (($cdom eq '') && ($urlcdom ne '')) {
246: my $cprimary_id = &Apache::lonnet::domain($urlcdom,'primary');
247: if ($cprimary_id ne '') {
248: my $cintdom = &Apache::lonnet::internet_dom($cprimary_id);
249: if (($cintdom ne '') && (grep(/^\Q$cintdom\E$/,@intdoms))) {
250: $cdom = $urlcdom;
251: }
252: } else {
253: $urlcnum = '';
254: }
255: }
256: }
257: if ($cdom eq '') {
258: if ($udom ne '') {
259: $cdom = $udom;
260: } else {
261: $cdom = $defdom;
262: }
263: }
264:
265: #
266: # Retrieve information for LTI Consumers in course domain
267: # and populate hash -- %lti_by_key -- for which keys
268: # are those defined in domain configuration for LTI.
269: #
270:
271: my %lti = &Apache::lonnet::get_domain_lti($cdom,'provider');
272: unless (keys(%lti) > 0) {
1.5 raeburn 273: &invalid_request($r,6);
1.1 raeburn 274: return OK;
275: }
276: my %lti_by_key;
277: if (keys(%lti)) {
278: foreach my $id (keys(%lti)) {
279: if (ref($lti{$id}) eq 'HASH') {
280: my $key = $lti{$id}{'key'};
281: push(@{$lti_by_key{$key}},$id);
282: }
283: }
284: }
285:
286: #
287: # Verify the signed request using the secret for those
288: # Consumers for which the key in the POSTed data matches
289: # keys in the domain configuration for LTI.
290: #
291: my $hostname = $r->hostname;
292: my $protocol = 'http';
293: if ($ENV{'SERVER_PORT'} == 443) {
294: $protocol = 'https';
295: }
296:
1.12 raeburn 297: if (exists($params->{'oauth_callback'})) {
298: $Net::OAuth::PROTOCOL_VERSION = Net::OAuth::PROTOCOL_VERSION_1_0A;
299: } else {
300: $Net::OAuth::PROTOCOL_VERSION = Net::OAuth::PROTOCOL_VERSION_1_0;
301: }
302:
1.6 raeburn 303: my ($itemid,$consumer_key,$secret);
1.3 raeburn 304: $consumer_key = $params->{'oauth_consumer_key'};
305: if (ref($lti_by_key{$consumer_key}) eq 'ARRAY') {
306: foreach my $id (@{$lti_by_key{$consumer_key}}) {
1.1 raeburn 307: if (ref($lti{$id}) eq 'HASH') {
1.2 raeburn 308: $secret = $lti{$id}{'secret'};
1.1 raeburn 309: my $request = Net::OAuth->request('request token')->from_hash($params,
310: request_url => $protocol.'://'.$hostname.$requri,
311: request_method => $env{'request.method'},
312: consumer_secret => $secret,);
313: if ($request->verify()) {
314: $itemid = $id;
315: last;
316: }
317: }
318: }
319: }
320:
321: #
322: # Request is invalid if the signed request could not be verified
323: # for the Consumer key and Consumer secret from the domain
324: # configuration in LON-CAPA for that LTI Consumer.
325: #
326: unless (($itemid) && (ref($lti{$itemid}) eq 'HASH')) {
1.5 raeburn 327: &invalid_request($r,7);
1.1 raeburn 328: return OK;
329: }
330:
331: #
332: # Determine if nonce in POSTed data has expired.
333: # If unexpired, confirm it has not already been used.
334: #
1.2 raeburn 335: unless (&LONCAPA::ltiutils::check_nonce($params->{'oauth_nonce'},$params->{'oauth_timestamp'},
336: $lti{$itemid}{'lifetime'},$cdom,$r->dir_config('lonLTIDir'))) {
1.5 raeburn 337: &invalid_request($r,8);
1.1 raeburn 338: return OK;
339: }
340:
341: #
1.6 raeburn 342: # Determine if source of username matches requirement from the
1.1 raeburn 343: # domain configuration for the specific LTI Consumer.
344: #
345:
346: if ($lti{$itemid}{'mapuser'} eq $possmapuser) {
347: $uname = $possuname;
348: } elsif ($lti{$itemid}{'mapuser'} eq 'lis_person_sourcedid') {
349: if ($params->{'lis_person_sourcedid'} =~ /^$match_username$/) {
350: $uname = $possuname;
351: }
352: } elsif ($lti{$itemid}{'mapuser'} eq 'lis_person_contact_email_primary') {
353: if ($params->{'lis_person_contact_email_primary'} =~ /^$match_username$/) {
354: $uname = $params->{'lis_person_contact_email_primary'};
355: }
356: } elsif (exists($params->{$lti{$itemid}{'mapuser'}})) {
357: if ($params->{$lti{$itemid}{'mapuser'}} =~ /^$match_username$/) {
358: $uname = $params->{$lti{$itemid}{'mapuser'}};
359: }
360: }
361:
362: #
363: # Determine the courseID of the LON-CAPA course to which the
364: # launch of LON-CAPA should provide access.
365: #
366: # Order is:
367: #
368: # (a) from course mapping (if the link between Consumer "course" and
369: # Provider "course" has been established previously).
1.9 raeburn 370: # (b) from tail of requested URL (after /adm/lti/) if it has format of a symb
1.1 raeburn 371: # (c) from tail of requested URL (after /adm/lti) if it has format of a map
372: # (d) from tail of requested URL (after /adm/lti) if it has format /domain/courseID
1.5 raeburn 373: # (e) from tail of requested URL (after /adm/lti) if it has format /tiny/domain/\w+
374: # i.e., a shortened URL (see bug #6400).
1.1 raeburn 375: #
376: # If Consumer course included in POSTed data points as a target course which
377: # has a format which matches a LON-CAPA courseID, but the course does not
378: # exist, the request is invalid.
379: #
380:
381: my ($sourcecrs,%consumers);
382: if ($lti{$itemid}{'mapcrs'} eq 'course_offering_sourcedid') {
383: $sourcecrs = $params->{'course_offering_sourcedid'};
384: } elsif ($lti{$itemid}{'mapcrs'} eq 'context_id') {
385: $sourcecrs = $params->{'context_id'};
386: } elsif ($lti{$itemid}{'mapcrs'} ne '') {
387: $sourcecrs = $params->{$lti{$itemid}{'mapcrs'}};
388: }
389:
390: my $posscnum;
391: if ($sourcecrs ne '') {
392: %consumers = &Apache::lonnet::get_dom('lticonsumers',[$sourcecrs],$cdom);
393: if (exists($consumers{$sourcecrs})) {
394: if ($consumers{$sourcecrs} =~ /^$match_courseid$/) {
395: my $crshome = &Apache::lonnet::homeserver($consumers{$sourcecrs},$cdom);
396: if ($crshome =~ /(con_lost|no_host|no_such_host)/) {
1.5 raeburn 397: &invalid_request($r,9);
1.1 raeburn 398: return OK;
399: } else {
400: $posscnum = $consumers{$sourcecrs};
401: }
402: }
403: }
404: }
405:
406: if ($urlcnum ne '') {
407: if ($posscnum ne '') {
408: if ($posscnum ne $urlcnum) {
1.5 raeburn 409: &invalid_request($r,10);
1.1 raeburn 410: return OK;
411: } else {
412: $cnum = $posscnum;
413: }
414: } else {
415: my $crshome = &Apache::lonnet::homeserver($urlcnum,$cdom);
416: if ($crshome =~ /(con_lost|no_host|no_such_host)/) {
1.5 raeburn 417: &invalid_request($r,11);
1.1 raeburn 418: return OK;
419: } else {
420: $cnum = $urlcnum;
421: }
422: }
423: } elsif ($posscnum ne '') {
424: $cnum = $posscnum;
425: }
426:
427: #
1.6 raeburn 428: # Get LON-CAPA role(s) to use from role-mapping of Consumer roles
1.1 raeburn 429: # defined in domain configuration for the appropriate LTI
430: # Consumer.
431: #
1.6 raeburn 432: # If multiple LON-CAPA roles are indicated for the current user,
433: # ordering (from first to last) is: cc/co, in, ta, ep, st.
1.1 raeburn 434: #
435:
1.6 raeburn 436: my (@ltiroles,@lcroles);
437: my @lcroleorder = ('cc','in','ta','ep','st');
1.15 raeburn 438: my ($lcrolesref,$ltirolesref) =
439: &LONCAPA::ltiutils::get_lc_roles($params->{'roles'},
440: \@lcroleorder,
441: $lti{$itemid}{maproles});
1.13 raeburn 442: if (ref($lcrolesref) eq 'ARRAY') {
443: @lcroles = @{$lcrolesref};
1.1 raeburn 444: }
1.13 raeburn 445: if (ref($ltirolesref) eq 'ARRAY') {
446: @ltiroles = @{$ltirolesref};
1.1 raeburn 447: }
448:
449: #
450: # If no LON-CAPA username -- is user allowed to create one?
451: #
452:
453: my $selfcreate;
454: if (($uname ne '') && ($udom ne '')) {
455: $uhome = &Apache::lonnet::homeserver($uname,$udom);
456: if ($uhome =~ /(con_lost|no_host|no_such_host)/) {
457: &Apache::lonnet::logthis(" LTI authorized unknown user $uname:$udom ");
458: if (ref($lti{$itemid}{'makeuser'}) eq 'ARRAY') {
459: if (@{$lti{$itemid}{'makeuser'}} > 0) {
460: foreach my $ltirole (@ltiroles) {
461: if (grep(/^\Q$ltirole\E$/,@{$lti{$itemid}{'makeuser'}})) {
462: $selfcreate = 1;
1.6 raeburn 463: last;
1.1 raeburn 464: }
465: }
466: }
467: }
468: if ($selfcreate) {
1.13 raeburn 469: my (%rulematch,%inst_results,%curr_rules,%got_rules,%alerts);
470: my $domdesc = &Apache::lonnet::domain($udom,'description');
471: my %data = (
472: 'permanentemail' => $env{'form.lis_person_contact_email_primary'},
473: 'firstname' => $env{'form.lis_person_name_given'},
474: 'lastname' => $env{'form.lis_person_name_family'},
475: 'fullname' => $env{'form.lis_person_name_full'},
476: );
477: my $result =
478: &LONCAPA::ltiutils::create_user($lti{$itemid},$uname,$udom,
479: $domdesc,\%data,\%alerts,\%rulematch,
480: \%inst_results,\%curr_rules,%got_rules);
481: if ($result eq 'notallowed') {
1.6 raeburn 482: &invalid_request($r,12);
1.13 raeburn 483: } elsif ($result eq 'ok') {
1.6 raeburn 484: if (($ltiroles[0] eq 'Instructor') && ($lcroles[0] eq 'cc') && ($lti{$itemid}{'mapcrs'}) &&
485: ($lti{$itemid}{'makecrs'})) {
486: unless (&Apache::lonnet::usertools_access($uname,$udom,'lti','reload','requestcourses')) {
1.10 raeburn 487: &Apache::lonnet::put('environment',{ 'requestcourses.lti' => 'autolimit=', },$udom,$uname);
1.6 raeburn 488: }
489: }
490: } else {
491: &invalid_request($r,13);
492: return OK;
493: }
1.1 raeburn 494: } else {
1.6 raeburn 495: &invalid_request($r,14);
1.1 raeburn 496: return OK;
1.6 raeburn 497: }
498: }
1.1 raeburn 499: } else {
1.6 raeburn 500: &invalid_request($r,15);
1.1 raeburn 501: return OK;
502: }
503:
504: #
505: # If no LON-CAPA course available, check if domain's configuration
506: # for the specific LTI Consumer allows a new course to be created
1.6 raeburn 507: # (requires role in Consumer to be: Instructor and Instructor to map to CC)
1.1 raeburn 508: #
509:
1.6 raeburn 510: my $reqcrs;
1.1 raeburn 511: if ($cnum eq '') {
1.6 raeburn 512: if ((@ltiroles) && ($lti{$itemid}{'mapcrs'}) &&
513: ($ltiroles[0] eq 'Instructor') && ($lcroles[0] eq 'cc') && ($lti{$itemid}{'makecrs'})) {
514: my (%can_request,%request_domains);
515: &Apache::lonnet::check_can_request($cdom,\%can_request,\%request_domains,$uname,$udom);
516: if ($can_request{'lti'}) {
517: $reqcrs = 1;
518: <i_session($r,$itemid,$uname,$udom,$uhome,$lonhost,undef,$mapurl,$tail,
519: $symb,$cdom,$cnum,$params,\@ltiroles,$lti{$itemid},\@lcroles,
520: $reqcrs,$sourcecrs);
521: } else {
522: &invalid_request($r,16);
523: }
1.1 raeburn 524: } else {
1.6 raeburn 525: &invalid_request($r,17);
1.1 raeburn 526: }
1.6 raeburn 527: return OK;
1.1 raeburn 528: }
529:
530: #
531: # If LON-CAPA course is a Community, and LON-CAPA role
532: # indicated is cc, change role indicated to co.
533: #
534:
1.6 raeburn 535: my %crsenv;
536: if ($lcroles[0] eq 'cc') {
1.1 raeburn 537: if (($cdom ne '') && ($cnum ne '')) {
1.6 raeburn 538: %crsenv = &Apache::lonnet::coursedescription($cdom.'_'.$cnum,{ 'one_time' => 1,});
1.1 raeburn 539: if ($crsenv{'type'} eq 'Community') {
1.6 raeburn 540: $lcroles[0] = 'co';
541: }
542: }
543: }
544:
545: #
546: # Determine if user has a LON-CAPA role in the mapped LON-CAPA course.
547: # If multiple LON-CAPA roles are available for the user's assigned LTI roles,
548: # choose the first available LON-CAPA role in the order: cc/co, in, ta, ep, st
549: #
550:
551: my ($role,$usec,$withsec);
552: unless ((($lcroles[0] eq 'cc') || ($lcroles[0] eq 'co')) && (@lcroles == 1)) {
553: if ($lti{$itemid}{'section'} ne '') {
554: if ($lti{$itemid}{'section'} eq 'course_section_sourcedid') {
555: if ($env{'form.course_section_sourcedid'} !~ /\W/) {
556: $usec = $env{'form.course_section_sourcedid'};
557: }
558: } elsif ($env{'form.'.$lti{$itemid}{'section'}} !~ /\W/) {
559: $usec = $env{'form.'.$lti{$itemid}{'section'}};
560: }
561: }
562: if ($usec ne '') {
563: $withsec = 1;
564: }
565: }
566:
567: if (@lcroles) {
568: my %crsroles = &Apache::lonnet::get_my_roles($uname,$udom,'userroles',undef,\@lcroles,
569: [$cdom],$withsec);
570: foreach my $reqrole (@lcroles) {
571: if ($withsec) {
572: my $incsec;
573: if (($reqrole eq 'cc') || ($reqrole eq 'co')) {
574: $incsec = '';
575: } else {
576: $incsec = $usec;
577: }
578: if (exists($crsroles{$cnum.':'.$cdom.':'.$reqrole.':'.$incsec})) {
579: $role = $reqrole.'./'.$cdom.'/'.$cnum;
580: if ($incsec ne '') {
581: $role .= '/'.$usec;
582: }
583: last;
584: }
585: } else {
586: if (exists($crsroles{$cnum.':'.$cdom.':'.$reqrole})) {
587: $role = $reqrole.'./'.$cdom.'/'.$cnum;
588: last;
589: }
1.1 raeburn 590: }
591: }
592: }
593:
594: #
1.6 raeburn 595: # Determine if user can selfenroll
1.1 raeburn 596: #
597:
1.6 raeburn 598: my ($reqrole,$selfenrollrole);
599: if ($role eq '') {
600: if ((@ltiroles) && (ref($lti{$itemid}{'selfenroll'}) eq 'ARRAY')) {
601: foreach my $ltirole (@ltiroles) {
602: if (grep(/^\Q$ltirole\E$/,@{$lti{$itemid}{'selfenroll'}})) {
603: if (ref($lti{$itemid}{maproles}) eq 'HASH') {
604: $reqrole = $lti{$itemid}{maproles}{$ltirole};
605: last;
606: }
607: }
608: }
609: }
610: if ($reqrole eq '') {
611: &invalid_request($r,18);
1.1 raeburn 612: return OK;
613: } else {
1.6 raeburn 614: unless (%crsenv) {
615: %crsenv = &Apache::lonnet::coursedescription($cdom.'_'.$cnum);
616: }
617: my $default_enrollment_start_date = $crsenv{'default_enrollment_start_date'};
618: my $default_enrollment_end_date = $crsenv{'default_enrollment_end_date'};
619: my $now = time;
620: if ($default_enrollment_end_date && $default_enrollment_end_date <= $now) {
621: &invalid_request($r,19);
622: return OK;
623: } elsif ($default_enrollment_start_date && $default_enrollment_start_date >$now) {
624: &invalid_request($r,20);
625: return OK;
626: } else {
627: $selfenrollrole = $reqrole.'./'.$cdom.'/'.$cnum;
628: if (($withsec) && ($reqrole ne 'cc') && ($reqrole ne 'co')) {
629: if ($usec ne '') {
630: $selfenrollrole .= '/'.$usec;
631: }
632: }
633: }
1.1 raeburn 634: }
635: }
636:
637: #
638: # Store consumer-to-LON-CAPA course mapping
639: #
1.6 raeburn 640:
1.1 raeburn 641: if (($sourcecrs ne '') && ($consumers{$sourcecrs} eq '') && ($cnum ne '')) {
642: &Apache::lonnet::put_dom('lticonsumers',{ $sourcecrs => $cnum },$cdom);
643: }
644:
645: #
1.6 raeburn 646: # Start user session
647: #
648:
649: <i_session($r,$itemid,$uname,$udom,$uhome,$lonhost,$role,$mapurl,$tail,$symb,
650: $cdom,$cnum,$params,\@ltiroles,$lti{$itemid},\@lcroles,undef,$sourcecrs,
651: $selfenrollrole);
652: return OK;
653: }
654:
655: sub lti_enroll {
656: my ($uname,$udom,$selfenrollrole) = @_;
657: my $enrollresult;
658: my ($role,$cdom,$cnum,$sec) =
659: ($selfenrollrole =~ m{^(\w+)\./($match_domain)/($match_courseid)(?:|/(\w*))$});
660: if (($cnum ne '') && ($cdom ne '')) {
661: my $chome = &Apache::lonnet::homeserver($cnum,$cdom);
662: if ($chome ne 'no_host') {
663: my %coursehash = &Apache::lonnet::coursedescription($cdom.'_'.$cnum);
664: my $start = $coursehash{'default_enrollment_start_date'};
665: my $end = $coursehash{'default_enrollment_end_date'};
1.14 raeburn 666: $enrollresult = &LONCAPA::ltiutils::enrolluser($udom,$uname,$role,$cdom,$cnum,$sec,
667: $start,$end,1);
1.6 raeburn 668: }
669: }
670: return $enrollresult;
671: }
672:
673: sub lti_reqcrs {
674: my ($r,$cdom,$form,$uname,$udom) = @_;
675: my (%can_request,%request_domains);
676: &Apache::lonnet::check_can_request($cdom,\%can_request,\%request_domains,$uname,$udom);
677: if ($can_request{'lti'}) {
678: my %domconfig = &Apache::lonnet::get_dom('configuration',['requestcourses'],$cdom);
679: my %domdefs = &Apache::lonnet::get_domain_defaults($cdom);
680: &Apache::lonrequestcourse::print_textbook_form($r,$cdom,[$cdom],\%domdefs,
681: $domconfig{'requestcourses'},
682: \%can_request,'lti',$form);
683: } else {
684: $r->print(
685: &Apache::loncommon::start_page('Invalid LTI call',undef,{'only_body' => 1}).
686: &mt('Invalid LTI call').
687: &Apache::loncommon::end_page()
688: );
689: }
690: }
691:
692: sub lti_session {
693: my ($r,$itemid,$uname,$udom,$uhome,$lonhost,$role,$mapurl,$tail,$symb,$cdom,$cnum,
694: $params,$ltiroles,$ltihash,$lcroles,$reqcrs,$sourcecrs,$selfenrollrole) = @_;
695: return unless ((ref($params) eq 'HASH') && (ref($ltiroles) eq 'ARRAY') &&
696: (ref($ltihash) eq 'HASH') && (ref($lcroles) eq 'ARRAY'));
697: #
1.1 raeburn 698: # Check if user should be hosted here or switched to another server.
699: #
700: $r->user($uname);
1.6 raeburn 701: if ($cnum) {
702: if ($role) {
703: &Apache::lonnet::logthis(" LTI authorized user ($itemid): $uname:$udom, role: $role, course: $cdom\_$cnum");
704: } elsif ($selfenrollrole =~ m{^(\w+)\./$cdom/$cnum}) {
705: &Apache::lonnet::logthis(" LTI authorized user ($itemid): $uname:$udom, desired role: $1 course: $cdom\_$cnum");
706: }
707: } else {
708: &Apache::lonnet::logthis(" LTI authorized user ($itemid): $uname:$udom, course dom: $cdom");
709: }
1.1 raeburn 710: my ($is_balancer,$otherserver,$hosthere);
711: ($is_balancer,$otherserver) =
712: &Apache::lonnet::check_loadbalancing($uname,$udom,'login');
713: if ($is_balancer) {
714: if ($otherserver eq '') {
715: my $lowest_load;
716: ($otherserver,undef,undef,undef,$lowest_load) = &Apache::lonnet::choose_server($udom);
717: if ($lowest_load > 100) {
718: $otherserver = &Apache::lonnet::spareserver($lowest_load,$lowest_load,1,$udom);
719: }
720: }
721: if ($otherserver ne '') {
722: my @hosts = &Apache::lonnet::current_machine_ids();
723: if (grep(/^\Q$otherserver\E$/,@hosts)) {
724: $hosthere = $otherserver;
725: }
726: }
727: }
728: if (($is_balancer) && (!$hosthere)) {
729: # login but immediately go to switch server.
730: &Apache::lonauth::success($r,$uname,$udom,$uhome,'noredirect');
731: if ($symb) {
732: $env{'form.symb'} = $symb;
1.16 ! raeburn 733: $env{'request.lti.uri'} = $tail;
1.6 raeburn 734: } else {
735: if ($mapurl) {
736: $env{'form.origurl'} = $mapurl;
1.8 raeburn 737: $env{'request.lti.uri'} = $mapurl;
1.6 raeburn 738: } elsif ($tail =~ m{^\Q/tiny/$cdom/\E\w+$}) {
739: $env{'form.origurl'} = $tail;
1.8 raeburn 740: $env{'request.lti.uri'} = $tail;
1.9 raeburn 741: } elsif ($tail eq "/$cdom/$cnum") {
742: $env{'form.origurl'} = '/adm/navmaps';
743: $env{'request.lti.uri'} = $tail;
1.6 raeburn 744: } else {
745: unless ($tail eq '/adm/roles') {
746: $env{'form.origurl'} = '/adm/navmaps';
747: }
748: }
1.1 raeburn 749: }
750: if ($role) {
751: $env{'form.role'} = $role;
752: }
1.6 raeburn 753: if (($lcroles->[0] eq 'cc') && ($reqcrs)) {
754: $env{'request.lti.reqcrs'} = 1;
755: $env{'request.lti.reqrole'} = 'cc';
756: $env{'request.lti.sourcecrs'} = $sourcecrs;
757: }
758: if ($selfenrollrole) {
759: $env{'request.lti.selfenroll'} = $selfenrollrole;
760: $env{'request.lti.sourcecrs'} = $sourcecrs;
761: }
762: if ($ltihash->{'passback'}) {
1.1 raeburn 763: if ($params->{'lis_result_sourcedid'}) {
764: $env{'request.lti.passbackid'} = $params->{'lis_result_sourcedid'};
765: }
766: if ($params->{'lis_outcome_service_url'}) {
767: $env{'request.lti.passbackurl'} = $params->{'lis_outcome_service_url'};
768: }
769: }
1.6 raeburn 770: if (($ltihash->{'roster'}) && (grep(/^Instructor$/,@{$ltiroles}))) {
1.1 raeburn 771: if ($params->{'ext_ims_lis_memberships_id'}) {
1.6 raeburn 772: $env{'request.lti.rosterid'} = $params->{'ext_ims_lis_memberships_id'};
1.1 raeburn 773: }
774: if ($params->{'ext_ims_lis_memberships_url'}) {
775: $env{'request.lti.rosterurl'} = $params->{'ext_ims_lis_memberships_url'};
776: }
777: }
1.10 raeburn 778: $env{'request.lti.login'} = $itemid;
1.8 raeburn 779: if ($params->{'launch_presentation_document_target'}) {
780: $env{'request.lti.target'} = $params->{'launch_presentation_document_target'};
781: }
1.1 raeburn 782: foreach my $key (%{$params}) {
783: delete($env{'form.'.$key});
784: }
785: my $redirecturl = '/adm/switchserver';
786: if ($otherserver ne '') {
787: $redirecturl .= '?otherserver='.$otherserver;
788: }
789: $r->internal_redirect($redirecturl);
790: $r->set_handlers('PerlHandler'=> undef);
791: } else {
792: # need to login them in, so generate the need data that
793: # migrate expects to do login
794: foreach my $key (%{$params}) {
795: delete($env{'form.'.$key});
796: }
797: my $ip = $r->get_remote_host();
798: my %info=('ip' => $ip,
799: 'domain' => $udom,
800: 'username' => $uname,
801: 'server' => $lonhost,
1.10 raeburn 802: 'lti.login' => $itemid,
1.8 raeburn 803: 'lti.uri' => $tail,
1.1 raeburn 804: );
805: if ($role) {
806: $info{'role'} = $role;
807: }
808: if ($symb) {
1.6 raeburn 809: $info{'symb'} = $symb;
810: }
811: if (($lcroles->[0] eq 'cc') && ($reqcrs)) {
812: $info{'lti.reqcrs'} = 1;
813: $info{'lti.reqrole'} = 'cc';
814: $info{'lti.sourcecrs'} = $sourcecrs;
815: }
816: if ($selfenrollrole) {
817: $info{'lti.selfenrollrole'} = $selfenrollrole;
1.1 raeburn 818: }
1.6 raeburn 819: if ($ltihash->{'passback'}) {
1.1 raeburn 820: if ($params->{'lis_result_sourcedid'}) {
821: $info{'lti.passbackid'} = $params->{'lis_result_sourcedid'}
822: }
823: if ($params->{'lis_outcome_service_url'}) {
824: $info{'lti.passbackurl'} = $params->{'lis_outcome_service_url'}
825: }
826: }
1.6 raeburn 827: if (($ltihash->{'roster'}) && (grep(/^Instructor$/,@{$ltiroles}))) {
1.1 raeburn 828: if ($params->{'ext_ims_lis_memberships_id'}) {
829: $info{'lti.rosterid'} = $params->{'ext_ims_lis_memberships_id'};
830: }
831: if ($params->{'ext_ims_lis_memberships_url'}) {
832: $info{'lti.rosterurl'} = $params->{'ext_ims_lis_memberships_url'};
833: }
834: }
1.8 raeburn 835: if ($params->{'launch_presentation_document_target'}) {
836: $info{'lti.target'} = $params->{'launch_presentation_document_target'};
837: }
838:
1.1 raeburn 839: unless ($info{'symb'}) {
840: if ($mapurl) {
841: $info{'origurl'} = $mapurl;
1.6 raeburn 842: } elsif ($tail =~ m{^\Q/tiny/$cdom/\E\w+$}) {
843: $info{'origurl'} = $tail;
1.1 raeburn 844: } else {
845: unless ($tail eq '/adm/roles') {
846: $info{'origurl'} = '/adm/navmaps';
847: }
848: }
849: }
850: if (($is_balancer) && ($hosthere)) {
851: $info{'noloadbalance'} = $hosthere;
852: }
853: my $token = &Apache::lonnet::tmpput(\%info,$lonhost);
854: $env{'form.token'} = $token;
855: $r->internal_redirect('/adm/migrateuser');
856: $r->set_handlers('PerlHandler'=> undef);
857: }
1.6 raeburn 858: return;
1.1 raeburn 859: }
860:
861: sub invalid_request {
862: my ($r,$num) = @_;
863: &Apache::loncommon::content_type($r,'text/html');
864: $r->send_http_header;
865: if ($r->header_only) {
866: return;
867: }
868: &Apache::lonlocal::get_language_handle($r);
869: $r->print(
1.10 raeburn 870: &Apache::loncommon::start_page('Invalid LTI call','',{ 'only_body' => 1,}).
1.1 raeburn 871: &mt('Invalid LTI call [_1]',$num).
872: &Apache::loncommon::end_page());
873: return;
874: }
875:
876: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>