1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Store repo credentials per-user

This commit is contained in:
Nick O'Leary 2017-12-06 22:39:30 +00:00
parent f86d3a69d2
commit 304c597a2f
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
3 changed files with 66 additions and 45 deletions

View File

@ -155,6 +155,12 @@ Project.prototype.loadBranches = function() {
}); });
} }
Project.prototype.update = function (user, data) { Project.prototype.update = function (user, data) {
var username;
if (!user) {
username = "_";
} else {
username = user.username;
}
var promises = []; var promises = [];
var project = this; var project = this;
@ -210,23 +216,9 @@ Project.prototype.update = function (user, data) {
savePackage = true; savePackage = true;
this.package.description = data.summary; this.package.description = data.summary;
} }
// if (data.hasOwnProperty('remote')) {
// // TODO: this MUST move under 'git'
// for (var remote in data.remote) {
// if (data.remote.hasOwnProperty(remote)) {
// authCache.set(project.name,remote,data.remote[remote]);
// }
// }
// }
if (data.hasOwnProperty('git')) { if (data.hasOwnProperty('git')) {
if (data.git.hasOwnProperty('user')) { if (data.git.hasOwnProperty('user')) {
var username;
if (!user) {
username = "_";
} else {
username = user.username;
}
globalProjectSettings.projects[this.name].git = globalProjectSettings.projects[this.name].git || {}; globalProjectSettings.projects[this.name].git = globalProjectSettings.projects[this.name].git || {};
globalProjectSettings.projects[this.name].git.user = globalProjectSettings.projects[this.name].git.user || {}; globalProjectSettings.projects[this.name].git.user = globalProjectSettings.projects[this.name].git.user || {};
globalProjectSettings.projects[this.name].git.user[username] = { globalProjectSettings.projects[this.name].git.user[username] = {
@ -254,7 +246,7 @@ Project.prototype.update = function (user, data) {
} }
if (data.git.remotes[name].username && data.git.remotes[name].password) { if (data.git.remotes[name].username && data.git.remotes[name].password) {
var url = data.git.remotes[name].url || project.remotes[name].fetch; var url = data.git.remotes[name].url || project.remotes[name].fetch;
authCache.set(project.name,url,data.git.remotes[name]); authCache.set(project.name,url,username,data.git.remotes[name]);
} }
} }
}) })
@ -338,7 +330,7 @@ Project.prototype.getFile = function (filePath,treeish) {
} }
}; };
Project.prototype.status = function() { Project.prototype.status = function(user) {
var self = this; var self = this;
var fetchPromise; var fetchPromise;
@ -352,7 +344,7 @@ Project.prototype.status = function() {
match = remote; match = remote;
} }
}) })
return self.fetch(match); return self.fetch(user, match);
} }
}); });
} else { } else {
@ -389,20 +381,32 @@ Project.prototype.status = function() {
}) })
}; };
Project.prototype.push = function (remoteBranchName,setRemote) { Project.prototype.push = function (user,remoteBranchName,setRemote) {
var remote = this.parseRemoteBranch(remoteBranchName); var username;
return gitTools.push(this.path, remote.remote || this.currentRemote,remote.branch, setRemote, authCache.get(this.name,this.remotes[remote.remote || this.currentRemote].fetch)); if (!user) {
username = "_";
} else {
username = user.username;
}
var remote = this.parseRemoteBranch(remoteBranchName||this.branches.remote);
return gitTools.push(this.path, remote.remote || this.currentRemote,remote.branch, setRemote, authCache.get(this.name,this.remotes[remote.remote || this.currentRemote].fetch,username));
}; };
Project.prototype.pull = function (remoteBranchName,setRemote) { Project.prototype.pull = function (user,remoteBranchName,setRemote) {
var username;
if (!user) {
username = "_";
} else {
username = user.username;
}
var self = this; var self = this;
if (setRemote) { if (setRemote) {
return gitTools.setUpstream(this.path, remoteBranchName).then(function() { return gitTools.setUpstream(this.path, remoteBranchName).then(function() {
return gitTools.pull(self.path, null, null, authCache.get(self.name,this.remotes[this.currentRemote].fetch)); return gitTools.pull(self.path, null, null, authCache.get(self.name,this.remotes[this.currentRemote].fetch,username));
}) })
} else { } else {
var remote = this.parseRemoteBranch(remoteBranchName); var remote = this.parseRemoteBranch(remoteBranchName);
return gitTools.pull(this.path, remote.remote, remote.branch, authCache.get(this.name,this.remotes[this.currentRemote].fetch)); return gitTools.pull(this.path, remote.remote, remote.branch, authCache.get(this.name,this.remotes[this.currentRemote].fetch,username));
} }
}; };
@ -451,11 +455,11 @@ Project.prototype.abortMerge = function () {
return gitTools.abortMerge(this.path); return gitTools.abortMerge(this.path);
}; };
Project.prototype.getBranches = function (isRemote) { Project.prototype.getBranches = function (user, isRemote) {
var self = this; var self = this;
var fetchPromise; var fetchPromise;
if (isRemote) { if (isRemote) {
fetchPromise = self.fetch(); fetchPromise = self.fetch(user);
} else { } else {
fetchPromise = when.resolve(); fetchPromise = when.resolve();
} }
@ -464,10 +468,17 @@ Project.prototype.getBranches = function (isRemote) {
}); });
}; };
Project.prototype.fetch = function(remoteName) { Project.prototype.fetch = function(user,remoteName) {
var username;
if (!user) {
username = "_";
console.log(new Error().stack);
} else {
username = user.username;
}
var project = this; var project = this;
if (remoteName) { if (remoteName) {
return gitTools.fetch(project.path,remoteName,authCache.get(project.name,project.remotes[remoteName].fetch)).catch(function(err) { return gitTools.fetch(project.path,remoteName,authCache.get(project.name,project.remotes[remoteName].fetch,username)).catch(function(err) {
err.remote = remoteName; err.remote = remoteName;
throw err; throw err;
}) })
@ -476,7 +487,7 @@ Project.prototype.fetch = function(remoteName) {
var promise = when.resolve(); var promise = when.resolve();
remotes.forEach(function(remote) { remotes.forEach(function(remote) {
promise = promise.then(function() { promise = promise.then(function() {
return gitTools.fetch(project.path,remote,authCache.get(project.name,project.remotes[remote].fetch)) return gitTools.fetch(project.path,remote,authCache.get(project.name,project.remotes[remote].fetch,username))
}).catch(function(err) { }).catch(function(err) {
err.remote = remote; err.remote = remote;
throw err; throw err;
@ -637,6 +648,13 @@ function checkProjectFiles(project) {
} }
function createProject(user, metadata) { function createProject(user, metadata) {
var username;
if (!user) {
username = "_";
} else {
username = user.username;
}
var project = metadata.name; var project = metadata.name;
return when.promise(function(resolve,reject) { return when.promise(function(resolve,reject) {
var projectPath = fspath.join(projectsDir,project); var projectPath = fspath.join(projectsDir,project);
@ -658,12 +676,12 @@ function createProject(user, metadata) {
var originRemote = metadata.git.remotes.origin; var originRemote = metadata.git.remotes.origin;
var auth; var auth;
if (originRemote.hasOwnProperty("username") && originRemote.hasOwnProperty("password")) { if (originRemote.hasOwnProperty("username") && originRemote.hasOwnProperty("password")) {
authCache.set(project,originRemote.url,{ // TODO: hardcoded remote name authCache.set(project,originRemote.url,username,{ // TODO: hardcoded remote name
username: originRemote.username, username: originRemote.username,
password: originRemote.password password: originRemote.password
} }
); );
auth = authCache.get(project,originRemote.url); auth = authCache.get(project,originRemote.url,username);
} }
return gitTools.clone(originRemote,auth,projectPath).then(function(result) { return gitTools.clone(originRemote,auth,projectPath).then(function(result) {
// Check this is a valid project // Check this is a valid project

View File

@ -17,23 +17,26 @@
var authCache = {} var authCache = {}
module.exports = { module.exports = {
clear: function(project,remote) { clear: function(project,remote, user) {
if (remote && authCache.hasOwnProperty(project)) { if (user && remote && authCache[project] && authCache[project][remote]) {
delete authCache[project][remote][user];
} else if (remote && authCache.hasOwnProperty(project)) {
delete authCache[project][remote]; delete authCache[project][remote];
return; } else {
}
delete authCache[project]; delete authCache[project];
}
}, },
set: function(project,remote,auth) { set: function(project,remote,user,auth) {
console.log("AuthCache.set",remote,auth); // console.log("AuthCache.set",remote,user,auth);
authCache[project] = authCache[project]||{}; authCache[project] = authCache[project]||{};
authCache[project][remote] = auth; authCache[project][remote] = authCache[project][remote]||{};
authCache[project][remote][user] = auth;
// console.log(JSON.stringify(authCache,'',4)); // console.log(JSON.stringify(authCache,'',4));
}, },
get: function(project,remote) { get: function(project,remote,user) {
console.log("AuthCache.get",remote,authCache[project]&&authCache[project][remote]); // console.log("AuthCache.get",remote,user,authCache[project]&&authCache[project][remote]&&authCache[project][remote][user]);
if (authCache.hasOwnProperty(project)) { if (authCache[project] && authCache[project][remote]) {
return authCache[project][remote]; return authCache[project][remote][user];
} }
return return
} }

View File

@ -182,17 +182,17 @@ function getFile(user, project,filePath,sha) {
} }
function push(user, project,remoteBranchName,setRemote) { function push(user, project,remoteBranchName,setRemote) {
checkActiveProject(project); checkActiveProject(project);
return activeProject.push(remoteBranchName,setRemote); return activeProject.push(user,remoteBranchName,setRemote);
} }
function pull(user, project,remoteBranchName,setRemote) { function pull(user, project,remoteBranchName,setRemote) {
checkActiveProject(project); checkActiveProject(project);
return activeProject.pull(remoteBranchName,setRemote).then(function() { return activeProject.pull(user,remoteBranchName,setRemote).then(function() {
return reloadActiveProject("pull"); return reloadActiveProject("pull");
}); });
} }
function getStatus(user, project) { function getStatus(user, project) {
checkActiveProject(project); checkActiveProject(project);
return activeProject.status(); return activeProject.status(user);
} }
function resolveMerge(user, project,file,resolution) { function resolveMerge(user, project,file,resolution) {
checkActiveProject(project); checkActiveProject(project);
@ -206,7 +206,7 @@ function abortMerge(user, project) {
} }
function getBranches(user, project,isRemote) { function getBranches(user, project,isRemote) {
checkActiveProject(project); checkActiveProject(project);
return activeProject.getBranches(isRemote); return activeProject.getBranches(user, isRemote);
} }
function setBranch(user, project,branchName,isCreate) { function setBranch(user, project,branchName,isCreate) {
checkActiveProject(project); checkActiveProject(project);