1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-12 23:09:03 +01:00
fab-manager/app/assets/javascripts/controllers/members.js

335 lines
11 KiB
JavaScript
Raw Normal View History

/* eslint-disable
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
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
2018-11-21 11:08:53 +01:00
'use strict';
2015-05-05 03:10:25 +02:00
/**
* Controller used in the members listing page
*/
Application.Controllers.controller('MembersController', ['$scope', 'Member', 'membersPromise', function ($scope, Member, membersPromise) {
/* PRIVATE STATIC CONSTANTS */
// number of invoices loaded each time we click on 'load more...'
2018-11-21 11:08:53 +01:00
const MEMBERS_PER_PAGE = 10;
/* PUBLIC SCOPE */
// currently displayed page of members
2018-11-21 11:08:53 +01:00
$scope.page = 1;
// members list
2018-11-21 11:08:53 +01:00
$scope.members = membersPromise;
2015-05-05 03:10:25 +02:00
// true when all members are loaded
2018-11-21 11:08:53 +01:00
$scope.noMoreResults = false;
/**
* Callback for the 'load more' button.
* Will load the next results of the current search, if any
*/
$scope.showNextMembers = function () {
2018-11-21 11:08:53 +01:00
$scope.page += 1;
return Member.query({
requested_attributes: '[profile]',
page: $scope.page,
size: MEMBERS_PER_PAGE
}, function (members) {
2018-11-21 11:08:53 +01:00
$scope.members = $scope.members.concat(members);
if (!members[0] || (members[0].maxMembers <= $scope.members.length)) {
2018-11-21 11:08:53 +01:00
return $scope.noMoreResults = true;
}
2018-11-21 11:08:53 +01:00
});
};
/* PRIVATE SCOPE */
/**
* Kind of constructor: these actions will be realized first when the controller is loaded
*/
const initialize = function () {
if (!membersPromise[0] || (membersPromise[0].maxMembers <= $scope.members.length)) {
2018-11-21 11:08:53 +01:00
return $scope.noMoreResults = true;
}
2018-11-21 11:08:53 +01:00
};
// !!! MUST BE CALLED AT THE END of the controller
2018-11-21 11:08:53 +01:00
return initialize();
}
2018-11-21 11:08:53 +01:00
]);
2015-05-05 03:10:25 +02:00
/**
* Controller used when editing the current user's profile (in dashboard)
*/
Application.Controllers.controller('EditProfileController', ['$scope', '$rootScope', '$state', '$window', '$sce', '$cookies', '$injector', 'Member', 'Auth', 'Session', 'activeProviderPromise', 'growl', 'dialogs', 'CSRF', 'memberPromise', 'groups', '_t',
function ($scope, $rootScope, $state, $window, $sce, $cookies, $injector, Member, Auth, Session, activeProviderPromise, growl, dialogs, CSRF, memberPromise, groups, _t) {
2018-11-27 15:16:23 +01:00
/* PUBLIC SCOPE */
2015-05-05 03:10:25 +02:00
// API URL where the form will be posted
2018-11-21 11:08:53 +01:00
$scope.actionUrl = `/api/members/${$scope.currentUser.id}`;
2015-05-05 03:10:25 +02:00
// list of groups
2018-11-21 11:08:53 +01:00
$scope.groups = groups.filter(g => !g.disabled);
2016-03-23 18:39:41 +01:00
// Form action on the above URL
2018-11-21 11:08:53 +01:00
$scope.method = 'patch';
2015-05-05 03:10:25 +02:00
// Current user's profile
2018-11-21 11:08:53 +01:00
$scope.user = memberPromise;
2016-03-23 18:39:41 +01:00
// default : do not show the group changing form
$scope.group =
2018-11-21 11:08:53 +01:00
{ change: false };
2016-03-23 18:39:41 +01:00
// group ID of the current/selected user
2018-11-21 11:08:53 +01:00
$scope.userGroup = memberPromise.group_id;
2016-03-23 18:39:41 +01:00
// active authentication provider parameters
2018-11-21 11:08:53 +01:00
$scope.activeProvider = activeProviderPromise;
2016-03-23 18:39:41 +01:00
// allow the user to change his password except if he connect from an SSO
2018-11-21 11:08:53 +01:00
$scope.preventPassword = false;
2016-03-23 18:39:41 +01:00
// get the status of cookies acceptance
$scope.cookiesStatus = $cookies.get('fab-manager-cookies-consent');
// mapping of fields to disable
2018-11-21 11:08:53 +01:00
$scope.preventField = {};
2015-05-05 03:10:25 +02:00
// Should the passord be modified?
$scope.password = { change: false };
// Angular-Bootstrap datepicker configuration for birthday
$scope.datePicker = {
format: Fablab.uibDateFormat,
opened: false, // default: datePicker is not shown
options: {
startingDay: Fablab.weekStartingDay
}
2018-11-21 11:08:53 +01:00
};
// This boolean value will tell if the current user is the super-admin
$scope.isSuperAdmin = memberPromise.id === Fablab.superadminId;
/**
* Return the group object, identified by the ID set in $scope.userGroup
*/
$scope.getUserGroup = function () {
for (const group of Array.from($scope.groups)) {
if (group.id === $scope.userGroup) {
2018-11-21 11:08:53 +01:00
return group;
}
}
2018-11-21 11:08:53 +01:00
};
/**
* Change the group of the current user to the one set in $scope.userGroup
*/
$scope.selectGroup = () =>
Member.update({ id: $scope.user.id }, { user: { group_id: $scope.userGroup } }, function (user) {
2018-11-21 11:08:53 +01:00
$scope.user = user;
$rootScope.currentUser = user;
Auth._currentUser.group_id = user.group_id;
$scope.group.change = false;
return growl.success(_t('app.logged.dashboard.settings.your_group_has_been_successfully_changed'));
}
, function (err) {
growl.error(_t('app.logged.dashboard.settings.an_unexpected_error_prevented_your_group_from_being_changed'));
2018-11-21 11:08:53 +01:00
return console.error(err);
});
/**
* Callback to diplay the datepicker as a dropdown when clicking on the input field
* @param $event {Object} jQuery event object
*/
$scope.openDatePicker = function ($event) {
2018-11-21 11:08:53 +01:00
$event.preventDefault();
$event.stopPropagation();
return $scope.datePicker.opened = true;
};
/**
* For use with ngUpload (https://github.com/twilson63/ngUpload).
* Intended to be the callback when the upload is done: any raised error will be stacked in the
* $scope.alerts array. If everything goes fine, the user's profile is updated and the user is
* redirected to the home page
* @param content {Object} JSON - The upload's result
*/
$scope.submited = function (content) {
if ((content.id == null)) {
2018-11-21 11:08:53 +01:00
$scope.alerts = [];
return angular.forEach(content, (v, k) =>
angular.forEach(v, err =>
$scope.alerts.push({
msg: k + ': ' + err,
type: 'danger'
})
)
2018-11-21 11:08:53 +01:00
);
} else {
2018-11-21 11:08:53 +01:00
$scope.currentUser.profile.user_avatar = content.profile.user_avatar;
Auth._currentUser.profile.user_avatar = content.profile.user_avatar;
$scope.currentUser.name = content.name;
Auth._currentUser.name = content.name;
$scope.currentUser = content;
Auth._currentUser = content;
$rootScope.currentUser = content;
return $state.go('app.public.home');
}
2018-11-21 11:08:53 +01:00
};
/**
* Ask for confirmation then delete the current user's account
* @param user {Object} the current user (to delete)
*/
$scope.deleteUser = user =>
dialogs.confirm({
resolve: {
object () {
return {
title: _t('app.logged.dashboard.settings.confirmation_required'),
2019-06-11 16:56:11 +02:00
msg: $sce.trustAsHtml(
_t('app.logged.dashboard.settings.confirm_delete_your_account') + '<br/>' +
'<strong>' + _t('app.logged.dashboard.settings.all_data_will_be_lost') + '</strong><br/><br/>' +
_t('app.logged.dashboard.settings.invoicing_data_kept') + '<br/>' +
_t('app.logged.dashboard.settings.statistic_data_anonymized') + '<br/>' +
_t('app.logged.dashboard.settings.no_further_access_to_projects')
2019-06-11 16:56:11 +02:00
)
2018-11-21 11:08:53 +01:00
};
}
}
}
, () => // cancel confirmed
Member.remove({ id: user.id }, () =>
Auth.logout().then(function () {
2018-11-21 11:08:53 +01:00
$state.go('app.public.home');
return growl.success(_t('app.logged.dashboard.settings.your_user_account_has_been_successfully_deleted_goodbye'));
})
, function (error) {
2018-11-21 11:08:53 +01:00
console.log(error);
return growl.error(_t('app.logged.dashboard.settings.an_error_occured_preventing_your_account_from_being_deleted'));
})
2018-11-21 11:08:53 +01:00
);
/**
* For use with 'ng-class', returns the CSS class name for the uploads previews.
* The preview may show a placeholder or the content of the file depending on the upload state.
* @param v {*} any attribute, will be tested for truthiness (see JS evaluation rules)
*/
$scope.fileinputClass = function (v) {
if (v) {
2018-11-21 11:08:53 +01:00
return 'fileinput-exists';
} else {
2018-11-21 11:08:53 +01:00
return 'fileinput-new';
}
2018-11-21 11:08:53 +01:00
};
/**
* Check if the of the properties editable by the user are linked to the SSO
* @return {boolean} true if some editable fields are mapped with the SSO, false otherwise
*/
$scope.hasSsoFields = () =>
// if check if keys > 1 because there's a minimum of 1 mapping (id <-> provider-uid)
// so the user may want to edit his profile on the SSO if at least 2 mappings exists
2018-11-21 11:08:53 +01:00
Object.keys($scope.preventField).length > 1;
/**
* Disconnect and re-connect the user to the SSO to force the synchronisation of the profile's data
*/
$scope.syncProfile = () =>
Auth.logout().then(function (oldUser) {
2018-11-21 11:08:53 +01:00
Session.destroy();
$rootScope.currentUser = null;
$rootScope.toCheckNotifications = false;
$scope.notifications = {
total: 0,
unread: 0
2018-11-21 11:08:53 +01:00
};
return $window.location.href = $scope.activeProvider.link_to_sso_connect;
});
2016-03-23 18:39:41 +01:00
/**
* Destroy the cookie used to save the user's preference, this will trigger the choice popup again
*/
$scope.resetCookies = function () {
$cookies.remove('fab-manager-cookies-consent');
$scope.cookiesStatus = undefined;
$injector.get('$state').reload();
};
/* PRIVATE SCOPE */
2016-03-23 18:39:41 +01:00
/**
* Kind of constructor: these actions will be realized first when the controller is loaded
*/
const initialize = function () {
2018-11-21 11:08:53 +01:00
CSRF.setMetaTags();
2016-03-23 18:39:41 +01:00
// init the birth date to JS object
$scope.user.statistic_profile.birthday = moment($scope.user.statistic_profile.birthday).toDate();
2016-03-23 18:39:41 +01:00
if ($scope.activeProvider.providable_type !== 'DatabaseProvider') {
2018-11-21 11:08:53 +01:00
$scope.preventPassword = true;
}
// bind fields protection with sso fields
2018-11-21 11:08:53 +01:00
return angular.forEach(activeProviderPromise.mapping, map => $scope.preventField[map] = true);
};
2016-03-23 18:39:41 +01:00
// !!! MUST BE CALLED AT THE END of the controller
2018-11-21 11:08:53 +01:00
return initialize();
}
2018-11-21 11:08:53 +01:00
]);
2016-03-23 18:39:41 +01:00
/**
* Controller used on the public user's profile page (seeing another user's profile)
*/
Application.Controllers.controller('ShowProfileController', ['$scope', 'memberPromise', 'SocialNetworks', function ($scope, memberPromise, SocialNetworks) {
// Selected user's information
2018-11-21 11:08:53 +01:00
$scope.user = memberPromise; // DEPENDENCY WITH NAVINUM GAMIFICATION PLUGIN !!!!
2016-05-17 16:41:32 +02:00
// List of social networks associated with this user and toggle 'show all' state
$scope.social = {
showAllLinks: false,
2016-05-17 16:41:32 +02:00
networks: SocialNetworks
2018-11-21 11:08:53 +01:00
};
2016-05-17 16:41:32 +02:00
/* PRIVATE SCOPE */
2016-05-17 16:41:32 +02:00
/**
* Kind of constructor: these actions will be realized first when the controller is loaded
*/
2018-11-21 11:08:53 +01:00
const initialize = () => $scope.social.networks = filterNetworks();
2016-05-17 16:41:32 +02:00
/**
* Filter social network or website that are associated with the profile of the user provided in promise
* and return the filtered networks
* @return {Array}
*/
var filterNetworks = function () {
2018-11-21 11:08:53 +01:00
const networks = [];
for (const network of Array.from(SocialNetworks)) {
if ($scope.user.profile[network] && ($scope.user.profile[network].length > 0)) {
2018-11-21 11:08:53 +01:00
networks.push(network);
}
}
2018-11-21 11:08:53 +01:00
return networks;
};
2016-05-17 16:41:32 +02:00
// !!! MUST BE CALLED AT THE END of the controller
2018-11-21 11:08:53 +01:00
return initialize();
}
2016-05-17 16:41:32 +02:00
2018-11-21 11:08:53 +01:00
]);