prevent editing initial project channel

Signed-off-by: MiniDigger <admin@benndorf.dev>
This commit is contained in:
MiniDigger 2022-01-08 17:04:56 +01:00 committed by MiniDigger | Martin
parent 2c31783a45
commit e52f9a2c4e
12 changed files with 34 additions and 13 deletions

View File

@ -102,6 +102,7 @@ export default class ChannelModal extends HangarFormModal {
name: '',
color: '',
nonReviewed: false,
editable: true,
versionCount: 0,
};

View File

@ -25,14 +25,14 @@
<td>
<ChannelModal :project-id="project.id" edit :channel="channel" @create="editChannel">
<template #activator="{ on, attrs }">
<v-btn small color="warning" v-bind="attrs" v-on="on">
<v-btn small color="warning" v-bind="attrs" :disabled="!channel.editable" v-on="on">
{{ $t('channel.manage.editButton') }}
</v-btn>
</template>
</ChannelModal>
</td>
<td v-if="channels.length !== 1">
<v-btn v-if="channel.versionCount === 0" small color="error" @click="deleteChannel(channel)">
<v-btn v-if="channel.versionCount === 0" small color="error" :disabled="!channel.editable" @click="deleteChannel(channel)">
{{ $t('channel.manage.deleteButton') }}
</v-btn>
</td>

View File

@ -28,6 +28,7 @@ declare module 'hangar-internal' {
interface ProjectChannel extends Named, Partial<Table> {
color: string;
nonReviewed: boolean;
editable: boolean;
temp?: boolean;
versionCount: number;
}

View File

@ -71,7 +71,7 @@ public class ChannelController extends HangarComponent {
@PermissionRequired(type = PermissionType.PROJECT, perms = NamedPermission.EDIT_TAGS, args = "{#projectId}")
@PostMapping(path = "/{projectId}/create", consumes = MediaType.APPLICATION_JSON_VALUE)
public void createChannel(@PathVariable long projectId, @Valid @RequestBody ChannelForm channelForm) {
channelService.createProjectChannel(channelForm.getName(), channelForm.getColor(), projectId, channelForm.isNonReviewed());
channelService.createProjectChannel(channelForm.getName(), channelForm.getColor(), projectId, channelForm.isNonReviewed(), true);
}
@Unlocked

View File

@ -19,7 +19,7 @@ public interface ProjectChannelsDAO {
@Timestamped
@GetGeneratedKeys
@SqlUpdate("INSERT INTO project_channels (created_at, name, color, project_id, non_reviewed) VALUES (:now, :name, :color, :projectId, :nonReviewed)")
@SqlUpdate("INSERT INTO project_channels (created_at, name, color, project_id, non_reviewed, editable) VALUES (:now, :name, :color, :projectId, :nonReviewed, :editable)")
ProjectChannelTable insert(@BindBean ProjectChannelTable projectChannelTable);
@SqlUpdate("UPDATE project_channels SET name = :name, color = :color, non_reviewed = :nonReviewed WHERE id = :id")

View File

@ -14,21 +14,24 @@ public class ProjectChannelTable extends Table implements Named {
private Color color;
private final long projectId;
private boolean nonReviewed;
private boolean editable;
@JdbiConstructor
public ProjectChannelTable(OffsetDateTime createdAt, long id, String name, @EnumByOrdinal Color color, long projectId, boolean nonReviewed) {
public ProjectChannelTable(OffsetDateTime createdAt, long id, String name, @EnumByOrdinal Color color, long projectId, boolean nonReviewed, boolean editable) {
super(createdAt, id);
this.name = name;
this.color = color;
this.projectId = projectId;
this.nonReviewed = nonReviewed;
this.editable = editable;
}
public ProjectChannelTable(String name, Color color, long projectId, boolean nonReviewed) {
public ProjectChannelTable(String name, Color color, long projectId, boolean nonReviewed, boolean editable) {
this.name = name;
this.color = color;
this.projectId = projectId;
this.nonReviewed = nonReviewed;
this.editable = editable;
}
@Override
@ -61,6 +64,14 @@ public class ProjectChannelTable extends Table implements Named {
this.nonReviewed = nonReviewed;
}
public boolean isEditable() {
return editable;
}
public void setEditable(boolean editable) {
this.editable = editable;
}
@Override
public String toString() {
return "ProjectChannelTable{" +
@ -68,6 +79,7 @@ public class ProjectChannelTable extends Table implements Named {
", color=" + color +
", projectId=" + projectId +
", nonReviewed=" + nonReviewed +
", editable=" + editable +
"} " + super.toString();
}
}

View File

@ -10,8 +10,8 @@ public class HangarChannel extends ProjectChannelTable {
private final int versionCount;
public HangarChannel(OffsetDateTime createdAt, long id, String name, @EnumByOrdinal Color color, long projectId, boolean nonReviewed, int versionCount) {
super(createdAt, id, name, color, projectId, nonReviewed);
public HangarChannel(OffsetDateTime createdAt, long id, String name, @EnumByOrdinal Color color, long projectId, boolean nonReviewed, int versionCount, boolean editable) {
super(createdAt, id, name, color, projectId, nonReviewed, editable);
this.versionCount = versionCount;
}

View File

@ -63,9 +63,9 @@ public class ChannelService extends HangarComponent {
}
@Transactional
public ProjectChannelTable createProjectChannel(String name, Color color, long projectId, boolean nonReviewed) {
public ProjectChannelTable createProjectChannel(String name, Color color, long projectId, boolean nonReviewed, boolean editable) {
validateChannel(name, color, projectId, nonReviewed, projectChannelsDAO.getProjectChannels(projectId));
ProjectChannelTable channelTable = projectChannelsDAO.insert(new ProjectChannelTable(name, color, projectId, nonReviewed));
ProjectChannelTable channelTable = projectChannelsDAO.insert(new ProjectChannelTable(name, color, projectId, nonReviewed, editable));
actionLogger.project(LogAction.PROJECT_CHANNEL_CREATED.create(ProjectContext.of(projectId), formatChannelChange(channelTable), ""));
return channelTable;
}
@ -76,6 +76,9 @@ public class ChannelService extends HangarComponent {
if (projectChannelTable == null) {
throw new HangarApiException(HttpStatus.NOT_FOUND);
}
if (!projectChannelTable.isEditable()) {
throw new HangarApiException(HttpStatus.BAD_REQUEST, "channel.modal.error.cannotEdit");
}
validateChannel(name, color, projectId, nonReviewed, projectChannelsDAO.getProjectChannels(projectId).stream().filter(ch -> ch.getId() != channelId).collect(Collectors.toList()));
String old = formatChannelChange(projectChannelTable);
projectChannelTable.setName(name);
@ -91,6 +94,9 @@ public class ChannelService extends HangarComponent {
if (hangarChannel == null) {
throw new HangarApiException(HttpStatus.NOT_FOUND);
}
if (!hangarChannel.isEditable()) {
throw new HangarApiException(HttpStatus.BAD_REQUEST, "channel.modal.error.cannotDelete");
}
if (hangarChannel.getVersionCount() != 0 || getProjectChannels(projectId).size() == 1) {
// Cannot delete channels with versions or if its the last channel
throw new HangarApiException(HttpStatus.BAD_REQUEST, "channel.modal.error.cannotDelete");

View File

@ -66,7 +66,7 @@ public class ProjectFactory extends HangarComponent {
ProjectTable projectTable = null;
try {
projectTable = projectsDAO.insert(new ProjectTable(projectOwner, newProject));
channelService.createProjectChannel(config.channels.getNameDefault(), config.channels.getColorDefault(), projectTable.getId(), false);
channelService.createProjectChannel(config.channels.getNameDefault(), config.channels.getColorDefault(), projectTable.getId(), false, false);
projectMemberService.addNewAcceptedByDefaultMember(ProjectRole.PROJECT_OWNER.create(projectTable.getId(), projectOwner.getUserId(), true));
String newPageContent = newProject.getPageContent();
if (newPageContent == null) {

View File

@ -208,7 +208,7 @@ public class VersionFactory extends HangarComponent {
try {
ProjectChannelTable projectChannelTable = channelService.getProjectChannel(projectId, pendingVersion.getChannelName(), pendingVersion.getChannelColor());
if (projectChannelTable == null) {
projectChannelTable = channelService.createProjectChannel(pendingVersion.getChannelName(), pendingVersion.getChannelColor(), projectId, pendingVersion.isChannelNonReviewed());
projectChannelTable = channelService.createProjectChannel(pendingVersion.getChannelName(), pendingVersion.getChannelColor(), projectId, pendingVersion.isChannelNonReviewed(), true);
}
Long fileSize = null;

View File

@ -47,7 +47,7 @@ spring:
# Fake User #
#############
fake-user:
enabled: false
enabled: true
name: paper
username: paper
email: paper@papermc.io

View File

@ -201,6 +201,7 @@ CREATE TABLE project_channels
REFERENCES projects
ON DELETE CASCADE,
non_reviewed boolean DEFAULT FALSE NOT NULL,
editable boolean DEFAULT FALSE NOT NULL,
CONSTRAINT channels_project_id_name_key
UNIQUE (project_id, name),
CONSTRAINT channels_project_id_color_id_key