refactor: NotebookServer.java

This commit is contained in:
1ambda 2017-01-25 09:26:03 +09:00
parent 1227d7dc56
commit 4fec44cc46
2 changed files with 121 additions and 93 deletions

View file

@ -701,6 +701,63 @@ public class NotebookServer extends WebSocketServlet
.toString())));
}
/**
* @return false if user doesn't have reader permission for this paragraph
*/
private boolean hasParagraphReaderPermission(NotebookSocket conn,
Notebook notebook, String noteId,
HashSet<String> userAndRoles,
String principal, String op)
throws IOException {
NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization();
if (!notebookAuthorization.isReader(noteId, userAndRoles)) {
permissionError(conn, op, principal, userAndRoles,
notebookAuthorization.getOwners(noteId));
return false;
}
return true;
}
/**
* @return false if user doesn't have writer permission for this paragraph
*/
private boolean hasParagraphWriterPermission(NotebookSocket conn,
Notebook notebook, String noteId,
HashSet<String> userAndRoles,
String principal, String op)
throws IOException {
NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization();
if (!notebookAuthorization.isWriter(noteId, userAndRoles)) {
permissionError(conn, op, principal, userAndRoles,
notebookAuthorization.getOwners(noteId));
return false;
}
return true;
}
/**
* @return false if user doesn't have owner permission for this paragraph
*/
private boolean hasParagraphOwnerPermission(NotebookSocket conn,
Notebook notebook, String noteId,
HashSet<String> userAndRoles,
String principal, String op)
throws IOException {
NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization();
if (!notebookAuthorization.isOwner(noteId, userAndRoles)) {
permissionError(conn, op, principal, userAndRoles,
notebookAuthorization.getOwners(noteId));
return false;
}
return true;
}
private void sendNote(NotebookSocket conn, HashSet<String> userAndRoles, Notebook notebook,
Message fromMessage) throws IOException {
@ -716,13 +773,13 @@ public class NotebookServer extends WebSocketServlet
String user = fromMessage.principal;
Note note = notebook.getNote(noteId);
NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization();
if (note != null) {
if (!notebookAuthorization.isReader(noteId, userAndRoles)) {
permissionError(conn, "read", fromMessage.principal, userAndRoles,
notebookAuthorization.getReaders(noteId));
if (!hasParagraphReaderPermission(conn, notebook, noteId,
userAndRoles, fromMessage.principal, "read")) {
return;
}
addConnectionToNote(note.getId(), conn);
if (note.isPersonalizedMode()) {
@ -746,12 +803,11 @@ public class NotebookServer extends WebSocketServlet
}
if (note != null) {
NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization();
if (!notebookAuthorization.isReader(noteId, userAndRoles)) {
permissionError(conn, "read", fromMessage.principal, userAndRoles,
notebookAuthorization.getReaders(noteId));
if (!hasParagraphReaderPermission(conn, notebook, noteId,
userAndRoles, fromMessage.principal, "read")) {
return;
}
addConnectionToNote(note.getId(), conn);
conn.send(serializeMessage(new Message(OP.NOTE).put("note", note)));
sendAllAngularObjects(note, user, conn);
@ -773,10 +829,8 @@ public class NotebookServer extends WebSocketServlet
return;
}
NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization();
if (!notebookAuthorization.isWriter(noteId, userAndRoles)) {
permissionError(conn, "update", fromMessage.principal, userAndRoles,
notebookAuthorization.getWriters(noteId));
if (!hasParagraphWriterPermission(conn, notebook, noteId,
userAndRoles, fromMessage.principal, "update")) {
return;
}
@ -807,10 +861,8 @@ public class NotebookServer extends WebSocketServlet
return;
}
NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization();
if (!notebookAuthorization.isOwner(noteId, userAndRoles)) {
permissionError(conn, "persoanlized ", fromMessage.principal, userAndRoles,
notebookAuthorization.getOwners(noteId));
if (!hasParagraphOwnerPermission(conn, notebook, noteId,
userAndRoles, fromMessage.principal, "persoanlized")) {
return;
}
@ -839,10 +891,8 @@ public class NotebookServer extends WebSocketServlet
return;
}
NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization();
if (!notebookAuthorization.isOwner(noteId, userAndRoles)) {
permissionError(conn, "rename", fromMessage.principal, userAndRoles,
notebookAuthorization.getOwners(noteId));
if (!hasParagraphOwnerPermission(conn, notebook, noteId,
userAndRoles, fromMessage.principal, "rename")) {
return;
}
@ -873,12 +923,10 @@ public class NotebookServer extends WebSocketServlet
return;
}
NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization();
for (Note note : notebook.getNotesUnderFolder(oldFolderId)) {
String noteId = note.getId();
if (!notebookAuthorization.isOwner(noteId, userAndRoles)) {
permissionError(conn, op + " folder of '" + note.getName() + "'", fromMessage.principal,
userAndRoles, notebookAuthorization.getOwners(noteId));
if (!hasParagraphOwnerPermission(conn, notebook, noteId,
userAndRoles, fromMessage.principal, op + " folder of '" + note.getName() + "'")) {
return;
}
}
@ -963,11 +1011,8 @@ public class NotebookServer extends WebSocketServlet
return;
}
Note note = notebook.getNote(noteId);
NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization();
if (!notebookAuthorization.isOwner(noteId, userAndRoles)) {
permissionError(conn, "remove", fromMessage.principal, userAndRoles,
notebookAuthorization.getOwners(noteId));
if (!hasParagraphOwnerPermission(conn, notebook, noteId,
userAndRoles, fromMessage.principal, "remove")) {
return;
}
@ -985,13 +1030,12 @@ public class NotebookServer extends WebSocketServlet
return;
}
NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization();
List<Note> notes = notebook.getNotesUnderFolder(folderId);
for (Note note : notes) {
String noteId = note.getId();
if (!notebookAuthorization.isOwner(noteId, userAndRoles)) {
permissionError(conn, "remove folder of '" + note.getName() + "'", fromMessage.principal,
userAndRoles, notebookAuthorization.getOwners(noteId));
if (!hasParagraphOwnerPermission(conn, notebook, noteId,
userAndRoles, fromMessage.principal, "remove folder of '" + note.getName() + "'")) {
return;
}
}
@ -1110,17 +1154,16 @@ public class NotebookServer extends WebSocketServlet
Map<String, Object> params = (Map<String, Object>) fromMessage.get("params");
Map<String, Object> config = (Map<String, Object>) fromMessage.get("config");
String noteId = getOpenNoteId(conn);
final Note note = notebook.getNote(noteId);
NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization();
AuthenticationInfo subject = new AuthenticationInfo(fromMessage.principal);
if (!notebookAuthorization.isWriter(noteId, userAndRoles)) {
permissionError(conn, "write", fromMessage.principal, userAndRoles,
notebookAuthorization.getWriters(noteId));
if (!hasParagraphWriterPermission(conn, notebook, noteId,
userAndRoles, fromMessage.principal, "write")) {
return;
}
final Note note = notebook.getNote(noteId);
Paragraph p = note.getParagraph(paragraphId);
AuthenticationInfo subject = new AuthenticationInfo(fromMessage.principal);
if (note.isPersonalizedMode()) {
p = p.getUserParagraphMap().get(subject.getUser());
}
@ -1157,14 +1200,13 @@ public class NotebookServer extends WebSocketServlet
if (StringUtils.isBlank(noteId)) {
return;
}
Note note = notebook.getNote(noteId);
NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization();
if (!notebookAuthorization.isWriter(noteId, userAndRoles)) {
permissionError(conn, "clear output", fromMessage.principal, userAndRoles,
notebookAuthorization.getOwners(noteId));
if (!hasParagraphWriterPermission(conn, notebook, noteId,
userAndRoles, fromMessage.principal, "clear output")) {
return;
}
Note note = notebook.getNote(noteId);
note.clearAllParagraphOutput();
broadcastNote(note);
}
@ -1196,17 +1238,16 @@ public class NotebookServer extends WebSocketServlet
return;
}
String noteId = getOpenNoteId(conn);
final Note note = notebook.getNote(noteId);
NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization();
AuthenticationInfo subject = new AuthenticationInfo(fromMessage.principal);
if (!notebookAuthorization.isWriter(noteId, userAndRoles)) {
permissionError(conn, "write", fromMessage.principal, userAndRoles,
notebookAuthorization.getWriters(noteId));
if (!hasParagraphWriterPermission(conn, notebook, noteId,
userAndRoles, fromMessage.principal, "write")) {
return;
}
/** We dont want to remove the last paragraph */
final Note note = notebook.getNote(noteId);
if (!note.isLastParagraph(paragraphId)) {
AuthenticationInfo subject = new AuthenticationInfo(fromMessage.principal);
Paragraph para = note.removeParagraph(subject.getUser(), paragraphId);
note.persist(subject);
if (para != null) {
@ -1222,14 +1263,14 @@ public class NotebookServer extends WebSocketServlet
if (paragraphId == null) {
return;
}
String noteId = getOpenNoteId(conn);
final Note note = notebook.getNote(noteId);
NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization();
if (!notebookAuthorization.isWriter(noteId, userAndRoles)) {
permissionError(conn, "write", fromMessage.principal, userAndRoles,
notebookAuthorization.getWriters(noteId));
if (!hasParagraphWriterPermission(conn, notebook, noteId,
userAndRoles, fromMessage.principal, "write")) {
return;
}
final Note note = notebook.getNote(noteId);
note.clearParagraphOutput(paragraphId);
Paragraph paragraph = note.getParagraph(paragraphId);
broadcastParagraph(note, paragraph);
@ -1473,14 +1514,13 @@ public class NotebookServer extends WebSocketServlet
final int newIndex = (int) Double.parseDouble(fromMessage.get("index").toString());
String noteId = getOpenNoteId(conn);
final Note note = notebook.getNote(noteId);
NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization();
AuthenticationInfo subject = new AuthenticationInfo(fromMessage.principal);
if (!notebookAuthorization.isWriter(noteId, userAndRoles)) {
permissionError(conn, "write", fromMessage.principal, userAndRoles,
notebookAuthorization.getWriters(noteId));
if (!hasParagraphWriterPermission(conn, notebook, noteId,
userAndRoles, fromMessage.principal, "write")) {
return;
}
AuthenticationInfo subject = new AuthenticationInfo(fromMessage.principal);
note.moveParagraph(paragraphId, newIndex);
note.persist(subject);
broadcast(note.getId(),
@ -1492,11 +1532,10 @@ public class NotebookServer extends WebSocketServlet
final int index = (int) Double.parseDouble(fromMessage.get("index").toString());
String noteId = getOpenNoteId(conn);
final Note note = notebook.getNote(noteId);
NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization();
AuthenticationInfo subject = new AuthenticationInfo(fromMessage.principal);
if (!notebookAuthorization.isWriter(noteId, userAndRoles)) {
permissionError(conn, "write", fromMessage.principal, userAndRoles,
notebookAuthorization.getWriters(noteId));
if (!hasParagraphWriterPermission(conn, notebook, noteId,
userAndRoles, fromMessage.principal, "write")) {
return null;
}
@ -1527,14 +1566,13 @@ public class NotebookServer extends WebSocketServlet
}
String noteId = getOpenNoteId(conn);
final Note note = notebook.getNote(noteId);
NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization();
if (!notebookAuthorization.isWriter(noteId, userAndRoles)) {
permissionError(conn, "write", fromMessage.principal, userAndRoles,
notebookAuthorization.getWriters(noteId));
if (!hasParagraphWriterPermission(conn, notebook, noteId,
userAndRoles, fromMessage.principal, "write")) {
return;
}
final Note note = notebook.getNote(noteId);
Paragraph p = note.getParagraph(paragraphId);
p.abort();
}
@ -1547,11 +1585,8 @@ public class NotebookServer extends WebSocketServlet
return;
}
Note note = notebook.getNote(noteId);
NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization();
if (!notebookAuthorization.isWriter(noteId, userAndRoles)) {
permissionError(conn, "run all paragraphs", fromMessage.principal, userAndRoles,
notebookAuthorization.getOwners(noteId));
if (!hasParagraphWriterPermission(conn, notebook, noteId,
userAndRoles, fromMessage.principal, "run all paragraphs")) {
return;
}
@ -1570,6 +1605,7 @@ public class NotebookServer extends WebSocketServlet
Map<String, Object> params = (Map<String, Object>) raw.get("params");
Map<String, Object> config = (Map<String, Object>) raw.get("config");
Note note = notebook.getNote(noteId);
Paragraph p = setParagraphUsingMessage(note, fromMessage,
paragraphId, text, title, params, config);
@ -1587,11 +1623,9 @@ public class NotebookServer extends WebSocketServlet
}
String noteId = getOpenNoteId(conn);
final Note note = notebook.getNote(noteId);
NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization();
if (!notebookAuthorization.isWriter(noteId, userAndRoles)) {
permissionError(conn, "write", fromMessage.principal, userAndRoles,
notebookAuthorization.getWriters(noteId));
if (!hasParagraphWriterPermission(conn, notebook, noteId,
userAndRoles, fromMessage.principal, "write")) {
return;
}
@ -1601,6 +1635,7 @@ public class NotebookServer extends WebSocketServlet
Map<String, Object> params = (Map<String, Object>) fromMessage.get("params");
Map<String, Object> config = (Map<String, Object>) fromMessage.get("config");
final Note note = notebook.getNote(noteId);
Paragraph p = setParagraphUsingMessage(note, fromMessage, paragraphId,
text, title, params, config);
p.setStatus(status);
@ -1624,11 +1659,9 @@ public class NotebookServer extends WebSocketServlet
}
String noteId = getOpenNoteId(conn);
final Note note = notebook.getNote(noteId);
NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization();
if (!notebookAuthorization.isWriter(noteId, userAndRoles)) {
permissionError(conn, "write", fromMessage.principal, userAndRoles,
notebookAuthorization.getWriters(noteId));
if (!hasParagraphWriterPermission(conn, notebook, noteId,
userAndRoles, fromMessage.principal, "write")) {
return;
}
@ -1636,6 +1669,8 @@ public class NotebookServer extends WebSocketServlet
String title = (String) fromMessage.get("title");
Map<String, Object> params = (Map<String, Object>) fromMessage.get("params");
Map<String, Object> config = (Map<String, Object>) fromMessage.get("config");
final Note note = notebook.getNote(noteId);
Paragraph p = setParagraphUsingMessage(note, fromMessage, paragraphId,
text, title, params, config);
@ -1757,10 +1792,8 @@ public class NotebookServer extends WebSocketServlet
String revisionId = (String) fromMessage.get("revisionId");
AuthenticationInfo subject = new AuthenticationInfo(fromMessage.principal);
NotebookAuthorization notebookAuthorization = notebook.getNotebookAuthorization();
if (!notebookAuthorization.isWriter(noteId, userAndRoles)) {
permissionError(conn, "update", fromMessage.principal, userAndRoles,
notebookAuthorization.getWriters(noteId));
if (!hasParagraphWriterPermission(conn, notebook, noteId,
userAndRoles, fromMessage.principal, "update")) {
return;
}

View file

@ -12,11 +12,6 @@
* limitations under the License.
*/
import {
SpellResult,
DefaultDisplayType,
} from '../../app/spell';
angular.module('zeppelinWebApp').service('websocketMsgSrv', websocketMsgSrv);
websocketMsgSrv.$inject = ['$rootScope', 'websocketEvents'];