11package io .github .timemachinelab .adapter .web .controller ;
22
3+ import io .github .timemachinelab .adapter .web .auth .ConsoleSessionPrincipal ;
34import io .github .timemachinelab .adapter .web .delegate .ApiAssetWebDelegate ;
45import io .github .timemachinelab .api .req .AttachAiCapabilityProfileReq ;
56import io .github .timemachinelab .api .req .ListApiAssetReq ;
910import io .github .timemachinelab .api .resp .ApiAssetResp ;
1011import io .github .timemachinelab .common .annotation .AutoResp ;
1112import jakarta .validation .Valid ;
13+ import org .springframework .web .bind .annotation .DeleteMapping ;
1214import org .springframework .web .bind .annotation .GetMapping ;
1315import org .springframework .web .bind .annotation .PatchMapping ;
1416import org .springframework .web .bind .annotation .PathVariable ;
1517import org .springframework .web .bind .annotation .PostMapping ;
1618import org .springframework .web .bind .annotation .PutMapping ;
19+ import org .springframework .web .bind .annotation .RequestAttribute ;
1720import org .springframework .web .bind .annotation .RequestBody ;
1821import org .springframework .web .bind .annotation .RequestMapping ;
1922import org .springframework .web .bind .annotation .RestController ;
2023
24+ import java .security .Principal ;
25+
2126/**
22- * API 资产管理 HTTP 接入点。
27+ * Current-user asset workspace HTTP entry point.
2328 */
2429@ RestController
25- @ RequestMapping ("/api/v1/assets" )
30+ @ RequestMapping ("/api/v1/current-user/ assets" )
2631@ AutoResp
2732public class ApiAssetController {
2833
@@ -33,39 +38,118 @@ public ApiAssetController(ApiAssetWebDelegate delegate) {
3338 }
3439
3540 @ GetMapping
36- public ApiAssetPageResp listAssets (@ Valid ListApiAssetReq req ) {
37- return delegate .listAssets (req );
41+ public ApiAssetPageResp listAssets (
42+ @ Valid ListApiAssetReq req ,
43+ @ RequestAttribute (name = ConsoleSessionPrincipal .REQUEST_ATTRIBUTE , required = false )
44+ ConsoleSessionPrincipal consoleSessionPrincipal ,
45+ Principal principal ) {
46+ return delegate .listAssets (currentUserId (consoleSessionPrincipal , principal ), req );
3847 }
3948
4049 @ PostMapping
41- public ApiAssetResp registerAsset (@ Valid @ RequestBody RegisterApiAssetReq req ) {
42- return delegate .registerAsset (req );
50+ public ApiAssetResp registerAsset (
51+ @ Valid @ RequestBody RegisterApiAssetReq req ,
52+ @ RequestAttribute (name = ConsoleSessionPrincipal .REQUEST_ATTRIBUTE , required = false )
53+ ConsoleSessionPrincipal consoleSessionPrincipal ,
54+ Principal principal ) {
55+ return delegate .registerAsset (
56+ currentUserId (consoleSessionPrincipal , principal ),
57+ publisherDisplayName (consoleSessionPrincipal , principal ),
58+ req
59+ );
4360 }
4461
4562 @ GetMapping ("/{apiCode}" )
46- public ApiAssetResp getAssetByCode (@ PathVariable ("apiCode" ) String apiCode ) {
47- return delegate .getAssetByCode (apiCode );
63+ public ApiAssetResp getAssetByCode (
64+ @ PathVariable ("apiCode" ) String apiCode ,
65+ @ RequestAttribute (name = ConsoleSessionPrincipal .REQUEST_ATTRIBUTE , required = false )
66+ ConsoleSessionPrincipal consoleSessionPrincipal ,
67+ Principal principal ) {
68+ return delegate .getAssetByCode (currentUserId (consoleSessionPrincipal , principal ), apiCode );
4869 }
4970
5071 @ PutMapping ("/{apiCode}" )
5172 public ApiAssetResp reviseAsset (
52- @ PathVariable ("apiCode" ) String apiCode , @ Valid @ RequestBody ReviseApiAssetReq req ) {
53- return delegate .reviseAsset (apiCode , req );
73+ @ PathVariable ("apiCode" ) String apiCode ,
74+ @ Valid @ RequestBody ReviseApiAssetReq req ,
75+ @ RequestAttribute (name = ConsoleSessionPrincipal .REQUEST_ATTRIBUTE , required = false )
76+ ConsoleSessionPrincipal consoleSessionPrincipal ,
77+ Principal principal ) {
78+ return delegate .reviseAsset (
79+ currentUserId (consoleSessionPrincipal , principal ),
80+ publisherDisplayName (consoleSessionPrincipal , principal ),
81+ apiCode ,
82+ req
83+ );
84+ }
85+
86+ @ PatchMapping ("/{apiCode}/publish" )
87+ public ApiAssetResp publishAsset (
88+ @ PathVariable ("apiCode" ) String apiCode ,
89+ @ RequestAttribute (name = ConsoleSessionPrincipal .REQUEST_ATTRIBUTE , required = false )
90+ ConsoleSessionPrincipal consoleSessionPrincipal ,
91+ Principal principal ) {
92+ return delegate .publishAsset (
93+ currentUserId (consoleSessionPrincipal , principal ),
94+ publisherDisplayName (consoleSessionPrincipal , principal ),
95+ apiCode
96+ );
5497 }
5598
56- @ PatchMapping ("/{apiCode}/enable" )
57- public ApiAssetResp enableAsset (@ PathVariable ("apiCode" ) String apiCode ) {
58- return delegate .enableAsset (apiCode );
99+ @ PatchMapping ("/{apiCode}/unpublish" )
100+ public ApiAssetResp unpublishAsset (
101+ @ PathVariable ("apiCode" ) String apiCode ,
102+ @ RequestAttribute (name = ConsoleSessionPrincipal .REQUEST_ATTRIBUTE , required = false )
103+ ConsoleSessionPrincipal consoleSessionPrincipal ,
104+ Principal principal ) {
105+ return delegate .unpublishAsset (currentUserId (consoleSessionPrincipal , principal ), apiCode );
59106 }
60107
61- @ PatchMapping ("/{apiCode}/disable" )
62- public ApiAssetResp disableAsset (@ PathVariable ("apiCode" ) String apiCode ) {
63- return delegate .disableAsset (apiCode );
108+ @ DeleteMapping ("/{apiCode}" )
109+ public ApiAssetResp deleteAsset (
110+ @ PathVariable ("apiCode" ) String apiCode ,
111+ @ RequestAttribute (name = ConsoleSessionPrincipal .REQUEST_ATTRIBUTE , required = false )
112+ ConsoleSessionPrincipal consoleSessionPrincipal ,
113+ Principal principal ) {
114+ return delegate .deleteAsset (currentUserId (consoleSessionPrincipal , principal ), apiCode );
64115 }
65116
66117 @ PutMapping ("/{apiCode}/ai-profile" )
67118 public ApiAssetResp attachAiCapabilityProfile (
68- @ PathVariable ("apiCode" ) String apiCode , @ Valid @ RequestBody AttachAiCapabilityProfileReq req ) {
69- return delegate .attachAiCapabilityProfile (apiCode , req );
119+ @ PathVariable ("apiCode" ) String apiCode ,
120+ @ Valid @ RequestBody AttachAiCapabilityProfileReq req ,
121+ @ RequestAttribute (name = ConsoleSessionPrincipal .REQUEST_ATTRIBUTE , required = false )
122+ ConsoleSessionPrincipal consoleSessionPrincipal ,
123+ Principal principal ) {
124+ return delegate .attachAiCapabilityProfile (
125+ currentUserId (consoleSessionPrincipal , principal ),
126+ publisherDisplayName (consoleSessionPrincipal , principal ),
127+ apiCode ,
128+ req
129+ );
130+ }
131+
132+ private String currentUserId (ConsoleSessionPrincipal consoleSessionPrincipal , Principal principal ) {
133+ if (consoleSessionPrincipal != null
134+ && consoleSessionPrincipal .getUserId () != null
135+ && !consoleSessionPrincipal .getUserId ().isBlank ()) {
136+ return consoleSessionPrincipal .getUserId ();
137+ }
138+ if (principal == null || principal .getName () == null || principal .getName ().isBlank ()) {
139+ throw new IllegalArgumentException ("Current user id must not be blank" );
140+ }
141+ return principal .getName ();
142+ }
143+
144+ private String publisherDisplayName (ConsoleSessionPrincipal consoleSessionPrincipal , Principal principal ) {
145+ if (consoleSessionPrincipal != null ) {
146+ if (consoleSessionPrincipal .getDisplayName () != null && !consoleSessionPrincipal .getDisplayName ().isBlank ()) {
147+ return consoleSessionPrincipal .getDisplayName ();
148+ }
149+ if (consoleSessionPrincipal .getLoginName () != null && !consoleSessionPrincipal .getLoginName ().isBlank ()) {
150+ return consoleSessionPrincipal .getLoginName ();
151+ }
152+ }
153+ return currentUserId (consoleSessionPrincipal , principal );
70154 }
71155}
0 commit comments