Skip to content

Commit 0510ae1

Browse files
authored
Merge pull request #29 from MetheaX/dev
Enhancement - v0.0.2
2 parents 81ad670 + 81d5a99 commit 0510ae1

72 files changed

Lines changed: 703 additions & 530 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

api/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<artifactId>api</artifactId>
1313

1414
<properties>
15+
<java.version>1.8</java.version>
1516
<postgres.version>42.2.1</postgres.version>
1617
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1718
</properties>

api/src/main/java/io/methea/api/service/MetheaAuthenticationService.java

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
import io.methea.domain.configuration.jwt.entity.TSessionManagement;
1313
import io.methea.domain.configuration.permission.view.PermissionView;
1414
import io.methea.domain.configuration.user.entity.TUser;
15+
import io.methea.domain.webservice.client.entity.Client;
1516
import io.methea.domain.webservice.system.entity.SystemCertificate;
1617
import io.methea.exception.CertificateNotFoundException;
1718
import io.methea.exception.InvalidClientSecretException;
1819
import io.methea.repository.configuration.group.UserGroupRepository;
1920
import io.methea.repository.configuration.jwt.SessionManagementRepository;
2021
import io.methea.repository.configuration.permission.UserGrantedPermissionRepository;
2122
import io.methea.repository.configuration.user.UserRepository;
23+
import io.methea.repository.webservice.client.ClientRepository;
2224
import io.methea.repository.webservice.system.SystemCertificateRepository;
2325
import io.methea.utils.SystemUtils;
2426
import io.methea.utils.auth.JwtUtil;
@@ -50,6 +52,7 @@
5052
public class MetheaAuthenticationService implements UserDetailsService, AuthenticationService {
5153

5254
private final UserRepository userRepository;
55+
private final ClientRepository clientRepository;
5356
private final UserGrantedPermissionRepository userGrantedPermissionRepository;
5457
private final UserGroupRepository userGroupRepository;
5558
private final SystemCertificateRepository certificateRepository;
@@ -59,10 +62,11 @@ public class MetheaAuthenticationService implements UserDetailsService, Authenti
5962

6063
@Inject
6164
public MetheaAuthenticationService(UserRepository userRepository,
62-
UserGrantedPermissionRepository userGrantedPermissionRepository,
63-
UserGroupRepository userGroupRepository,
64-
SystemCertificateRepository certificateRepository, SessionManagementRepository sessionManagementRepository) {
65+
ClientRepository clientRepository, UserGrantedPermissionRepository userGrantedPermissionRepository,
66+
UserGroupRepository userGroupRepository, SystemCertificateRepository certificateRepository,
67+
SessionManagementRepository sessionManagementRepository) {
6568
this.userRepository = userRepository;
69+
this.clientRepository = clientRepository;
6670
this.userGrantedPermissionRepository = userGrantedPermissionRepository;
6771
this.userGroupRepository = userGroupRepository;
6872
this.certificateRepository = certificateRepository;
@@ -73,7 +77,10 @@ public MetheaAuthenticationService(UserRepository userRepository,
7377
public Token generateTokenFromUser(RequestTokenPayload payload, HttpServletRequest request) {
7478
PrincipalAuthentication authentication = loadUserAsClient(payload);
7579
if (ObjectUtils.isEmpty(authentication)) {
76-
return null;
80+
authentication = loadClient(payload);
81+
if (ObjectUtils.isEmpty(authentication)) {
82+
return null;
83+
}
7784
}
7885
return generateToken(authentication, request, false);
7986
}
@@ -136,7 +143,11 @@ public PrincipalAuthentication loadUserByUsername(String s) throws UsernameNotFo
136143
TUser user = userRepository.findByUsernameAndStatus(s, MConstant.ACTIVE_STATUS);
137144

138145
if (ObjectUtils.isEmpty(user)) {
139-
return null;
146+
Client client = clientRepository.findClientByClientIdAndStatus(s, MConstant.ACTIVE_STATUS);
147+
if (ObjectUtils.isEmpty(client)) {
148+
return null;
149+
}
150+
return buildPrincipal(client);
140151
}
141152
return buildPrincipal(user);
142153
}
@@ -201,6 +212,28 @@ private PrincipalAuthentication loadUserAsClient(RequestTokenPayload payload) {
201212
return buildPrincipal(user);
202213
}
203214

215+
private PrincipalAuthentication loadClient(RequestTokenPayload payload) {
216+
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
217+
Client client = clientRepository.findClientByClientIdAndStatus(payload.getClientId(), MConstant.ACTIVE_STATUS);
218+
if (ObjectUtils.isEmpty(client)) {
219+
return null;
220+
}
221+
if (!encoder.matches(payload.getClientSecret(), client.getClientSecret())) {
222+
return null;
223+
}
224+
return buildPrincipal(client);
225+
}
226+
227+
private PrincipalAuthentication buildPrincipal(Client client) {
228+
MetheaPrincipal principal = new MetheaPrincipal();
229+
Map<String, Object> param = new HashMap<>();
230+
param.put("username", client.getClientId());
231+
principal.setUsername(client.getClientId());
232+
principal.setForceUserResetPassword(MConstant.NO);
233+
return new PrincipalAuthentication(client.getClientId(), client.getClientSecret(), new ArrayList<>(),
234+
userGrantedPermissionRepository.getByQuery(param, PermissionView.class), principal);
235+
}
236+
204237
private PrincipalAuthentication buildPrincipal(TUser user) {
205238
MetheaPrincipal principal = new MetheaPrincipal();
206239
Map<String, Object> param = new HashMap<>();

core/src/main/java/io/methea/constant/MConstant.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ private MConstant() {
2828
public static final String CORE_TEMPLATE_KEY = "methea.core.freemarker.templates";
2929
public static final String CLIENT_TEMPLATE_KEY = "methea.client.freemarker.templates";
3030

31+
public static final String ID = "id";
3132
public static final String COMMA = ",";
3233
public static final String COLON = ":";
3334
public static final String SPACE = " ";

core/src/main/java/io/methea/controller/AbstractSimpleMetheaController.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,20 @@ public abstract class AbstractSimpleMetheaController<E extends AbstractMetheaEnt
5353
protected String configViewName;
5454

5555
private final DataTableUIService dataTableUIService;
56-
protected AbstractSimpleMetheaService metheaService;
56+
protected AbstractSimpleMetheaService simpleMetheaService;
5757
protected AbstractMetheaValidator validator;
5858

5959
public AbstractSimpleMetheaController(DataTableUIService dataTableUIService) {
6060
this.dataTableUIService = dataTableUIService;
6161
}
6262

63-
@RequestMapping(value = StringUtils.EMPTY, method = RequestMethod.GET)
63+
@GetMapping(value = StringUtils.EMPTY)
6464
public String viewAllWithFilter(Model model, B binder, F filter, Pagination pagination, HttpServletRequest request) {
6565
dataTableAttributes(model, binder, filter, pagination, request);
6666
return templatePath;
6767
}
6868

69-
@RequestMapping(value = SAVE_URL, method = RequestMethod.POST)
69+
@PostMapping(value = SAVE_URL)
7070
public ModelAndView save(B binder, Model model, F filter, Pagination pagination, HttpServletRequest request) {
7171
Map<String, String> errors = new HashMap<>();
7272
validator.validate(binder, errors);
@@ -77,11 +77,11 @@ public ModelAndView save(B binder, Model model, F filter, Pagination pagination,
7777
model.addAttribute("hasErrors", true);
7878
return new ModelAndView(templatePath);
7979
}
80-
metheaService.saveEntity(getEntityFromBinder(binder), binder);
80+
simpleMetheaService.saveEntity(getEntityFromBinder(binder), binder);
8181
return new ModelAndView("redirect:" + ROOT_URL.concat(MConstant.SLASH).concat(entity));
8282
}
8383

84-
@RequestMapping(value = MODIFY_URL, method = RequestMethod.POST)
84+
@PostMapping(value = MODIFY_URL)
8585
public ModelAndView modify(B binder, Model model, F filter, Pagination pagination, HttpServletRequest request) {
8686
Map<String, String> errors = new HashMap<>();
8787
errors.put("validateType", "EDIT");
@@ -94,7 +94,7 @@ public ModelAndView modify(B binder, Model model, F filter, Pagination paginatio
9494
model.addAttribute("errors", errors);
9595
return new ModelAndView(templatePath);
9696
}
97-
metheaService.modifyEntity(getEntityId(binder), binder);
97+
simpleMetheaService.modifyEntity(getEntityId(binder), binder);
9898
return new ModelAndView("redirect:" + ROOT_URL.concat(MConstant.SLASH).concat(entity));
9999
}
100100

@@ -104,7 +104,7 @@ public ResponseEntity<Map<String, Object>> getById(@RequestParam String id) {
104104
Map<String, Object> map = new HashMap<>();
105105
map.put(MConstant.JSON_STATUS, 500);
106106
map.put(MConstant.JSON_MESSAGE, String.format("Failed to get %s", entity));
107-
Object view = metheaService.getEntityViewById(id);
107+
Object view = simpleMetheaService.getEntityViewById(id);
108108
if (!ObjectUtils.isEmpty(view)) {
109109
map.put(MConstant.JSON_STATUS, 200);
110110
map.put(MConstant.JSON_MESSAGE, String.format("Get %s success!", entity));
@@ -119,7 +119,7 @@ public ResponseEntity<Map<String, Object>> getEntityById(@RequestParam String id
119119
Map<String, Object> map = new HashMap<>();
120120
map.put(MConstant.JSON_STATUS, 500);
121121
map.put(MConstant.JSON_MESSAGE, String.format("Failed to get %s", entity));
122-
Optional<E> o = metheaService.getEntityById(id);
122+
Optional<E> o = simpleMetheaService.getEntityById(id);
123123
if (o.isPresent()) {
124124
map.put(MConstant.JSON_STATUS, 200);
125125
map.put(MConstant.JSON_MESSAGE, String.format("Get %s success!", entity));
@@ -135,7 +135,7 @@ public ResponseEntity<Map<String, Object>> activateEntity(@RequestBody Map<Strin
135135
map.put(MConstant.JSON_STATUS, 500);
136136
map.put(MConstant.JSON_MESSAGE, String.format("Failed to activate %s", entity));
137137
try {
138-
if (metheaService.activateEntity(payload.get("id"))) {
138+
if (simpleMetheaService.activateEntity(payload.get("id"))) {
139139
map.put(MConstant.JSON_STATUS, 200);
140140
map.put(MConstant.JSON_MESSAGE, String.format("Activate %s success!", entity));
141141
}
@@ -153,7 +153,7 @@ public ResponseEntity<Map<String, Object>> deactivateEntity(@RequestBody Map<Str
153153
map.put(MConstant.JSON_STATUS, 500);
154154
map.put(MConstant.JSON_MESSAGE, String.format("Failed to deactivate %s", entity));
155155
try {
156-
if (metheaService.deactivateEntity(payload.get("id"))) {
156+
if (simpleMetheaService.deactivateEntity(payload.get("id"))) {
157157
map.put(MConstant.JSON_STATUS, 200);
158158
map.put(MConstant.JSON_MESSAGE, String.format("Deactivate %s success!", entity));
159159
}
@@ -170,7 +170,7 @@ protected void dataTableAttributes(Model model, B binder, F filter, Pagination p
170170
}
171171

172172
Map<String, List<?>> map = new HashMap<>();
173-
map.put(MConstant.JSON_DATA, metheaService.getAllEntityViewByFilter(getFilterColumns(filter), pagination));
173+
map.put(MConstant.JSON_DATA, simpleMetheaService.getAllEntityViewByFilter(getFilterColumns(filter), pagination));
174174
model.addAttribute(MConstant.CONTEXT_PATH_KEY, SystemUtils.getBaseUrl(request));
175175
model.addAttribute("tableHead", MCache.CACHE_META_DATA.get(configViewName.concat(MConstant.COLUMNS_LABEL)));
176176
model.addAttribute("tableColumns", MCache.CACHE_META_DATA.get(configViewName.concat(MConstant.COLUMNS_KEY)));

core/src/main/java/io/methea/domain/common/entity/BaseEntity.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.methea.domain.common.entity;
22

3-
43
import com.fasterxml.jackson.annotation.JsonIgnore;
54
import io.methea.domain.common.entity.abs.AbstractMetheaEntity;
65
import org.hibernate.annotations.CreationTimestamp;

core/src/main/java/io/methea/domain/configuration/account/entity/TAccount.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.methea.domain.configuration.account.entity;
22

33
import io.methea.domain.common.entity.BaseEntity;
4-
import org.springframework.util.StringUtils;
4+
import org.apache.commons.lang3.StringUtils;
55

66
import javax.persistence.*;
77
import java.util.UUID;

core/src/main/java/io/methea/domain/configuration/account/view/AccountView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import io.methea.repository.hibernateextension.annotation.Column;
1010
import io.methea.repository.hibernateextension.annotation.SelectFrom;
1111

12-
@SelectFrom(fromClause = "FROM TAccount o", orderBy = "ORDER BY o.updatedDateTime DESC")
12+
@SelectFrom(fromClause = "FROM TAccount o", orderBy = "ORDER BY o.accountName ASC")
1313
public class AccountView extends BaseView<AccountView> {
1414

1515
private static final long serialVersionUID = -8462411991270288224L;

core/src/main/java/io/methea/domain/configuration/display/view/DataTableView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Author : DKSilverX
99
* Date : 18/04/2020
1010
*/
11-
@SelectFrom(fromClause = "FROM TDataTableView o", orderBy = "ORDER BY o.updatedDateTime DESC")
11+
@SelectFrom(fromClause = "FROM TDataTableView o", orderBy = "ORDER BY o.viewName ASC")
1212
public class DataTableView extends BaseView<DataTableView> {
1313
private static final long serialVersionUID = 2335231132063689758L;
1414

core/src/main/java/io/methea/domain/configuration/group/dropdown/GroupDropdown.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
@SelectFrom(fromClause = "FROM TUserGroup o", orderBy = "ORDER BY o.groupName ASC")
1212
public class GroupDropdown extends BaseView<GroupDropdown> {
1313
private static final long serialVersionUID = -8990771004017469895L;
14-
@Column(name = "o.id")
14+
@Column(name = "o.id", key = "accountId", where = "AND o.accountId LIKE :accountId")
1515
private String id;
1616
@Column(name = "o.groupName", key = "status", where = "AND o.status = :status", isLastColumn = true)
1717
private String name;

core/src/main/java/io/methea/domain/configuration/group/entity/TUserGroup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.methea.domain.configuration.group.entity;
22

33
import io.methea.domain.common.entity.BaseEntity;
4-
import org.springframework.util.StringUtils;
4+
import org.apache.commons.lang3.StringUtils;
55

66
import javax.persistence.Column;
77
import javax.persistence.Entity;

0 commit comments

Comments
 (0)