Skip to content
Open
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
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

### Behaviour changes worth reading before upgrading

- **Only weblog Admins can replace, move or delete a custom theme's files.**
The scripts, stylesheets and images a custom theme keeps in the media
library are part of the weblog's design. Authors still manage their own
media files.

- **Only global admins manage Admin on the aggregated front-page weblog.**
With `site.frontpage.weblog.aggregated=true`, that weblog's templates can
read the whole site, so granting, removing or inviting an Admin there now
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. The ASF licenses this file to You
* under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. For additional information regarding
* copyright in this work, please see the NOTICE file in the top level
* directory of this distribution.
*/

package org.apache.roller.weblogger.business;

import org.apache.commons.lang3.StringUtils;
import org.apache.roller.weblogger.pojos.MediaFile;
import org.apache.roller.weblogger.pojos.User;
import org.apache.roller.weblogger.pojos.Weblog;
import org.apache.roller.weblogger.pojos.WeblogPermission;

/**
* A media file with an original path is what a custom theme serves at that
* path, such as the theme's scripts and stylesheets. Changing one changes the
* weblog's design, so only the weblog's Admins may replace, move or delete it.
*/
public final class ThemeMediaFiles {

private ThemeMediaFiles() {
}

/** Whether the file is served as one of the weblog's theme resources. */
public static boolean isThemeFile(MediaFile file) {
return file != null && StringUtils.isNotBlank(file.getOriginalPath());
}

/** Whether the user may replace, move or delete the file. */
public static boolean mayChange(Weblog weblog, User user, MediaFile file) {
return !isThemeFile(file)
|| (user != null && weblog.hasUserPermission(user, WeblogPermission.ADMIN));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.apache.roller.weblogger.ui.struts2.editor;

import org.apache.roller.weblogger.business.ThemeMediaFiles;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -53,6 +54,14 @@ public List<String> requiredWeblogPermissionActions() {
return Collections.singletonList(WeblogPermission.POST);
}

/**
* Whether the current user may replace, move or delete the file. Theme
* files can be changed only by the weblog's Admins.
*/
protected boolean mayChange(MediaFile mediaFile) {
return ThemeMediaFiles.mayChange(getActionWeblog(), getAuthenticatedUser(), mediaFile);
}

/**
* Deletes media file
*/
Expand All @@ -67,6 +76,10 @@ protected void doDeleteMediaFile() {
addError("mediaFile.delete.error", this.mediaFileId);
return;
}
if (!mayChange(mediaFile)) {
addError("mediaFile.themeFileAdminOnly", mediaFile.getName());
return;
}
manager.removeMediaFile(getActionWeblog(), mediaFile);
// flush changes
WebloggerFactory.getWeblogger().flush();
Expand Down Expand Up @@ -117,6 +130,13 @@ protected void doDeleteSelected() {
if (fileIds != null && fileIds.length > 0) {
log.debug("Processing delete of " + fileIds.length
+ " media files.");
for (String fileId : fileIds) {
MediaFile mediaFile = manager.getMediaFile(getActionWeblog(), fileId);
if (mediaFile != null && !mayChange(mediaFile)) {
addError("mediaFile.themeFileAdminOnly", mediaFile.getName());
return;
}
}
for (String fileId : fileIds) {
log.debug("Deleting media file - " + fileId);
MediaFile mediaFile = manager.getMediaFile(getActionWeblog(), fileId);
Expand Down Expand Up @@ -169,6 +189,10 @@ void doMoveSelected() {
addError("mediaFile.move.errors");
return;
}
if (!mayChange(mediaFile)) {
addError("mediaFile.themeFileAdminOnly", mediaFile.getName());
return;
}
filesToMove.add(mediaFile);
}
for (MediaFile mediaFile : filesToMove) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ public String save() {
MediaFileManager manager = WebloggerFactory.getWeblogger().getMediaFileManager();
try {
MediaFile mediaFile = manager.getMediaFile(getActionWeblog(), getMediaFileId());
boolean moving = getBean().getDirectoryId() != null
&& !getBean().getDirectoryId().equals(mediaFile.getDirectory().getId());
if ((uploadedFile != null || moving) && !mayChange(mediaFile)) {
addError("mediaFile.themeFileAdminOnly", mediaFile.getName());
return INPUT;
}
bean.copyTo(mediaFile);
if (getBooleanProp("mediafile.originalPathEdit.enabled")) {
mediaFile.setOriginalPath(bean.getOriginalPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,14 @@ public String deleteFolder() {
+ directoryName + ")");
MediaFileDirectory mediaFileDir = manager
.getMediaFileDirectory(getActionWeblog(), directoryId);
if (mediaFileDir != null) {
for (MediaFile mediaFile : mediaFileDir.getMediaFiles()) {
if (!mayChange(mediaFile)) {
addError("mediaFile.themeFileAdminOnly", mediaFile.getName());
return execute();
}
}
}
manager.removeMediaFileDirectory(mediaFileDir);
refreshAllDirectories();
WebloggerFactory.getWeblogger().getWeblogManager()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.roller.weblogger.webservices.atomprotocol;

import org.apache.roller.weblogger.business.ThemeMediaFiles;
import com.rometools.propono.atom.common.rome.AppModule;
import com.rometools.propono.atom.common.rome.AppModuleImpl;
import com.rometools.propono.atom.server.AtomException;
Expand Down Expand Up @@ -457,6 +458,9 @@ public void putMedia(AtomRequest areq) throws AtomException {
if (mf == null) {
throw new AtomNotFoundException("Cannot find media file: " + path);
}
if (!ThemeMediaFiles.mayChange(website, user, mf)) {
throw new AtomNotAuthorizedException("Not authorized to change theme file: " + path);
}

tempFile = File.createTempFile(UUID.randomUUID().toString(), "tmp");
try (InputStream is = areq.getInputStream()) {
Expand Down Expand Up @@ -522,10 +526,15 @@ public void deleteEntry(AtomRequest areq) throws AtomException {
String fileName = path.substring(0, path.length() - ".media-link".length());
MediaFileManager fmgr = roller.getMediaFileManager();
MediaFile mf = fmgr.getMediaFileByPath(website, path);
if (!ThemeMediaFiles.mayChange(website, user, mf)) {
throw new AtomNotAuthorizedException("Not authorized to change theme file: " + path);
}
fmgr.removeMediaFile(website, mf);
log.debug("Deleted media entry: " + fileName);
return;

} catch (AtomException ae) {
throw ae;
} catch (Exception e) {
String msg = "ERROR deleting media entry";
log.error(msg, e);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/resources/ApplicationResources.properties
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,7 @@ mediaFile.deleteFolder.confirm=Delete entire folder including all its media file
mediaFile.delete.error=Error deleting media file {0}.
mediaFile.move.success=Media file(s) successfully moved.
mediaFile.move.errors=Error moving media files.
mediaFile.themeFileAdminOnly=Only a weblog admin can replace, move or delete {0}, because the weblog\u2019s theme uses it.
mediaFile.update.success=Media file successfully updated.

# errors from validation
Expand Down
Loading
Loading