1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-04 15:24:23 +01:00
fab-manager/app/assets/javascripts/controllers/profile.js.erb

223 lines
7.4 KiB
Plaintext
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:
* 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
*/
2016-03-23 18:39:41 +01:00
2018-11-21 11:08:53 +01:00
'use strict';
2016-03-23 18:39:41 +01:00
Application.Controllers.controller('CompleteProfileController', ['$scope', '$rootScope', '$state', '$window', '_t', 'growl', 'CSRF', 'Auth', 'Member', 'settingsPromise', 'activeProviderPromise', 'groupsPromise', 'cguFile', 'memberPromise', 'Session', 'dialogs', 'AuthProvider', 'phoneRequiredPromise',
function ($scope, $rootScope, $state, $window, _t, growl, CSRF, Auth, Member, settingsPromise, activeProviderPromise, groupsPromise, cguFile, memberPromise, Session, dialogs, AuthProvider, phoneRequiredPromise) {
/* PUBLIC SCOPE */
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
// API URL where the form will be posted
2018-11-21 11:08:53 +01:00
$scope.actionUrl = `/api/members/${memberPromise.id}`;
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
// Form action on the above URL
2018-11-21 11:08:53 +01:00
$scope.method = 'patch';
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
// genre of the application name (eg. "_le_ Fablab" or "_la_ Fabrique")
2018-11-21 11:08:53 +01:00
$scope.nameGenre = settingsPromise.name_genre;
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
// name of the current fablab application (eg. "Fablab de la Casemate")
2018-11-21 11:08:53 +01:00
$scope.fablabName = settingsPromise.fablab_name;
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
// information from the current SSO provider
2018-11-21 11:08:53 +01:00
$scope.activeProvider = activeProviderPromise;
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
// list of user's groups (student/standard/...)
2018-11-21 11:08:53 +01:00
$scope.groups = groupsPromise;
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
// current user, contains information retrieved from the SSO
2018-11-21 11:08:53 +01:00
$scope.user = memberPromise;
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
// disallow the user to change his password as he connect from SSO
2018-11-21 11:08:53 +01:00
$scope.preventPassword = true;
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
// mapping of fields to disable
2018-11-21 11:08:53 +01:00
$scope.preventField = {};
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
// CGU
2018-11-21 11:08:53 +01:00
$scope.cgu = cguFile.custom_asset;
2016-03-23 18:39:41 +01:00
// is the phone number required in _member_form?
$scope.phoneRequired = (phoneRequiredPromise.setting.value === 'true');
2018-11-19 16:17:49 +01:00
// 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
};
2018-11-19 16:17:49 +01:00
/**
* 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;
};
2018-11-19 16:17:49 +01:00
/**
* 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 = [];
2018-11-20 12:26:06 +01:00
angular.forEach(content, function (v, k) {
angular.forEach(v, function (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.user.profile.user_avatar = content.profile.user_avatar;
Auth._currentUser.profile.user_avatar = content.profile.user_avatar;
$scope.user.name = content.name;
Auth._currentUser.name = content.name;
$scope.user = content;
Auth._currentUser = content;
$rootScope.currentUser = content;
return $state.go('app.public.home');
}
2018-11-21 11:08:53 +01:00
};
2018-11-19 16:17:49 +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
};
2018-11-19 16:17:49 +01:00
/**
* Merge the current user into the account with the given auth_token
*/
2018-11-20 12:26:06 +01:00
$scope.registerAuthToken = function () {
Member.merge({ id: $rootScope.currentUser.id }, { user: { auth_token: $scope.user.auth_token } }, function (user) {
2018-11-21 11:08:53 +01:00
$scope.user = user;
Auth._currentUser = user;
$rootScope.currentUser = user;
$state.go('app.public.home');
2018-11-21 10:59:07 +01:00
}
, function (err) {
if (err.data.error) {
2018-11-21 11:08:53 +01:00
growl.error(err.data.error);
2018-11-21 10:59:07 +01:00
} else {
growl.error(_t('app.logged.profile_completion.an_unexpected_error_occurred_check_your_authentication_code'));
2018-11-21 11:08:53 +01:00
console.error(err);
}
2018-11-21 11:08:53 +01:00
});
};
2018-11-19 16:17:49 +01:00
/**
* Return the email given by the SSO provider, parsed if needed
* @return {String} E-mail of the current user
*/
$scope.ssoEmail = function () {
2018-11-21 11:08:53 +01:00
const { email } = memberPromise;
if (email) {
2018-11-21 11:08:53 +01:00
const duplicate = email.match(/^<([^>]+)>.{20}-duplicate$/);
if (duplicate) {
2018-11-21 11:08:53 +01:00
return duplicate[1];
}
}
2018-11-21 11:08:53 +01:00
return email;
};
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
/**
* Test if the user's mail is marked as duplicate
* @return {boolean}
*/
$scope.hasDuplicate = function () {
2018-11-21 11:08:53 +01:00
const { email } = memberPromise;
if (email) {
2018-11-21 11:08:53 +01:00
return !(email.match(/^<([^>]+)>.{20}-duplicate$/) === null);
}
2018-11-21 11:08:53 +01:00
};
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
/**
* Ask for email confirmation and send the SSO merging token again
2018-11-20 12:26:06 +01:00
* @param event {Object} jQuery event object
2018-11-19 16:17:49 +01:00
*/
$scope.resendCode = function (event) {
2018-11-21 11:08:53 +01:00
event.preventDefault();
event.stopPropagation();
2018-11-20 12:26:06 +01:00
dialogs.confirm(
{
templateUrl: '<%= asset_path "profile/resend_code_modal.html" %>',
resolve: {
object () {
2018-11-21 11:08:53 +01:00
return { email: memberPromise.email };
2018-11-20 12:26:06 +01:00
}
}
2018-11-20 12:26:06 +01:00
},
function (email) {
// Request the server to send an auth-migration email to the current user
AuthProvider.send_code({ email },
function (res) { growl.info(_t('app.logged.profile_completion.code_successfully_sent_again')); },
2018-11-21 11:08:53 +01:00
function (err) { growl.error(err.data.error); }
);
}
2018-11-21 11:08:53 +01:00
);
};
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
/**
* Disconnect and re-connect the user to the SSO to force the synchronisation of the profile's data
*/
2018-11-20 12:26:06 +01:00
$scope.syncProfile = function () {
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
};
$window.location.href = activeProviderPromise.link_to_sso_connect;
});
};
2016-03-23 18:39:41 +01:00
/* PRIVATE SCOPE */
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +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 = $scope.user.statistic_profile.birthday ? moment($scope.user.statistic_profile.birthday).toDate() : undefined;
2016-03-23 18:39:41 +01:00
// bind fields protection with sso fields
2018-11-21 11:08:53 +01:00
angular.forEach(activeProviderPromise.mapping, function (map) { $scope.preventField[map] = true; });
};
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
// !!! MUST BE CALLED AT THE END of the controller
2018-11-21 11:08:53 +01:00
return initialize();
}
2016-03-23 18:39:41 +01:00
2018-11-21 11:08:53 +01:00
]);