Skip to content

Commit 1d7284e

Browse files
author
Bob Garner
committed
Converted some if...log fatal patterns to the new assert instruction. Also reformatted SecurityTemplate.eml.
1 parent 234f61a commit 1d7284e

File tree

2 files changed

+115
-122
lines changed

2 files changed

+115
-122
lines changed

security/standalone/AuthorizationAuthor.eml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ $[author to org.entityc.springboot.service]
1313
$[import "util/AutoWired"]
1414
$[let entityInSecurity = entity|domain:Security]
1515
$[let userEntity = (space|domain:Security).entityTagged("user")]
16+
$[assert userEntity != null fatal]Cannot find user entity.$[/assert]
1617
$[let roleEnum = (space|domain:Security).enumTagged("role")]
18+
$[assert roleEnum != null fatal]Cannot find roles enum.$[/assert]
1719
$[let rolesAttribute = userEntity.attributeOfTypeTagged("role")]
18-
$[if rolesAttribute == null]$[log fatal]Cannot find rolesAttribute!$[/log]$[/if]
20+
$[assert rolesAttribute != null fatal]Cannot find rolesAttribute on user entity.$[/assert]
1921
$[if userEntity != null && roleEnum != null && rolesAttribute != null]
2022
$[let enabledAttribute = (userEntity|domain:Security).attributeTagged("login:enabled")]
2123
$[let usernameAttribute = (userEntity|domain:Security).attributeTagged("login:username")]
Lines changed: 112 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,146 @@
1-
$[language java]
2-
$[domain Security]
3-
$[D summary "This template builds code associated with user authentication."]
4-
$[D "This template builds code associated with user authentication. It uses tags placed on"]
5-
$[D "domain entities and attributes to enable and guide code generation."]
1+
$[ language java ]
2+
$[ domain Security ]
3+
$[ D summary "This template builds code associated with user authentication." ]
4+
$[ D "This template builds code associated with user authentication. It uses tags placed on" ]
5+
$[ D "domain entities and attributes to enable and guide code generation." ]
66

7-
$[import "security/SecurityFunctions"]
8-
$[import "security/standalone/AuthorizationAuthor"]
7+
$[ import "security/SecurityFunctions" ]
8+
$[ import "security/standalone/AuthorizationAuthor" ]
99

10-
$[let destDir = (domain.namespace|path) + "/jwt"]
11-
$[install "security/standalone/jwt/AuthEntryPointJwt.java" destDir]
12-
$[install "security/standalone/jwt/AuthTokenFilter.java" destDir]
13-
$[install "security/standalone/jwt/JwtResponse.java" destDir]
14-
$[install "security/standalone/jwt/JwtUtils.java" destDir]
10+
$[ let destDir = (domain.namespace|path) + "/jwt" ]
11+
$[ install "security/standalone/jwt/AuthEntryPointJwt.java" destDir ]
12+
$[ install "security/standalone/jwt/AuthTokenFilter.java" destDir ]
13+
$[ install "security/standalone/jwt/JwtResponse.java" destDir ]
14+
$[ install "security/standalone/jwt/JwtUtils.java" destDir ]
1515

16-
$[let destDir = (domain.namespace|path) + "/workfactor"]
17-
$[install "security/standalone/workfactor/BcCryptWorkFactorService.java" destDir]
18-
$[install "security/standalone/workfactor/BcryptWorkFactor.java" destDir]
19-
$[install "security/standalone/workfactor/Pbkdf2WorkFactorService.java" destDir]
16+
$[ let destDir = (domain.namespace|path) + "/workfactor" ]
17+
$[ install "security/standalone/workfactor/BcCryptWorkFactorService.java" destDir ]
18+
$[ install "security/standalone/workfactor/BcryptWorkFactor.java" destDir ]
19+
$[ install "security/standalone/workfactor/Pbkdf2WorkFactorService.java" destDir ]
2020

21-
$[call findUserEntity()->(userEntity: userEntity, passwordAttribute: passwordAttribute, usernameAttribute: usernameAttribute)]
22-
$[call findRoleEnum()->(roleEnum: roleEnum, defaultEnumItem: defaultEnumItem)]
21+
$[ call findUserEntity()->(userEntity: userEntity, passwordAttribute: passwordAttribute, usernameAttribute: usernameAttribute) ]
22+
$[ call findRoleEnum()->(roleEnum: roleEnum, defaultEnumItem: defaultEnumItem) ]
2323

24-
$[if userEntity == null]
25-
$[log fatal]Security features require an entity tagged "user" in the Security domain, but none found.$[/log]
26-
$[/if]
24+
$[* Check to make sure all required variables are set *]
25+
$[ assert userEntity != null error ]Security features require an entity tagged "user" in the Security domain, but none found.$[/ assert ]
26+
$[ assert usernameAttribute != null error ]Security features require an attribute tagged "login:username" in the Security domain, but none found.$[/ assert ]
27+
$[ assert passwordAttribute != null error ]Security features require an attribute tagged "login:password" in the Security domain, but none found.$[/ assert ]
28+
$[ assert roleEnum != null error ]Security features require an enum tagged "role" in the Security domain, but none found.$[/ assert ]
29+
$[ assert !__assert_error fatal ]Add the required tags and try again.$[/ assert ]
2730

28-
$[if usernameAttribute == null]
29-
$[log fatal]Security features require an attribute tagged "login:username" in the Security domain, but none found.$[/log]
30-
$[/if]
31+
$[ let destDir = domain.namespace|path ]
32+
$[ install "security/standalone/userdetails/UserDetailsMapper.java" destDir ]
33+
$[ install "security/standalone/userdetails/PersistentUserDetailsService.java" destDir ]
34+
$[ install "security/standalone/userdetails/PersistentUserDetailsPasswordService.java" destDir ]
3135

32-
$[if passwordAttribute == null]
33-
$[log fatal]Security features require an attribute tagged "login:password" in the Security domain, but none found.$[/log]
34-
$[/if]
36+
$[ install "security/standalone/SecurityConfig.java" destDir ]
37+
$[ install "security/standalone/SecurityService.java" destDir ]
38+
$[ install "security/standalone/SecurityServiceImpl.java" destDir ]
39+
$[ install "security/standalone/AuthController.java" destDir ]
40+
$[ install "security/standalone/UserLoginDto.java" destDir ]
41+
$[ if domain.hasTag("feature:invite") && (space|domain:Security).hasEntityTagged("invite") ]
42+
$[ install "security/standalone/UserInviteAcceptDto.java" destDir ]
43+
$[ else ]
44+
$[ install "security/standalone/UserSignupDto.java" destDir ]
45+
$[/ if ]
46+
$[ install "security/standalone/WebConfig.java" destDir ]
3547

36-
$[if roleEnum == null]
37-
$[log fatal]Security features require an enum tagged "role" in the Security domain, but none found.$[/log]
38-
$[/if]
39-
40-
$[let destDir = domain.namespace|path]
41-
$[install "security/standalone/userdetails/UserDetailsMapper.java" destDir]
42-
$[install "security/standalone/userdetails/PersistentUserDetailsService.java" destDir]
43-
$[install "security/standalone/userdetails/PersistentUserDetailsPasswordService.java" destDir]
44-
45-
$[install "security/standalone/SecurityConfig.java" destDir]
46-
$[install "security/standalone/SecurityService.java" destDir]
47-
$[install "security/standalone/SecurityServiceImpl.java" destDir]
48-
$[install "security/standalone/AuthController.java" destDir]
49-
$[install "security/standalone/UserLoginDto.java" destDir]
50-
$[if domain.hasTag("feature:invite") && (space|domain:Security).hasEntityTagged("invite")]
51-
$[install "security/standalone/UserInviteAcceptDto.java" destDir]
52-
$[else]
53-
$[install "security/standalone/UserSignupDto.java" destDir]
54-
$[/if]
55-
$[install "security/standalone/WebConfig.java" destDir]
56-
57-
$[author to org.entityc.springboot.controller]
58-
$[author to update]
59-
$[author to outlet insideTop
60-
D "Add code at top of update method to basically null out incoming attribute values if the logged"
61-
D "in user's role does not let them update those attributes."
48+
$[ author to org.entityc.springboot.controller ]
49+
$[ author to update ]
50+
$[ author to outlet insideTop
51+
D "Add code at top of update method to basically null out incoming attribute values if the logged"
52+
D "in user's role does not let them update those attributes."
6253
]
63-
$[send imports]
54+
$[ send imports ]
6455
import org.springframework.security.core.annotation.AuthenticationPrincipal;
6556
import org.springframework.security.core.userdetails.UserDetails;
66-
$[/send]
67-
$[if (entity|domain:Security).hasTag("user")]
68-
$[call getUserEntityAttributes(userEntity: entity)->(passwordAttribute: unused, usernameAttribute: usernameAttribute)]
69-
$[call findRoleEnum(space: entity.space)->(roleEnum: rolesEnum, defaultEnumItem: unused)]
70-
$[send imports]
57+
$[/ send ]
58+
$[ if (entity|domain:Security).hasTag("user") ]
59+
$[ call getUserEntityAttributes(userEntity: entity)->(passwordAttribute: unused, usernameAttribute: usernameAttribute) ]
60+
$[ call findRoleEnum(space: entity.space)->(roleEnum: rolesEnum, defaultEnumItem: unused) ]
61+
$[ send imports ]
7162
import ${rolesEnum|domain:Model|fullname};
7263
import java.util.Set;
73-
$[/send]
64+
$[/ send ]
7465
${entity|domain:Model|name} user = ${service}.getBy${usernameAttribute|domain:Model|name|capitalize}(userDetails.getUsername());
7566
Set<${rolesEnum|domain:Model|name}> roles = user.get${(entity|domain:Security).attributeOfTypeTagged("role")|name|capitalize}();
7667
requestDto.adjustUpdateForRoles(user, roles);
77-
$[elseif (entity|domain:Security).hasTag("access:object:level")]
78-
$[send imports]
68+
$[ elseif (entity|domain:Security).hasTag("access:object:level") ]
69+
$[ send imports ]
7970
import ${exceptionNamespace}.ForbiddenException;
80-
$[/send]
71+
$[/ send ]
8172
if (!${service}.canEdit(id)) {
8273
throw new ForbiddenException("User does not have permission to edit this card.");
8374
}
84-
$[else]
75+
$[ else ]
8576
requestDto.adjustUpdateForRoles(userDetailsMapper.rolesForUserDetails(userDetails));
86-
$[/if]
87-
$[/author]
88-
$[/author]
89-
$[author to createWithParent, create, associate, update, deleteById, deleteByRelationship, release]
90-
$[author to outlet annotation
91-
D "Make sure the user has write permission for these methods."
77+
$[/ if ]
78+
$[/ author ]
79+
$[/ author ]
80+
$[ author to deleteById, associate, createWithParent, update, release, deleteByRelationship, create ]
81+
$[ author to outlet annotation
82+
D "Make sure the user has write permission for these methods."
9283
]
93-
$[if !(entity|domain:Security).hasTag("access:object:level")]
94-
$[call preAuthorizeEntity(accessType: "write", entity: entity)]
95-
$[/if]
96-
$[/author]
97-
$[/author]
98-
$[author to getById, getListByRelationship, getList]
99-
$[author to outlet annotation
100-
D "Make sure the user has read permission for these methods."
84+
$[ if !(entity|domain:Security).hasTag("access:object:level") ]
85+
$[ call preAuthorizeEntity(accessType: "write", entity: entity) ]
86+
$[/ if ]
87+
$[/ author ]
88+
$[/ author ]
89+
$[ author to getById, getListByRelationship, getList ]
90+
$[ author to outlet annotation
91+
D "Make sure the user has read permission for these methods."
10192
]
102-
$[call preAuthorizeEntity(accessType: "read", entity: entity)]
103-
$[/author]
104-
$[/author]
105-
$[/author]
93+
$[ call preAuthorizeEntity(accessType: "read", entity: entity) ]
94+
$[/ author ]
95+
$[/ author ]
96+
$[/ author ]
10697

107-
$[author to org.entityc.springboot.dto]
108-
$[author to outlet methods
109-
D "Add the adjustUpdateForRoles() method to the DTO class."
98+
$[ author to org.entityc.springboot.dto ]
99+
$[ author to outlet methods
100+
D "Add the adjustUpdateForRoles() method to the DTO class."
110101
]
111-
$[if space.hasDomain("Security")]
112-
$[call findRoleEnum()->(roleEnum: rolesEnum)]
113-
$[/if]
114-
$[if rolesEnum != null]
115-
$[send imports]
102+
$[ if space.hasDomain("Security") ]
103+
$[ call findRoleEnum()->(roleEnum: rolesEnum) ]
104+
$[/ if ]
105+
$[ if rolesEnum != null ]
106+
$[ send imports ]
116107
import ${rolesEnum|domain:Model|fullname};
117-
$[/send]
118-
$[let isUserEntity = (entity|domain:Security).hasTag("user")]
119-
$[if isUserEntity]
120-
$[send imports]
108+
$[/ send ]
109+
$[ let isUserEntity = (entity|domain:Security).hasTag("user") ]
110+
$[ if isUserEntity ]
111+
$[ send imports ]
121112
import ${entity|domain:Model|fullname};
122-
$[/send]
123-
$[/if]
113+
$[/ send ]
114+
$[/ if ]
124115

125-
public void adjustUpdateForRoles($[if isUserEntity]${entity|domain:Model|name} user, $[/if]Set<${rolesEnum|domain:Model|name}> roles) {
126-
$[foreach attribute in entity.attributes]
127-
$[if attribute.isCreation || attribute.isModification]
116+
public void adjustUpdateForRoles($[ if isUserEntity ]${entity|domain:Model|name} user, $[/ if ]Set<${rolesEnum|domain:Model|name}> roles) {
117+
$[ foreach attribute in entity.attributes ]
118+
$[ if attribute.isCreation || attribute.isModification ]
128119
this.${attribute|domain:JSONDTO|name} = null;
129-
$[/if]
130-
$[/foreach]
131-
$[let checkUser = isUserEntity && (entity|domain:Security).hasAttributeWithTagPrefixed("access:write:user")]
132-
$[if checkUser]
133-
$[let pk = entity.primaryKeyAttribute]
120+
$[/ if ]
121+
$[/ foreach ]
122+
$[ let checkUser = isUserEntity && (entity|domain:Security).hasAttributeWithTagPrefixed("access:write:user") ]
123+
$[ if checkUser ]
124+
$[ let pk = entity.primaryKeyAttribute ]
134125
boolean _sameUser = user.get${pk|domain:Model|name|capitalize}().equals(this.${pk|domain:JSONDTO|name});
135-
$[/if]
136-
$[let hasAttributeLevelRoles = (entity|domain:Security).hasAttributeWithTagPrefixed("access:write:")]
137-
$[if hasAttributeLevelRoles]
138-
$[foreach attribute in entity.attributes]
139-
$[if (attribute|domain:Security).hasTag("login:password")]$[continue]$[/if]
140-
$[call attributeRolesContains(accessType: "write", rolesArrayName: "roles", attribute: attribute)->(hasRoles: hasRoles, rolesExpr: rolesExpr)]
141-
$[let checkUserForAttribute = checkUser && (attribute|domain:Security).hasTag("access:write:user")]
142-
$[if checkUserForAttribute]
143-
$[if !hasRoles]$[let rolesExpr = "_sameUser"]$[else]$[let rolesExpr = rolesExpr + " || _sameUser"]$[/if]
144-
$[/if]
145-
$[if hasRoles || checkUserForAttribute]
126+
$[/ if ]
127+
$[ let hasAttributeLevelRoles = (entity|domain:Security).hasAttributeWithTagPrefixed("access:write:") ]
128+
$[ if hasAttributeLevelRoles ]
129+
$[ foreach attribute in entity.attributes ]
130+
$[ if (attribute|domain:Security).hasTag("login:password") ]$[ continue ]$[/ if ]
131+
$[ call attributeRolesContains(accessType: "write", rolesArrayName: "roles", attribute: attribute)->(hasRoles: hasRoles, rolesExpr: rolesExpr) ]
132+
$[ let checkUserForAttribute = checkUser && (attribute|domain:Security).hasTag("access:write:user") ]
133+
$[ if checkUserForAttribute ]
134+
$[ if !hasRoles ]$[ let rolesExpr = "_sameUser" ]$[ else ]$[ let rolesExpr = rolesExpr + " || _sameUser" ]$[/ if ]
135+
$[/ if ]
136+
$[ if hasRoles || checkUserForAttribute ]
146137
if (!(${rolesExpr})) {
147138
this.${attribute|domain:JSONDTO|name} = null;
148139
}
149-
$[/if]
150-
$[/foreach]
151-
$[/if]
140+
$[/ if ]
141+
$[/ foreach ]
142+
$[/ if ]
152143
}
153-
$[/if]
154-
$[/author]
155-
$[/author]
144+
$[/ if ]
145+
$[/ author ]
146+
$[/ author ]

0 commit comments

Comments
 (0)