Commit 0bad7b0b authored by Pedro Eduardo Trujillo's avatar Pedro Eduardo Trujillo
Browse files

Centraliza comprobación de usuario editor

En lugar de evaluar en cada vista que lo necesite si el usuario actual
tiene capacidades de edición (para cargar las definiciones adecuadas en
ese caso), se resuelve esta comprobación al nivel del componente global
ModuleStore, apoyado en la información que le provee el componente
global Credentials.

Para simplificar el proceso de carga, se asume que el nombre del fichero
que extiende con capacidades de edición en cada caso, es siempre
'_Edition.js' y que se encuentra en el mismo directorio que la vista que
puede usarlo.

Se actualizan todas las vistas y extensiones de edición creadas hasta el
momento para aprovechar esta nueva funcionalidad.
parent de50c987
Loading
Loading
Loading
Loading
+18 −2
Original line number Diff line number Diff line
@@ -33,7 +33,8 @@ define([
					REMOVED: 'removed',
					ADDED: 'added',
					ACCEPT_COOKIES: 'acceptCookies',
					REQUEST_FAILED: 'requestFailed'
					REQUEST_FAILED: 'requestFailed',
					USER_HAS_EDITION_CAPABILITIES: 'userHasEditionCapabilities'
				},
				actions: {
					GET_CREDENTIALS: 'getCredentials',
@@ -45,7 +46,9 @@ define([
					COOKIES_ACCEPTED: 'cookiesAccepted',
					ACCEPT_COOKIES: 'acceptCookies',
					COOKIES_STATE: 'cookiesState',
					REQUEST_FAILED: 'requestFailed'
					REQUEST_FAILED: 'requestFailed',
					HAS_USER_EDITION_CAPABILITIES: 'hasUserEditionCapabilities',
					USER_HAS_EDITION_CAPABILITIES: 'userHasEditionCapabilities'
				},

				target: redmicConfig.services.profile,
@@ -92,6 +95,9 @@ define([
			},{
				channel : this.getChannel('ACCEPT_COOKIES'),
				callback: '_subAcceptCookies'
			},{
				channel : this.getChannel('HAS_USER_EDITION_CAPABILITIES'),
				callback: '_subHasUserEditionCapabilities'
			});
		},

@@ -115,6 +121,9 @@ define([
			},{
				event: 'REQUEST_FAILED',
				channel: this.getChannel('REQUEST_FAILED')
			},{
				event: 'USER_HAS_EDITION_CAPABILITIES',
				channel: this.getChannel('USER_HAS_EDITION_CAPABILITIES')
			});
		},

@@ -153,6 +162,13 @@ define([
			Credentials.set('cookiesAccepted', 'true');
		},

		_subHasUserEditionCapabilities: function(_req) {

			this._emitEvt('USER_HAS_EDITION_CAPABILITIES', {
				editionCapabilities: Credentials.userIsEditor()
			});
		},

		_onAccessTokenChanged: function(evt) {

			var value = evt.value;
+108 −61
Original line number Diff line number Diff line
define([
	"app/base/views/_View"
	, 'src/redmicConfig'
	, "dojo/_base/declare"
	, "dojo/_base/lang"
	, "dojo/Deferred"
	, "dojo/promise/all"
	, "dojo/store/Memory"
	, "src/component/base/_Module"
	'src/redmicConfig'
	, 'dojo/_base/declare'
	, 'dojo/_base/lang'
	, 'dojo/Deferred'
	, 'dojo/promise/all'
	, 'dojo/store/Memory'
	, 'src/component/base/_Module'
], function(
	_View
	, redmicConfig
	redmicConfig
	, declare
	, lang
	, Deferred
@@ -17,6 +15,7 @@ define([
	, Memory
	, _Module
) {

	return declare(_Module, {
		//	summary:
		//		Módulo encargado de almacenar los módulos de la aplicación.
@@ -42,23 +41,25 @@ define([
			this.config = {
				moduleStore: new Memory(),
				actions: {
					AVAILABLE_ALLOWED_MODULES: "availableAllowedModules",
					GET_MODULE: "getModule",
					AVAILABLE_MODULE: "availableModule",
					AVAILABLE_ALLOWED_MODULES: 'availableAllowedModules',
					HAS_USER_EDITION_CAPABILITIES: 'hasUserEditionCapabilities',
					USER_HAS_EDITION_CAPABILITIES: 'userHasEditionCapabilities',
					GET_MODULE: 'getModule',
					AVAILABLE_MODULE: 'availableModule',
					CLEAR_MODULE: 'clearModule'
				},
				events: {
					GET_MODULE: "getModule"
					GET_MODULE: 'getModule'
				},
				// mediator params
				ownChannel: "moduleStore",
				ownChannel: 'moduleStore',

				maxInstances: 10,

				viewSeparator: "/",
				viewSeparator: '/',

				parameterRegExp: "([\-a-zA-Z0-9_\+=]+)",
				parameterDelimiter: "{id}"
				parameterRegExp: '([\-a-zA-Z0-9_\+=]+)',
				parameterDelimiter: '{id}'
			};

			lang.mixin(this, this.config, args);
@@ -68,13 +69,13 @@ define([

			this.subscriptionsConfig.push({
				channel : this._buildChannel(this.credentialsChannel, 'AVAILABLE_ALLOWED_MODULES'),
				callback: "_subAllowedModules",
				callback: '_subAllowedModules',
				options: {
					predicate: lang.hitch(this, this._chkPublicationIsForMe)
				}
			},{
				channel : this.getChannel("GET_MODULE"),
				callback: "_subGetModule"
				channel : this.getChannel('GET_MODULE'),
				callback: '_subGetModule'
			},{
				channel : this.getChannel('CLEAR_MODULE'),
				callback: '_subClearModule'
@@ -85,7 +86,7 @@ define([

			this.publicationsConfig.push({
				event: 'GET_MODULE',
				channel: this.getChannel("AVAILABLE_MODULE")
				channel: this.getChannel('AVAILABLE_MODULE')
			});
		},

@@ -149,7 +150,7 @@ define([
					for (var j = 0; j < category.modules.length; j++) {
						var moduleItem = category.modules[j];
						if (moduleItem.enable) {
							this._addModule(category.name + "/" + moduleItem.name, moduleItem.name,
							this._addModule(category.name + '/' + moduleItem.name, moduleItem.name,
								moduleItem.internPath);
						}
					}
@@ -173,7 +174,9 @@ define([
			//	internPath:
			//		ubicación del js de la vista

			if (this._moduleExists(id)) return;
			if (this._moduleExists(id)) {
				return;
			}

			this.moduleStore.put({
				id: id,
@@ -225,28 +228,29 @@ define([
					arrayAux = regex.exec(item.id);

				while (arrayAux[3] !== undefined) {
					copyPath = copyPath.replace("{" + arrayAux[3] + "}", this.parameterRegExp);
					copyPath = copyPath.replace('{' + arrayAux[3] + '}', this.parameterRegExp);
					results[arrayAux[3]] = true;

					arrayAux = regex.exec(item.id);
				}

				var reg = new RegExp("^" + copyPath + "$");
				var reg = new RegExp('^' + copyPath + '$');

				if (reg.test(path)) {
					var i = 1;
					for (var key in results) {
						results[key] = path.replace(reg, "$" + i).replace("\/","");
						results[key] = path.replace(reg, '$' + i).replace('\/','');
						this.pathVariableId = results[key];
						i++;
					}

					var lengthResults = Object.keys(results).length;

					if (lengthResults > 1)
					if (lengthResults > 1) {
						this.pathVariableId = results;
					else if (lengthResults === 0)
					} else if (lengthResults === 0) {
						this.pathVariableId = null;
					}

					return item;
				}
@@ -280,10 +284,10 @@ define([
			moduleItem.timeStamp = new Date().getTime();
			this.moduleStore.put(moduleItem);

			this._publish(moduleItem.instance.getChannel("CONNECT"));
			this._publish(moduleItem.instance.getChannel('CONNECT'));

			this._publish(moduleItem.instance.getChannel('SET_PROPS'), {
				pathVariableId: this.pathVariableId !== "$1" ? this.pathVariableId : null
				pathVariableId: this.pathVariableId !== '$1' ? this.pathVariableId : null
			});

			var dfd = new Deferred();
@@ -292,50 +296,93 @@ define([
			return dfd; // return Object
		},

		_createModule: function(/*Object*/ moduleItem) {
		_createModule: function(/*Object*/ moduleStoreItem) {
			//	summary:
			//		Crea la instancia de un módulo de la aplicación.
			//	tags:
			//		private
			//	moduleItem:
			//		Módulo a crear
			//	moduleStoreItem:
			//		Elemento de configuración del módulo a crear
			//	returns:
			//		Promesa de la instancia del módulo

			var dfd = new Deferred(),
				parentChannel = redmicConfig.isOuterPath(moduleItem.id) ? this.outerAppChannel : this.innerAppChannel,
				moduleDefinitionPath = moduleItem.internPath;
			var instanceDfd = new Deferred(),
				viewDefinitionPath = moduleStoreItem.internPath;

			// TODO parche para compatibilidad con antiguas rutas parciales, las nuevas ya han de empezar con 'src/' e
			// incluir la terminación 'View' para ser totalmente explícitos
			if (moduleDefinitionPath.indexOf('src/') !== 0) {
				moduleDefinitionPath = 'app' + moduleDefinitionPath + 'View';
			if (viewDefinitionPath.indexOf('src/') !== 0) {
				viewDefinitionPath = 'app' + viewDefinitionPath + 'View';
			}

			require([moduleDefinitionPath], lang.hitch(this, function(moduleObj, ModuleView) {
			var channelToSubscribe = this._buildChannel(this.credentialsChannel, 'USER_HAS_EDITION_CAPABILITIES');
			this._once(channelToSubscribe, lang.hitch(this, this._requireViewDefinition, {
				moduleStoreItem: moduleStoreItem,
				viewDefinitionPath: viewDefinitionPath,
				instanceDfd: instanceDfd
			}));

			var channelToPublish = this._buildChannel(this.credentialsChannel, 'HAS_USER_EDITION_CAPABILITIES');
			this._publish(channelToPublish);

			return instanceDfd; // return Object
		},

		_requireViewDefinition: function(/*Object*/ args, /*Object*/ res) {

			var viewDefinitionPath = args.viewDefinitionPath,
				pathsToRequire = ['app/base/views/_View', viewDefinitionPath];

				var moduleDefinition = declare([ModuleView, _View]);
			if (res.editionCapabilities) {
				var viewDefinitionPathSplitted = viewDefinitionPath.split(this.viewSeparator),
					viewDefinitionParentPath = viewDefinitionPathSplitted.slice(0, -1);

				viewDefinitionParentPath.push('_Edition');
				var viewEditionDefinitionPath = viewDefinitionParentPath.join(this.viewSeparator);
				pathsToRequire.push(viewEditionDefinitionPath);
			}

			require(pathsToRequire, lang.hitch(this, this._onViewDefinitionRequired, {
				moduleStoreItem: args.moduleStoreItem,
				instanceDfd: args.instanceDfd
			}));
		},

		_onViewDefinitionRequired: function(
			/*Object*/ args,
			/*Object*/ _View,
			/*Object*/ ViewDefinition,
			/*Object?*/ EditionDefinition
		) {

			var moduleStoreItem = args.moduleStoreItem,
				instanceDfd = args.instanceDfd,
				isOuterView = redmicConfig.isOuterPath(moduleStoreItem.id),
				pathsToDeclare = [ViewDefinition];

			// Si existe y fue posible obtener la extensión de edición para la vista, se añade
			if (EditionDefinition && EditionDefinition !== 'not-a-module') {
				pathsToDeclare.push(EditionDefinition);
			}
			pathsToDeclare.push(_View);

			// Creamos el módulo
				var moduleInstance = new moduleDefinition({
					parentChannel: parentChannel,
					ownChannel: this.viewSeparator + moduleObj.id,
					pathVariableId: this.pathVariableId !== "$1" ? this.pathVariableId : null
			var viewInstance = new declare(pathsToDeclare)({
				parentChannel: isOuterView ? this.outerAppChannel : this.innerAppChannel,
				ownChannel: this.viewSeparator + moduleStoreItem.id,
				pathVariableId: this.pathVariableId !== '$1' ? this.pathVariableId : null
			});

			// Añadimos al store la instancia del módulo
				moduleObj.instance = moduleInstance;
				moduleObj.timeStamp = new Date().getTime();
				this.moduleStore.put(moduleObj);
			moduleStoreItem.instance = viewInstance;
			moduleStoreItem.timeStamp = new Date().getTime();
			this.moduleStore.put(moduleStoreItem);

			// Resolvemos para devolver la instancia creada
				dfd.resolve(moduleInstance);
			instanceDfd.resolve(viewInstance);

			// Limpiamos las instancias antiguas
			this._clearOldInstances();
			}, moduleItem));

			return dfd;	// return Object
		},

		_subClearModule: function(/*Object*/ req) {
@@ -362,7 +409,7 @@ define([
				return item.instance !== null;
			}, {
				sort: [{
					attribute: "timeStamp"
					attribute: 'timeStamp'
				}]
			});

+1 −11
Original line number Diff line number Diff line
@@ -6,13 +6,11 @@ define([
	, 'dojo/_base/declare'
	, 'dojo/_base/lang'
	, 'src/catalog/_GenerateReport'
	, 'src/catalog/activity/_ActivityEdition'
	, 'src/component/browser/_Select'
	, 'src/component/browser/bars/Order'
	, 'src/component/browser/bars/SelectionBox'
	, 'src/component/browser/bars/Total'
	, 'src/redmicConfig'
	, 'src/util/Credentials'
	, 'templates/ActivityList'
], function(
	_Main
@@ -22,23 +20,15 @@ define([
	, declare
	, lang
	, _GenerateReport
	, _ActivityEdition
	, _Select
	, Order
	, SelectionBox
	, Total
	, redmicConfig
	, Credentials
	, templateList
) {

	var declareItems = [Layout, Controller, _Main, _AddComposite, _GenerateReport];

	if (Credentials.userIsEditor()) {
		declareItems.push(_ActivityEdition);
	}

	return declare(declareItems, {
	return declare([Layout, Controller, _Main, _AddComposite, _GenerateReport], {
		//	summary:
		//		Vista de catálogo de actividades.

+1 −11
Original line number Diff line number Diff line
@@ -7,12 +7,10 @@ define([
	, "dojo/_base/lang"
	, "templates/ProgramList"
	, 'src/catalog/_GenerateReport'
	, 'src/catalog/program/_ProgramEdition'
	, "src/component/browser/_Select"
	, "src/component/browser/bars/SelectionBox"
	, "src/component/browser/bars/Order"
	, "src/component/browser/bars/Total"
	, 'src/util/Credentials'
], function(
	_Main
	, Controller
@@ -22,21 +20,13 @@ define([
	, lang
	, templateList
	, _GenerateReport
	, _ProgramEdition
	, _Select
	, SelectionBox
	, Order
	, Total
	, Credentials
) {

	var declareItems = [Layout, Controller, _Main, _GenerateReport];

	if (Credentials.userIsEditor()) {
		declareItems.push(_ProgramEdition);
	}

	return declare(declareItems, {
	return declare([Layout, Controller, _Main, _GenerateReport], {
		//	summary:
		//		Vista de catálogo de programas.

Loading