Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate authority domain and rest layers #24954

Merged
merged 2 commits into from
Jan 25, 2024
Merged
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
22 changes: 21 additions & 1 deletion generators/app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,26 @@ entity Company {}

`female` variant is supported by `pt-br` locale.

##### Authority

Secure the entity with the authority.

```
@EntityAuthority(ROLE_ADMIN)
@EntityReadAuthority(ROLE_USER)
entity Department {}
```

##### Admin Entity

Declare the entity as an admin entity like User and Authority.
The entity is added to the admin menu and secured with ROLE_ADMIN authority.

```
@AdminEntity
entity Department {}
```

### Fields

#### Notable customizations
Expand All @@ -87,7 +107,7 @@ entity Company {
##### Label

```jdl
relationship ManyToOnly {
relationship ManyToOne {
@RelationshipNameHumanized('Company user') Company{user} to User
}
```
Expand Down
4 changes: 4 additions & 0 deletions generators/base-application/support/prepare-entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ const BASE_TEMPLATE_DATA = {
existingEnum: false,
searchEngine: NO_SEARCH_ENGINE,
microserviceName: undefined,
entityAuthority: undefined,
entityReadAuthority: undefined,
adminEntity: undefined,

requiresPersistableImplementation: false,
updatableEntity: undefined,
Expand Down Expand Up @@ -171,6 +174,7 @@ export default function prepareEntity(entityWithConfig, generator, application)
entityInstance: lowerFirst(entityName),
entityTableName: hibernateSnakeCase(entityName),
entityNamePlural: pluralize(entityName),
entityAuthority: entityWithConfig.adminEntity ? 'ROLE_ADMIN' : undefined,
});

const dto = entityWithConfig.dto && entityWithConfig.dto !== NO_DTO;
Expand Down
3 changes: 3 additions & 0 deletions generators/base-application/types/entity.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ type Entity = Required<BaseEntity> &
builtInUser?: boolean;
builtInAuthority?: boolean;
microserviceName?: string;
adminEntity?: boolean;
entityAuthority?: string;
entityReadAuthority?: string;

entityNameCapitalized: string;
entityClass: string;
Expand Down
6 changes: 3 additions & 3 deletions generators/bootstrap-application-base/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export function createAuthorityEntity(customAuthorityData = {}, application) {
name: authorityEntityName,
entitySuffix: '',
builtIn: true,
fluentMethods: false,
adminEntity: true,
entityTableName: `${application.jhiTablePrefix}_authority`,
relationships: [],
fields: entityDefinition ? entityDefinition.fields || [] : [],
Expand All @@ -130,9 +130,9 @@ export function createAuthorityEntity(customAuthorityData = {}, application) {
service: 'no',
dto: 'no',
skipDbChangelog: true,
entityDomainLayer: false,
entityDomainLayer: application.backendTypeSpringBoot,
entityPersistenceLayer: false,
entityRestLayer: false,
entityRestLayer: application.backendTypeSpringBoot,
...customAuthorityData,
};

Expand Down
9 changes: 9 additions & 0 deletions generators/bootstrap-application/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ describe(`generator - ${generator}`, () => {
expectedEntity(entity),
`
{
"adminEntity": undefined,
"adminUserDto": "AdminUserDTO",
"allReferences": Any<Array>,
"annotations": {
Expand Down Expand Up @@ -222,6 +223,7 @@ describe(`generator - ${generator}`, () => {
"entityApi": "",
"entityApiDescription": undefined,
"entityApiUrl": "users",
"entityAuthority": undefined,
"entityClass": "User",
"entityClassHumanized": "User",
"entityClassPlural": "Users",
Expand All @@ -246,6 +248,7 @@ describe(`generator - ${generator}`, () => {
"entityPersistenceLayer": false,
"entityPluralFileName": "usersundefined",
"entityReactName": "User",
"entityReadAuthority": undefined,
"entityRestLayer": false,
"entitySearchLayer": false,
"entityServiceFileName": "user",
Expand Down Expand Up @@ -699,6 +702,7 @@ describe(`generator - ${generator}`, () => {
expectedEntity(entity),
`
{
"adminEntity": undefined,
"allReferences": Any<Array>,
"annotations": {
"changelogDate": "20220129025419",
Expand Down Expand Up @@ -743,6 +747,7 @@ describe(`generator - ${generator}`, () => {
"entityApi": "",
"entityApiDescription": undefined,
"entityApiUrl": "entity-as",
"entityAuthority": undefined,
"entityClass": "EntityA",
"entityClassHumanized": "Entity A",
"entityClassPlural": "EntityAS",
Expand All @@ -765,6 +770,7 @@ describe(`generator - ${generator}`, () => {
"entityParentPathAddition": "",
"entityPluralFileName": "entity-asundefined",
"entityReactName": "EntityA",
"entityReadAuthority": undefined,
"entityServiceFileName": "entity-a",
"entityStateName": "entity-a",
"entitySuffix": "",
Expand Down Expand Up @@ -1031,6 +1037,7 @@ describe(`generator - ${generator}`, () => {
expectedEntity(entity),
`
{
"adminEntity": undefined,
"allReferences": Any<Array>,
"annotations": {
"changelogDate": "20220129025419",
Expand Down Expand Up @@ -1075,6 +1082,7 @@ describe(`generator - ${generator}`, () => {
"entityApi": "",
"entityApiDescription": undefined,
"entityApiUrl": "entity-as",
"entityAuthority": undefined,
"entityClass": "EntityA",
"entityClassHumanized": "Entity A",
"entityClassPlural": "EntityAS",
Expand All @@ -1097,6 +1105,7 @@ describe(`generator - ${generator}`, () => {
"entityParentPathAddition": "",
"entityPluralFileName": "entity-asundefined",
"entityReactName": "EntityA",
"entityReadAuthority": undefined,
"entityServiceFileName": "entity-a",
"entityStateName": "entity-a",
"entitySuffix": "",
Expand Down
15 changes: 15 additions & 0 deletions generators/generate-blueprint/__snapshots__/generator.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,21 @@ exports[`generator - generate-blueprint with all option should match snapshot 1`
"generators/server/templates/template-file-server.ejs": {
"stateCleared": "modified",
},
"generators/spring-boot/command.mjs": {
"stateCleared": "modified",
},
"generators/spring-boot/generator.mjs": {
"stateCleared": "modified",
},
"generators/spring-boot/generator.spec.mjs": {
"stateCleared": "modified",
},
"generators/spring-boot/index.mjs": {
"stateCleared": "modified",
},
"generators/spring-boot/templates/template-file-spring-boot.ejs": {
"stateCleared": "modified",
},
"generators/spring-cache/command.mjs": {
"stateCleared": "modified",
},
Expand Down
1 change: 1 addition & 0 deletions generators/generator-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const GENERATOR_MAVEN = 'maven';
export const GENERATOR_PROJECT_NAME = 'project-name';
export const GENERATOR_REACT = 'react';
export const GENERATOR_SERVER = 'server';
export const GENERATOR_SPRING_BOOT = 'spring-boot';
export const GENERATOR_SPRING_CACHE = 'spring-cache';
export const GENERATOR_SPRING_CLOUD_STREAM = 'spring-cloud-stream';
export const GENERATOR_SPRING_DATA_CASSANDRA = 'spring-data-cassandra';
Expand Down
9 changes: 9 additions & 0 deletions generators/java/__snapshots__/generator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ exports[`generator - java with default config should match files snapshot 1`] =
"src/main/java/com/mycompany/myapp/GeneratedByJHipster.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/myapp/domain/Authority.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/myapp/domain/Bar.java": {
"stateCleared": "modified",
},
Expand All @@ -32,6 +35,12 @@ exports[`generator - java with default config should match files snapshot 1`] =
"src/main/java/com/mycompany/myapp/package-info.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/AuthorityTest.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/AuthorityTestSamples.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/BarTest.java": {
"stateCleared": "modified",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ Object.keys(uniqueEnums).forEach(function(element) { _%>

import <%= entityAbsolutePackage %>.domain.enumeration.<%= element %>;
<%_ }); _%>
<%_ if (!updatableEntity) { _%>
import java.util.Objects;
<%_ } _%>

<%_ for (const otherEntity of otherEntities.filter(otherEntity => otherEntity.entityPackage !== entityPackage)) { _%>
import <%= otherEntity.entityAbsoluteClass %>;
Expand Down Expand Up @@ -347,8 +350,12 @@ _%>

@Override
public int hashCode() {
<%_ if (updatableEntity) { _%>
// see https://vladmihalcea.com/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
return getClass().hashCode();
<%_ } else { _%>
return Objects.hashCode(get<%= primaryKey.nameCapitalized %>());
<%_ } _%>
}

// prettier-ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,20 @@ class <%= persistClass %>Test {

<%= persistInstance %>2 = get<%= persistClass %>Sample2();
assertThat(<%= persistInstance %>1).isNotEqualTo(<%= persistInstance %>2);
<%_} _%>
<%_ } _%>
}
<%_if (!updatableEntity) { _%>

@Test
void hashCodeVerifier() throws Exception {
<%= persistClass %> <%= persistInstance %> = new <%= persistClass %>();
assertThat(<%= persistInstance %>.hashCode()).isZero();

<%= persistClass %> <%= persistInstance %>1 = get<%= persistClass %>Sample1();
<%= persistInstance %>.set<%= primaryKey.nameCapitalized %>(<%= persistInstance %>1.get<%= primaryKey.nameCapitalized %>());
assertThat(<%= persistInstance %>).hasSameHashCodeAs(<%= persistInstance %>1);
}
<%_ } _%>
<%_ for (const relationship of relationships.filter(relationship => !relationship.otherEntity.builtIn)) { _%>

@Test
Expand Down
12 changes: 12 additions & 0 deletions generators/server/__snapshots__/generator.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,9 @@ exports[`generator - server with entities should match files snapshot 1`] = `
"src/main/java/com/mycompany/myapp/web/rest/AuthenticateController.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/myapp/web/rest/AuthorityResource.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/myapp/web/rest/BarResource.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -1118,6 +1121,12 @@ exports[`generator - server with entities should match files snapshot 1`] = `
"src/test/java/com/mycompany/myapp/config/timezone/HibernateTimeZoneIT.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/AuthorityTest.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/AuthorityTestSamples.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/BarTest.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -1178,6 +1187,9 @@ exports[`generator - server with entities should match files snapshot 1`] = `
"src/test/java/com/mycompany/myapp/web/rest/AuthenticateControllerIT.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/web/rest/AuthorityResourceIT.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/web/rest/BarResourceIT.java": {
"stateCleared": "modified",
},
Expand Down
2 changes: 1 addition & 1 deletion generators/server/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ export const baseServerFiles = {
condition: generator => generator.generateBuiltInAuthorityEntity,
path: `${SERVER_MAIN_SRC_DIR}_package_/`,
renameTo: moveToJavaPackageSrcDir,
templates: ['domain/Authority.java', 'repository/AuthorityRepository.java'],
templates: ['repository/AuthorityRepository.java'],
},
{
condition: generator =>
Expand Down
6 changes: 6 additions & 0 deletions generators/server/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
GENERATOR_SPRING_DATA_MONGODB,
GENERATOR_SPRING_DATA_NEO4J,
GENERATOR_SERVER,
GENERATOR_SPRING_BOOT,
GENERATOR_SPRING_CACHE,
GENERATOR_SPRING_WEBSOCKET,
GENERATOR_SPRING_DATA_RELATIONAL,
Expand Down Expand Up @@ -258,6 +259,11 @@ export default class JHipsterServerGenerator extends BaseApplicationGenerator {

get composing() {
return this.asComposingTaskGroup({
async composeBackendType() {
if (!this.jhipsterConfig.backendType || ['spring-boot', 'java'].includes(this.jhipsterConfig.backendType.toLowerCase())) {
await this.composeWithJHipster(GENERATOR_SPRING_BOOT);
}
},
async composing() {
const {
buildTool,
Expand Down
12 changes: 12 additions & 0 deletions generators/server/support/__snapshots__/needles.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ exports[`generator - server - support - needles generated project should match s
"src/main/java/com/mycompany/myapp/web/rest/AuthenticateController.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/myapp/web/rest/AuthorityResource.java": {
"stateCleared": "modified",
},
"src/main/java/com/mycompany/myapp/web/rest/PublicUserResource.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -371,6 +374,12 @@ exports[`generator - server - support - needles generated project should match s
"src/test/java/com/mycompany/myapp/config/timezone/HibernateTimeZoneIT.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/AuthorityTest.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/domain/AuthorityTestSamples.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/management/SecurityMetersServiceTests.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -416,6 +425,9 @@ exports[`generator - server - support - needles generated project should match s
"src/test/java/com/mycompany/myapp/web/rest/AuthenticateControllerIT.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/web/rest/AuthorityResourceIT.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/web/rest/PublicUserResourceIT.java": {
"stateCleared": "modified",
},
Expand Down
Loading
Loading