From 141f979ceaa1326ec0f900bd066d4c14dcb81d02 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Fri, 23 Feb 2018 12:28:36 +0100 Subject: [PATCH] MOBILE-2333 siteaddons: Support restrict users and courses --- src/core/siteaddons/providers/helper.ts | 67 ++++++++++++++++----- src/core/siteaddons/providers/siteaddons.ts | 31 +--------- 2 files changed, 54 insertions(+), 44 deletions(-) diff --git a/src/core/siteaddons/providers/helper.ts b/src/core/siteaddons/providers/helper.ts index 17431668a..2869aca16 100644 --- a/src/core/siteaddons/providers/helper.ts +++ b/src/core/siteaddons/providers/helper.ts @@ -30,6 +30,7 @@ import { CoreSiteAddonsModuleIndexComponent } from '../components/module-index/m import { CoreSiteAddonsProvider } from './siteaddons'; import { CoreSiteAddonsModulePrefetchHandler } from '../classes/module-prefetch-handler'; import { CoreCompileProvider } from '../../compile/providers/compile'; +import { CoreCoursesProvider } from '../../courses/providers/courses'; /** * Helper service to provide functionalities regarding site addons. It basically has the features to load and register site @@ -45,30 +46,37 @@ export class CoreSiteAddonsHelperProvider { private mainMenuDelegate: CoreMainMenuDelegate, private moduleDelegate: CoreCourseModuleDelegate, private userDelegate: CoreUserDelegate, private langProvider: CoreLangProvider, private siteAddonsProvider: CoreSiteAddonsProvider, private prefetchDelegate: CoreCourseModulePrefetchDelegate, - private compileProvider: CoreCompileProvider, private utils: CoreUtilsProvider) { + private compileProvider: CoreCompileProvider, private utils: CoreUtilsProvider, + private coursesProvider: CoreCoursesProvider) { this.logger = logger.getInstance('CoreSiteAddonsHelperProvider'); } /** - * Bootstrap a handler if it has some bootstrap JS. + * Bootstrap a handler if it has some bootstrap method. * * @param {any} addon Data of the addon. * @param {string} handlerName Name of the handler in the addon. * @param {any} handlerSchema Data about the handler. - * @return {Promise} Promise resolved when done. The resolve param is the result of the javascript execution (if any). + * @return {Promise<{restrict?: any, jsResult?: any}>} Promise resolved when done. It returns the "restrict" of the handler and + * the result of the javascript execution (if any). */ - protected bootstrapHandler(addon: any, handlerName: string, handlerSchema: any): Promise { + protected bootstrapHandler(addon: any, handlerName: string, handlerSchema: any): Promise<{restrict?: any, jsResult?: any}> { if (!handlerSchema.bootstrap) { - return Promise.resolve(); + return Promise.resolve({}); } const siteId = this.sitesProvider.getCurrentSiteId(), preSets = {getFromCache: false}; // Try to ignore cache. return this.siteAddonsProvider.getContent(addon.component, handlerSchema.bootstrap, {}, preSets).then((result) => { + const data = { + restrict: result.restrict, + jsResult: undefined + }; + if (!result.javascript || this.sitesProvider.getCurrentSiteId() != siteId) { // No javascript or site has changed, stop. - return; + return data; } // Create a "fake" instance to hold all the libraries. @@ -76,7 +84,9 @@ export class CoreSiteAddonsHelperProvider { this.compileProvider.injectLibraries(instance); // Now execute the javascript using this instance. - return this.compileProvider.executeJavascript(instance, result.javascript); + data.jsResult = this.compileProvider.executeJavascript(instance, result.javascript); + + return data; }); } @@ -205,15 +215,16 @@ export class CoreSiteAddonsHelperProvider { switch (handlerSchema.delegate) { case 'CoreMainMenuDelegate': - uniqueName = this.registerMainMenuHandler(addon, handlerName, handlerSchema, result); + uniqueName = this.registerMainMenuHandler(addon, handlerName, handlerSchema, result.jsResult, result.restrict); break; case 'CoreCourseModuleDelegate': - uniqueName = this.registerModuleHandler(addon, handlerName, handlerSchema, result); + uniqueName = this.registerModuleHandler(addon, handlerName, handlerSchema, result.jsResult, result.restrict); break; case 'CoreUserDelegate': - uniqueName = this.registerUserProfileHandler(addon, handlerName, handlerSchema, result); + uniqueName = this.registerUserProfileHandler(addon, handlerName, handlerSchema, result.jsResult, + result.restrict); break; default: @@ -239,9 +250,11 @@ export class CoreSiteAddonsHelperProvider { * @param {string} handlerName Name of the handler in the addon. * @param {any} handlerSchema Data about the handler. * @param {any} [bootstrapResult] Result of executing the bootstrap JS. + * @param {any} [restrict] List of users and courses the handler is restricted to. * @return {string} A string to identify the handler. */ - protected registerMainMenuHandler(addon: any, handlerName: string, handlerSchema: any, bootstrapResult?: any): string { + protected registerMainMenuHandler(addon: any, handlerName: string, handlerSchema: any, bootstrapResult?: any, restrict?: any) + : string { if (!handlerSchema || !handlerSchema.displaydata) { // Required data not provided, stop. return; @@ -284,9 +297,11 @@ export class CoreSiteAddonsHelperProvider { * @param {string} handlerName Name of the handler in the addon. * @param {any} handlerSchema Data about the handler. * @param {any} [bootstrapResult] Result of executing the bootstrap JS. + * @param {any} [restrict] List of users and courses the handler is restricted to. * @return {string} A string to identify the handler. */ - protected registerModuleHandler(addon: any, handlerName: string, handlerSchema: any, bootstrapResult?: any): string { + protected registerModuleHandler(addon: any, handlerName: string, handlerSchema: any, bootstrapResult?: any, restrict?: any) + : string { if (!handlerSchema || !handlerSchema.displaydata) { // Required data not provided, stop. return; @@ -342,9 +357,11 @@ export class CoreSiteAddonsHelperProvider { * @param {string} handlerName Name of the handler in the addon. * @param {any} handlerSchema Data about the handler. * @param {any} [bootstrapResult] Result of executing the bootstrap JS. + * @param {any} [restrict] List of users and courses the handler is restricted to. * @return {string} A string to identify the handler. */ - protected registerUserProfileHandler(addon: any, handlerName: string, handlerSchema: any, bootstrapResult?: any): string { + protected registerUserProfileHandler(addon: any, handlerName: string, handlerSchema: any, bootstrapResult?: any, restrict?: any) + : string { if (!handlerSchema || !handlerSchema.displaydata) { // Required data not provided, stop. return; @@ -360,11 +377,31 @@ export class CoreSiteAddonsHelperProvider { userHandler = Object.assign(baseHandler, { priority: handlerSchema.priority, type: handlerSchema.type, - isEnabledForUser: (user: any, courseId: number, navOptions?: any, admOptions?: any): boolean => { - if (handlerSchema.restricted == 'current' && user.id != this.sitesProvider.getCurrentSite().getUserId()) { + isEnabledForUser: (user: any, courseId: number, navOptions?: any, admOptions?: any): boolean | Promise => { + if (handlerSchema.restricttocurrentuser && user.id != this.sitesProvider.getCurrentSite().getUserId()) { + // Only enabled for current user. return false; } + if (restrict) { + if (restrict.users && restrict.users.indexOf(user.id) == -1) { + // User is not in the list of restricted users. + return false; + } else if (restrict.courses && restrict.courses.indexOf(courseId) == -1) { + // Course is not in the list of restricted courses. + return false; + } + } + + if (handlerSchema.restricttoenrolledcourses || typeof handlerSchema.restricttoenrolledcourses == 'undefined') { + // Only enabled for courses the user is enrolled to. Check if the user is enrolled in the course. + return this.coursesProvider.getUserCourse(courseId, true).then(() => { + return true; + }).catch(() => { + return false; + }); + } + return true; }, getDisplayData: (user: any, courseId: number): CoreUserProfileHandlerData => { diff --git a/src/core/siteaddons/providers/siteaddons.ts b/src/core/siteaddons/providers/siteaddons.ts index 662fb7da7..dd3bd05ae 100644 --- a/src/core/siteaddons/providers/siteaddons.ts +++ b/src/core/siteaddons/providers/siteaddons.ts @@ -51,32 +51,6 @@ export interface CoreSiteAddonsHandler { bootstrapResult?: any; } -export interface CoreSiteAddonsGetContentResult { - /** - * The content in HTML. - * @type {string} - */ - html: string; - - /** - * The javascript for the content. - * @type {string} - */ - javascript: string; - - /** - * The files for the content. - * @type {any[]} - */ - files?: any[]; - - /** - * Other data. - * @type {any} - */ - otherdata?: any; -} - /** * Service to provide functionalities regarding site addons. */ @@ -187,10 +161,9 @@ export class CoreSiteAddonsProvider { * @param {any} args The params for the method. * @param {CoreSiteWSPreSets} [preSets] Extra options. * @param {string} [siteId] Site ID. If not defined, current site. - * @return {Promise} Promise resolved with the result. + * @return {Promise} Promise resolved with the result. */ - getContent(component: string, method: string, args: any, preSets?: CoreSiteWSPreSets, siteId?: string) - : Promise { + getContent(component: string, method: string, args: any, preSets?: CoreSiteWSPreSets, siteId?: string): Promise { this.logger.debug(`Get content for component '${component}' and method '${method}'`); return this.sitesProvider.getSite(siteId).then((site) => {