Skip to content

Commit b841472

Browse files
authored
fix: Update modified_by and created_by to id (#19)
1 parent bf883a4 commit b841472

17 files changed

Lines changed: 108 additions & 59 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
output
22
/target/
3+
/logs/
34
*.iml
45
.arcconfig
56
.idea
@@ -10,4 +11,4 @@ output
1011
pom.xml.versionsBackup
1112
.java-version
1213
.DS_Store
13-
*.tar.gz
14+
*.tar.gz

src/main/java/com/featureprobe/api/auth/UserPasswordAuthenticationProcessingFilter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public Authentication attemptAuthentication(HttpServletRequest request, HttpServ
3030
Map<String, String> authParam = mapper.readValue(body, Map.class);
3131
String account = authParam.get("account");
3232
String password = authParam.get("password");
33-
return getAuthenticationManager().authenticate(new UserPasswordAuthenticationToken(account,password));
33+
34+
return getAuthenticationManager().authenticate(new UserPasswordAuthenticationToken(account, password));
3435
}
3536
}

src/main/java/com/featureprobe/api/auth/UserPasswordAuthenticationProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public Authentication authenticate(Authentication authentication) throws Authent
3232
if (member.isPresent()
3333
&& new BCryptPasswordEncoder().matches(token.getPassword(), member.get().getPassword())) {
3434
memberService.updateVisitedTime(token.getAccount());
35-
return new UserPasswordAuthenticationToken(member.get().getAccount(), member.get().getRole().name(),
35+
36+
return new UserPasswordAuthenticationToken(member.get(),
3637
Arrays.asList(new SimpleGrantedAuthority(member.get().getRole().name())));
3738
}
3839
}
Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.featureprobe.api.auth;
22

33
import com.featureprobe.api.base.enums.RoleEnum;
4+
import com.featureprobe.api.entity.Member;
45
import org.springframework.security.authentication.AbstractAuthenticationToken;
56
import org.springframework.security.core.GrantedAuthority;
67

@@ -12,7 +13,7 @@ public class UserPasswordAuthenticationToken extends AbstractAuthenticationToken
1213

1314
private String password;
1415

15-
private String role;
16+
private Member principal;
1617

1718
public UserPasswordAuthenticationToken(String account, String password) {
1819
super(null);
@@ -21,16 +22,15 @@ public UserPasswordAuthenticationToken(String account, String password) {
2122
super.setAuthenticated(false);
2223
}
2324

24-
public UserPasswordAuthenticationToken(String account, String role,
25-
Collection<? extends GrantedAuthority> authorities) {
25+
public UserPasswordAuthenticationToken(Member principal, Collection<? extends GrantedAuthority> authorities) {
2626
super(authorities);
27-
this.account = account;
28-
this.role = role;
27+
this.principal = principal;
28+
this.account = principal.getAccount();
2929
super.setAuthenticated(true);
3030
}
3131

3232
public boolean isAdmin() {
33-
return RoleEnum.ADMIN.name().equals(role);
33+
return RoleEnum.ADMIN.name().equals(getRole());
3434
}
3535

3636
@Override
@@ -39,31 +39,24 @@ public Object getCredentials() {
3939
}
4040

4141
@Override
42-
public Object getPrincipal() {
43-
return "admin";
42+
public Member getPrincipal() {
43+
return principal;
4444
}
4545

4646
public String getAccount() {
4747
return account;
4848
}
4949

50-
public void setAccount(String account) {
51-
this.account = account;
52-
}
53-
5450
public String getPassword() {
5551
return password;
5652
}
5753

58-
public void setPassword(String password) {
59-
this.password = password;
60-
}
6154

6255
public String getRole() {
63-
return role;
56+
if (principal == null) {
57+
return null;
58+
}
59+
return principal.getRole().name();
6460
}
6561

66-
public void setRole(String role) {
67-
this.role = role;
68-
}
6962
}
Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.featureprobe.api.base.config;
22

33
import com.featureprobe.api.auth.UserPasswordAuthenticationToken;
4+
import com.featureprobe.api.entity.Member;
45
import org.springframework.context.annotation.Configuration;
56
import org.springframework.data.domain.AuditorAware;
67
import org.springframework.security.authentication.AnonymousAuthenticationToken;
@@ -13,23 +14,15 @@
1314
@Configuration
1415
public class AuditingConfig implements AuditorAware {
1516

16-
private static final String ANONYMOUS_OPERATE = "Anonymous";
17-
18-
private static final String SYSTEM_OPERATE = "System";
19-
20-
2117
@Override
22-
public Optional getCurrentAuditor() {
18+
public Optional<Member> getCurrentAuditor() {
2319
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
24-
if (Objects.isNull(authentication)) {
25-
return Optional.of(SYSTEM_OPERATE);
26-
} else if (authentication instanceof AnonymousAuthenticationToken) {
27-
return Optional.of(ANONYMOUS_OPERATE);
28-
} else {
29-
UserPasswordAuthenticationToken userPasswordAuthenticationToken =
30-
(UserPasswordAuthenticationToken) authentication;
31-
return Optional.of(userPasswordAuthenticationToken.getAccount());
20+
if (!(authentication instanceof UserPasswordAuthenticationToken)) {
21+
return null;
3222
}
23+
UserPasswordAuthenticationToken userPasswordAuthenticationToken =
24+
(UserPasswordAuthenticationToken) authentication;
25+
return Optional.of(userPasswordAuthenticationToken.getPrincipal());
3326
}
3427

3528
}

src/main/java/com/featureprobe/api/base/entity/AbstractAuditEntity.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.featureprobe.api.base.entity;
22

3+
import com.featureprobe.api.entity.Member;
34
import lombok.Data;
45
import org.springframework.data.annotation.CreatedBy;
56
import org.springframework.data.annotation.CreatedDate;
@@ -12,6 +13,8 @@
1213
import javax.persistence.GeneratedValue;
1314
import javax.persistence.GenerationType;
1415
import javax.persistence.Id;
16+
import javax.persistence.JoinColumn;
17+
import javax.persistence.ManyToOne;
1518
import javax.persistence.MappedSuperclass;
1619
import javax.persistence.Temporal;
1720
import javax.persistence.TemporalType;
@@ -32,16 +35,18 @@ public abstract class AbstractAuditEntity implements Serializable {
3235
@CreatedDate
3336
private Date createdTime;
3437

35-
@Column(name = "created_by", nullable = false, updatable = false)
3638
@CreatedBy
37-
private String createdBy;
39+
@ManyToOne
40+
@JoinColumn(name = "created_by", referencedColumnName = "id", nullable = false, updatable = false)
41+
private Member createdBy;
3842

3943
@Temporal(TemporalType.TIMESTAMP)
4044
@Column(name = "modified_time", nullable = false)
4145
@LastModifiedDate
4246
private Date modifiedTime;
4347

44-
@Column(name = "modified_by", nullable = false)
4548
@LastModifiedBy
46-
private String modifiedBy;
49+
@ManyToOne
50+
@JoinColumn(name = "modified_by", referencedColumnName = "id", nullable = false)
51+
private Member modifiedBy;
4752
}

src/main/java/com/featureprobe/api/dto/SegmentResponse.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ public class SegmentResponse {
2020

2121
private Date createdTime;
2222

23-
private String createdBy;
24-
2523
private Date modifiedTime;
2624

2725
private String modifiedBy;

src/main/java/com/featureprobe/api/dto/TargetingVersionResponse.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,4 @@ public class TargetingVersionResponse {
2323
private Long version;
2424

2525
private Date createdTime;
26-
27-
private String createdBy;
2826
}

src/main/java/com/featureprobe/api/dto/ToggleResponse.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ public class ToggleResponse {
2929

3030
private Date createdTime;
3131

32-
private String createdBy;
33-
3432
private Date modifiedTime;
3533

3634
private String modifiedBy;

src/main/java/com/featureprobe/api/entity/Member.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import lombok.ToString;
1010
import org.hibernate.annotations.DynamicInsert;
1111
import org.hibernate.annotations.Where;
12+
import org.springframework.security.core.AuthenticatedPrincipal;
1213

1314
import javax.persistence.Column;
1415
import javax.persistence.Entity;
@@ -17,16 +18,14 @@
1718
import javax.persistence.Table;
1819
import java.util.Date;
1920

20-
@EqualsAndHashCode(callSuper = true)
2121
@NoArgsConstructor
2222
@AllArgsConstructor
2323
@Data
2424
@Entity
2525
@Table(name = "member")
2626
@Where(clause = "deleted = 0")
2727
@DynamicInsert
28-
@ToString(callSuper = true)
29-
public class Member extends AbstractAuditEntity {
28+
public class Member extends AbstractAuditEntity implements AuthenticatedPrincipal {
3029

3130
private String account;
3231

@@ -40,4 +39,8 @@ public class Member extends AbstractAuditEntity {
4039

4140
private Boolean deleted;
4241

42+
@Override
43+
public String getName() {
44+
return account;
45+
}
4346
}

0 commit comments

Comments
 (0)