Skip to content

Commit

Permalink
Merge pull request #3216 from pfirpfel/gmf.authentication
Browse files Browse the repository at this point in the history
Adapt angular architecture: gmf.authentication
  • Loading branch information
pfirpfel authored Dec 13, 2017
2 parents f2639d9 + 79bc2eb commit 1fa38e8
Show file tree
Hide file tree
Showing 18 changed files with 689 additions and 664 deletions.
2 changes: 2 additions & 0 deletions contribs/gmf/apps/appmodule.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ goog.require('gmf');

goog.require('ngeo.utils.module');
goog.require('gmf.search.module');
goog.require('gmf.authentication.module');


/**
Expand All @@ -20,6 +21,7 @@ goog.require('gmf.search.module');
app.module = angular.module('app', [
ngeo.utils.module.module.name,
gmf.search.module.name,
gmf.authentication.module.name,
gmf.module.name
]);

Expand Down
9 changes: 6 additions & 3 deletions contribs/gmf/examples/authentication.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
goog.provide('gmfapp.authentication');

/** @suppress {extraRequire} */
goog.require('gmf.authenticationDirective');

goog.require('gmf.authentication.module');


/** @type {!angular.Module} **/
gmfapp.module = angular.module('gmfapp', ['gmf']);
gmfapp.module = angular.module('gmfapp', [
gmf.module.name,
gmf.authentication.module.name
]);


gmfapp.module.value(
Expand Down
8 changes: 5 additions & 3 deletions contribs/gmf/examples/editfeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ goog.provide('gmfapp.editfeature');

/** @suppress {extraRequire} */
goog.require('ngeo.proj.EPSG21781');
/** @suppress {extraRequire} */
goog.require('gmf.authenticationDirective');
goog.require('gmf.authentication.module');
/** @suppress {extraRequire} */
goog.require('gmf.mapDirective');
goog.require('gmf.EditFeature');
Expand All @@ -19,7 +18,10 @@ goog.require('ol.source.ImageWMS');


/** @type {!angular.Module} **/
gmfapp.module = angular.module('gmfapp', ['gmf']);
gmfapp.module = angular.module('gmfapp', [
gmf.module.name,
gmf.authentication.module.name
]);


gmfapp.module.value(
Expand Down
3 changes: 2 additions & 1 deletion contribs/gmf/examples/editfeatureselector.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ goog.provide('gmfapp.editfeatureselector');
goog.require('gmf.Themes');
goog.require('gmf.TreeManager');
/** @suppress {extraRequire} */
goog.require('gmf.authenticationDirective');
goog.require('gmf.authentication.module');
/** @suppress {extraRequire} */
goog.require('gmf.editfeatureselectorDirective');
/** @suppress {extraRequire} */
Expand All @@ -28,6 +28,7 @@ goog.require('ol.source.Vector');
gmfapp.module = angular.module('gmfapp', [
'gmf',
ngeo.ToolActivateMgr.module.name,
gmf.authentication.module.name,
]);


Expand Down
3 changes: 2 additions & 1 deletion contribs/gmf/examples/filterselector.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
goog.provide('gmfapp.filterselector');

/** @suppress {extraRequire} */
goog.require('gmf.authenticationDirective');
goog.require('gmf.authentication.module');
goog.require('gmf.Themes');
goog.require('gmf.TreeManager');
/** @suppress {extraRequire} */
Expand Down Expand Up @@ -32,6 +32,7 @@ goog.require('ol.source.OSM');
gmfapp.module = angular.module('gmfapp', [
'gmf',
ngeo.ToolActivateMgr.module.name,
gmf.authentication.module.name,
]);


Expand Down
2 changes: 0 additions & 2 deletions contribs/gmf/examples/importdatasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

goog.provide('gmfapp.importdatasource');

/** @suppress {extraRequire} */
goog.require('gmf.authenticationDirective');
goog.require('gmf.Themes');
goog.require('gmf.TreeManager');
/** @suppress {extraRequire} */
Expand Down
20 changes: 20 additions & 0 deletions contribs/gmf/options/gmfx.js
Original file line number Diff line number Diff line change
Expand Up @@ -699,3 +699,23 @@ gmfx.openPopup_
* }>}
*/
gmfx.AuthenticationEvent;


/**
* @typedef {{
* functionalities: (gmfx.AuthenticationFunctionalities|undefined),
* is_password_changed: (boolean|undefined),
* role_id: (number|undefined),
* role_name: (string|undefined),
* username: (string|undefined)
* }}
*/
gmfx.AuthenticationLoginResponse;


/**
* @typedef {{
* success: boolean
* }}
*/
gmfx.AuthenticationDefaultResponse;
2 changes: 2 additions & 0 deletions contribs/gmf/src/authentication/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
extends:
- ../.eslintrc-googshift.yaml
221 changes: 221 additions & 0 deletions contribs/gmf/src/authentication/Service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
goog.provide('gmf.authentication.Service');

goog.require('ngeo.CustomEvent');
goog.require('gmf');
goog.require('ol');
goog.require('ol.events.EventTarget');

/**
* An "authentication" service for a GeoMapFish application. Upon loading, it
* launches a request to determine whether a user is currently logged in or
* not.
*
* The possible API requests it supports, which are all self-explanatory, are:
*
* - changePassword
* - login
* - logout
* - resetPassword
*
* @extends {ol.events.EventTarget}
*/
gmf.authentication.Service = class extends ol.events.EventTarget {

/**
* @param {angular.$http} $http Angular http service.
* @param {string} authenticationBaseUrl URL to "authentication" web service.
* @param {gmfx.User} gmfUser User.
* @ngInject
*/
constructor($http, authenticationBaseUrl, gmfUser) {

super();

/**
* @type {angular.$http}
* @private
*/
this.$http_ = $http;

/**
* The authentication url without trailing slash
* @type {string}
* @private
*/
this.baseUrl_ = authenticationBaseUrl.replace(/\/$/, '');

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

this.load_();
}

/**
* Load the authentication service, which sends an asynch request to
* determine whether the user is currently connected or not.
* @private
*/
load_() {
const url = `${this.baseUrl_}/${gmf.authentication.Service.RouteSuffix.IS_LOGGED_IN}`;
this.$http_.get(url, {withCredentials: true}).then(
this.handleLogin_.bind(this, true)
);
}

/**
* @param {string} oldPwd Old password.
* @param {string} newPwd New password.
* @param {string} confPwd New password confirmation.
* @return {angular.$q.Promise} Promise.
* @export
*/
changePassword(oldPwd, newPwd, confPwd) {
const url = `${this.baseUrl_}/${gmf.authentication.Service.RouteSuffix.CHANGE_PASSWORD}`;

return this.$http_.post(url, $.param({
'oldPassword': oldPwd,
'newPassword': newPwd,
'confirmNewPassword': confPwd
}), {
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
withCredentials: true
});
}

/**
* @param {string} login Login name.
* @param {string} pwd Password.
* @return {angular.$q.Promise} Promise.
* @export
*/
login(login, pwd) {
const url = `${this.baseUrl_}/${gmf.authentication.Service.RouteSuffix.LOGIN}`;

return this.$http_.post(url, $.param({'login': login, 'password': pwd}), {
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
withCredentials: true
}).then(
this.handleLogin_.bind(this, false));
}

/**
* @return {angular.$q.Promise} Promise.
* @export
*/
logout() {
const url = `${this.baseUrl_}/${gmf.authentication.Service.RouteSuffix.LOGOUT}`;
return this.$http_.get(url, {withCredentials: true}).then(
this.resetUser_.bind(this));
}

/**
* @param {string} login Login name.
* @return {angular.$q.Promise} Promise.
* @export
*/
resetPassword(login) {
const url = `${this.baseUrl_}/${gmf.authentication.Service.RouteSuffix.RESET_PASSWORD}`;

/**
* @param {angular.$http.Response} resp Ajax response.
* @return {gmfx.AuthenticationDefaultResponse} Response.
*/
const successFn = function(resp) {
const respData = /** @type gmfx.AuthenticationDefaultResponse} */ (
resp.data);
return respData;
}.bind(this);

return this.$http_.post(url, $.param({'login': login}), {
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(successFn);
}

/**
* @return {?gmfx.AuthenticationFunctionalities} The role functionalities.
*/
getFunctionalities() {
return this.user_.functionalities;
}

/**
* @return {number|null} The role ID.
*/
getRoleId() {
return this.user_.role_id;
}

/**
* @param {boolean} checkingLoginStatus Checking the login status?
* @param {angular.$http.Response} resp Ajax response.
* @return {angular.$http.Response} Response.
* @private
*/
handleLogin_(checkingLoginStatus, resp) {
const respData = /** @type {gmfx.AuthenticationLoginResponse} */ (resp.data);
this.setUser_(respData, !checkingLoginStatus);
if (checkingLoginStatus) {
/** @type {gmfx.AuthenticationEvent} */
const event = new ngeo.CustomEvent('ready', {user: this.user_});
this.dispatchEvent(event);
}
return resp;
}

/**
* @param {gmfx.AuthenticationLoginResponse} respData Response.
* @param {boolean} emitEvent Emit a login event?
* @private
*/
setUser_(respData, emitEvent) {
for (const key in respData) {
this.user_[key] = respData[key];
}
if (emitEvent && respData.username !== undefined) {
/** @type {gmfx.AuthenticationEvent} */
const event = new ngeo.CustomEvent('login', {user: this.user_});
this.dispatchEvent(event);
}
}

/**
* @private
*/
resetUser_() {
for (const key in this.user_) {
this.user_[key] = null;
}
/** @type {gmfx.AuthenticationEvent} */
const event = new ngeo.CustomEvent('logout', {user: this.user_});
this.dispatchEvent(event);
}
};

/**
* @enum {string}
*/
gmf.authentication.Service.RouteSuffix = {
CHANGE_PASSWORD: 'loginchange',
IS_LOGGED_IN: 'loginuser',
LOGIN: 'login',
LOGOUT: 'logout',
RESET_PASSWORD: 'loginresetpassword'
};

/**
* @type {!angular.Module}
*/
gmf.authentication.Service.module = angular.module('gmfAuthenticationService', []);
gmf.authentication.Service.module.service('gmfAuthenticationService', gmf.authentication.Service);
gmf.module.requires.push(gmf.authentication.Service.module.name);

gmf.authentication.Service.module.value('gmfUser', {
'functionalities': null,
'is_password_changed': null,
'role_id': null,
'role_name': null,
'username': null
});
Loading

0 comments on commit 1fa38e8

Please sign in to comment.