66 */
77namespace OC \Collaboration \Collaborators ;
88
9+ use OCA \Federation \TrustedServers ;
10+ use OCA \Files_Sharing \Config \ConfigLexicon ;
911use OCP \Collaboration \Collaborators \ISearchPlugin ;
1012use OCP \Collaboration \Collaborators \ISearchResult ;
1113use OCP \Collaboration \Collaborators \SearchResultType ;
1214use OCP \Contacts \IManager ;
1315use OCP \Federation \ICloudIdManager ;
16+ use OCP \IAppConfig ;
1417use OCP \IConfig ;
1518use OCP \IUserManager ;
1619use OCP \IUserSession ;
@@ -27,11 +30,45 @@ public function __construct(
2730 private IConfig $ config ,
2831 private IUserManager $ userManager ,
2932 IUserSession $ userSession ,
33+ private ?IAppConfig $ appConfig = null ,
34+ private ?TrustedServers $ trustedServers = null ,
3035 ) {
3136 $ this ->userId = $ userSession ->getUser ()?->getUID() ?? '' ;
3237 $ this ->shareeEnumeration = $ this ->config ->getAppValue ('core ' , 'shareapi_allow_share_dialog_user_enumeration ' , 'yes ' ) === 'yes ' ;
3338 }
3439
40+ private function shouldOnlyShowTrustedServers (): bool {
41+ if ($ this ->appConfig === null ) {
42+ return false ;
43+ }
44+
45+ $ showFederatedToTrustedAsInternal = $ this ->appConfig ->getValueBool (
46+ 'files_sharing ' ,
47+ ConfigLexicon::SHOW_FEDERATED_TO_TRUSTED_AS_INTERNAL ,
48+ false
49+ );
50+ $ showFederatedAsInternal = $ this ->appConfig ->getValueBool (
51+ 'files_sharing ' ,
52+ ConfigLexicon::SHOW_FEDERATED_AS_INTERNAL ,
53+ false
54+ );
55+
56+ return $ showFederatedToTrustedAsInternal && !$ showFederatedAsInternal ;
57+ }
58+
59+ private function isServerTrusted (string $ serverUrl ): bool {
60+ if ($ this ->trustedServers === null ) {
61+ return true ;
62+ }
63+
64+ $ normalizedUrl = $ serverUrl ;
65+ if (!str_contains ($ normalizedUrl , ':// ' )) {
66+ $ normalizedUrl = 'https:// ' . $ normalizedUrl ;
67+ }
68+
69+ return $ this ->trustedServers ->isTrustedServer ($ normalizedUrl );
70+ }
71+
3572 public function search ($ search , $ limit , $ offset , ISearchResult $ searchResult ): bool {
3673 $ result = ['wide ' => [], 'exact ' => []];
3774 $ resultType = new SearchResultType ('remotes ' );
@@ -82,33 +119,37 @@ public function search($search, $limit, $offset, ISearchResult $searchResult): b
82119 ];
83120 }
84121
85- if (strtolower ($ contact ['FN ' ]) === $ lowerSearch || strtolower ($ cloudId ) === $ lowerSearch ) {
86- if (strtolower ($ cloudId ) === $ lowerSearch ) {
87- $ searchResult ->markExactIdMatch ($ resultType );
122+ $ shouldFilter = $ this ->shouldOnlyShowTrustedServers ();
123+
124+ if (!$ shouldFilter || $ this ->isServerTrusted ($ serverUrl )) {
125+ if (strtolower ($ contact ['FN ' ]) === $ lowerSearch || strtolower ($ cloudId ) === $ lowerSearch ) {
126+ if (strtolower ($ cloudId ) === $ lowerSearch ) {
127+ $ searchResult ->markExactIdMatch ($ resultType );
128+ }
129+ $ result ['exact ' ][] = [
130+ 'label ' => $ contact ['FN ' ] . " ( $ cloudId) " ,
131+ 'uuid ' => $ contact ['UID ' ],
132+ 'name ' => $ contact ['FN ' ],
133+ 'type ' => $ cloudIdType ,
134+ 'value ' => [
135+ 'shareType ' => IShare::TYPE_REMOTE ,
136+ 'shareWith ' => $ cloudId ,
137+ 'server ' => $ serverUrl ,
138+ ],
139+ ];
140+ } else {
141+ $ result ['wide ' ][] = [
142+ 'label ' => $ contact ['FN ' ] . " ( $ cloudId) " ,
143+ 'uuid ' => $ contact ['UID ' ],
144+ 'name ' => $ contact ['FN ' ],
145+ 'type ' => $ cloudIdType ,
146+ 'value ' => [
147+ 'shareType ' => IShare::TYPE_REMOTE ,
148+ 'shareWith ' => $ cloudId ,
149+ 'server ' => $ serverUrl ,
150+ ],
151+ ];
88152 }
89- $ result ['exact ' ][] = [
90- 'label ' => $ contact ['FN ' ] . " ( $ cloudId) " ,
91- 'uuid ' => $ contact ['UID ' ],
92- 'name ' => $ contact ['FN ' ],
93- 'type ' => $ cloudIdType ,
94- 'value ' => [
95- 'shareType ' => IShare::TYPE_REMOTE ,
96- 'shareWith ' => $ cloudId ,
97- 'server ' => $ serverUrl ,
98- ],
99- ];
100- } else {
101- $ result ['wide ' ][] = [
102- 'label ' => $ contact ['FN ' ] . " ( $ cloudId) " ,
103- 'uuid ' => $ contact ['UID ' ],
104- 'name ' => $ contact ['FN ' ],
105- 'type ' => $ cloudIdType ,
106- 'value ' => [
107- 'shareType ' => IShare::TYPE_REMOTE ,
108- 'shareWith ' => $ cloudId ,
109- 'server ' => $ serverUrl ,
110- ],
111- ];
112153 }
113154 }
114155 }
@@ -120,24 +161,25 @@ public function search($search, $limit, $offset, ISearchResult $searchResult): b
120161 $ result ['wide ' ] = array_slice ($ result ['wide ' ], $ offset , $ limit );
121162 }
122163
123- /**
124- * Add generic share with remote item for valid cloud ids that are not users of the local instance
125- */
126164 if (!$ searchResult ->hasExactIdMatch ($ resultType ) && $ this ->cloudIdManager ->isValidCloudId ($ search ) && $ offset === 0 ) {
127165 try {
128166 [$ remoteUser , $ serverUrl ] = $ this ->splitUserRemote ($ search );
129167 $ localUser = $ this ->userManager ->get ($ remoteUser );
130168 if ($ localUser === null || $ search !== $ localUser ->getCloudId ()) {
131- $ result ['exact ' ][] = [
132- 'label ' => $ remoteUser . " ( $ serverUrl) " ,
133- 'uuid ' => $ remoteUser ,
134- 'name ' => $ remoteUser ,
135- 'value ' => [
136- 'shareType ' => IShare::TYPE_REMOTE ,
137- 'shareWith ' => $ search ,
138- 'server ' => $ serverUrl ,
139- ],
140- ];
169+ $ shouldFilter = $ this ->shouldOnlyShowTrustedServers ();
170+
171+ if (!$ shouldFilter || $ this ->isServerTrusted ($ serverUrl )) {
172+ $ result ['exact ' ][] = [
173+ 'label ' => $ remoteUser . " ( $ serverUrl) " ,
174+ 'uuid ' => $ remoteUser ,
175+ 'name ' => $ remoteUser ,
176+ 'value ' => [
177+ 'shareType ' => IShare::TYPE_REMOTE ,
178+ 'shareWith ' => $ search ,
179+ 'server ' => $ serverUrl ,
180+ ],
181+ ];
182+ }
141183 }
142184 } catch (\InvalidArgumentException $ e ) {
143185 }
0 commit comments