// start the transaction
beginTransaction();
ResourcePath processedPath = new ResourcePath(commentPath);
Comment comment = new Comment();
comment.setCommentPath(commentPath);
context.setResourcePath(processedPath);
context.setComment(comment);
context.setOldComments(getComments(commentPath));
registryContext.getHandlerManager().removeComment(context);
if (!context.isProcessingComplete()) {
if (!processedPath.isCurrentVersion()) {
String msg = "Failed to remove tag from the resource " + processedPath +
". Given path refers to an archived version of the resource.";
log.error(msg);
throw new RegistryException(msg);
}
String user = CurrentSession.getUser();
UserRealm userRealm = CurrentSession.getUserRealm();
boolean adminUser = false;
// getting the realm config to get admin role, user details
RealmConfiguration realmConfig;
try {
realmConfig = userRealm.getRealmConfiguration();
} catch (UserStoreException e) {
String msg = "Failed to retrieve realm configuration.";
log.error(msg, e);
throw new RegistryException(msg, e);
}
// check is the user belongs to the admin role
try {
String[] roles = userRealm.getUserStoreManager().getRoleListOfUser(user);
String adminRoleName = realmConfig.getAdminRoleName();
if (RegistryUtils.containsString(adminRoleName, roles)) {
adminUser = true;
}
} catch (UserStoreException e) {
String msg = "Failed to get roles of the current user. " +
"User will be considered as non-admin user.\n" + e.getMessage();
log.error(msg, e);
adminUser = false;
}
// check if the user is the admin user
// TODO - do we really need to do this check? Won't this user always be in the
// admin role?
String adminUsername = realmConfig.getAdminUserName();
if (adminUsername.equals(user)) {
adminUser = true;
}
String[] parts = commentPath.split(RegistryConstants.URL_SEPARATOR);
String commentPart = parts[1];
String commentId = null;
if (parts.length == 2 && commentPart.startsWith("comments:")) {
commentId = parts[1].substring(9);
}
Comment temp = commentsDAO.getComment(Long.parseLong(commentId),
processedPath.getPath());
if (adminUser) {
commentsDAO.deleteComment(Long.parseLong(commentId));
} else {
ResourceImpl resource =
(ResourceImpl) repository.getMetaData(processedPath.getPath());
String author = resource.getAuthorUserName();
if (user.equals(author)) {
commentsDAO.deleteComment(Long.parseLong(commentId));
} else {
if (temp != null && user.equals(temp.getUser())) {
commentsDAO.deleteComment(Long.parseLong(commentId));
} else {
String msg = "User: " + user +
" is not authorized to delete the comment on the resource: " +
processedPath.getPath();
log.warn(msg);
throw new AuthorizationFailedException(msg);
}
}
}
if (context.isLoggingActivity()) {
if (temp != null) {
registryContext.getLogWriter().addLog(processedPath.getPath(),
user, LogEntry.DELETE_COMMENT, temp.getText());
} else {
registryContext.getLogWriter().addLog(processedPath.getPath(),
user, LogEntry.DELETE_COMMENT, commentPath);
}
}