Skip to content

Commit a8068d3

Browse files
authored
Merge pull request #313 from elct9620/feature/disable_anonymous_view
WIP: Add options to limit anonymous view note
2 parents 258a59a + d6be0cf commit a8068d3

5 files changed

Lines changed: 47 additions & 20 deletions

File tree

lib/models/note.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var logger = require("../logger.js");
2323
var ot = require("../ot/index.js");
2424

2525
// permission types
26-
var permissionTypes = ["freely", "editable", "locked", "private"];
26+
var permissionTypes = ["freely", "editable", "limited", "private", "protected", "locked"];
2727

2828
module.exports = function (sequelize, DataTypes) {
2929
var Note = sequelize.define("Note", {
@@ -333,7 +333,7 @@ module.exports = function (sequelize, DataTypes) {
333333
if (meta.slideOptions && (typeof meta.slideOptions == "object"))
334334
_meta.slideOptions = meta.slideOptions;
335335
}
336-
return _meta;
336+
return _meta;
337337
},
338338
updateAuthorshipByOperation: function (operation, userId, authorships) {
339339
var index = 0;
@@ -532,4 +532,4 @@ module.exports = function (sequelize, DataTypes) {
532532
});
533533

534534
return Note;
535-
};
535+
};

lib/realtime.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,13 @@ function getStatus(callback) {
251251
return logger.error('count user failed: ' + err);
252252
});
253253
}).catch(function (err) {
254-
return logger.error('count note failed: ' + err);
254+
return logger.error('count note failed: ' + err);
255255
});
256256
}
257257

258258
function isReady() {
259-
return realtime.io
260-
&& Object.keys(notes).length == 0 && Object.keys(users).length == 0
259+
return realtime.io
260+
&& Object.keys(notes).length == 0 && Object.keys(users).length == 0
261261
&& connectionSocketQueue.length == 0 && !isConnectionBusy
262262
&& disconnectSocketQueue.length == 0 && !isDisconnectBusy;
263263
}
@@ -420,7 +420,7 @@ function finishConnection(socket, note, user) {
420420
function startConnection(socket) {
421421
if (isConnectionBusy) return;
422422
isConnectionBusy = true;
423-
423+
424424
var noteId = socket.noteId;
425425
if (!noteId) {
426426
return failConnection(404, 'note id not found', socket);
@@ -521,7 +521,7 @@ function disconnect(socket) {
521521
logger.info("SERVER disconnected a client");
522522
logger.info(JSON.stringify(users[socket.id]));
523523
}
524-
524+
525525
if (users[socket.id]) {
526526
delete users[socket.id];
527527
}
@@ -618,12 +618,12 @@ function ifMayEdit(socket, callback) {
618618
case "freely":
619619
//not blocking anyone
620620
break;
621-
case "editable":
621+
case "editable": case "limited":
622622
//only login user can change
623623
if (!socket.request.user || !socket.request.user.logged_in)
624624
mayEdit = false;
625625
break;
626-
case "locked": case "private":
626+
case "locked": case "private": case "protected":
627627
//only owner can change
628628
if (!note.owner || note.owner != socket.request.user.id)
629629
mayEdit = false;
@@ -672,7 +672,7 @@ function operationCallback(socket, operation) {
672672
var noteId = note.alias ? note.alias : LZString.compressToBase64(note.id);
673673
if (note.server) history.updateHistory(userId, noteId, note.server.document);
674674
}, 0);
675-
675+
676676
}
677677
// save authorship
678678
note.authorship = models.Note.updateAuthorshipByOperation(operation, userId, note.authorship);
@@ -689,10 +689,10 @@ function connection(socket) {
689689
}
690690

691691
if (isDuplicatedInSocketQueue(socket, connectionSocketQueue)) return;
692-
692+
693693
// store noteId in this socket session
694694
socket.noteId = noteId;
695-
695+
696696
//initialize user data
697697
//random color
698698
var color = randomcolor();

lib/response.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ function checkViewPermission(req, note) {
124124
return false;
125125
else
126126
return true;
127+
} else if (note.permission == 'limited' || note.permission == 'protected') {
128+
if( !req.isAuthenticated() ) {
129+
return false;
130+
}
131+
return true;
127132
} else {
128133
return true;
129134
}
@@ -163,7 +168,7 @@ function showNote(req, res, next) {
163168
findNote(req, res, function (note) {
164169
// force to use note id
165170
var noteId = req.params.noteId;
166-
var id = LZString.compressToBase64(note.id);
171+
var id = LZString.compressToBase64(note.id);
167172
if ((note.alias && noteId != note.alias) || (!note.alias && noteId != id))
168173
return res.redirect(config.serverurl + "/" + (note.alias || id));
169174
return responseHackMD(res, note);
@@ -415,7 +420,7 @@ function publishSlideActions(req, res, next) {
415420
res.redirect(config.serverurl + '/' + (note.alias ? note.alias : LZString.compressToBase64(note.id)));
416421
break;
417422
default:
418-
res.redirect(config.serverurl + '/p/' + note.shortid);
423+
res.redirect(config.serverurl + '/p/' + note.shortid);
419424
break;
420425
}
421426
});

public/js/index.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,9 @@ window.ui = {
857857
freely: $(".ui-permission-freely"),
858858
editable: $(".ui-permission-editable"),
859859
locked: $(".ui-permission-locked"),
860-
private: $(".ui-permission-private")
860+
private: $(".ui-permission-private"),
861+
limited: $(".ui-permission-limited"),
862+
protected: $(".ui-permission-protected")
861863
},
862864
delete: $(".ui-delete-note")
863865
},
@@ -2247,6 +2249,14 @@ ui.infobar.permission.locked.click(function () {
22472249
ui.infobar.permission.private.click(function () {
22482250
emitPermission("private");
22492251
});
2252+
//limited
2253+
ui.infobar.permission.limited.click(function() {
2254+
emitPermission("limited");
2255+
});
2256+
//protected
2257+
ui.infobar.permission.protected.click(function() {
2258+
emitPermission("protected");
2259+
});
22502260
// delete note
22512261
ui.infobar.delete.click(function () {
22522262
$('.delete-modal').modal('show');
@@ -2277,14 +2287,22 @@ function updatePermission(newPermission) {
22772287
label = '<i class="fa fa-shield"></i> Editable';
22782288
title = "Signed people can edit";
22792289
break;
2280-
case "locked":
2281-
label = '<i class="fa fa-lock"></i> Locked';
2282-
title = "Only owner can edit";
2290+
case "limited":
2291+
label = '<i class="fa fa-id-card"></i> Limited';
2292+
title = "Signed people can edit & guest can't view"
22832293
break;
22842294
case "private":
22852295
label = '<i class="fa fa-hand-stop-o"></i> Private';
22862296
title = "Only owner can view & edit";
22872297
break;
2298+
case "protected":
2299+
label = '<i class="fa fa-umbrella"></i> Protected';
2300+
title = "Only owner can edit & guest can't view";
2301+
break;
2302+
case "locked":
2303+
label = '<i class="fa fa-lock"></i> Locked';
2304+
title = "Only owner can edit";
2305+
break;
22882306
}
22892307
if (personalInfo.userid && owner && personalInfo.userid == owner) {
22902308
label += ' <i class="fa fa-caret-down"></i>';
@@ -2302,6 +2320,7 @@ function havePermission() {
23022320
bool = true;
23032321
break;
23042322
case "editable":
2323+
case "limited":
23052324
if (!personalInfo.login) {
23062325
bool = false;
23072326
} else {
@@ -2310,6 +2329,7 @@ function havePermission() {
23102329
break;
23112330
case "locked":
23122331
case "private":
2332+
case "protected":
23132333
if (!owner || personalInfo.userid != owner) {
23142334
bool = false;
23152335
} else {

public/views/body.ejs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
<ul class="dropdown-menu" aria-labelledby="permissionLabel">
1818
<li class="ui-permission-freely"<% if(!allowAnonymous) { %> style="display: none;"<% } %>><a><i class="fa fa-leaf fa-fw"></i> Freely - Anyone can edit</a></li>
1919
<li class="ui-permission-editable"><a><i class="fa fa-shield fa-fw"></i> Editable - Signed people can edit</a></li>
20-
<li class="ui-permission-locked"><a><i class="fa fa-lock fa-fw"></i> Locked - Only owner can edit</a></li>
20+
<li class="ui-permission-limited"><a><i class="fa fa-id-card fa-fw"></i> Limited - Signed people can edit &amp; view</a></li>
2121
<li class="ui-permission-private"><a><i class="fa fa-hand-stop-o fa-fw"></i> Private - Only owner can view &amp; edit</a></li>
22+
<li class="ui-permission-protected"><a><i class="fa fa-umbrella fa-fw"></i> Protected - Only owner can edit</a></li>
23+
<li class="ui-permission-locked"><a><i class="fa fa-lock fa-fw"></i> Locked - Only owner can edit</a></li>
2224
<li class="divider"></li>
2325
<li class="ui-delete-note"><a><i class="fa fa-trash-o fa-fw"></i> Delete this note</a></li>
2426
</ul>

0 commit comments

Comments
 (0)