This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/static/js/pages/user-view.js
2018-05-08 10:39:10 -04:00

236 lines
No EOL
7.3 KiB
JavaScript

(function() {
/**
* Page that displays details about an user.
*/
angular.module('quayPages').config(['pages', function(pages) {
pages.create('user-view', 'user-view.html', UserViewCtrl, {
'newLayout': true,
'title': 'User {{ user.username }}',
'description': 'User {{ user.username }}'
})
}]);
function UserViewCtrl($scope, $routeParams, $timeout, ApiService, UserService, UIService, AvatarService, Config, ExternalLoginService, CookieService) {
var username = $routeParams.username;
$scope.Config = Config;
$scope.showAppsCounter = 0;
$scope.showRobotsCounter = 0;
$scope.showBillingCounter = 0;
$scope.showLogsCounter = 0;
$scope.changeEmailInfo = null;
$scope.changePasswordInfo = null;
$scope.changeMetadataInfo = null;
$scope.hasSingleSignin = ExternalLoginService.hasSingleSignin();
$scope.context = {};
$scope.oidcLoginProvider = null;
if (Config['INTERNAL_OIDC_SERVICE_ID']) {
ExternalLoginService.EXTERNAL_LOGINS.forEach(function(provider) {
if (provider.id == Config['INTERNAL_OIDC_SERVICE_ID']) {
$scope.oidcLoginProvider = provider;
}
});
}
UserService.updateUserIn($scope, function(user) {
if (user && user.username) {
if ($scope.oidcLoginProvider && $routeParams['idtoken']) {
$scope.context.idTokenCredentials = {
'username': UserService.getCLIUsername(),
'password': $routeParams['idtoken'],
'namespace': UserService.currentUser().username
};
}
}
});
var loadRepositories = function() {
var options = {
'public': true,
'namespace': username,
'last_modified': true,
'popularity': true
};
$scope.context.viewuser.repositories = ApiService.listReposAsResource().withOptions(options).get(function(resp) {
return resp.repositories;
});
};
var loadUser = function() {
$scope.userResource = ApiService.getUserInformationAsResource({'username': username}).get(function(user) {
$scope.context.viewuser = user;
$scope.viewuser = user;
$timeout(function() {
// Load the repositories.
loadRepositories();
// Show the password change dialog if immediately after an account recovery.
if ($routeParams.action == 'password' && UserService.isNamespaceAdmin(username)) {
$scope.showChangePassword();
}
}, 10);
});
};
// Load the user.
loadUser();
$scope.showRobots = function() {
$scope.showRobotsCounter++;
};
$scope.showLogs = function() {
$scope.showLogsCounter++;
};
$scope.showApplications = function() {
$scope.showAppsCounter++;
};
$scope.showChangePassword = function() {
$scope.changePasswordInfo = {};
};
$scope.changePassword = function(info, callback) {
if (Config.AUTHENTICATION_TYPE != 'Database') { return; }
var data = {
'password': $scope.changePasswordInfo.password
};
var errorDisplay = ApiService.errorDisplay('Could not change password', callback);
ApiService.changeUserDetails(data).then(function(resp) {
// Reload the user.
UserService.load();
callback(true);
}, errorDisplay);
};
$scope.generateClientToken = function() {
var generateToken = function(password) {
if (!password) {
return;
}
var data = {
'password': password
};
ApiService.generateUserClientKey(data).then(function(resp) {
$scope.context.encryptedPasswordCredentials = {
'username': UserService.getCLIUsername(),
'password': resp['key'],
'namespace': UserService.currentUser().username
};
}, ApiService.errorDisplay('Could not generate token'));
};
UIService.showPasswordDialog('Enter your password to generate an encrypted version:', generateToken);
};
$scope.showChangeMetadata = function(field_name, field_title) {
$scope.changeMetadataInfo = {
'value': $scope.context.viewuser[field_name],
'field': field_name,
'title': field_title
};
};
$scope.updateMetadataInfo = function(info, callback) {
var details = {};
details[info.field] = info.value;
var errorDisplay = ApiService.errorDisplay('Could not update ' + info.title, callback);
ApiService.changeUserDetails(details).then(function() {
$scope.context.viewuser[info.field] = info.value;
callback(true);
}, errorDisplay);
};
$scope.showChangeEmail = function() {
$scope.changeEmailInfo = {
'email': $scope.context.viewuser.email
};
};
$scope.changeEmail = function(info, callback) {
var details = {
'email': $scope.changeEmailInfo.email
};
var errorDisplay = ApiService.errorDisplay('Could not change email address', callback);
ApiService.changeUserDetails(details).then(function() {
$scope.context.emailAwaitingChange = $scope.changeEmailInfo.email;
callback(true);
}, errorDisplay);
};
$scope.showChangeAccount = function() {
$scope.convertAccountInfo = {
'user': $scope.context.viewuser
};
};
$scope.showBilling = function() {
$scope.showBillingCounter++;
};
$scope.notificationsPermissionsEnabled = window['Notification']
&& Notification.permission === 'granted'
&& CookieService.get('quay.enabledDesktopNotifications') === 'on';
$scope.desktopNotificationsPermissionIsDisabled = () => window['Notification'] && Notification.permission === 'denied';
$scope.toggleDesktopNotifications = () => {
if (!window['Notification']) { // unsupported in IE & some older browsers, we'll just tell the user it's not available
bootbox.dialog({
"message": 'Desktop Notifications unsupported in this browser',
"title": 'Unsupported Option',
"buttons": {
"close": {
"label": "Close",
"className": "btn-primary"
}
}
});
return;
}
if (CookieService.get('quay.enabledDesktopNotifications') === 'on') {
CookieService.putPermanent('quay.enabledDesktopNotifications', 'off');
CookieService.clear('quay.notifications.mostRecentTimestamp');
$scope.notificationsPermissionsEnabled = false;
} else {
if (Notification.permission === 'default') {
Notification.requestPermission()
.then((newPermission) => {
if (newPermission === 'granted') {
CookieService.putPermanent('quay.enabledDesktopNotifications', 'on');
CookieService.putPermanent('quay.notifications.mostRecentTimestamp', new Date().getTime().toString());
$scope.notificationsPermissionsEnabled = true;
} else {
$scope.notificationsPermissionsEnabled = false;
}
});
} else if (Notification.permission === 'granted') {
CookieService.putPermanent('quay.enabledDesktopNotifications', 'on');
CookieService.putPermanent('quay.notifications.mostRecentTimestamp', new Date().getTime().toString());
$scope.notificationsPermissionsEnabled = true;
}
}
};
}
})();