Skip to content

Commit

Permalink
Feature: add new permission (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zorin95670 authored Oct 15, 2024
2 parents cba6514 + 09800a3 commit 91e7440
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 26 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ List of permissions:
|------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `{"entity": "ADMIN", "action": "ACCESS"}` | Allows user to access and use `leto-modelizer-admin`. |
| `{"entity": "PROJECT", "action": "CREATE"}` | Allows user to create a project in `leto-modelizer`. |
| `{"entity": "PROJECT_GIT", "action": "CREATE"}` | Allows user to import a project from git in `leto-modelizer`. |
| `{"entity": "PROJECT_TEMPLATE", "action": "CREATE"}` | Allows user to create a project from template in `leto-modelizer`. |
| `{"entity": "DIAGRAM", "action": "CREATE"}` | Allows user to create a diagram in `leto-modelizer`. |
| `{"entity": "DIAGRAM", "action": "DELETE"}` | Allows user to delete a diagram in `leto-modelizer`. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,37 @@
*/
public class JsonNodeToLibraryFunction implements Function<JsonNode, Library> {

/**
* Library to set.
*/
private Library fromLibrary;

/**
* Default constructor.
*/
public JsonNodeToLibraryFunction() {
this(null);
}

/**
* Provide a library to use it as default value.
* All value of this library can be upgraded by values in json.
* @param fromLibrary Library to use as default.
*/
public JsonNodeToLibraryFunction(final Library fromLibrary) {
this.fromLibrary = fromLibrary;
}

@Override
public Library apply(final JsonNode json) {
Library library = new Library();
Library library = null;

if (fromLibrary != null) {
library = fromLibrary;
} else {
library = new Library();
}

library.setName(json.get("name").asText());
library.setVersion(json.get("version").asText());
library.setMaintainer(json.get("maintainer").asText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.github.erosb.jsonsKema.ValidatorConfig;
import jakarta.transaction.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -190,18 +189,16 @@ public Library create(final LibraryRecord libraryRecord, final String login) thr
throw new ApiException(ErrorType.UNAUTHORIZED_LIBRARY_URL, "url", libraryRecord.url());
}

AccessControl role = null;
if (StringUtils.isNotBlank(libraryRecord.role())) {
role = accessControlService.create(AccessControlType.ROLE, new AccessControlRecord(libraryRecord.role()));
accessControlService.associateUser(AccessControlType.ROLE, role.getId(), login);
}

String libraryUrl = libraryRecord.url().replace("index.json", "");

if (libraryRepository.existsByUrl(libraryUrl)) {
throw new ApiException(ErrorType.ENTITY_ALREADY_EXISTS, "url", libraryUrl);
}

AccessControl role = accessControlService.create(AccessControlType.ROLE,
new AccessControlRecord(libraryRecord.role()));
accessControlService.associateUser(AccessControlType.ROLE, role.getId(), login);

Library library = save(libraryUrl, null);

permissionService.createLibraryPermissions(library, role);
Expand Down Expand Up @@ -255,13 +252,15 @@ Library save(final String url, final UUID id) throws JsonProcessingException {

// JsonProcessingException can't be thrown because we already validate json before.
JsonNode libraryJson = new ObjectMapper().readTree(libraryValue);
Library library = new JsonNodeToLibraryFunction().apply(libraryJson);
library.setUrl(url);

Library library = null;
if (id != null) {
library.setId(id);
library = libraryRepository.findById(id).orElse(null);
}

library = new JsonNodeToLibraryFunction(library).apply(libraryJson);
library.setUrl(url);

library = libraryRepository.save(library);
final UUID libraryId = library.getId();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE TYPE entity_type AS ENUM ('ADMIN', 'PROJECT', 'PROJECT_TEMPLATE', 'DIAGRAM', 'DIAGRAM_TEMPLATE', 'COMPONENT', 'COMPONENT_TEMPLATE', 'LIBRARY');
CREATE TYPE entity_type AS ENUM ('ADMIN', 'PROJECT', 'PROJECT_GIT', 'PROJECT_TEMPLATE', 'DIAGRAM', 'DIAGRAM_TEMPLATE', 'COMPONENT', 'COMPONENT_TEMPLATE', 'LIBRARY');
CREATE TYPE action_type AS ENUM ('ACCESS', 'CREATE', 'DELETE', 'UPDATE');

CREATE TABLE IF NOT EXISTS permissions (
Expand Down Expand Up @@ -26,6 +26,7 @@ COMMENT ON COLUMN permissions.update_date IS 'Last update date of this row.';
INSERT INTO permissions(entity, action, lib_id) VALUES
('ADMIN', 'ACCESS', NULL),
('PROJECT', 'CREATE', NULL),
('PROJECT_GIT', 'CREATE', NULL),
('PROJECT_TEMPLATE', 'CREATE', NULL),
('DIAGRAM', 'CREATE', NULL),
('DIAGRAM', 'DELETE', NULL),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ SELECT (SELECT aco_id FROM access_controls WHERE name = 'ADMINISTRATOR'), per_id

-- Add permission of all actions on leto-modelizer to Developer.
INSERT INTO access_controls_permissions(aco_id, per_id)
SELECT (SELECT aco_id FROM access_controls WHERE name = 'DEVELOPER'), per_id FROM permissions WHERE entity in ('PROJECT', 'PROJECT_TEMPLATE', 'DIAGRAM', 'DIAGRAM_TEMPLATE', 'COMPONENT', 'COMPONENT_TEMPLATE');
SELECT (SELECT aco_id FROM access_controls WHERE name = 'DEVELOPER'), per_id FROM permissions WHERE entity in ('PROJECT', 'PROJECT_GIT', 'PROJECT_TEMPLATE', 'DIAGRAM', 'DIAGRAM_TEMPLATE', 'COMPONENT', 'COMPONENT_TEMPLATE');
4 changes: 2 additions & 2 deletions src/main/resources/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

<div class="box">
<h1 class="title">Authentication Success</h1>
<p class="subtitle">You will be redirected in <span id="timer">5</span> second(s).</p>
<p class="subtitle">You will be redirected in <span id="timer">3</span> second(s).</p>
<button id="redirect-button">Go to application</button>
</div>

Expand All @@ -62,7 +62,7 @@ <h1 class="title">Authentication Success</h1>
confettiNumber: 2000,
});

var timer = 5;
var timer = 3;
var intervalId = setInterval(function () {
if (timer <= 0) {
clearInterval(intervalId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertEquals;

@Tag("unit")
Expand Down Expand Up @@ -58,4 +60,34 @@ void testApply() {

assertEquals(expectedLibrary, mapper.apply(json));
}

@Test
@DisplayName("Test apply: should keep default if it's not override")
void testApplyKeepValue() {
Library defaultLibrary = new Library();
defaultLibrary.setId(UUID.randomUUID());
defaultLibrary.setVersion("version");

JsonNodeToLibraryFunction mapper = new JsonNodeToLibraryFunction(defaultLibrary);

Library expectedLibrary = new Library();

expectedLibrary.setId(defaultLibrary.getId());
expectedLibrary.setName("name");
expectedLibrary.setVersion("version");
expectedLibrary.setMaintainer("maintainer");
expectedLibrary.setDescription("description");
expectedLibrary.setDocumentationUrl("documentationUrl");
expectedLibrary.setIcon("icon");

ObjectNode json = JsonNodeFactory.instance.objectNode();
json.put("name", "name");
json.put("version", "version");
json.put("maintainer", "maintainer");
json.put("description", "description");
json.put("documentationUrl", "documentationUrl");
json.put("icon", "icon");

assertEquals(expectedLibrary, mapper.apply(json));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ void testCreateWithAlreadyExistsUrl() throws JsonProcessingException {
ApiException exception = null;

try {
service.create(new LibraryRecord("http://localhost:8080/test/index.json", null), "login");
service.create(new LibraryRecord("http://localhost:8080/test/index.json", "TEST"), "login");
} catch (ApiException e) {
exception = e;
}
Expand Down
30 changes: 20 additions & 10 deletions src/test/resources/features/Library.feature
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ Feature: Library feature

Scenario: Should return 400 on unknown library url
Given I initialize the admin user
And I clean role "TEST"

When I request "/libraries" with method "POST" with json
| key | value |
| url | http://[LIBRARY_HOST]/invalid/unknown/index.json |
| key | value |
| role | TEST |
| url | http://[LIBRARY_HOST]/invalid/unknown/index.json |
Then I expect "400" as status code
And I expect response field "message" is "Wrong field value."
And I expect response field "code" is "206"
Expand All @@ -65,10 +67,12 @@ Feature: Library feature

Scenario: Should return 400 on invalid library json
Given I initialize the admin user
And I clean role "TEST"

When I request "/libraries" with method "POST" with json
| key | value |
| url | http://[LIBRARY_HOST]/invalid/simple/index.json |
| key | value |
| role | TEST |
| url | http://[LIBRARY_HOST]/invalid/simple/index.json |
Then I expect "400" as status code
And I expect response field "message" is "Index.json of library is invalid."
And I expect response field "code" is "210"
Expand All @@ -87,15 +91,19 @@ Feature: Library feature
Scenario: Should return 400 on already exists library
Given I initialize the admin user
And I clean library "http://[LIBRARY_HOST]/valid/simple/"
And I clean role "TEST1"
And I clean role "TEST2"

When I request "/libraries" with method "POST" with json
| key | value |
| url | http://[LIBRARY_HOST]/valid/simple/index.json |
| key | value |
| role | TEST1 |
| url | http://[LIBRARY_HOST]/valid/simple/index.json |
Then I expect "201" as status code

When I request "/libraries" with method "POST" with json
| key | value |
| url | http://[LIBRARY_HOST]/valid/simple/index.json |
| key | value |
| role | TEST2 |
| url | http://[LIBRARY_HOST]/valid/simple/index.json |
Then I expect "400" as status code
And I expect response field "message" is "Entity already exists."
And I expect response field "code" is "208"
Expand All @@ -105,6 +113,7 @@ Feature: Library feature
Scenario: Should return 200 on a valid library creation
Given I initialize the admin user
And I clean library "http://[LIBRARY_HOST]/valid/simple/"
And I clean role "TEST"

When I request "/libraries/validate" with method "POST" with body
| value | type |
Expand All @@ -113,8 +122,9 @@ Feature: Library feature

# Library creation
When I request "/libraries" with method "POST" with json
| key | value |
| url | http://[LIBRARY_HOST]/valid/simple/index.json |
| key | value |
| role | TEST |
| url | http://[LIBRARY_HOST]/valid/simple/index.json |
Then I expect "201" as status code
And I set response field "id" to context "libraryId"

Expand Down

0 comments on commit 91e7440

Please sign in to comment.