Skip to content

Commit eb5873a

Browse files
committed
Update to move gitlab api path to sub path and fix its find user method for PR #121
1 parent 5bb4423 commit eb5873a

3 files changed

Lines changed: 52 additions & 32 deletions

File tree

app.js

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ var formidable = require('formidable');
1616
var morgan = require('morgan');
1717
var passportSocketIo = require("passport.socketio");
1818
var helmet = require('helmet');
19-
var request = require('request');
2019

2120
//core
2221
var config = require("./lib/config.js");
@@ -83,9 +82,6 @@ var sessionStore = new SequelizeStore({
8382
//compression
8483
app.use(compression());
8584

86-
//cookies
87-
app.use(cookieParser());
88-
8985
// use hsts to tell https users stick to this
9086
app.use(helmet.hsts({
9187
maxAge: 31536000 * 1000, // 365 days
@@ -310,8 +306,7 @@ if (config.gitlab) {
310306
res.redirect(config.serverurl);
311307
});
312308
//gitlab callback actions
313-
// TODO: Maybe in the future
314-
//app.get('/auth/gitlab/callback/:noteId/:action', response.gitlabActions);
309+
app.get('/auth/gitlab/callback/:noteId/:action', response.gitlabActions);
315310
}
316311
//dropbox auth
317312
if (config.dropbox) {
@@ -442,29 +437,6 @@ app.post('/uploadimage', function (req, res) {
442437
}
443438
});
444439
});
445-
//get gitlab parameters
446-
app.get('/gitlab', function (req, res) {
447-
var ret = { baseURL: config.gitlab.baseURL };
448-
models.User.findById(req.cookies.userid)
449-
.then(function(user) {
450-
ret.accesstoken = user.accessToken;
451-
ret.profileid = user.profileid;
452-
request(
453-
config.gitlab.baseURL + '/api/v3/projects?access_token=' + user.accessToken,
454-
function(error, httpResponse, body) {
455-
if (!error && httpResponse.statusCode == 200) {
456-
ret.projects = JSON.parse(body);
457-
return res.send(ret);
458-
} else {
459-
return res.send(ret);
460-
}
461-
}
462-
);
463-
}).catch(function(err) {
464-
logger.error('user search failed: ' + err);
465-
return response.errorInternalError(res);
466-
});
467-
});
468440
//get new note
469441
app.get("/new", response.newNote);
470442
//get publish note

lib/response.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ var response = {
5151
showIndex: showIndex,
5252
noteActions: noteActions,
5353
publishNoteActions: publishNoteActions,
54-
githubActions: githubActions
54+
githubActions: githubActions,
55+
gitlabActions: gitlabActions
5556
};
5657

5758
function responseError(res, code, detail, msg) {
@@ -435,6 +436,53 @@ function githubActionGist(req, res, note) {
435436
}
436437
}
437438

439+
function gitlabActions(req, res, next) {
440+
var noteId = req.params.noteId;
441+
findNote(req, res, function (note) {
442+
var action = req.params.action;
443+
switch (action) {
444+
case "projects":
445+
gitlabActionProjects(req, res, note);
446+
break;
447+
default:
448+
res.redirect(config.serverurl + '/' + noteId);
449+
break;
450+
}
451+
});
452+
}
453+
454+
function gitlabActionProjects(req, res, note) {
455+
if (req.isAuthenticated()) {
456+
models.User.findOne({
457+
where: {
458+
id: req.user.id
459+
}
460+
}).then(function (user) {
461+
if (!user)
462+
return response.errorNotFound(res);
463+
var ret = { baseURL: config.gitlab.baseURL };
464+
ret.accesstoken = user.accessToken;
465+
ret.profileid = user.profileid;
466+
request(
467+
config.gitlab.baseURL + '/api/v3/projects?access_token=' + user.accessToken,
468+
function(error, httpResponse, body) {
469+
if (!error && httpResponse.statusCode == 200) {
470+
ret.projects = JSON.parse(body);
471+
return res.send(ret);
472+
} else {
473+
return res.send(ret);
474+
}
475+
}
476+
);
477+
}).catch(function (err) {
478+
logger.error('gitlab action projects failed: ' + err);
479+
return response.errorInternalError(res);
480+
});
481+
} else {
482+
return response.errorForbidden(res);
483+
}
484+
}
485+
438486
function showPublishSlide(req, res, next) {
439487
findNote(req, res, function (note) {
440488
note.increment('viewcount').then(function (note) {

public/js/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,7 @@ ui.toolbar.export.gist.attr("href", noteurl + "/gist");
11821182
//export to snippet
11831183
ui.toolbar.export.snippet.click(function() {
11841184
ui.spinner.show();
1185-
$.get(serverurl + '/gitlab')
1185+
$.get(serverurl + '/auth/gitlab/callback/' + noteid + '/projects')
11861186
.success(function (data) {
11871187
$("#snippetExportModalAccessToken").val(data.accesstoken);
11881188
$("#snippetExportModalBaseURL").val(data.baseURL);
@@ -1268,7 +1268,7 @@ ui.toolbar.import.gist.click(function () {
12681268
//import from snippet
12691269
ui.toolbar.import.snippet.click(function () {
12701270
ui.spinner.show();
1271-
$.get(serverurl + '/gitlab')
1271+
$.get(serverurl + '/auth/gitlab/callback/' + noteid + '/projects')
12721272
.success(function (data) {
12731273
$("#snippetImportModalAccessToken").val(data.accesstoken);
12741274
$("#snippetImportModalBaseURL").val(data.baseURL);

0 commit comments

Comments
 (0)