Skip to content

Commit

Permalink
Show login form when restricted layers in permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-morvan committed Sep 27, 2018
1 parent dfad709 commit be9b930
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 3 deletions.
4 changes: 3 additions & 1 deletion contribs/gmf/apps/desktop/index.html.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@
{{'Login' | translate}}
<a class="btn close" ng-click="mainCtrl.loginActive = false">&times;</a>
</div>
<gmf-authentication></gmf-authentication>
<gmf-authentication
gmf-authentication-info-message="mainCtrl.loginInfoMessage"
></gmf-authentication>
</div>
</div>
<div ng-show="mainCtrl.printPanelActive" class="row">
Expand Down
5 changes: 5 additions & 0 deletions contribs/gmf/src/authentication/component.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ <h4 class="modal-title">
</div>

<div ng-if="!$ctrl.gmfUser.username">

<div class="alert alert-warning" ng-show="$ctrl.infoMessage">
<span>{{ $ctrl.infoMessage }}</span>
</div>

<form
name="loginForm"
role="form"
Expand Down
11 changes: 10 additions & 1 deletion contribs/gmf/src/authentication/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function gmfAuthenticationTemplateUrl($element, $attrs, gmfAuthenticationTemplat
* Example:
*
* <gmf-authentication
* gmf-authentication-info-message="mainCtrl.loginInfoMessage"
* gmf-authentication-allow-password-change="::true">
* </gmf-authentication>
*
Expand All @@ -80,6 +81,7 @@ function gmfAuthenticationTemplateUrl($element, $attrs, gmfAuthenticationTemplat
* should also allow the user to change its password. Don't add this option alone, use
* it in a dedicated authentication component, in a ngeo-modal, directly in
* your index.html (see example 2.)
* @htmlAttribute {string} gmf-authentication-info-message Message to show above the authentication form.
*
* Example 2:
*
Expand Down Expand Up @@ -107,7 +109,8 @@ exports.component_ = {
'allowPasswordReset': '<?gmfAuthenticationAllowPasswordReset',
'allowPasswordChange': '<?gmfAuthenticationAllowPasswordChange',
'passwordValidator': '<?gmfAuthenticationPasswordValidator',
'forcePasswordChange': '<?gmfAuthenticationForcePasswordChange'
'forcePasswordChange': '<?gmfAuthenticationForcePasswordChange',
'infoMessage': '=?gmfAuthenticationInfoMessage'
},
controller: 'GmfAuthenticationController',
templateUrl: gmfAuthenticationTemplateUrl
Expand Down Expand Up @@ -190,6 +193,12 @@ exports.AuthenticationController_ = class {
*/
this.forcePasswordChange;

/**
* @type {?string}
* @export
*/
this.infoMessage = null;

/**
* @type {boolean}
* @export
Expand Down
35 changes: 35 additions & 0 deletions contribs/gmf/src/controllers/AbstractAppController.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,45 @@ const exports = function(config, $scope, $injector) {
});
};

/**
* Url to redirect to after login success.
* @type {?string}
*/
this.loginRedirectUrl = null;

/**
* Information message for the login form.
* @type {?string}
*/
this.loginInfoMessage = null;

$scope.$on('authenticationrequired', (event, args) => {
/** @type {angularGettext.Catalog} */
const gettextCatalog = $injector.get('gettextCatalog');
this.loginInfoMessage = gettextCatalog.getString(
'Some layers in this link are not accessible to unauthenticated users. ' +
'Please log in to see whole data.');
this.loginRedirectUrl = args.url;
this.loginActive = true;
$scope.$applyAsync();

const unbind = $scope.$watch(() => this.loginActive, () => {
if (!this.loginActive) {
this.loginInfoMessage = null;
this.loginRedirectUrl = null;
unbind();
}
});
});

/**
* @param {gmfx.AuthenticationEvent} evt Event.
*/
const userChange = (evt) => {
if (this.loginRedirectUrl) {
window.location = this.loginRedirectUrl;
return;
}
const user = evt.detail.user;
const roleId = (user.username !== null) ? user.role_id : undefined;

Expand Down
25 changes: 24 additions & 1 deletion contribs/gmf/src/permalink/Permalink.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ import olLayerGroup from 'ol/layer/Group.js';
* @param {ngeo.misc.EventHelper} ngeoEventHelper Ngeo event helper service
* @param {ngeo.statemanager.Service} ngeoStateManager The ngeo statemanager service.
* @param {ngeo.statemanager.Location} ngeoLocation ngeo location service.
* @param {gmfx.User} gmfUser User.
* @ngInject
* @ngdoc service
* @ngname gmfPermalink
*/
const exports = function($q, $timeout, $rootScope, $injector, ngeoDebounce, gettextCatalog, ngeoEventHelper,
ngeoStateManager, ngeoLocation) {
ngeoStateManager, ngeoLocation, gmfUser) {

/**
* @type {!angular.$q}
Expand Down Expand Up @@ -216,6 +217,12 @@ const exports = function($q, $timeout, $rootScope, $injector, ngeoDebounce, gett
this.gmfTreeManager_ = $injector.has('gmfTreeManager') ?
$injector.get('gmfTreeManager') : null;

/**
* @type {gmfx.User}
* @private
*/
this.user_ = gmfUser;

// == other properties ==

/**
Expand Down Expand Up @@ -467,6 +474,7 @@ const exports = function($q, $timeout, $rootScope, $injector, ngeoDebounce, gett
this.initLayers_();
};


// === Map X, Y, Z ===

/**
Expand Down Expand Up @@ -910,6 +918,9 @@ exports.prototype.defaultThemeNameFromFunctionalities = function() {
* @private
*/
exports.prototype.initLayers_ = function() {
const initialUri = window.location.href;
let authenticationRequired = false;

if (!this.gmfThemes_) {
return;
}
Expand Down Expand Up @@ -939,6 +950,8 @@ exports.prototype.initLayers_ = function() {
const group = gmfThemeThemes.findGroupByName(themes, groupName);
if (group) {
firstLevelGroups.push(group);
} else {
authenticationRequired = true;
}
});
}
Expand Down Expand Up @@ -984,9 +997,15 @@ exports.prototype.initLayers_ = function() {
treeCtrl.traverseDepthFirst((treeCtrl) => {
if (treeCtrl.node.children === undefined) {
const enable = olArray.includes(groupLayersArray, treeCtrl.node.name);
if (enable) {
groupLayersArray.splice(groupLayersArray.indexOf(treeCtrl.node.name), 1);
}
treeCtrl.setState(enable ? 'on' : 'off', false);
}
});
if (groupLayersArray.length > 0) {
authenticationRequired = true;
}
}
}
});
Expand All @@ -999,6 +1018,10 @@ exports.prototype.initLayers_ = function() {
}
});
});

if (authenticationRequired && this.user_.role_id === null) {
this.rootScope_.$broadcast('authenticationrequired', {url: initialUri});
}
});
});
};
Expand Down

0 comments on commit be9b930

Please sign in to comment.