Skip to content

Commit

Permalink
Download important files xwikisas#8
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiuchiuSorin committed Sep 5, 2023
1 parent a1096b3 commit a2a79ba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@
import org.xwiki.component.phase.InitializationException;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.WikiReference;
import org.xwiki.security.authorization.ContextualAuthorizationManager;
import org.xwiki.security.authorization.AuthorizationManager;
import org.xwiki.security.authorization.Right;

import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiContext;
import com.xwiki.admintools.LogsDownloader;
import com.xwiki.admintools.internal.data.identifiers.CurrentServer;
Expand All @@ -63,7 +62,7 @@ public class DownloadsManager implements Initializable
private CurrentServer currentServer;

@Inject
private ContextualAuthorizationManager authorizationManager;
private AuthorizationManager authorizationManager;

/**
* A list of all supported server file downloaders.
Expand Down Expand Up @@ -108,12 +107,9 @@ public byte[] downloadXWikiFile(String fileType) throws IOException
public boolean isAdmin()
{
XWikiContext wikiContext = xcontextProvider.get();
XWiki wiki = wikiContext.getWiki();
DocumentReference a = wikiContext.getUserReference();
WikiReference b = wikiContext.getWikiReference();
boolean c = this.authorizationManager.hasAccess(Right.ADMIN);
String d = "";
return c;
DocumentReference user = wikiContext.getUserReference();
WikiReference wikiReference = wikiContext.getWikiReference();
return this.authorizationManager.hasAccess(Right.ADMIN, user, wikiReference);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
Expand Down Expand Up @@ -61,21 +60,19 @@ public class DefaultResources extends ModifiablePageResource implements AdminToo
* Handles downloads requests.
*/
@Inject
private Provider<DownloadsManager> downloadsManagerProvider;
private DownloadsManager downloadsManager;

@Override
public Response getConfigs(String type) throws XWikiRestException
{
// boolean a = request.isUserInRole("admin");
// Check to see if the request was made by a user with admin rights.
if (downloadsManagerProvider.get().isAdmin()) {
if (!downloadsManager.isAdmin()) {
logger.warn("Failed to get file xwiki.[{}] due to restricted rights.", type);
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
}
try {
byte[] xWikiFileContent = downloadsManagerProvider.get().downloadXWikiFile(type);
byte[] xWikiFileContent = downloadsManager.downloadXWikiFile(type);
InputStream inputStream = new ByteArrayInputStream(xWikiFileContent);

Response.ResponseBuilder response = Response.ok(inputStream);
response.type(MediaType.TEXT_PLAIN_TYPE);

Expand All @@ -98,11 +95,15 @@ public Response getConfigs(String type) throws XWikiRestException
@Override
public Response getLogs(String from, String to) throws XWikiRestException
{
if (!downloadsManager.isAdmin()) {
logger.warn("Failed to get server logs due to restricted rights.");
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
}
try {
Map<String, String> filters = new HashMap<>();
filters.put("from", from);
filters.put("to", to);
byte[] logsArchive = downloadsManagerProvider.get().downloadLogs(filters);
byte[] logsArchive = downloadsManager.downloadLogs(filters);
if (logsArchive != null) {
// Set the appropriate response headers to indicate a zip file download.
Response.ResponseBuilder response = Response.ok(logsArchive);
Expand Down

0 comments on commit a2a79ba

Please sign in to comment.