1 package com.supwisdom.institute.backend.base.api.v1.controller.authn;
3 import java.util.HashMap;
7 import io.swagger.annotations.Api;
8 import lombok.extern.slf4j.Slf4j;
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.http.HttpStatus;
12 import org.springframework.util.MimeTypeUtils;
13 import org.springframework.web.bind.annotation.GetMapping;
14 import org.springframework.web.bind.annotation.PathVariable;
15 import org.springframework.web.bind.annotation.RequestMapping;
16 import org.springframework.web.bind.annotation.RequestParam;
17 import org.springframework.web.bind.annotation.ResponseBody;
18 import org.springframework.web.bind.annotation.ResponseStatus;
19 import org.springframework.web.bind.annotation.RestController;
21 import com.supwisdom.institute.backend.base.api.v1.vo.authn.response.AuthnAccountPermissionsResponseData;
22 import com.supwisdom.institute.backend.base.api.v1.vo.authn.response.AuthnAccountResourcesResponseData;
23 import com.supwisdom.institute.backend.base.api.v1.vo.authn.response.AuthnAccountResponseData;
24 import com.supwisdom.institute.backend.base.api.v1.vo.authn.response.AuthnAccountRolesResponseData;
25 import com.supwisdom.institute.backend.base.api.v1.vo.authn.response.AuthnApplicationsResponseData;
26 import com.supwisdom.institute.backend.base.api.v1.vo.authn.response.AuthnPermissionRoleListResponseData;
27 import com.supwisdom.institute.backend.base.api.v1.vo.authn.response.AuthnResourceRoleListResponseData;
28 import com.supwisdom.institute.backend.base.api.v1.vo.authn.response.AuthnRolesResponseData;
29 import com.supwisdom.institute.backend.base.api.v1.vo.authn.response.AuthnRoutesResponseData;
30 import com.supwisdom.institute.backend.base.domain.entity.Account;
31 import com.supwisdom.institute.backend.base.domain.entity.Application;
32 import com.supwisdom.institute.backend.base.domain.entity.Permission;
33 import com.supwisdom.institute.backend.base.domain.entity.Resource;
34 import com.supwisdom.institute.backend.base.domain.entity.Role;
35 import com.supwisdom.institute.backend.base.domain.entity.Route;
36 import com.supwisdom.institute.backend.base.domain.model.PermissionRoleSet;
37 import com.supwisdom.institute.backend.base.domain.model.ResourceRoleSet;
38 import com.supwisdom.institute.backend.base.domain.service.AccountService;
39 import com.supwisdom.institute.backend.base.domain.service.ApplicationService;
40 import com.supwisdom.institute.backend.base.domain.service.PermissionService;
41 import com.supwisdom.institute.backend.base.domain.service.ResourceService;
42 import com.supwisdom.institute.backend.base.domain.service.RoleService;
43 import com.supwisdom.institute.backend.base.domain.service.RouteService;
44 import com.supwisdom.institute.backend.common.framework.vo.response.DefaultApiResponse;
46 @Api(value = "BaseAuthn", tags = { "BaseAuthn" }, description = "认证授权接口")
49 @RequestMapping("/v1/authn")
50 public class AuthnController {
53 private AccountService accountService;
56 private RoleService roleService;
59 private ApplicationService applicationService;
62 private PermissionService permissionService;
65 private ResourceService resourceService;
68 private RouteService routeService;
70 @GetMapping(path = "/{username}/account", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
71 @ResponseStatus(value = HttpStatus.OK)
73 public DefaultApiResponse<AuthnAccountResponseData> account(
74 @PathVariable("username") String username) {
76 if (username == null || username.length() == 0) {
77 throw new RuntimeException("exception.get.username.must.not.empty");
80 Account account = accountService.selectByUsername(username);
82 if (account == null) {
83 throw new RuntimeException("exception.get.account.not.exist");
86 AuthnAccountResponseData data = AuthnAccountResponseData.of(account);
88 return new DefaultApiResponse<AuthnAccountResponseData>(data);
91 @GetMapping(path = "/{username}/roles", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
92 @ResponseStatus(value = HttpStatus.OK)
94 public DefaultApiResponse<AuthnAccountRolesResponseData> accountRoles(
95 @PathVariable("username") String username) {
97 if (username == null || username.length() == 0) {
98 throw new RuntimeException("exception.get.username.must.not.empty");
101 Account account = accountService.selectByUsername(username);
103 if (account == null) {
104 throw new RuntimeException("exception.get.account.not.exist");
107 List<Role> roles = roleService.selectByUsername(username);
109 AuthnAccountRolesResponseData data = AuthnAccountRolesResponseData.of(roles);
111 return new DefaultApiResponse<AuthnAccountRolesResponseData>(data);
114 @GetMapping(path = "/{username}/applications", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
115 @ResponseStatus(value = HttpStatus.OK)
117 public DefaultApiResponse<AuthnAccountPermissionsResponseData> accountApplications(
118 @PathVariable("username") String username,
119 @RequestParam(name = "applicationId", required = false) String applicationId) {
121 if (username == null || username.length() == 0) {
122 throw new RuntimeException("exception.get.username.must.not.empty");
125 Account account = accountService.selectByUsername(username);
127 if (account == null) {
128 throw new RuntimeException("exception.get.account.not.exist");
131 List<Permission> applications = permissionService.selectByUsername(username, null, Permission.TYPE_APPLICATION);
133 AuthnAccountPermissionsResponseData data = AuthnAccountPermissionsResponseData.of(applications);
135 return new DefaultApiResponse<AuthnAccountPermissionsResponseData>(data);
138 @GetMapping(path = "/{username}/menus", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
139 @ResponseStatus(value = HttpStatus.OK)
141 public DefaultApiResponse<AuthnAccountPermissionsResponseData> accountMenus(
142 @PathVariable("username") String username,
143 @RequestParam(name = "applicationId", required = false) String applicationId) {
145 if (username == null || username.length() == 0) {
146 throw new RuntimeException("exception.get.username.must.not.empty");
149 Account account = accountService.selectByUsername(username);
151 if (account == null) {
152 throw new RuntimeException("exception.get.account.not.exist");
155 List<Permission> menus = permissionService.selectByUsername(username, applicationId, Permission.TYPE_MENU);
157 AuthnAccountPermissionsResponseData data = AuthnAccountPermissionsResponseData.of(menus);
159 return new DefaultApiResponse<AuthnAccountPermissionsResponseData>(data);
162 @GetMapping(path = "/{username}/operations", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
163 @ResponseStatus(value = HttpStatus.OK)
165 public DefaultApiResponse<AuthnAccountPermissionsResponseData> accountOperations(
166 @PathVariable("username") String username,
167 @RequestParam(name = "applicationId", required = false) String applicationId) {
169 if (username == null || username.length() == 0) {
170 throw new RuntimeException("exception.get.username.must.not.empty");
173 Account account = accountService.selectByUsername(username);
175 if (account == null) {
176 throw new RuntimeException("exception.get.account.not.exist");
179 List<Permission> operations = permissionService.selectByUsername(username, applicationId, Permission.TYPE_OPERATION);
181 AuthnAccountPermissionsResponseData data = AuthnAccountPermissionsResponseData.of(operations);
183 return new DefaultApiResponse<AuthnAccountPermissionsResponseData>(data);
186 @GetMapping(path = "/{username}/resources", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
187 @ResponseStatus(value = HttpStatus.OK)
189 public DefaultApiResponse<AuthnAccountResourcesResponseData> accountResources(
190 @PathVariable("username") String username,
191 @RequestParam(name = "applicationId", required = false) String applicationId) {
193 if (username == null || username.length() == 0) {
194 throw new RuntimeException("exception.get.username.must.not.empty");
197 Account account = accountService.selectByUsername(username);
199 if (account == null) {
200 throw new RuntimeException("exception.get.account.not.exist");
203 List<Resource> resources = null;// FIXME: resourceService.selectByUsername(username, applicationId);
205 AuthnAccountResourcesResponseData data = AuthnAccountResourcesResponseData.of(resources);
207 return new DefaultApiResponse<AuthnAccountResourcesResponseData>(data);
210 // @GetMapping(path = "/resources", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
211 // @ResponseStatus(value = HttpStatus.OK)
213 // public DefaultApiResponse<AuthnResourceRoleListResponseData> applicationResources(
214 // @RequestParam(name = "applicationId", required = false) String applicationId) {
216 // List<ResourceRoleSet> resourceRoleSets = resourceService.selectByApplication(applicationId);
218 // AuthnResourceRoleListResponseData data = AuthnResourceRoleListResponseData.of(resourceRoleSets);
220 // return new DefaultApiResponse<AuthnResourceRoleListResponseData>(data);
224 @GetMapping(path = "/applications", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
225 @ResponseStatus(value = HttpStatus.OK)
227 public DefaultApiResponse<AuthnApplicationsResponseData> applications() {
229 Map<String, Object> mapBean = new HashMap<>();
230 Map<String, String> orderBy = null;
232 mapBean.put("status", "1");
234 List<Application> applications = applicationService.selectList(mapBean, orderBy);
236 AuthnApplicationsResponseData data = AuthnApplicationsResponseData.of(applications);
238 return new DefaultApiResponse<AuthnApplicationsResponseData>(data);
241 @GetMapping(path = "/roles", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
242 @ResponseStatus(value = HttpStatus.OK)
244 public DefaultApiResponse<AuthnRolesResponseData> roles() {
246 Map<String, Object> mapBean = new HashMap<>();
247 Map<String, String> orderBy = null;
249 mapBean.put("status", "1");
251 List<Role> roles = roleService.selectList(mapBean, orderBy);
253 AuthnRolesResponseData data = AuthnRolesResponseData.of(roles);
255 return new DefaultApiResponse<AuthnRolesResponseData>(data);
259 @GetMapping(path = "/permissionRoleSets", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
260 @ResponseStatus(value = HttpStatus.OK)
262 public DefaultApiResponse<AuthnPermissionRoleListResponseData> permissionRoleSets(
263 @RequestParam(name = "applicationId", required = false) String applicationId) {
265 Map<String, Object> mapBean = new HashMap<>();
267 mapBean.put("status", "1");
268 if (applicationId != null && !applicationId.isEmpty()) {
269 mapBean.put("applicationId", applicationId);
272 List<PermissionRoleSet> permissionRoleSets = permissionService.selectPermissionRoleSet(mapBean);
274 AuthnPermissionRoleListResponseData data = AuthnPermissionRoleListResponseData.of(permissionRoleSets);
276 return new DefaultApiResponse<AuthnPermissionRoleListResponseData>(data);
280 @GetMapping(path = "/resourceRoleSets", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
281 @ResponseStatus(value = HttpStatus.OK)
283 public DefaultApiResponse<AuthnResourceRoleListResponseData> resourceRoleSets(
284 @RequestParam(name = "applicationId", required = false) String applicationId) {
286 Map<String, Object> mapBean = new HashMap<>();
288 mapBean.put("status", "1");
290 List<ResourceRoleSet> resourceRoleSets = resourceService.selectResourceRoleSet(mapBean);
292 AuthnResourceRoleListResponseData data = AuthnResourceRoleListResponseData.of(resourceRoleSets);
294 return new DefaultApiResponse<AuthnResourceRoleListResponseData>(data);
299 @GetMapping(path = "/routes", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
300 @ResponseStatus(value = HttpStatus.OK)
302 public DefaultApiResponse<AuthnRoutesResponseData> routes() {
304 Map<String, Object> mapBean = new HashMap<>();
305 Map<String, String> orderBy = null;
307 mapBean.put("status", "1");
309 List<Route> routes = routeService.selectList(mapBean, orderBy);
311 AuthnRoutesResponseData data = AuthnRoutesResponseData.of(routes);
313 return new DefaultApiResponse<AuthnRoutesResponseData>(data);