Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/anonymous access #109

Merged
merged 4 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

##
## Added
- opening documents for viewing by an anonymous user
- edit button in ONLYOFFICE preview macro
- link to docs cloud

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/onlyoffice/OnlyOfficeEditorServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@ public OnlyOfficeEditorServlet(final I18nResolver i18n, final UrlManager urlMana
@Override
public void doGet(final HttpServletRequest request, final HttpServletResponse response)
throws ServletException, IOException {
if (!authContext.checkUserAuthorization(request, response)) {
return;
}

ConfluenceUser user = AuthenticatedUserThreadLocal.get();

String attachmentIdString = request.getParameter("attachmentId");
String actionDataString = request.getParameter("actionData");
String referer = request.getHeader("referer");

if (attachmentIdString == null || attachmentIdString.isEmpty()) {
if (!authContext.checkUserAuthorization(request, response)) {
return;
}

String fileName = request.getParameter("fileName");
String fileExt = request.getParameter("fileExt");
String pageId = request.getParameter("pageId");
Expand Down Expand Up @@ -118,7 +118,7 @@ public void doGet(final HttpServletRequest request, final HttpServletResponse re
}

if (!attachmentUtil.checkAccess(attachmentId, user, false)) {
response.sendRedirect(attachment.getContainer().getUrlPath());
response.sendRedirect(authContext.getLoginUrl(request));
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/onlyoffice/OnlyOfficeFileProviderServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ public void doGet(final HttpServletRequest request, final HttpServletResponse re
throw new SecurityException("Invalid link token!");
}

String userKeyString = bodyFromToken.getString("userKey");
String userKeyString = bodyFromToken.has("userKey") ? bodyFromToken.getString("userKey") : null;
String attachmentIdString = bodyFromToken.getString("attachmentId");

UserAccessor userAccessor = (UserAccessor) ContainerManager.getComponent("userAccessor");

UserKey userKey = new UserKey(userKeyString);
UserKey userKey = userKeyString == null || userKeyString.equals("") ? null : new UserKey(userKeyString);
ConfluenceUser user = userAccessor.getUserByKey(userKey);
Long attachmentId = Long.parseLong(attachmentIdString);

Expand Down
1 change: 1 addition & 0 deletions src/main/java/onlyoffice/managers/auth/AuthContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@

public interface AuthContext {
boolean checkUserAuthorization(HttpServletRequest request, HttpServletResponse response) throws IOException;
String getLoginUrl(HttpServletRequest request) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public boolean checkUserAuthorization(final HttpServletRequest request, final Ht
return true;
}

private String getLoginUrl(final HttpServletRequest request) throws IOException {
public String getLoginUrl(final HttpServletRequest request) throws IOException {
StringBuilder stringBuilder = new StringBuilder(request.getContextPath());
String fullUrl = stringBuilder.append("/login.action?permissionViolation=true&os_destination=")
.append("plugins%2Fservlet%2Fonlyoffice%2Fdoceditor").append("?")
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/onlyoffice/managers/url/UrlManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ public String getFileUri(final Long attachmentId) {
ConfluenceUser user = AuthenticatedUserThreadLocal.get();

Map<String, String> params = new HashMap<>();
params.put("userKey", user.getKey().getStringValue());

if (user != null) {
params.put("userKey", user.getKey().getStringValue());
}
params.put("attachmentId", attachmentId.toString());
params.put("action", "download");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,12 @@ public Attachment getAttachmentByName(final String fileName, final Long pageId)
}

public boolean checkAccess(final Long attachmentId, final User user, final boolean forEdit) {
if (user == null) {
return false;
}

Attachment attachment = attachmentManager.getAttachment(attachmentId);

return checkAccess(attachment, user, forEdit);
}

public boolean checkAccess(final Attachment attachment, final User user, final boolean forEdit) {
if (user == null) {
return false;
}

PermissionManager permissionManager = (PermissionManager) ContainerManager.getComponent("permissionManager");

if (forEdit) {
Expand Down