1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-20 14:54:15 +01:00

266 lines
9.8 KiB
JavaScript
Raw Normal View History

/* eslint-disable
camelcase,
no-return-assign,
no-undef,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
2018-11-21 11:08:53 +01:00
'use strict';
/* COMMON CODE */
2018-11-19 16:17:49 +01:00
// list of supported authentication methods
const METHODS = {
2021-07-01 11:23:58 +02:00
DatabaseProvider: 'local_database',
OAuth2Provider: 'o_auth2'
2018-11-21 11:08:53 +01:00
};
2018-11-19 16:17:49 +01:00
/**
* Iterate through the provided array and return the index of the requested element
* @param elements {Array<{id:*}>}
* @param id {*} id of the element to retrieve in the list
* @returns {number} index of the requested element, in the provided array
*/
2018-11-20 12:26:06 +01:00
const findIdxById = function (elements, id) {
2018-11-21 11:08:53 +01:00
return (elements.map(function (elem) { return elem.id; })).indexOf(id);
};
2018-11-19 16:17:49 +01:00
/**
2018-11-20 12:26:06 +01:00
* For OAuth2 authentications, mapping the user's ID is mandatory. This function will check that this mapping
2018-11-19 16:17:49 +01:00
* is effective and will return false otherwise
* @param mappings {Array<Object>} expected: $scope.provider.auth_provider_mappings_attributes
2018-11-19 16:17:49 +01:00
* @returns {Boolean} true if the mapping is declared
*/
const check_oauth2_id_is_mapped = function (mappings) {
2021-07-01 11:23:58 +02:00
for (const mapping of Array.from(mappings)) {
if ((mapping.local_model === 'user') && (mapping.local_field === 'uid') && !mapping._destroy) {
2018-11-21 11:08:53 +01:00
return true;
}
}
2018-11-21 11:08:53 +01:00
return false;
};
2018-11-19 16:17:49 +01:00
/**
* Page listing all authentication providers
*/
Application.Controllers.controller('AuthentificationController', ['$scope', '$state', '$rootScope', 'dialogs', 'growl', 'authProvidersPromise', 'AuthProvider', '_t',
function ($scope, $state, $rootScope, dialogs, growl, authProvidersPromise, AuthProvider, _t) {
/* PUBLIC SCOPE */
2018-11-19 16:17:49 +01:00
// full list of authentication providers
2018-11-21 11:08:53 +01:00
$scope.providers = authProvidersPromise;
2018-11-19 16:17:49 +01:00
/**
* Translate the classname into an explicit textual message
* @param type {string} Ruby polymorphic model classname
* @returns {string}
*/
$scope.getType = function (type) {
2018-11-21 11:08:53 +01:00
const text = METHODS[type];
if (typeof text !== 'undefined') {
return _t(`app.admin.members.authentication_form.${text}`);
} else {
return _t('app.admin.members.authentication_form.unknown') + type;
}
2018-11-21 11:08:53 +01:00
};
2018-11-19 16:17:49 +01:00
/**
* Translate the status string into an explicit textual message
* @param status {string} active | pending | previous
* @returns {string}
*/
$scope.getState = function (status) {
switch (status) {
case 'active': return _t('app.admin.members.authentication_form.active');
case 'pending': return _t('app.admin.members.authentication_form.pending');
case 'previous': return _t('app.admin.members.authentication_form.previous_provider');
default: return _t('app.admin.members.authentication_form.unknown') + status;
}
2018-11-21 11:08:53 +01:00
};
2018-11-19 16:17:49 +01:00
/**
* Ask for confirmation then delete the specified provider
* @param providers {Array} full list of authentication providers
* @param provider {Object} provider to delete
*/
2018-11-20 12:26:06 +01:00
$scope.destroyProvider = function (providers, provider) {
dialogs.confirm(
{
resolve: {
object () {
return {
title: _t('app.admin.members.authentication_form.confirmation_required'),
msg: _t('app.admin.members.authentication_form.do_you_really_want_to_delete_the_TYPE_authentication_provider_NAME', { TYPE: $scope.getType(provider.providable_type), NAME: provider.name })
2018-11-21 11:08:53 +01:00
};
}
}
2018-11-20 12:26:06 +01:00
},
2018-11-21 10:59:07 +01:00
function () {
2018-11-20 12:26:06 +01:00
// the admin has confirmed, delete
AuthProvider.delete(
{ id: provider.id },
function () {
2018-11-21 11:08:53 +01:00
providers.splice(findIdxById(providers, provider.id), 1);
growl.success(_t('app.admin.members.authentication_form.authentication_provider_successfully_deleted'));
2018-11-20 12:26:06 +01:00
},
function () { growl.error(_t('app.admin.members.authentication_form.an_error_occurred_unable_to_delete_the_specified_provider')); }
2018-11-21 11:08:53 +01:00
);
}
2018-11-21 11:08:53 +01:00
);
};
}
2016-03-23 18:39:41 +01:00
2018-11-21 11:08:53 +01:00
]);
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
/**
* Page to add a new authentication provider
*/
Application.Controllers.controller('NewAuthenticationController', ['$scope', '$state', '$rootScope', '$uibModal', 'dialogs', 'growl', 'mappingFieldsPromise', 'authProvidersPromise', 'AuthProvider', '_t',
function ($scope, $state, $rootScope, $uibModal, dialogs, growl, mappingFieldsPromise, authProvidersPromise, AuthProvider, _t) {
2018-11-21 11:08:53 +01:00
$scope.mode = 'creation';
2022-04-04 18:19:59 +02:00
/**
* Shows a success message forwarded from a child react component
*/
$scope.onSuccess = function (message) {
growl.success(message);
};
/**
* Callback triggered by react components
*/
$scope.onError = function (message) {
growl.error(message);
};
2018-11-19 16:17:49 +01:00
// default parameters for the new authentication provider
$scope.provider = {
name: '',
providable_type: '',
providable_attributes: {}
2018-11-21 11:08:53 +01:00
};
2018-11-19 16:17:49 +01:00
/**
* Initialize some provider's specific properties when selecting the provider type
*/
$scope.updateProvidable = function () {
// === OAuth2Provider ===
if ($scope.provider.providable_type === 'OAuth2Provider') {
if (typeof $scope.provider.auth_provider_mappings_attributes === 'undefined') {
return $scope.provider.auth_provider_mappings_attributes = [];
}
}
2018-11-21 11:08:53 +01:00
};
// Add others providers initializers here if needed ...
2018-11-19 16:17:49 +01:00
/**
* Validate and save the provider parameters in database
*/
$scope.registerProvider = function () {
// === DatabaseProvider ===
2018-11-21 11:08:53 +01:00
let provider;
if ($scope.provider.providable_type === 'DatabaseProvider') {
// prevent from adding mode than 1
for (provider of Array.from(authProvidersPromise)) {
if (provider.providable_type === 'DatabaseProvider') {
growl.error(_t('app.admin.authentication_new.a_local_database_provider_already_exists_unable_to_create_another'));
2018-11-21 11:08:53 +01:00
return false;
}
}
return AuthProvider.save({ auth_provider: $scope.provider }, function (provider) {
growl.success(_t('app.admin.authentication_new.local_provider_successfully_saved'));
2018-11-21 11:08:53 +01:00
return $state.go('app.admin.members');
});
// === OAuth2Provider ===
} else if ($scope.provider.providable_type === 'OAuth2Provider') {
// check the ID mapping
if (!check_oauth2_id_is_mapped($scope.provider.auth_provider_mappings_attributes)) {
growl.error(_t('app.admin.authentication_new.it_is_required_to_set_the_matching_between_User.uid_and_the_API_to_add_this_provider'));
2018-11-21 11:08:53 +01:00
return false;
}
// discourage the use of unsecure SSO
if (!($scope.provider.providable_attributes.base_url.indexOf('https://') > -1)) {
2018-11-20 12:26:06 +01:00
dialogs.confirm(
{
size: 'l',
resolve: {
object () {
return {
title: _t('app.admin.authentication_new.security_issue_detected'),
msg: _t('app.admin.authentication_new.beware_the_oauth2_authenticatoin_provider_you_are_about_to_add_isnt_using_HTTPS') +
_t('app.admin.authentication_new.this_is_a_serious_security_issue_on_internet_and_should_never_be_used_except_for_testing_purposes') +
_t('app.admin.authentication_new.do_you_really_want_to_continue')
2018-11-21 11:08:53 +01:00
};
}
}
2018-11-20 12:26:06 +01:00
},
function () { // unsecured http confirmed
2018-11-21 10:59:07 +01:00
AuthProvider.save({ auth_provider: $scope.provider }, function (provider) {
growl.success(_t('app.admin.authentication_new.unsecured_oauth2_provider_successfully_added'));
2018-11-21 11:08:53 +01:00
return $state.go('app.admin.members');
});
}
2018-11-21 11:08:53 +01:00
);
} else {
2018-11-20 12:26:06 +01:00
AuthProvider.save({ auth_provider: $scope.provider }, function (provider) {
growl.success(_t('app.admin.authentication_new.oauth2_provider_successfully_added'));
2018-11-21 11:08:53 +01:00
return $state.go('app.admin.members');
});
}
}
2018-11-21 11:08:53 +01:00
};
}
2018-11-21 11:08:53 +01:00
]);
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
/**
* Page to edit an already added authentication provider
*/
2022-03-15 17:10:33 +01:00
Application.Controllers.controller('EditAuthenticationController', ['$scope', '$state', '$rootScope', '$uibModal', 'dialogs', 'growl', 'providerPromise', 'mappingFieldsPromise', 'AuthProvider', '_t',
function ($scope, $state, $rootScope, $uibModal, dialogs, growl, providerPromise, mappingFieldsPromise, AuthProvider, _t) {
2018-11-19 16:17:49 +01:00
// parameters of the currently edited authentication provider
2018-11-21 11:08:53 +01:00
$scope.provider = providerPromise;
2018-11-21 11:08:53 +01:00
$scope.mode = 'edition';
2018-11-19 16:17:49 +01:00
/**
* Update the current provider with the new inputs
*/
$scope.updateProvider = function () {
// check the ID mapping
if (!check_oauth2_id_is_mapped($scope.provider.auth_provider_mappings_attributes)) {
growl.error(_t('app.admin.authentication_edit.it_is_required_to_set_the_matching_between_User.uid_and_the_API_to_add_this_provider'));
2018-11-21 11:08:53 +01:00
return false;
}
2018-11-20 12:26:06 +01:00
return AuthProvider.update(
{ id: $scope.provider.id },
{ auth_provider: $scope.provider },
function (provider) {
growl.success(_t('app.admin.authentication_edit.provider_successfully_updated'));
2018-11-21 11:08:53 +01:00
$state.go('app.admin.members');
2018-11-20 12:26:06 +01:00
},
function () { growl.error(_t('app.admin.authentication_edit.an_error_occurred_unable_to_update_the_provider')); }
2018-11-21 11:08:53 +01:00
);
};
2016-03-23 18:39:41 +01:00
/**
* Shows a success message forwarded from a child react component
*/
$scope.onSuccess = function (message) {
growl.success(message);
};
/**
* Callback triggered by react components
*/
$scope.onError = function (message) {
growl.error(message);
};
}
2018-11-21 11:08:53 +01:00
]);