1 package com.supwisdom.institute.backend.base.api.v1.authn;
5 import io.swagger.annotations.Api;
6 import lombok.extern.slf4j.Slf4j;
8 import org.springframework.beans.factory.annotation.Autowired;
9 import org.springframework.http.HttpStatus;
10 import org.springframework.util.MimeTypeUtils;
11 import org.springframework.web.bind.annotation.GetMapping;
12 import org.springframework.web.bind.annotation.PathVariable;
13 import org.springframework.web.bind.annotation.RequestMapping;
14 import org.springframework.web.bind.annotation.RequestParam;
15 import org.springframework.web.bind.annotation.ResponseBody;
16 import org.springframework.web.bind.annotation.ResponseStatus;
17 import org.springframework.web.bind.annotation.RestController;
19 import com.supwisdom.institute.backend.base.api.vo.response.AuthnAccountPermissionsResponseData;
20 import com.supwisdom.institute.backend.base.api.vo.response.AuthnAccountResourcesResponseData;
21 import com.supwisdom.institute.backend.base.api.vo.response.AuthnAccountResponseData;
22 import com.supwisdom.institute.backend.base.api.vo.response.AuthnAccountRolesResponseData;
23 import com.supwisdom.institute.backend.base.domain.entity.Account;
24 import com.supwisdom.institute.backend.base.domain.entity.Permission;
25 import com.supwisdom.institute.backend.base.domain.entity.Resource;
26 import com.supwisdom.institute.backend.base.domain.entity.Role;
27 import com.supwisdom.institute.backend.base.domain.service.AccountService;
28 import com.supwisdom.institute.backend.base.domain.service.PermissionService;
29 import com.supwisdom.institute.backend.base.domain.service.ResourceService;
30 import com.supwisdom.institute.backend.base.domain.service.RoleService;
31 import com.supwisdom.institute.backend.common.framework.vo.response.DefaultApiResponse;
33 @Api(value = "BaseAuthnAccount", tags = { "BaseAuthnAccount" }, description = "帐号接口(认证、授权用)")
36 @RequestMapping("/v1/authn")
37 public class AuthnAccountController {
40 private AccountService accountService;
43 private RoleService roleService;
46 private PermissionService permissionService;
49 private ResourceService resourceService;
51 @GetMapping(path = "/{username}/account", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
52 @ResponseStatus(value = HttpStatus.OK)
54 public DefaultApiResponse<AuthnAccountResponseData> account(
55 @PathVariable("username") String username) {
57 if (username == null || username.length() == 0) {
58 throw new RuntimeException("exception.get.username.must.not.empty");
61 Account account = accountService.selectByUsername(username);
63 if (account == null) {
64 throw new RuntimeException("exception.get.account.not.exist");
67 AuthnAccountResponseData data = AuthnAccountResponseData.of(account);
69 return new DefaultApiResponse<AuthnAccountResponseData>(data);
73 @GetMapping(path = "/{username}/roles", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
74 @ResponseStatus(value = HttpStatus.OK)
76 public DefaultApiResponse<AuthnAccountRolesResponseData> roles(
77 @PathVariable("username") String username) {
79 if (username == null || username.length() == 0) {
80 throw new RuntimeException("exception.get.username.must.not.empty");
83 Account account = accountService.selectByUsername(username);
85 if (account == null) {
86 throw new RuntimeException("exception.get.account.not.exist");
89 List<Role> roles = roleService.selectByUsername(username);
91 AuthnAccountRolesResponseData data = AuthnAccountRolesResponseData.of(roles);
93 return new DefaultApiResponse<AuthnAccountRolesResponseData>(data);
96 @GetMapping(path = "/{username}/applications", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
97 @ResponseStatus(value = HttpStatus.OK)
99 public DefaultApiResponse<AuthnAccountPermissionsResponseData> applications(
100 @PathVariable("username") String username,
101 @RequestParam(name = "applicationId", required = false) String applicationId) {
103 if (username == null || username.length() == 0) {
104 throw new RuntimeException("exception.get.username.must.not.empty");
107 Account account = accountService.selectByUsername(username);
109 if (account == null) {
110 throw new RuntimeException("exception.get.account.not.exist");
113 List<Permission> applications = permissionService.selectByUsername(username, null, Permission.TYPE_APPLICATION);
115 AuthnAccountPermissionsResponseData data = AuthnAccountPermissionsResponseData.of(applications);
117 return new DefaultApiResponse<AuthnAccountPermissionsResponseData>(data);
120 @GetMapping(path = "/{username}/menus", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
121 @ResponseStatus(value = HttpStatus.OK)
123 public DefaultApiResponse<AuthnAccountPermissionsResponseData> menus(
124 @PathVariable("username") String username,
125 @RequestParam(name = "applicationId", required = false) String applicationId) {
127 if (username == null || username.length() == 0) {
128 throw new RuntimeException("exception.get.username.must.not.empty");
131 Account account = accountService.selectByUsername(username);
133 if (account == null) {
134 throw new RuntimeException("exception.get.account.not.exist");
137 List<Permission> menus = permissionService.selectByUsername(username, applicationId, Permission.TYPE_MENU);
139 AuthnAccountPermissionsResponseData data = AuthnAccountPermissionsResponseData.of(menus);
141 return new DefaultApiResponse<AuthnAccountPermissionsResponseData>(data);
144 @GetMapping(path = "/{username}/operations", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
145 @ResponseStatus(value = HttpStatus.OK)
147 public DefaultApiResponse<AuthnAccountPermissionsResponseData> operations(
148 @PathVariable("username") String username,
149 @RequestParam(name = "applicationId", required = false) String applicationId) {
151 if (username == null || username.length() == 0) {
152 throw new RuntimeException("exception.get.username.must.not.empty");
155 Account account = accountService.selectByUsername(username);
157 if (account == null) {
158 throw new RuntimeException("exception.get.account.not.exist");
161 List<Permission> operations = permissionService.selectByUsername(username, applicationId, Permission.TYPE_OPERATION);
163 AuthnAccountPermissionsResponseData data = AuthnAccountPermissionsResponseData.of(operations);
165 return new DefaultApiResponse<AuthnAccountPermissionsResponseData>(data);
168 @GetMapping(path = "/{username}/resources", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
169 @ResponseStatus(value = HttpStatus.OK)
171 public DefaultApiResponse<AuthnAccountResourcesResponseData> resources(
172 @PathVariable("username") String username,
173 @RequestParam(name = "applicationId", required = false) String applicationId) {
175 if (username == null || username.length() == 0) {
176 throw new RuntimeException("exception.get.username.must.not.empty");
179 Account account = accountService.selectByUsername(username);
181 if (account == null) {
182 throw new RuntimeException("exception.get.account.not.exist");
185 List<Resource> resources = null;// FIXME: resourceService.selectByUsername(username, applicationId);
187 AuthnAccountResourcesResponseData data = AuthnAccountResourcesResponseData.of(resources);
189 return new DefaultApiResponse<AuthnAccountResourcesResponseData>(data);