Skip to content

Commit

Permalink
generate authority domain and rest layers
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Jan 24, 2024
1 parent 84b7a90 commit 8e4a180
Show file tree
Hide file tree
Showing 27 changed files with 549 additions and 226 deletions.
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
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
7 changes: 7 additions & 0 deletions generators/server/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -634,9 +634,16 @@ export default class JHipsterServerGenerator extends BaseApplicationGenerator {
get preparingEachEntity() {
return this.asPreparingEachEntityTaskGroup({
prepareEntity({ entity }) {
const hasAnyAuthority = authorities =>
authorities.length > 0 ? `hasAnyAuthority(${authorities.map(auth => `'${auth}'`).join(',')})` : undefined;
mutateData(entity, {
entityPersistenceLayer: true,
entityRestLayer: true,
entitySpringPreAuthorize: hasAnyAuthority(entity.entityAuthority?.split(',') ?? []),
entitySpringReadPreAuthorize: hasAnyAuthority([
...(entity.entityAuthority?.split(',') ?? []),
...(entity.entityReadAuthority?.split(',') ?? []),
]),
});
},
});
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

0 comments on commit 8e4a180

Please sign in to comment.