76663d2514680e65f4a1f9fa56abe25981f96af9
[institute/sw-backend.git] /
1 package com.supwisdom.institute.backend.base.api.v1.authn;
2
3 import java.util.List;
4
5 import io.swagger.annotations.Api;
6 import lombok.extern.slf4j.Slf4j;
7
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;
18
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;
32
33 @Api(value = "BaseAuthnAccount", tags = { "BaseAuthnAccount" }, description = "帐号接口(认证、授权用)")
34 @Slf4j
35 @RestController
36 @RequestMapping("/v1/authn")
37 public class AuthnAccountController {
38   
39   @Autowired
40   private AccountService accountService;
41   
42   @Autowired
43   private RoleService roleService;
44   
45   @Autowired
46   private PermissionService permissionService; 
47
48   @Autowired
49   private ResourceService resourceService; 
50
51   @GetMapping(path = "/{username}/account", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
52   @ResponseStatus(value = HttpStatus.OK)
53   @ResponseBody
54   public DefaultApiResponse<AuthnAccountResponseData> account(
55       @PathVariable("username") String username) {
56
57     if (username == null || username.length() == 0) {
58       throw new RuntimeException("exception.get.username.must.not.empty");
59     }
60
61     Account account = accountService.selectByUsername(username);
62
63     if (account == null) {
64       throw new RuntimeException("exception.get.account.not.exist");
65     }
66     
67     AuthnAccountResponseData data = AuthnAccountResponseData.of(account);
68
69     return new DefaultApiResponse<AuthnAccountResponseData>(data);
70   }
71   
72   
73   @GetMapping(path = "/{username}/roles", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
74   @ResponseStatus(value = HttpStatus.OK)
75   @ResponseBody
76   public DefaultApiResponse<AuthnAccountRolesResponseData> roles(
77       @PathVariable("username") String username) {
78
79     if (username == null || username.length() == 0) {
80       throw new RuntimeException("exception.get.username.must.not.empty");
81     }
82
83     Account account = accountService.selectByUsername(username);
84
85     if (account == null) {
86       throw new RuntimeException("exception.get.account.not.exist");
87     }
88     
89     List<Role> roles = roleService.selectByUsername(username);
90     
91     AuthnAccountRolesResponseData data = AuthnAccountRolesResponseData.of(roles);
92
93     return new DefaultApiResponse<AuthnAccountRolesResponseData>(data);
94   }
95
96   @GetMapping(path = "/{username}/applications", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
97   @ResponseStatus(value = HttpStatus.OK)
98   @ResponseBody
99   public DefaultApiResponse<AuthnAccountPermissionsResponseData> applications(
100       @PathVariable("username") String username,
101       @RequestParam(name = "applicationId", required = false) String applicationId) {
102
103     if (username == null || username.length() == 0) {
104       throw new RuntimeException("exception.get.username.must.not.empty");
105     }
106
107     Account account = accountService.selectByUsername(username);
108
109     if (account == null) {
110       throw new RuntimeException("exception.get.account.not.exist");
111     }
112     
113     List<Permission> applications = permissionService.selectByUsername(username, null, Permission.TYPE_APPLICATION);
114     
115     AuthnAccountPermissionsResponseData data = AuthnAccountPermissionsResponseData.of(applications);
116
117     return new DefaultApiResponse<AuthnAccountPermissionsResponseData>(data);
118   }
119
120   @GetMapping(path = "/{username}/menus", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
121   @ResponseStatus(value = HttpStatus.OK)
122   @ResponseBody
123   public DefaultApiResponse<AuthnAccountPermissionsResponseData> menus(
124       @PathVariable("username") String username, 
125       @RequestParam(name = "applicationId", required = false) String applicationId) {
126
127     if (username == null || username.length() == 0) {
128       throw new RuntimeException("exception.get.username.must.not.empty");
129     }
130
131     Account account = accountService.selectByUsername(username);
132
133     if (account == null) {
134       throw new RuntimeException("exception.get.account.not.exist");
135     }
136     
137     List<Permission> menus = permissionService.selectByUsername(username, applicationId, Permission.TYPE_MENU);
138     
139     AuthnAccountPermissionsResponseData data = AuthnAccountPermissionsResponseData.of(menus);
140
141     return new DefaultApiResponse<AuthnAccountPermissionsResponseData>(data);
142   }
143
144   @GetMapping(path = "/{username}/operations", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
145   @ResponseStatus(value = HttpStatus.OK)
146   @ResponseBody
147   public DefaultApiResponse<AuthnAccountPermissionsResponseData> operations(
148       @PathVariable("username") String username, 
149       @RequestParam(name = "applicationId", required = false) String applicationId) {
150
151     if (username == null || username.length() == 0) {
152       throw new RuntimeException("exception.get.username.must.not.empty");
153     }
154
155     Account account = accountService.selectByUsername(username);
156
157     if (account == null) {
158       throw new RuntimeException("exception.get.account.not.exist");
159     }
160     
161     List<Permission> operations = permissionService.selectByUsername(username, applicationId, Permission.TYPE_OPERATION);
162     
163     AuthnAccountPermissionsResponseData data = AuthnAccountPermissionsResponseData.of(operations);
164
165     return new DefaultApiResponse<AuthnAccountPermissionsResponseData>(data);
166   }
167
168   @GetMapping(path = "/{username}/resources", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
169   @ResponseStatus(value = HttpStatus.OK)
170   @ResponseBody
171   public DefaultApiResponse<AuthnAccountResourcesResponseData> resources(
172       @PathVariable("username") String username, 
173       @RequestParam(name = "applicationId", required = false) String applicationId) {
174
175     if (username == null || username.length() == 0) {
176       throw new RuntimeException("exception.get.username.must.not.empty");
177     }
178
179     Account account = accountService.selectByUsername(username);
180
181     if (account == null) {
182       throw new RuntimeException("exception.get.account.not.exist");
183     }
184     
185     List<Resource> resources = null;// FIXME: resourceService.selectByUsername(username, applicationId);
186     
187     AuthnAccountResourcesResponseData data = AuthnAccountResourcesResponseData.of(resources);
188
189     return new DefaultApiResponse<AuthnAccountResourcesResponseData>(data);
190   }
191
192 }