SetRoles() public method

Set the roles in the LtiRequest from an IList of LtiRoles.
public SetRoles ( IList roles ) : void
roles IList An IList of LtiRoles.
return void
Beispiel #1
0
        public void ProcessRequest(HttpContext context)
        {
            var ltiRequest = new LtiRequest(LtiConstants.BasicLaunchLtiMessageType)
            {
                ConsumerKey = "12345",
                ResourceLinkId = "launch",
                Url = new Uri(context.Request.Url, "Tool.aspx")
                //Url = "http://localhost/LTI/tool.php"
            };

            // Tool
            ltiRequest.ToolConsumerInfoProductFamilyCode = "LtiLibrary";
            ltiRequest.ToolConsumerInfoVersion = "1.2";

            // Context
            ltiRequest.ContextId = "Home";
            ltiRequest.ContextTitle = "Home";
            ltiRequest.ContextType = LisContextType.CourseSection;

            // Instance
            ltiRequest.ToolConsumerInstanceGuid = context.Request.Url.Authority;
            ltiRequest.ToolConsumerInstanceName = "LtiLibrary Sample";
            ltiRequest.ResourceLinkTitle = "Launch";
            ltiRequest.ResourceLinkDescription = "Perform a basic LTI 1.2 launch";

            // User
            ltiRequest.LisPersonEmailPrimary = "*****@*****.**";
            ltiRequest.LisPersonNameFamily = "Doe";
            ltiRequest.LisPersonNameGiven = "Joan";
            ltiRequest.UserId = "1";
            ltiRequest.SetRoles(new[] { Role.Instructor });

            // Outcomes service (WebApi controller)
            var controllerUrl = UrlHelper.GenerateUrl("DefaultApi", null, "OutcomesApi", 
                new RouteValueDictionary {{ "httproute", string.Empty }}, RouteTable.Routes, 
                context.Request.RequestContext, false);
            Uri controllerUri;
            if (Uri.TryCreate(context.Request.Url, controllerUrl, out controllerUri))
            {
                ltiRequest.LisOutcomeServiceUrl = controllerUri.AbsoluteUri;
                ltiRequest.LisResultSourcedId = "ltilibrary-jdoe-1";
            }

            // Tool Consumer Profile service (WebApi controller)
            controllerUrl = UrlHelper.GenerateUrl("DefaultApi", null, "ToolConsumerProfileApi",
                new RouteValueDictionary { { "httproute", string.Empty } }, RouteTable.Routes,
                context.Request.RequestContext, false);
            if (Uri.TryCreate(context.Request.Url, controllerUrl, out controllerUri))
            {
                ltiRequest.ToolConsumerProfileUrl = controllerUri.AbsoluteUri;
                ltiRequest.AddCustomParameter("tc_profile_url", "$ToolConsumerProfile.url");
            }

            // Send the launch request to the client browser
            context.Response.WriteLtiRequest(ltiRequest, "secret");
        }
        /// <summary>
        /// Send a basic LTI launch request to the Tool Provider.
        /// </summary>
        /// <remarks>
        /// This is the basic function of a Tool Consumer.
        /// </remarks>
        public ActionResult Launch()
        {
            Uri launchUri;
            var ltiRequest = new LtiRequest(LtiConstants.BasicLaunchLtiMessageType)
            {
                ConsumerKey = "12345",
                ResourceLinkId = "launch",
                Url = Uri.TryCreate(Request.Url, Url.Action("Tool", "Provider"), out launchUri) ? launchUri : null
            };

            // Tool
            ltiRequest.ToolConsumerInfoProductFamilyCode = "LtiLibrary";
            ltiRequest.ToolConsumerInfoVersion = "1.2";

            // Context
            ltiRequest.ContextId = "Home";
            ltiRequest.ContextTitle = "Home";
            ltiRequest.ContextType = LisContextType.CourseSection;

            // Instance
            ltiRequest.ToolConsumerInstanceGuid = Request.Url == null ? null : Request.Url.Authority;
            ltiRequest.ToolConsumerInstanceName = "LtiLibrary Sample";
            ltiRequest.ResourceLinkTitle = "Launch";
            ltiRequest.ResourceLinkDescription = "Perform a basic LTI 1.2 launch";

            // User
            ltiRequest.LisPersonEmailPrimary = "*****@*****.**";
            ltiRequest.LisPersonNameFamily = "Doe";
            ltiRequest.LisPersonNameGiven = "Joan";
            ltiRequest.UserId = "1";
            ltiRequest.SetRoles(new[] { Role.Instructor });

            // Basic Outcomes service (WebApi controller)
            var controllerUrl = UrlHelper.GenerateUrl("DefaultApi", null, "Outcomes",
                new RouteValueDictionary { { "httproute", string.Empty } }, RouteTable.Routes,
                Request.RequestContext, false);
            Uri controllerUri;
            if (Uri.TryCreate(Request.Url, controllerUrl, out controllerUri))
            {
                ltiRequest.LisOutcomeServiceUrl = controllerUri.AbsoluteUri;
            }
            ltiRequest.LisResultSourcedId = "ltilibrary-jdoe-1";

            // Outcomes V2 service (WebApi controller)
            controllerUrl = UrlHelper.GenerateUrl("LineItemsApi", null, "LineItems",
                new RouteValueDictionary
                {
                    { "httproute", string.Empty }, 
                    { "contextId", ltiRequest.ContextId }
                },
                RouteTable.Routes,
                Request.RequestContext, false);
            if (Uri.TryCreate(Request.Url, controllerUrl, out controllerUri))
            {
                ltiRequest.LineItemsServiceUrl = controllerUri.AbsoluteUri;
            }
            controllerUrl = UrlHelper.GenerateUrl("LineItemsApi", null, "LineItems",
                new RouteValueDictionary
                {
                    { "httproute", string.Empty }, 
                    { "contextId", ltiRequest.ContextId },
                    { "id", LineItemsController.LineItemId }
                },
                RouteTable.Routes,
                Request.RequestContext, false);
            if (Uri.TryCreate(Request.Url, controllerUrl, out controllerUri))
            {
                ltiRequest.LineItemServiceUrl = controllerUri.AbsoluteUri;
            }
            ltiRequest.AddCustomParameter("lineitem_url", "$LineItem.url");
            ltiRequest.AddCustomParameter("lineitems_url", "$LineItems.url");

            // Tool Consumer Profile service (WebApi controller)
            controllerUrl = UrlHelper.GenerateUrl("ToolConsumerProfileApi", null, "ToolConsumerProfile",
                new RouteValueDictionary { { "httproute", string.Empty } }, RouteTable.Routes,
                Request.RequestContext, false);
            if (Uri.TryCreate(Request.Url, controllerUrl, out controllerUri))
            {
                ltiRequest.ToolConsumerProfileUrl = controllerUri.AbsoluteUri;
            }
            ltiRequest.AddCustomParameter("tc_profile_url", "$ToolConsumerProfile.url");

            return View(ltiRequest.GetViewModel("secret"));
        }
Beispiel #3
0
        /// <summary>
        /// Create a basic launch request for the assignment.
        /// </summary>
        /// <param name="request">The browser request.</param>
        /// <param name="assignment">The assignment to launch.</param>
        /// <param name="user">The user that is launching the assignment.</param>
        /// <returns>An LtiRequestViewModel which can be displayed by the View in such
        /// a way that the browser will invoke the LTI launch.</returns>
        public static LtiRequestViewModel CreateBasicLaunchRequestViewModel(HttpRequestBase request, Assignment assignment, ApplicationUser user)
        {
            var ltiRequest = new LtiRequest(LtiConstants.BasicLaunchLtiMessageType)
            {
                ConsumerKey = assignment.ConsumerKey,
                ResourceLinkId = assignment.AssignmentId.ToString(CultureInfo.InvariantCulture),
                Url = new Uri(assignment.Url)
            };

            var course = assignment.Course;

            // Tool
            ltiRequest.ToolConsumerInfoProductFamilyCode = Assembly.GetExecutingAssembly().GetName().Name;
            ltiRequest.ToolConsumerInfoVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();

            // Context
            ltiRequest.ContextId = course.CourseId.ToString(CultureInfo.InvariantCulture);
            ltiRequest.ContextTitle = course.Name;
            ltiRequest.ContextType = course.EnrolledUsers.Any(u => u.Id == user.Id)
                ? LisContextType.CourseSection
                : LisContextType.CourseTemplate;

            // Instance
            ltiRequest.ToolConsumerInstanceGuid = "~/".ToAbsoluteUrl();
            var title = Assembly.GetExecutingAssembly().GetCustomAttributes<AssemblyTitleAttribute>().FirstOrDefault();
            ltiRequest.ToolConsumerInstanceName = title == null ? null : title.Title;
            ltiRequest.ResourceLinkTitle = assignment.Name;
            ltiRequest.ResourceLinkDescription = assignment.Description;

            // User
            if (user.SendEmail.GetValueOrDefault(true))
            {
                ltiRequest.LisPersonEmailPrimary = user.Email;
            }
            if (user.SendName.GetValueOrDefault(true))
            {
                ltiRequest.LisPersonNameFamily = user.LastName;
                ltiRequest.LisPersonNameGiven = user.FirstName;
            }
            ltiRequest.UserId = user.Id;
            ltiRequest.UserName = user.UserName;
            ltiRequest.SetRoles(GetLtiRolesForUser(user));

            // Outcomes service
            if (course.EnrolledUsers.Any(u => u.Id == user.Id))
            {
                var outcomesUrl = UrlHelper.GenerateUrl("DefaultApi", null, "Outcomes",
                    new RouteValueDictionary { { "httproute", string.Empty } }, RouteTable.Routes,
                    request.RequestContext, false);
                Uri outcomesUri;
                if (Uri.TryCreate(request.Url, outcomesUrl, out outcomesUri))
                {
                    ltiRequest.LisOutcomeServiceUrl = outcomesUri.AbsoluteUri;
                }
                ltiRequest.LisResultSourcedId =
                    JsonConvert.SerializeObject(new LisResultSourcedId
                    {
                        AssignmentId = assignment.AssignmentId, 
                        UserId = user.Id
                    });
            }

            // Tool Consumer Profile service
            var profileUrl = UrlHelper.GenerateUrl("DefaultApi", null, "ToolConsumerProfile",
                new RouteValueDictionary { { "httproute", string.Empty } }, RouteTable.Routes,
                request.RequestContext, false);
            Uri profileUri;
            if (Uri.TryCreate(request.Url, profileUrl, out profileUri))
            {
                ltiRequest.ToolConsumerProfileUrl = profileUri.AbsoluteUri;
            }

            // Add the custom parameters. This consumer supports 3 special substitutions.
            var customParameters = assignment.CustomParameters;

            if (!string.IsNullOrWhiteSpace(customParameters))
            {
                ltiRequest.AddCustomParameters(customParameters);
            }

            return ltiRequest.GetViewModel(assignment.ConsumerSecret);
        }
Beispiel #4
0
        public static LtiRequestViewModel CreateContentItemSelectionRequestViewModel(HttpRequestBase request, ContentItemTool contentItemTool, Course course, ApplicationUser user, string returnUrl)
        {
            var ltiRequest = new LtiRequest(LtiConstants.ContentItemSelectionRequestLtiMessageType)
            {
                ConsumerKey = contentItemTool.ConsumerKey,
                Url = new Uri(contentItemTool.Url)
            };

            // Tool Consumer
            ltiRequest.ToolConsumerInfoProductFamilyCode = Assembly.GetExecutingAssembly().GetName().Name;
            ltiRequest.ToolConsumerInfoVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();

            // Context
            ltiRequest.ContextId = course.CourseId.ToString(CultureInfo.InvariantCulture);
            ltiRequest.ContextTitle = course.Name;
            ltiRequest.ContextType = course.EnrolledUsers.Any(u => u.Id == user.Id)
                ? LisContextType.CourseSection
                : LisContextType.CourseTemplate;
            ltiRequest.LisCourseSectionSourcedId = course.CourseId.ToString(CultureInfo.InvariantCulture);

            // Instance
            ltiRequest.ToolConsumerInstanceGuid = "~/".ToAbsoluteUrl();
            var title = Assembly.GetExecutingAssembly().GetCustomAttributes<AssemblyTitleAttribute>().FirstOrDefault();
            ltiRequest.ToolConsumerInstanceName = title == null ? null : title.Title;

            // User
            if (user.SendEmail.GetValueOrDefault(true))
            {
                ltiRequest.LisPersonEmailPrimary = user.Email;
            }
            if (user.SendName.GetValueOrDefault(true))
            {
                ltiRequest.LisPersonNameFamily = user.LastName;
                ltiRequest.LisPersonNameGiven = user.FirstName;
            }
            ltiRequest.UserId = user.Id;
            ltiRequest.UserName = user.UserName;
            ltiRequest.SetRoles(GetLtiRolesForUser(user));

            // Presentation
            ltiRequest.LaunchPresentationDocumentTarget = DocumentTarget.iframe;

            // Content Item Tool
            ltiRequest.AcceptMediaTypes = LtiConstants.LtiLinkMediaType; // Only accept LTI Link
            ltiRequest.AcceptMultiple = true;
            ltiRequest.AcceptPresentationDocumentTargets = "iframe";
            ltiRequest.AcceptUnsigned = false;
            ltiRequest.ContentItemReturnUrl = returnUrl;
            //ltiRequest.ContentItemServiceUrl = "about:blank";
            ltiRequest.Data = JsonConvert.SerializeObject(new ContentItemData
            {
                ContentItemToolId = contentItemTool.ContentItemToolId, 
                CourseId = course.CourseId
            });

            // Add the custom parameters.
            var customParameters = contentItemTool.CustomParameters;

            // This tool consumer supports 3 non-spec custom parameters
            if (!string.IsNullOrWhiteSpace(customParameters))
            {
                ltiRequest.AddCustomParameters(customParameters);
            }

            return ltiRequest.GetViewModel(contentItemTool.ConsumerSecret);
        }