Skip to content

Commit

Permalink
Allow deletion of orphaned change requests
Browse files Browse the repository at this point in the history
  • Loading branch information
oharsta committed Jul 26, 2024
1 parent b7997c6 commit 3e8c20b
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ curl -u sysadmin:secret -X POST -H 'Content-Type: application/json' -d '@change_
```
Or the other supported flavour: an incremental change
```
curl -u sysadmin:secret -X POST -H 'Content-Type: application/json' -d '@incremental_change_request.json' 'https://manage.test2.surfconext.nl/manage/api/internal/change-requests'
curl -u sysadmin:secret -X POST -H 'Content-Type: application/json' -d '@incremental_change_request.json' 'http://localhost:8080/manage/api/internal/change-requests'
curl -u sp-portal:secret -X POST -H 'Content-Type: application/json' -d '@incremental_change_request.json' 'https://manage.test2.surfconext.nl/manage/api/internal/change-requests'
curl -u sp-portal:secret -X POST -H 'Content-Type: application/json' -d '@incremental_change_request.json' 'http://localhost:8080/manage/api/internal/change-requests'
```

2 changes: 1 addition & 1 deletion manage-gui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.openconext</groupId>
<artifactId>manage</artifactId>
<version>7.4.4</version>
<version>7.4.5-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 4 additions & 0 deletions manage-gui/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ export function rejectChangeRequest(changeRequest) {
return postPutJson("change-requests/reject", changeRequest, "put");
}

export function removeChangeRequests(type, metaDataId) {
return fetchDelete(`change-requests/remove/${type}/${metaDataId}`);
}

export function uniqueEntityId(entityid, type) {
return postPutJson(`uniqueEntityId/${type}`, {entityid}, "post");
}
Expand Down
2 changes: 1 addition & 1 deletion manage-gui/src/components/metadata/MetaData.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PropTypes from "prop-types";
import ReactTooltip from "react-tooltip";
import scrollIntoView from "scroll-into-view";

import {validation} from "./../../api";
import {validation} from "../../api";
import {Boolean, Number, SelectMulti, SelectOne, String, Strings, StringWithFormat} from "../form";
import {SelectNewMetaDataField} from "../metadata";
import {isEmpty} from "../../utils/Utils";
Expand Down
9 changes: 8 additions & 1 deletion manage-gui/src/locale/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ I18n.translations.en = {
policy_form: "Configuration",
policies: "Policies ({{nbr}})"
},
notFound: "No Metadata found",
notFound: "No Metadata found. You might want to search for this deleted entity in the 'FIND MY METADATA' section in the 'System' tab.",
existingChangeRequests: "There are orphaned change requests for this deleted entity. You probably want to remove them.",
deleteChangeRequests: "Delete change requests",
deleteChangeRequestsFlash: "Orphaned change requests are deleted",
entityId: "Entity ID",
entityIdAlreadyExists: "Entity ID {{entityid}} is already taken.",
metaDataUrl: "Metadata URL",
Expand Down Expand Up @@ -427,8 +430,12 @@ I18n.translations.en = {
info: "All outstanding change requests of {{name}}",
noChangeRequests: "There are no pending change requests",
created: "Created",
summary: "Summary",
content: "Content",
apiClient: "API client",
incremental: "Incremental",
pathUpdateType: "Update type",
type: "Collection",
revisionNotes: "Revision notes",
revisionNotesPlaceholder: "Mandatory notes for the new revision...",
changes: "Changes",
Expand Down
55 changes: 54 additions & 1 deletion manage-gui/src/pages/Detail.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
provisioningById,
relyingPartiesByResourceServer,
remove,
removeChangeRequests,
revisions,
save,
search,
Expand Down Expand Up @@ -330,6 +331,13 @@ class Detail extends React.PureComponent {
.catch(err => {
if (err.response && err.response.status === 404) {
this.setState({notFound: true, loaded: true});
if (!isNew) {
changeRequests(type, id).then(requests => {
requests.forEach(cr => cr.createdAt = new Date(cr.created));
requests.sort((a, b) => b.createdAt - a.createdAt);
this.setState({requests: requests, changeRequestsLoaded: true});
})
}
} else {
throw err;
}
Expand Down Expand Up @@ -1337,7 +1345,7 @@ class Detail extends React.PureComponent {
</section>
)}

{renderNotFound && <section>{I18n.t("metadata.notFound")}</section>}
{renderNotFound && this.renderMetaDataNotFound(changeRequestsLoaded, requests)}
{!notFound && (
<section className="tabs">
{tabs.map(tab => this.renderTab(tab, metaData, resourceServers, whiteListing, revisions, requests, relyingParties, policies))}
Expand Down Expand Up @@ -1365,6 +1373,51 @@ class Detail extends React.PureComponent {
</div>
);
}

renderMetaDataNotFound = (changeRequestsLoaded, requests) => {
return (
<section className="not-found">
<p className="removed">{I18n.t("metadata.notFound")}</p>
{(changeRequestsLoaded && !isEmpty(requests)) &&
<div className="change-requests">
<p>{I18n.t("metadata.existingChangeRequests")}</p>
<table className="change-requests-table">
<thead>
<tr>
<th className="summary">{I18n.t("changeRequests.summary")}</th>
<th className="type">{I18n.t("changeRequests.type")}</th>
<th className="pathUpdateType">{I18n.t("changeRequests.pathUpdateType")}</th>
<th className="content">{I18n.t("changeRequests.content")}</th>
<th className="created">{I18n.t("changeRequests.created")}</th>
<th className="note">{I18n.t("changeRequests.note")}</th>
</tr>
</thead>
<tbody>
{requests.map(request => <tr>
<td>{JSON.stringify(request.metaDataSummary)}</td>
<td>{request.type}</td>
<td>{request.pathUpdateType}</td>
<td><code>{JSON.stringify(request.pathUpdates)}</code></td>
<td>{request.created}</td>
<td>{request.note}</td>
</tr>)}
</tbody>
</table>
<a className="button red" href="/#"
onClick={e => {
stop(e);
const {type, id} = this.state;
removeChangeRequests(type, id)
.then(() => {
setFlash(I18n.t("metadata.deleteChangeRequestsFlash"));
this.props.navigate(`/search`)
}
);
}}>
{I18n.t("metadata.deleteChangeRequests")}</a>
</div>}
</section>);
}
}

export default withRouterHooks(Detail);
Expand Down
57 changes: 57 additions & 0 deletions manage-gui/src/pages/Detail.scss
Original file line number Diff line number Diff line change
Expand Up @@ -224,5 +224,62 @@
}
}

.not-found {


p.removed {
margin: 20px 0;

}

div.change-requests {

a {
display: block;
margin-top: 15px;
max-width: 280px;
}

table.change-requests-table {
width: 100%;
table-layout: fixed;
border: 1px solid $light-grey;
border-radius: $br;
margin: 15px;

thead th {
text-align: left;
padding: 10px 0;
background-color: $lightest-grey;

&.summary {
width: 20%;
}

&.content {
width: 25%;
}

&.created {
width: 15%;
}

&.type {
width: 10%;
}

&.pathUpdateType {
width: 10%;
}

&.note {
width: 20%;
}
}
}
}

}

}

2 changes: 1 addition & 1 deletion manage-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.openconext</groupId>
<artifactId>manage</artifactId>
<version>7.4.4</version>
<version>7.4.5-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package manage.control;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.mongodb.client.result.DeleteResult;
import manage.api.APIUser;
import manage.conf.MetaDataAutoConfiguration;
import manage.exception.EndpointNotAllowed;
import manage.model.*;
import manage.repository.MetaDataRepository;
import manage.service.ExporterService;
Expand All @@ -17,9 +17,7 @@
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
Expand All @@ -34,7 +32,6 @@
import java.util.Optional;

import static manage.api.Scope.TEST;
import static manage.api.Scope.WRITE_IDP;
import static manage.mongo.MongoChangelog.CHANGE_REQUEST_POSTFIX;
import static manage.mongo.MongoChangelog.REVISION_POSTFIX;

Expand Down Expand Up @@ -326,6 +323,14 @@ public MetaData rejectChangeRequest(@RequestBody @Validated ChangeRequest change
return metaDataService.doRejectChangeRequest(changeRequest, user);
}

@PreAuthorize("hasRole('ADMIN')")
@DeleteMapping("/client/change-requests/remove/{type}/{metaDataId}")
@Transactional
public DeleteResult removeChangeRequest(@PathVariable("type") String type,
@PathVariable("metaDataId") String metaDataId, FederatedUser user) {
return metaDataService.doDeleteChangeRequest(type, metaDataId, user);
}

@PreAuthorize("hasAnyRole('CHANGE_REQUEST_IDP', 'CHANGE_REQUEST_SP')")
@PutMapping("/internal/change-requests/reject")
@Transactional
Expand Down
12 changes: 11 additions & 1 deletion manage-server/src/main/java/manage/service/MetaDataService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package manage.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.mongodb.client.result.DeleteResult;
import lombok.SneakyThrows;
import manage.api.APIUser;
import manage.api.AbstractUser;
Expand Down Expand Up @@ -204,7 +205,7 @@ public MetaData doPost(@Validated MetaData metaData, AbstractUser user, boolean
public boolean doRemove(String type, String id, AbstractUser user, String revisionNote) {
MetaData current = metaDataRepository.findById(id, type);
checkNull(type, id, current);
//For security enforcement see th SecurityHook#preDelete
//For security enforcement see the SecurityHook#preDelete
current = metaDataHook.preDelete(current, user);
metaDataRepository.remove(current);

Expand Down Expand Up @@ -340,6 +341,15 @@ public MetaData doRejectChangeRequest(ChangeRequest changeRequest, AbstractUser
return mongoTemplate.findById(changeRequest.getMetaDataId(), MetaData.class, changeRequest.getType());
}

public DeleteResult doDeleteChangeRequest(String type, String metaDataId, AbstractUser user) {
LOG.info("Deleting change requests {} by {}", metaDataId, user.getName());

MongoTemplate mongoTemplate = metaDataRepository.getMongoTemplate();
String collectionName = type.concat(CHANGE_REQUEST_POSTFIX);
Query query = new Query(Criteria.where("metaDataId").is(metaDataId));
return mongoTemplate.remove(query, MetaDataChangeRequest.class, collectionName);
}

public MetaData restoreDeleted(RevisionRestore revisionRestore, FederatedUser federatedUser)
throws JsonProcessingException {
MetaData revision = metaDataRepository.findById(revisionRestore.getId(), revisionRestore.getType());
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.openconext</groupId>
<artifactId>manage</artifactId>
<version>7.4.4</version>
<version>7.4.5-SNAPSHOT</version>
<packaging>pom</packaging>

<name>manage</name>
Expand Down

0 comments on commit 3e8c20b

Please sign in to comment.