@@ -80,21 +80,31 @@ public ResponseEntity<ResponseMessage<?>> createAward(@RequestBody AwardExperien
8080 // 未指定用户ID - 拒绝访问
8181 logger .warn ("创建获奖经历时未指定用户ID,操作者用户ID: {}" , currentUser .getUserId ());
8282 throw new BusinessException (BusinessExceptionEnum .AUTHENTICATION_FAILED );
83- } else if (PermissionUtils .hasPermission (currentUser , "award:manage" )) {
84- // 管理员:验证目标用户是否存在
85- User targetUser = userService .getUserById (targetUserId );
86- if (targetUser == null ) {
87- logger .warn ("管理员尝试为不存在的用户创建获奖经历,目标用户ID: {}" , targetUserId );
88- throw new BusinessException (BusinessExceptionEnum .USER_NOT_FOUND );
89- }
90- logger .debug ("管理员为用户ID为{}的用户创建获奖经历" , targetUserId );
9183 } else {
92- // 普通用户:只能为自己创建获奖经历
93- if (!targetUserId .equals (currentUser .getUserId ())) {
94- logger .warn ("用户ID为{}的用户尝试为其他用户创建获奖经历,目标用户ID: {}" , currentUser .getUserId (), targetUserId );
95- throw new BusinessException (BusinessExceptionEnum .AUTHENTICATION_FAILED );
84+ // 检查当前用户是否具有 award:manage 权限
85+ boolean hasManagePermission = false ;
86+ if (SecurityContextHolder .getContext ().getAuthentication () != null ) {
87+ hasManagePermission = SecurityContextHolder .getContext ().getAuthentication ()
88+ .getAuthorities ().stream ()
89+ .anyMatch (auth -> "award:manage" .equals (auth .getAuthority ()));
90+ }
91+
92+ if (hasManagePermission ) {
93+ // 管理员:验证目标用户是否存在
94+ User targetUser = userService .getUserById (targetUserId );
95+ if (targetUser == null ) {
96+ logger .warn ("管理员尝试为不存在的用户创建获奖经历,目标用户ID: {}" , targetUserId );
97+ throw new BusinessException (BusinessExceptionEnum .USER_NOT_FOUND );
98+ }
99+ logger .debug ("管理员为用户ID为{}的用户创建获奖经历" , targetUserId );
100+ } else {
101+ // 普通用户:只能为自己创建获奖经历
102+ if (!targetUserId .equals (currentUser .getUserId ())) {
103+ logger .warn ("用户ID为{}的用户尝试为其他用户创建获奖经历,目标用户ID: {}" , currentUser .getUserId (), targetUserId );
104+ throw new BusinessException (BusinessExceptionEnum .AUTHENTICATION_FAILED );
105+ }
106+ logger .debug ("用户为自己创建获奖经历,用户ID: {}" , currentUser .getUserId ());
96107 }
97- logger .debug ("用户为自己创建获奖经历,用户ID: {}" , currentUser .getUserId ());
98108 }
99109
100110 // 设置获奖经历的用户ID
@@ -151,7 +161,11 @@ public ResponseEntity<ResponseMessage<?>> getAwardById(@PathVariable Integer id)
151161 }
152162
153163 // 权限检查:管理员可以查看所有,普通用户只能查看自己的
154- if (!PermissionUtils .hasPermission (currentUser , "award:manage" )) {
164+ boolean hasManagePermission = SecurityContextHolder .getContext ().getAuthentication ()
165+ .getAuthorities ().stream ()
166+ .anyMatch (auth -> "award:manage" .equals (auth .getAuthority ()));
167+
168+ if (!hasManagePermission ) {
155169 if (!award .getUserId ().equals (currentUser .getUserId ())) {
156170 logger .warn ("用户ID为{}的用户尝试查看其他用户的获奖经历,目标用户ID: {}, 获奖ID: {}" ,
157171 currentUser .getUserId (), award .getUserId (), id );
@@ -197,7 +211,11 @@ public ResponseEntity<ResponseMessage<?>> getAwardsByUserId(@PathVariable Intege
197211 User currentUser = userService .getUserByUsername (username );
198212
199213 // 权限检查:管理员可以查看所有,普通用户只能查看自己的
200- if (!PermissionUtils .hasPermission (currentUser , "award:manage" )) {
214+ boolean hasManagePermission = SecurityContextHolder .getContext ().getAuthentication ()
215+ .getAuthorities ().stream ()
216+ .anyMatch (auth -> "award:manage" .equals (auth .getAuthority ()));
217+
218+ if (!hasManagePermission ) {
201219 if (!userId .equals (currentUser .getUserId ())) {
202220 logger .warn ("用户ID为{}的用户尝试查看其他用户的获奖经历,目标用户ID: {}" , currentUser .getUserId (), userId );
203221 throw new BusinessException (BusinessExceptionEnum .PERMISSION_DENIED );
@@ -254,7 +272,11 @@ public ResponseEntity<ResponseMessage<?>> updateAward(@RequestBody AwardExperien
254272 }
255273
256274 // 权限检查:管理员可以修改所有人的,普通用户只能修改自己的
257- if (!PermissionUtils .hasPermission (currentUser , "award:manage" )) {
275+ boolean hasManagePermission = SecurityContextHolder .getContext ().getAuthentication ()
276+ .getAuthorities ().stream ()
277+ .anyMatch (auth -> "award:manage" .equals (auth .getAuthority ()));
278+
279+ if (!hasManagePermission ) {
258280 if (!originalAward .getUserId ().equals (currentUserId )) {
259281 logger .warn ("用户ID为{}的用户尝试更新其他用户的获奖经历,目标用户ID: {}, 获奖ID: {}" ,
260282 currentUserId , originalAward .getUserId (), awardExperience .getAwardId ());
0 commit comments