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 ]
6455import org.springframework.security.core.annotation.AuthenticationPrincipal;
6556import 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 ]
7162import ${rolesEnum|domain:Model|fullname};
7263import 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 ]
7970import ${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 ]
116107import ${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 ]
121112import ${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