6
6
7
7
namespace Chamilo \CoreBundle \Security \Authorization \Voter ;
8
8
9
+ use Chamilo \CoreBundle \Entity \Message ;
9
10
use Chamilo \CoreBundle \Entity \User ;
10
11
use Chamilo \CoreBundle \Entity \UserRelUser ;
12
+ use Doctrine \ORM \EntityManagerInterface ;
11
13
use Symfony \Bundle \SecurityBundle \Security ;
12
14
use Symfony \Component \Security \Core \Authentication \Token \TokenInterface ;
13
15
use Symfony \Component \Security \Core \Authorization \Voter \Voter ;
@@ -24,7 +26,8 @@ class UserVoter extends Voter
24
26
public const DELETE = 'DELETE ' ;
25
27
26
28
public function __construct (
27
- private Security $ security
29
+ private Security $ security ,
30
+ private EntityManagerInterface $ entityManager
28
31
) {}
29
32
30
33
protected function supports (string $ attribute , $ subject ): bool
@@ -46,10 +49,10 @@ protected function supports(string $attribute, $subject): bool
46
49
47
50
protected function voteOnAttribute (string $ attribute , $ subject , TokenInterface $ token ): bool
48
51
{
49
- /** @var User $currentUSer */
50
- $ currentUSer = $ token ->getUser ();
52
+ /** @var User $currentUser */
53
+ $ currentUser = $ token ->getUser ();
51
54
52
- if (!$ currentUSer instanceof UserInterface) {
55
+ if (!$ currentUser instanceof UserInterface) {
53
56
return false ;
54
57
}
55
58
@@ -61,27 +64,38 @@ protected function voteOnAttribute(string $attribute, $subject, TokenInterface $
61
64
$ user = $ subject ;
62
65
63
66
if (self ::VIEW === $ attribute ) {
64
- if ($ currentUSer === $ user ) {
67
+ if ($ currentUser === $ user ) {
65
68
return true ;
66
69
}
67
70
68
- if ($ user ->hasFriendWithRelationType ($ currentUSer , UserRelUser::USER_RELATION_TYPE_FRIEND )) {
71
+ if ($ user ->hasFriendWithRelationType ($ currentUser , UserRelUser::USER_RELATION_TYPE_FRIEND )) {
69
72
return true ;
70
73
}
71
74
72
- $ friendsOfFriends = $ currentUSer ->getFriendsOfFriends ();
75
+ $ friendsOfFriends = $ currentUser ->getFriendsOfFriends ();
73
76
if (\in_array ($ user , $ friendsOfFriends , true )) {
74
77
return true ;
75
78
}
76
79
77
80
if (
78
- $ user ->hasFriendWithRelationType ($ currentUSer , UserRelUser::USER_RELATION_TYPE_BOSS )
79
- || $ user ->isFriendWithMeByRelationType ($ currentUSer , UserRelUser::USER_RELATION_TYPE_BOSS )
81
+ $ user ->hasFriendWithRelationType ($ currentUser , UserRelUser::USER_RELATION_TYPE_BOSS )
82
+ || $ user ->isFriendWithMeByRelationType ($ currentUser , UserRelUser::USER_RELATION_TYPE_BOSS )
80
83
) {
81
84
return true ;
82
85
}
86
+
87
+ if ($ this ->haveSharedMessages ($ currentUser , $ user )) {
88
+ return true ;
89
+ }
83
90
}
84
91
85
92
return false ;
86
93
}
94
+
95
+ private function haveSharedMessages (User $ currentUser , User $ targetUser ): bool {
96
+
97
+ $ messageRepository = $ this ->entityManager ->getRepository (Message::class);
98
+
99
+ return $ messageRepository ->usersHaveSharedMessages ($ currentUser , $ targetUser );
100
+ }
87
101
}
0 commit comments