2015-02-19 21:21:54 +00:00
|
|
|
/**
|
|
|
|
* Service which monitors the current user session and provides methods for returning information
|
|
|
|
* about the user.
|
|
|
|
*/
|
|
|
|
angular.module('quay')
|
2016-08-09 21:58:33 +00:00
|
|
|
.factory('UserService', ['ApiService', 'CookieService', '$rootScope', 'Config', '$location',
|
2015-02-19 21:21:54 +00:00
|
|
|
|
2016-08-09 21:58:33 +00:00
|
|
|
function(ApiService, CookieService, $rootScope, Config, $location) {
|
2015-02-19 21:21:54 +00:00
|
|
|
var userResponse = {
|
|
|
|
verified: false,
|
|
|
|
anonymous: true,
|
|
|
|
username: null,
|
|
|
|
email: null,
|
|
|
|
organizations: [],
|
2015-04-08 19:15:21 +00:00
|
|
|
logins: [],
|
|
|
|
beforeload: true
|
2015-02-19 21:21:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var userService = {}
|
2016-06-14 22:09:42 +00:00
|
|
|
var _EXTERNAL_SERVICES = ['ldap', 'jwtauthn', 'keystone', 'dex'];
|
2015-02-19 21:21:54 +00:00
|
|
|
|
|
|
|
userService.hasEverLoggedIn = function() {
|
|
|
|
return CookieService.get('quay.loggedin') == 'true';
|
|
|
|
};
|
|
|
|
|
|
|
|
userService.updateUserIn = function(scope, opt_callback) {
|
|
|
|
scope.$watch(function () { return userService.currentUser(); }, function (currentUser) {
|
|
|
|
scope.user = currentUser;
|
|
|
|
if (opt_callback) {
|
|
|
|
opt_callback(currentUser);
|
|
|
|
}
|
|
|
|
}, true);
|
|
|
|
};
|
|
|
|
|
|
|
|
userService.load = function(opt_callback) {
|
|
|
|
var handleUserResponse = function(loadedUser) {
|
|
|
|
userResponse = loadedUser;
|
|
|
|
|
|
|
|
if (!userResponse.anonymous) {
|
|
|
|
if (Config.MIXPANEL_KEY) {
|
2016-05-16 19:54:52 +00:00
|
|
|
try {
|
|
|
|
mixpanel.identify(userResponse.username);
|
|
|
|
mixpanel.people.set({
|
|
|
|
'$email': userResponse.email,
|
|
|
|
'$username': userResponse.username,
|
|
|
|
'verified': userResponse.verified
|
|
|
|
});
|
|
|
|
mixpanel.people.set_once({
|
|
|
|
'$created': new Date()
|
|
|
|
})
|
|
|
|
} catch (e) {
|
|
|
|
window.console.log(e);
|
|
|
|
}
|
2015-02-19 21:21:54 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 17:48:35 +00:00
|
|
|
if (Config.MARKETO_MUNCHKIN_ID && userResponse['marketo_user_hash']) {
|
|
|
|
associateLeadBody = {'Email': userResponse.email};
|
|
|
|
if (window.Munchkin !== undefined) {
|
|
|
|
try {
|
|
|
|
Munchkin.munchkinFunction(
|
|
|
|
'associateLead',
|
|
|
|
associateLeadBody,
|
|
|
|
userResponse['marketo_user_hash']
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
window.__quay_munchkin_queue.push([
|
|
|
|
'associateLead',
|
|
|
|
associateLeadBody,
|
|
|
|
userResponse['marketo_user_hash']
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-19 21:21:54 +00:00
|
|
|
if (window.Raven !== undefined) {
|
2016-05-16 19:54:52 +00:00
|
|
|
try {
|
|
|
|
Raven.setUser({
|
|
|
|
email: userResponse.email,
|
|
|
|
id: userResponse.username
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
window.console.log(e);
|
|
|
|
}
|
2015-02-19 21:21:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CookieService.putPermanent('quay.loggedin', 'true');
|
|
|
|
} else {
|
|
|
|
if (window.Raven !== undefined) {
|
|
|
|
Raven.setUser();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-08 22:43:50 +00:00
|
|
|
// If the loaded user has a prompt, redirect them to the update page.
|
|
|
|
if (loadedUser.prompts && loadedUser.prompts.length) {
|
|
|
|
$location.path('/updateuser');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-19 21:21:54 +00:00
|
|
|
if (opt_callback) {
|
2016-09-08 22:43:50 +00:00
|
|
|
opt_callback(loadedUser);
|
2015-02-19 21:21:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ApiService.getLoggedInUser().then(function(loadedUser) {
|
|
|
|
handleUserResponse(loadedUser);
|
|
|
|
}, function() {
|
|
|
|
handleUserResponse({'anonymous': true});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-03-19 19:08:18 +00:00
|
|
|
userService.isOrganization = function(name) {
|
|
|
|
return !!userService.getOrganization(name);
|
|
|
|
};
|
|
|
|
|
2015-02-19 21:21:54 +00:00
|
|
|
userService.getOrganization = function(name) {
|
|
|
|
if (!userResponse || !userResponse.organizations) { return null; }
|
|
|
|
for (var i = 0; i < userResponse.organizations.length; ++i) {
|
|
|
|
var org = userResponse.organizations[i];
|
|
|
|
if (org.name == name) {
|
|
|
|
return org;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
userService.isNamespaceAdmin = function(namespace) {
|
|
|
|
if (namespace == userResponse.username) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
var org = userService.getOrganization(namespace);
|
|
|
|
if (!org) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return org.is_org_admin;
|
|
|
|
};
|
|
|
|
|
|
|
|
userService.isKnownNamespace = function(namespace) {
|
|
|
|
if (namespace == userResponse.username) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
var org = userService.getOrganization(namespace);
|
|
|
|
return !!org;
|
|
|
|
};
|
|
|
|
|
2016-05-12 21:59:49 +00:00
|
|
|
userService.getNamespace = function(namespace) {
|
|
|
|
var org = userService.getOrganization(namespace);
|
|
|
|
if (org) {
|
|
|
|
return org;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (namespace == userResponse.username) {
|
|
|
|
return userResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2016-06-14 22:09:42 +00:00
|
|
|
userService.getCLIUsername = function() {
|
|
|
|
if (!userResponse) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var externalUsername = null;
|
|
|
|
userResponse.logins.forEach(function(login) {
|
|
|
|
if (_EXTERNAL_SERVICES.indexOf(login.service) >= 0) {
|
|
|
|
externalUsername = login.service_identifier;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return externalUsername || userResponse.username;
|
|
|
|
};
|
|
|
|
|
2016-08-09 21:58:33 +00:00
|
|
|
userService.deleteNamespace = function(info, callback) {
|
|
|
|
var namespace = info.user ? info.user.username : info.organization.name;
|
2016-10-31 16:17:50 +00:00
|
|
|
if (!namespace) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-10 20:28:20 +00:00
|
|
|
var errorDisplay = ApiService.errorDisplay('Could not delete namespace', callback);
|
2016-08-09 21:58:33 +00:00
|
|
|
var deleteNamespaceItself = function() {
|
|
|
|
info.progress = 1;
|
|
|
|
info.progressMessage = 'Deleting namespace...';
|
|
|
|
|
|
|
|
if (info.user) {
|
|
|
|
ApiService.deleteCurrentUser().then(function(resp) {
|
|
|
|
// Reload the user.
|
|
|
|
userService.load();
|
|
|
|
callback(true);
|
|
|
|
$location.path('/');
|
|
|
|
}, errorDisplay);
|
|
|
|
} else {
|
|
|
|
var delParams = {
|
2016-11-10 20:28:20 +00:00
|
|
|
'orgname': info.organization.name
|
2016-08-09 21:58:33 +00:00
|
|
|
};
|
|
|
|
|
2016-11-10 20:28:20 +00:00
|
|
|
ApiService.deleteAdminedOrganization(null, delParams).then(function(resp) {
|
2016-08-09 21:58:33 +00:00
|
|
|
// Reload the user.
|
|
|
|
userService.load();
|
|
|
|
callback(true);
|
|
|
|
$location.path('/');
|
|
|
|
}, errorDisplay);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var repoIndex = 0;
|
|
|
|
var repositories = null;
|
|
|
|
var deleteAllRepos = function() {
|
|
|
|
if (repoIndex >= repositories.length) {
|
|
|
|
deleteNamespaceItself();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var repoParams = {
|
|
|
|
'repository': namespace + '/' + repositories[repoIndex]['name']
|
|
|
|
};
|
|
|
|
|
|
|
|
info.progress = repoIndex / repositories.length;
|
|
|
|
info.progressMessage = 'Deleting repository ' + repoParams['repository'] + '...';
|
|
|
|
|
|
|
|
ApiService.deleteRepository(null, repoParams).then(function() {
|
|
|
|
repoIndex++;
|
|
|
|
deleteAllRepos();
|
|
|
|
}, errorDisplay);
|
|
|
|
};
|
|
|
|
|
|
|
|
// First delete each repo for the namespace, updating the info so it can show a progress bar.
|
|
|
|
// This is not strictly necessary (as the namespace delete call will do it as well), but it is
|
|
|
|
// a better user experience.
|
|
|
|
var params = {
|
|
|
|
'namespace': namespace,
|
|
|
|
'public': false
|
|
|
|
};
|
|
|
|
|
|
|
|
ApiService.listRepos(null, params).then(function(resp) {
|
|
|
|
repositories = resp['repositories'];
|
|
|
|
deleteAllRepos();
|
|
|
|
}, errorDisplay);
|
|
|
|
};
|
|
|
|
|
2015-02-19 21:21:54 +00:00
|
|
|
userService.currentUser = function() {
|
|
|
|
return userResponse;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Update the user in the root scope.
|
|
|
|
userService.updateUserIn($rootScope);
|
|
|
|
|
|
|
|
// Load the user the first time.
|
|
|
|
userService.load();
|
|
|
|
|
|
|
|
return userService;
|
|
|
|
}]);
|