1
0
mirror of https://github.com/sismics/docs.git synced 2025-12-13 01:36:18 +00:00

Closes #159: cancel routes + email at route step validation

This commit is contained in:
Benjamin Gamard
2018-02-02 17:18:34 +01:00
parent 5b8cd18128
commit 706d244ff8
28 changed files with 487 additions and 102 deletions

View File

@@ -110,11 +110,12 @@ public class RouteResource extends BaseResource {
}
// Intialize ACLs on the first step
RouteStepDto routeStep = routeStepDao.getCurrentStep(documentId);
RoutingUtil.updateAcl(documentId, routeStep, null, principal.getId());
RouteStepDto routeStepDto = routeStepDao.getCurrentStep(documentId);
RoutingUtil.updateAcl(documentId, routeStepDto, null, principal.getId());
RoutingUtil.sendRouteStepEmail(documentId, routeStepDto);
JsonObjectBuilder response = Json.createObjectBuilder()
.add("route_step", routeStep.toJson());
.add("route_step", routeStepDto.toJson());
return Response.ok().entity(response.build()).build();
}
@@ -152,13 +153,13 @@ public class RouteResource extends BaseResource {
// Get the current step
RouteStepDao routeStepDao = new RouteStepDao();
RouteStepDto routeStep = routeStepDao.getCurrentStep(documentId);
if (routeStep == null) {
RouteStepDto routeStepDto = routeStepDao.getCurrentStep(documentId);
if (routeStepDto == null) {
throw new NotFoundException();
}
// Check permission to validate this step
if (!getTargetIdList(null).contains(routeStep.getTargetId())) {
if (!getTargetIdList(null).contains(routeStepDto.getTargetId())) {
throw new ForbiddenClientException();
}
@@ -166,16 +167,16 @@ public class RouteResource extends BaseResource {
ValidationUtil.validateRequired(transitionStr, "transition");
comment = ValidationUtil.validateLength(comment, "comment", 1, 500, true);
RouteStepTransition transition = RouteStepTransition.valueOf(transitionStr);
if (routeStep.getType() == RouteStepType.VALIDATE && transition != RouteStepTransition.VALIDATED
|| routeStep.getType() == RouteStepType.APPROVE && transition != RouteStepTransition.APPROVED && transition != RouteStepTransition.REJECTED) {
if (routeStepDto.getType() == RouteStepType.VALIDATE && transition != RouteStepTransition.VALIDATED
|| routeStepDto.getType() == RouteStepType.APPROVE && transition != RouteStepTransition.APPROVED && transition != RouteStepTransition.REJECTED) {
throw new ClientException("ValidationError", "Invalid transition for this route step type");
}
// Validate the step and update ACLs
routeStepDao.endRouteStep(routeStep.getId(), transition, comment, principal.getId());
routeStepDao.endRouteStep(routeStepDto.getId(), transition, comment, principal.getId());
RouteStepDto newRouteStep = routeStepDao.getCurrentStep(documentId);
RoutingUtil.updateAcl(documentId, newRouteStep, routeStep, principal.getId());
// TODO Send an email to the new route step
RoutingUtil.updateAcl(documentId, newRouteStep, routeStepDto, principal.getId());
RoutingUtil.sendRouteStepEmail(documentId, routeStepDto);
JsonObjectBuilder response = Json.createObjectBuilder()
.add("readable", aclDao.checkPermission(documentId, PermType.READ, getTargetIdList(null)));
@@ -253,5 +254,50 @@ public class RouteResource extends BaseResource {
return Response.ok().entity(json.build()).build();
}
// TODO Workflow cancellation
/**
* Cancel a route.
*
* @api {delete} /route Cancel a route
* @apiName DeleteRoute
* @apiRouteModel Route
* @apiParam {String} documentId Document ID
* @apiSuccess {String} status Status OK
* @apiError (client) ForbiddenError Access denied
* @apiError (client) NotFound Document or route not found
* @apiPermission user
* @apiVersion 1.5.0
*
* @return Response
*/
@DELETE
public Response delete(@QueryParam("documentId") String documentId) {
if (!authenticate()) {
throw new ForbiddenClientException();
}
// Get the document
AclDao aclDao = new AclDao();
if (!aclDao.checkPermission(documentId, PermType.WRITE, getTargetIdList(null))) {
throw new NotFoundException();
}
// Get the current step
RouteStepDao routeStepDao = new RouteStepDao();
RouteStepDto routeStepDto = routeStepDao.getCurrentStep(documentId);
if (routeStepDto == null) {
throw new NotFoundException();
}
// Remove the temporary ACLs
RoutingUtil.updateAcl(documentId, null, routeStepDto, principal.getId());
// Delete the route and the steps
RouteDao routeDao = new RouteDao();
routeDao.deleteRoute(routeStepDto.getRouteId());
// Always return OK
JsonObjectBuilder response = Json.createObjectBuilder()
.add("status", "ok");
return Response.ok().entity(response.build()).build();
}
}

View File

@@ -953,10 +953,11 @@ public class UserResource extends BaseResource {
// Check for user existence
UserDao userDao = new UserDao();
User user = userDao.getActiveByUsername(username);
if (user == null) {
List<UserDto> userDtoList = userDao.findByCriteria(new UserCriteria().setUserName(username), null);
if (userDtoList.isEmpty()) {
throw new ClientException("UserNotFound", "User not found: " + username);
}
UserDto user = userDtoList.get(0);
// Create the password recovery key
PasswordRecoveryDao passwordRecoveryDao = new PasswordRecoveryDao();

View File

@@ -149,6 +149,11 @@ angular.module('docs').controller('DocumentView', function ($scope, $state, $sta
comment: $scope.workflowComment
}).then(function (data) {
$scope.workflowComment = '';
var title = $translate.instant('document.view.workflow_validated_title');
var msg = $translate.instant('document.view.workflow_validated_message');
var btns = [{result: 'ok', label: $translate.instant('ok'), cssClass: 'btn-primary'}];
$dialog.messageBox(title, msg, btns);
if (data.readable) {
$scope.document.route_step = data.route_step;
} else {

View File

@@ -3,7 +3,10 @@
/**
* Document view workflow controller.
*/
angular.module('docs').controller('DocumentViewWorkflow', function ($scope, $stateParams, Restangular) {
angular.module('docs').controller('DocumentViewWorkflow', function ($scope, $stateParams, Restangular, $translate, $dialog) {
/**
* Load routes.
*/
$scope.loadRoutes = function () {
Restangular.one('route').get({
documentId: $stateParams.id
@@ -12,12 +15,9 @@ angular.module('docs').controller('DocumentViewWorkflow', function ($scope, $sta
});
};
// Load route models
Restangular.one('routemodel').get().then(function(data) {
$scope.routemodels = data.routemodels;
});
// Start the selected workflow
/**
* Start the selected workflow
*/
$scope.startWorkflow = function () {
Restangular.one('route').post('start', {
routeModelId: $scope.routemodel,
@@ -28,6 +28,34 @@ angular.module('docs').controller('DocumentViewWorkflow', function ($scope, $sta
});
};
/**
* Cancel the current workflow.
*/
$scope.cancelWorkflow = function () {
var title = $translate.instant('document.view.workflow.cancel_workflow_title');
var msg = $translate.instant('document.view.workflow.cancel_workflow_message');
var btns = [
{result: 'cancel', label: $translate.instant('cancel')},
{result: 'ok', label: $translate.instant('ok'), cssClass: 'btn-primary'}
];
$dialog.messageBox(title, msg, btns, function (result) {
if (result === 'ok') {
Restangular.one('route').remove({
documentId: $stateParams.id
}).then(function () {
delete $scope.document.route_step;
$scope.loadRoutes();
});
}
});
};
// Load route models
Restangular.one('routemodel').get().then(function(data) {
$scope.routemodels = data.routemodels;
});
// Load routes
$scope.loadRoutes();
});

View File

@@ -117,4 +117,11 @@ angular.module('docs').controller('SettingsWorkflowEdit', function($scope, $dial
}
});
};
/**
* Remove a route step.
*/
$scope.removeStep = function (step) {
$scope.workflow.steps.splice($scope.workflow.steps.indexOf(step), 1);
};
});

View File

@@ -94,6 +94,8 @@
"error_loading_comments": "Error loading comments",
"workflow_current": "Current workflow step",
"workflow_comment": "Add a workflow comment",
"workflow_validated_title": "Workflow step validated",
"workflow_validated_message": "The workflow step has been successfully validated.",
"content": {
"content": "Content",
"delete_file_title": "Delete file",
@@ -111,7 +113,10 @@
"workflow_start_label": "Which workflow to start?",
"add_more_workflow": "Add more workflows",
"start_workflow_submit": "Start workflow",
"full_name": "<strong>{{ name }}</strong> started on {{ create_date | date }}"
"full_name": "<strong>{{ name }}</strong> started on {{ create_date | date }}",
"cancel_workflow": "Cancel the current workflow",
"cancel_workflow_title": "Cancel the workflow",
"cancel_workflow_message": "Do you really want to cancel the current workflow?"
},
"permissions": {
"permissions": "Permissions",

View File

@@ -1,72 +1,76 @@
<p class="well-sm pull-right" ng-show="document.route_step && document.writable">
<span class="btn btn-danger" ng-click="cancelWorkflow()">
<span class="glyphicon glyphicon-trash"></span>
{{ 'document.view.workflow.cancel_workflow' | translate }}
</span>
</p>
<p class="well-sm">{{ 'document.view.workflow.message' | translate }}</p>
<form name="startWorkflowForm" class="form-horizontal" novalidate ng-show="!document.route_step">
<div class="form-group" ng-class="{ 'has-error': !startWorkflowForm.routemodel.$valid && startWorkflowForm.$dirty }">
<label for="inputRouteModel" class="col-sm-3">
{{ 'document.view.workflow.workflow_start_label' | translate }}
<p ng-if="userInfo.base_functions.indexOf('ADMIN') != -1">
<a href="#/settings/workflow">{{ 'document.view.workflow.add_more_workflow' | translate }}</a>
</p>
</label>
<div class="col-sm-6">
<select required class="form-control" id="inputRouteModel" name="routemodel" ng-model="routemodel">
<option ng-repeat="routemodel in routemodels" value="{{ routemodel.id }}">{{ routemodel.name }}</option>
</select>
</div>
<div class="col-sm-3">
<span class="help-block" ng-show="startWorkflowForm.routemodel.$error.required && startWorkflowForm.$dirty">{{ 'validation.required' | translate }}</span>
</div>
<div class="form-group" ng-class="{ 'has-error': !startWorkflowForm.routemodel.$valid && startWorkflowForm.$dirty }">
<label for="inputRouteModel" class="col-sm-3">
{{ 'document.view.workflow.workflow_start_label' | translate }}
<p ng-if="userInfo.base_functions.indexOf('ADMIN') != -1">
<a href="#/settings/workflow">{{ 'document.view.workflow.add_more_workflow' | translate }}</a>
</p>
</label>
<div class="col-sm-6">
<select required class="form-control" id="inputRouteModel" name="routemodel" ng-model="routemodel">
<option ng-repeat="routemodel in routemodels" value="{{ routemodel.id }}">{{ routemodel.name }}</option>
</select>
</div>
<div class="col-sm-3">
<span class="help-block" ng-show="startWorkflowForm.routemodel.$error.required && startWorkflowForm.$dirty">{{ 'validation.required' | translate }}</span>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<button type="submit" class="btn btn-primary"
ng-disabled="!startWorkflowForm.$valid"
ng-click="startWorkflow()">
{{ 'document.view.workflow.start_workflow_submit' | translate }}
</button>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<button type="submit" class="btn btn-primary"
ng-disabled="!startWorkflowForm.$valid"
ng-click="startWorkflow()">
{{ 'document.view.workflow.start_workflow_submit' | translate }}
</button>
</div>
</div>
</form>
<table class="table" ng-repeat="route in routes">
<caption translate="document.view.workflow.full_name"
translate-values="{ name: route.name, create_date: route.create_date }"></caption>
<thead>
<tr>
<th>Type</th>
<th>Name</th>
<th>For</th>
<th>Validation</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="step in route.steps" ng-class="{
'bg-success': step.transition == 'VALIDATED' || step.transition == 'APPROVED',
'bg-danger': step.transition == 'REJECTED'
}">
<td>
<span class="glyphicon glyphicon-transfer" ng-if="step.type == 'APPROVE'"></span>
<span class="glyphicon glyphicon-ok-circle" ng-if="step.type == 'VALIDATE'"></span>
{{ 'workflow_type.' + step.type | translate }}
</td>
<td>{{ step.name }}</td>
<td>
<acl data="step.target"></acl>
</td>
<td>
<span ng-show="step.end_date">
{{ 'workflow_transition.' + step.transition | translate }}
{{ step.end_date | timeAgo: dateTimeFormat }}
by
<a href="#/user/{{ step.validator_username }}" class="label label-default">{{ step.validator_username }}</a>
<span ng-show="step.comment" class="text-">
<br/>
<em>{{ step.comment }}</em>
</span>
</span>
</td>
</tr>
</tbody>
<caption translate="document.view.workflow.full_name"
translate-values="{ name: route.name, create_date: route.create_date }"></caption>
<thead>
<tr>
<th>Type</th>
<th>Name</th>
<th>For</th>
<th>Validation</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="step in route.steps" ng-class="{
'bg-success': step.transition == 'VALIDATED' || step.transition == 'APPROVED',
'bg-danger': step.transition == 'REJECTED'
}">
<td>
<span class="glyphicon glyphicon-transfer" ng-if="step.type == 'APPROVE'"></span>
<span class="glyphicon glyphicon-ok-circle" ng-if="step.type == 'VALIDATE'"></span>
{{ 'workflow_type.' + step.type | translate }}
</td>
<td>{{ step.name }}</td>
<td>
<acl data="step.target"></acl>
</td>
<td>
<span ng-show="step.end_date">
{{ 'workflow_transition.' + step.transition | translate }}
{{ step.end_date | timeAgo: dateTimeFormat }}
by
<a href="#/user/{{ step.validator_username }}" class="label label-default">{{ step.validator_username }}</a>
<span ng-show="step.comment" class="text-">
<br/><em>{{ step.comment }}</em>
</span>
</span>
</td>
</tr>
</tbody>
</table>

View File

@@ -43,7 +43,7 @@
</div>
</div>
<div class="row">
<div class="col-xs-6">
<div class="col-xs-5">
<div class="input-group">
<span class="input-group-addon">
<span class="glyphicon glyphicon-transfer" ng-if="step.type == 'APPROVE'"></span>
@@ -63,6 +63,9 @@
typeahead-editable="false"
typeahead-wait-ms="200" />
</div>
<div class="col-xs-1">
<span class="btn btn-link glyphicon glyphicon-trash" ng-click="removeStep(step)"></span>
</div>
</div>
<div class="row mt-10">
<div class="col-xs-12" ng-class="{ 'has-error': !editWorkflowForm['name-' + $index].$valid && editWorkflowForm.$dirty }">

View File

@@ -22,14 +22,23 @@ public class TestRouteResource extends BaseJerseyTest {
* Test the route resource.
*/
@Test
public void testRouteResource() {
public void testRouteResource() throws Exception {
// Login route1
clientUtil.createUser("route1");
String route1Token = clientUtil.login("route1");
// Login admin
String adminToken = clientUtil.login("admin", "admin", false);
// Change SMTP configuration to target Wiser
target().path("/app/config_smtp").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.post(Entity.form(new Form()
.param("hostname", "localhost")
.param("port", "2500")
.param("from", "contact@sismicsdocs.com")
), JsonObject.class);
// Get all route models
JsonObject json = target().path("/routemodel")
.queryParam("sort_column", "2")
@@ -59,6 +68,7 @@ public class TestRouteResource extends BaseJerseyTest {
.param("routeModelId", routeModels.getJsonObject(0).getString("id"))), JsonObject.class);
JsonObject step = json.getJsonObject("route_step");
Assert.assertEquals("Check the document's metadata", step.getString("name"));
Assert.assertTrue(popEmail().contains("workflow step"));
// Get the route on document 1
json = target().path("/route")
@@ -112,6 +122,7 @@ public class TestRouteResource extends BaseJerseyTest {
step = json.getJsonObject("route_step");
Assert.assertEquals("Add relevant files to the document", step.getString("name"));
Assert.assertTrue(json.getBoolean("readable"));
Assert.assertTrue(popEmail().contains("workflow step"));
// Get the route on document 1
json = target().path("/route")
@@ -150,6 +161,7 @@ public class TestRouteResource extends BaseJerseyTest {
step = json.getJsonObject("route_step");
Assert.assertEquals("Approve the document", step.getString("name"));
Assert.assertTrue(json.getBoolean("readable"));
Assert.assertTrue(popEmail().contains("workflow step"));
// Get the route on document 1
json = target().path("/route")
@@ -186,6 +198,7 @@ public class TestRouteResource extends BaseJerseyTest {
.param("transition", "APPROVED")), JsonObject.class);
Assert.assertFalse(json.containsKey("route_step"));
Assert.assertFalse(json.getBoolean("readable"));
Assert.assertTrue(popEmail().contains("workflow step"));
// Get the route on document 1
json = target().path("/route")
@@ -217,5 +230,44 @@ public class TestRouteResource extends BaseJerseyTest {
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, route1Token)
.get(JsonObject.class);
Assert.assertFalse(json.containsKey("route_step"));
// Start the default route on document 1
target().path("/route/start").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, route1Token)
.post(Entity.form(new Form()
.param("documentId", document1Id)
.param("routeModelId", routeModels.getJsonObject(0).getString("id"))), JsonObject.class);
Assert.assertTrue(popEmail().contains("workflow step"));
// Get document 1 as route1
json = target().path("/document/" + document1Id).request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, route1Token)
.get(JsonObject.class);
Assert.assertTrue(json.containsKey("route_step"));
// Get document 1 as admin
response = target().path("/document/" + document1Id).request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.get();
Assert.assertEquals(Response.Status.OK, Response.Status.fromStatusCode(response.getStatus()));
// Cancel the route on document 1
target().path("/route")
.queryParam("documentId", document1Id)
.request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, route1Token)
.delete(JsonObject.class);
// Get document 1 as route1
json = target().path("/document/" + document1Id).request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, route1Token)
.get(JsonObject.class);
Assert.assertFalse(json.containsKey("route_step"));
// Get document 1 as admin
response = target().path("/document/" + document1Id).request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.get();
Assert.assertEquals(Response.Status.NOT_FOUND, Response.Status.fromStatusCode(response.getStatus()));
}
}