44import com .halolight .domain .entity .DocumentShare ;
55import com .halolight .domain .entity .DocumentTag ;
66import com .halolight .domain .entity .Tag ;
7+ import com .halolight .domain .entity .TeamMember ;
78import com .halolight .domain .entity .User ;
89import com .halolight .domain .entity .id .DocumentTagId ;
10+ import com .halolight .domain .entity .id .TeamMemberId ;
911import com .halolight .domain .repository .DocumentRepository ;
1012import com .halolight .domain .repository .DocumentShareRepository ;
1113import com .halolight .domain .repository .TagRepository ;
14+ import com .halolight .domain .repository .TeamMemberRepository ;
1215import com .halolight .domain .repository .UserRepository ;
1316import com .halolight .dto .UserDTO ;
1417import com .halolight .dto .UserMapper ;
@@ -43,6 +46,7 @@ public class DocumentService {
4346 private final DocumentShareRepository documentShareRepository ;
4447 private final TagRepository tagRepository ;
4548 private final UserRepository userRepository ;
49+ private final TeamMemberRepository teamMemberRepository ;
4650 private final UserMapper userMapper ;
4751
4852 /**
@@ -430,14 +434,26 @@ private boolean hasAccess(Document document, String userId) {
430434 return true ;
431435 }
432436
433- // Check if document is shared with user
437+ // Check if document is shared directly with user
434438 if (documentShareRepository .existsByDocumentIdAndSharedWithId (document .getId (), userId )) {
435439 return true ;
436440 }
437441
438- // Check if document is shared with user's teams
439- // TODO: Implement team membership check when TeamMember repository is available
440- // For now, assuming team sharing is handled separately
442+ // Check if document is shared with any of user's teams
443+ List <DocumentShare > teamShares = documentShareRepository .findByDocumentId (document .getId ()).stream ()
444+ .filter (share -> share .getTeamId () != null )
445+ .collect (Collectors .toList ());
446+
447+ for (DocumentShare share : teamShares ) {
448+ // Check if user is a member of this team
449+ TeamMemberId teamMemberId = new TeamMemberId ();
450+ teamMemberId .setTeamId (share .getTeamId ());
451+ teamMemberId .setUserId (userId );
452+
453+ if (teamMemberRepository .existsById (teamMemberId )) {
454+ return true ;
455+ }
456+ }
441457
442458 return false ;
443459 }
@@ -455,9 +471,28 @@ private boolean hasEditPermission(Document document, String userId) {
455471 return true ;
456472 }
457473
458- // Check if user has EDIT permission via share
459- return documentShareRepository .findByDocumentIdAndSharedWithId (document .getId (), userId )
460- .map (share -> share .getPermission () == com .halolight .domain .entity .enums .SharePermission .EDIT )
461- .orElse (false );
474+ // Check if user has EDIT permission via direct share
475+ var directShare = documentShareRepository .findByDocumentIdAndSharedWithId (document .getId (), userId );
476+ if (directShare .isPresent () && directShare .get ().getPermission () == com .halolight .domain .entity .enums .SharePermission .EDIT ) {
477+ return true ;
478+ }
479+
480+ // Check if user has EDIT permission via team share
481+ List <DocumentShare > teamShares = documentShareRepository .findByDocumentId (document .getId ()).stream ()
482+ .filter (share -> share .getTeamId () != null && share .getPermission () == com .halolight .domain .entity .enums .SharePermission .EDIT )
483+ .collect (Collectors .toList ());
484+
485+ for (DocumentShare share : teamShares ) {
486+ // Check if user is a member of this team
487+ TeamMemberId teamMemberId = new TeamMemberId ();
488+ teamMemberId .setTeamId (share .getTeamId ());
489+ teamMemberId .setUserId (userId );
490+
491+ if (teamMemberRepository .existsById (teamMemberId )) {
492+ return true ;
493+ }
494+ }
495+
496+ return false ;
462497 }
463498}
0 commit comments