Skip to content

Commit 4ec87c8

Browse files
committed
after plugin with anywhere
1 parent 161af39 commit 4ec87c8

23 files changed

+921
-358
lines changed

src/main/java/com/sapanywhere/app/controller/OauthController.java

Lines changed: 152 additions & 257 deletions
Large diffs are not rendered by default.

src/main/java/com/sapanywhere/app/controller/SettingController.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,14 @@ public String editLeaveTypeRule(SettingPage settingPage){
187187
leaveTypeRule.setMaxDays(leaveTypeRuleForm.getMaxDays());
188188
leaveTypeRule.setType(leaveType);
189189
this.leaveTypeRuleRepository.save(leaveTypeRule);
190+
191+
// update LeaveDaysInfo in db
192+
for(User user: this.userService.findAll()){
193+
this.leaveDaysInfoService.save(user,leaveTypeRule);
194+
}
195+
190196
}
197+
191198
return "redirect:/setting.html";
192199
}
193200
}

src/main/java/com/sapanywhere/app/entity/OauthData.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public class OauthData {
2323
private String userPhone;
2424
private long expiration;
2525
private int tenantId;
26+
private String userCode;
27+
private String companyCode;
2628

2729
public long getId() {
2830
return id;
@@ -84,4 +86,16 @@ public int getTenantId() {
8486
public void setTenantId(int tenantId) {
8587
this.tenantId = tenantId;
8688
}
89+
public String getUserCode() {
90+
return userCode;
91+
}
92+
public void setUserCode(String userCode) {
93+
this.userCode = userCode;
94+
}
95+
public String getCompanyCode() {
96+
return companyCode;
97+
}
98+
public void setCompanyCode(String companyCode) {
99+
this.companyCode = companyCode;
100+
}
87101
}

src/main/java/com/sapanywhere/app/model/BaseForm.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ protected void AddFieldError(BindingResult result,
2121
null);
2222
result.addError(error);
2323
}
24+
25+
public void addError(BindingResult result,String errorCode){
26+
result.reject(errorCode);
27+
}
2428
}

src/main/java/com/sapanywhere/app/model/BindForm.java

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,30 @@
33
import javax.validation.constraints.NotNull;
44
import javax.validation.constraints.Size;
55

6-
import org.apache.commons.codec.binary.StringUtils;
76
import org.hibernate.validator.constraints.Email;
87
import org.springframework.validation.BindingResult;
8+
import org.thymeleaf.util.StringUtils;
99

10+
import com.sapanywhere.app.entity.OauthData;
1011
import com.sapanywhere.app.entity.User;
1112

12-
public class BindForm extends BaseForm{
13-
private static final String FORMNAME = "bindForm";
14-
13+
14+
public class BindForm extends BaseForm {
15+
public static final String FORM_NAME = "bindForm";
16+
17+
public BindForm() {
18+
19+
}
20+
21+
public BindForm(OauthData oauthData) {
22+
this.sapAccount = oauthData.getUserEmail();
23+
this.email = oauthData.getUserEmail();
24+
}
25+
1526
@NotNull
1627
@Size(min = 4, max = 30)
1728
private String sapAccount;
18-
29+
1930
@NotNull
2031
@Size(min = 4, max = 30)
2132
private String firstName;
@@ -74,8 +85,7 @@ public String getRepeatPassword() {
7485
public void setRepeatPassword(String repeatPassword) {
7586
this.repeatPassword = repeatPassword;
7687
}
77-
78-
88+
7989
public String getSapAccount() {
8090
return sapAccount;
8191
}
@@ -84,7 +94,7 @@ public void setSapAccount(String sapAccount) {
8494
this.sapAccount = sapAccount;
8595
}
8696

87-
public User parse(){
97+
public User parse() {
8898
User user = new User();
8999
user.setSapAccount(this.sapAccount);
90100
user.setFirstName(this.firstName);
@@ -96,18 +106,35 @@ public User parse(){
96106

97107
@Override
98108
public void onValid(BindingResult result) {
99-
if(result.hasErrors()){
109+
if (result.hasErrors()) {
100110
return;
101111
}
102-
103-
if(StringUtils.equals(this.password, this.repeatPassword)){
104-
this.AddFieldError(
105-
result,
106-
FORMNAME,
107-
"repeatPassword",
108-
new String[] { "bind.error.passwordnotmatch" },
109-
null);
112+
113+
if (!StringUtils.equals(this.password, this.repeatPassword)) {
114+
this.AddFieldError(result, FORM_NAME, "repeatPassword",
115+
new String[] { "bind.error.passwordnotmatch" }, null);
110116
}
111-
117+
118+
}
119+
120+
public void onValid(BindingResult result, OauthData oauthData) {
121+
if (!StringUtils.equalsIgnoreCase(oauthData.getUserEmail(),
122+
this.getSapAccount())) {
123+
124+
this.AddFieldError(result, FORM_NAME, "sapAccount",
125+
new String[] { "bind.error.sapaccountnotmatch" }, null);
126+
return;
127+
}
128+
129+
this.onValid(result);
130+
}
131+
132+
public void addFieldError(BindingResult result,String fieldName,String errorCode){
133+
this.AddFieldError(
134+
result,
135+
FORM_NAME,
136+
fieldName,
137+
new String[] { errorCode },
138+
null);
112139
}
113140
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.sapanywhere.app.oauth;
2+
3+
import com.sapanywhere.app.exception.BusinessException;
4+
5+
public interface ApiClient {
6+
7+
public UserInfo getUserInfo(String accessToken) throws BusinessException;
8+
9+
10+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.sapanywhere.app.oauth;
2+
3+
import org.apache.log4j.Logger;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.stereotype.Service;
6+
import org.thymeleaf.util.StringUtils;
7+
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import com.sapanywhere.app.exception.BusinessException;
10+
import com.sapanywhere.app.utils.SSLContextUtils;
11+
12+
13+
@Service
14+
public class ApiClientImpl implements ApiClient {
15+
16+
private static Logger logger = Logger.getLogger(ApiClientImpl.class);
17+
18+
@Autowired
19+
private AppProperties appProperties;
20+
21+
@Autowired
22+
private HttpClient httpClient;
23+
24+
private ObjectMapper objectMapper = new ObjectMapper();
25+
26+
@Override
27+
public UserInfo getUserInfo(String accessToken) throws BusinessException {
28+
String url = this.join("Users/me", accessToken);
29+
try {
30+
SSLContextUtils.byPassCert();
31+
String content = httpClient.get(url);
32+
if (!StringUtils.isEmpty(content)) {
33+
return objectMapper.readValue(content, UserInfo.class);
34+
}
35+
} catch (Exception ex) {
36+
logger.error("Get user info failed via SAP Anywhere Open API", ex);
37+
}
38+
39+
throw new BusinessException(ErrorCode.APP_ACCESS_OPEN_API_FAILED);
40+
}
41+
42+
private String join(String path, String accessToken)
43+
throws BusinessException {
44+
String host = this.appProperties.getOpenAPIUrl();
45+
if (!host.endsWith("/")) {
46+
host += "/";
47+
}
48+
return host + path + "?access_token=" + accessToken;
49+
50+
}
51+
52+
/*private URI getOpenAPIUri(){
53+
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(this.appProperties.getOpenAPIUrl()).path("/{boNames}")
54+
}*/
55+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.sapanywhere.app.oauth;
2+
3+
import org.hibernate.validator.constraints.NotBlank;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
@Configuration
8+
@ConfigurationProperties(locations = "classpath:application.properties", ignoreUnknownFields = false, prefix = "app")
9+
public class AppProperties {
10+
11+
@NotBlank
12+
private String apiKey;
13+
@NotBlank
14+
private String apiSecret;
15+
@NotBlank
16+
private String appScope;
17+
@NotBlank
18+
private String applicationUrl;
19+
@NotBlank
20+
private String installationUrl;
21+
@NotBlank
22+
private String openAPIUrl;
23+
@NotBlank
24+
private String authorizeServiceUrl;
25+
26+
public String getApiKey() {
27+
return apiKey;
28+
}
29+
30+
public void setApiKey(String apiKey) {
31+
this.apiKey = apiKey;
32+
}
33+
34+
public String getApiSecret() {
35+
return apiSecret;
36+
}
37+
38+
public void setApiSecret(String apiSecret) {
39+
this.apiSecret = apiSecret;
40+
}
41+
42+
public String getAppScope() {
43+
return appScope;
44+
}
45+
46+
public void setAppScope(String appScope) {
47+
this.appScope = appScope;
48+
}
49+
50+
public String getApplicationUrl() {
51+
return applicationUrl;
52+
}
53+
54+
public void setApplicationUrl(String applicationUrl) {
55+
this.applicationUrl = applicationUrl;
56+
}
57+
58+
public String getInstallationUrl() {
59+
return installationUrl;
60+
}
61+
62+
public void setInstallationUrl(String installationUrl) {
63+
this.installationUrl = installationUrl;
64+
}
65+
66+
public String getOpenAPIUrl() {
67+
return openAPIUrl;
68+
}
69+
70+
public void setOpenAPIUrl(String openAPIUrl) {
71+
this.openAPIUrl = openAPIUrl;
72+
}
73+
74+
public String getAuthorizeServiceUrl() {
75+
return authorizeServiceUrl;
76+
}
77+
78+
public void setAuthorizeServiceUrl(String authorizeServiceUrl) {
79+
this.authorizeServiceUrl = authorizeServiceUrl;
80+
}
81+
82+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.sapanywhere.app.oauth;
2+
3+
public class ErrorCode {
4+
5+
public static final String APP_CONFIG_VERIFY_FAILED = "E00001";
6+
public static final String APP_SIGN_VERIFY_FAILED = "E00002";
7+
public static final String APP_GET_TOKRN_FAILED = "E00003";
8+
public static final String APP_ACCESS_OPEN_API_FAILED = "E00004";
9+
public static final String AUTHORIZE_CODE_NONE = "E00005";
10+
}

0 commit comments

Comments
 (0)