99namespace OCA \Libresign \Controller ;
1010
1111use OCA \Libresign \AppInfo \Application ;
12+ use OCA \Libresign \Collaboration \Collaborators \SignerPlugin ;
1213use OCA \Libresign \Middleware \Attribute \RequireManager ;
1314use OCA \Libresign \ResponseDefinitions ;
1415use OCA \Libresign \Service \IdentifyMethod \Account ;
@@ -45,6 +46,7 @@ public function __construct(
4546 * Used to identify who can sign the document. The return of this endpoint is related with Administration Settiongs > LibreSign > Identify method.
4647 *
4748 * @param string $search search params
49+ * @param string $method filter by method (email, account, sms, signal, telegram, whatsapp, xmpp)
4850 * @param int $page the number of page to return. Default: 1
4951 * @param int $limit Total of elements to return. Default: 25
5052 * @return DataResponse<Http::STATUS_OK, LibresignIdentifyAccount[], array{}>
@@ -55,29 +57,43 @@ public function __construct(
5557 #[NoAdminRequired]
5658 #[RequireManager]
5759 #[ApiRoute(verb: 'GET ' , url: '/api/{apiVersion}/identify-account/search ' , requirements: ['apiVersion ' => '(v1) ' ])]
58- public function search (string $ search = '' , int $ page = 1 , int $ limit = 25 ): DataResponse {
59- $ shareTypes = $ this ->getShareTypes ();
60- $ lookup = false ;
61-
62- // only search for string larger than a given threshold
63- $ threshold = 1 ;
64- if (strlen ($ search ) < $ threshold ) {
60+ public function search (string $ search = '' , string $ method = '' , int $ page = 1 , int $ limit = 25 ): DataResponse {
61+ // only search for string larger than a minimum length
62+ if (strlen ($ search ) < 1 ) {
6563 return new DataResponse ();
6664 }
6765
66+ $ shareTypes = $ this ->getShareTypes ();
67+ $ lookup = false ;
68+
6869 $ offset = $ limit * ($ page - 1 );
70+ $ this ->registerPlugin ($ method );
6971 [$ result ] = $ this ->collaboratorSearch ->search ($ search , $ shareTypes , $ lookup , $ limit , $ offset );
7072 $ result ['exact ' ] = $ this ->unifyResult ($ result ['exact ' ]);
7173 $ result = $ this ->unifyResult ($ result );
7274 $ result = $ this ->excludeEmptyShareWith ($ result );
7375 $ return = $ this ->formatForNcSelect ($ result );
7476 $ return = $ this ->addHerselfAccount ($ return , $ search );
7577 $ return = $ this ->addHerselfEmail ($ return , $ search );
78+ $ return = $ this ->replaceShareTypeByMethod ($ return );
7679 $ return = $ this ->excludeNotAllowed ($ return );
7780
7881 return new DataResponse ($ return );
7982 }
8083
84+ private function registerPlugin (string $ method ): void {
85+ SignerPlugin::setMethod ($ method );
86+
87+ $ refObject = new \ReflectionObject ($ this ->collaboratorSearch );
88+ $ refProperty = $ refObject ->getProperty ('pluginList ' );
89+ $ refProperty ->setAccessible (true );
90+
91+ $ plugins = $ refProperty ->getValue ($ this ->collaboratorSearch );
92+ $ plugins [SignerPlugin::TYPE_SIGNER ] = [SignerPlugin::class];
93+
94+ $ refProperty ->setValue ($ this ->collaboratorSearch , $ plugins );
95+ }
96+
8197 private function getShareTypes (): array {
8298 if (count ($ this ->shareTypes ) > 0 ) {
8399 return $ this ->shareTypes ;
@@ -90,6 +106,8 @@ private function getShareTypes(): array {
90106 if ($ settings ['enabled ' ]) {
91107 $ this ->shareTypes [] = IShare::TYPE_USER ;
92108 }
109+
110+ $ this ->shareTypes [] = SignerPlugin::TYPE_SIGNER ;
93111 return $ this ->shareTypes ;
94112 }
95113
@@ -109,21 +127,35 @@ private function unifyResult(array $list): array {
109127 }
110128
111129 private function formatForNcSelect (array $ list ): array {
130+ $ formattedList = [];
112131 foreach ($ list as $ key => $ item ) {
113- $ list [$ key ] = [
132+ $ formattedList [$ key ] = [
114133 'id ' => $ item ['value ' ]['shareWith ' ],
115- 'isNoUser ' => $ item ['value ' ]['shareType ' ] !== IShare::TYPE_USER ,
134+ 'isNoUser ' => $ item ['value ' ]['shareType ' ] !== IShare::TYPE_USER
135+ && isset ($ item ['method ' ])
136+ && $ item ['method ' ] !== 'account ' ,
116137 'displayName ' => $ item ['label ' ],
117138 'subname ' => $ item ['shareWithDisplayNameUnique ' ] ?? '' ,
118- 'shareType ' => $ item ['value ' ]['shareType ' ],
119139 ];
120140 if ($ item ['value ' ]['shareType ' ] === IShare::TYPE_EMAIL ) {
121- $ list [$ key ]['icon ' ] = 'icon-mail ' ;
141+ $ formattedList [$ key ]['method ' ] = 'email ' ;
142+ $ formattedList [$ key ]['icon ' ] = 'icon-mail ' ;
122143 } elseif ($ item ['value ' ]['shareType ' ] === IShare::TYPE_USER ) {
123- $ list [$ key ]['icon ' ] = 'icon-user ' ;
144+ $ formattedList [$ key ]['method ' ] = 'account ' ;
145+ $ formattedList [$ key ]['icon ' ] = 'icon-user ' ;
146+ } elseif ($ item ['value ' ]['shareType ' ] === SignerPlugin::TYPE_SIGNER ) {
147+ $ formattedList [$ key ]['method ' ] = $ item ['method ' ] ?? '' ;
148+ if ($ item ['method ' ] === 'email ' ) {
149+ $ formattedList [$ key ]['icon ' ] = 'icon-mail ' ;
150+ } elseif ($ item ['method ' ] === 'account ' ) {
151+ $ formattedList [$ key ]['icon ' ] = 'icon-user ' ;
152+ } else {
153+ $ formattedList [$ key ]['iconSvg ' ] = 'svg ' . ucfirst ($ item ['method ' ]);
154+ $ formattedList [$ key ]['iconName ' ] = $ item ['method ' ];
155+ }
124156 }
125157 }
126- return $ list ;
158+ return $ formattedList ;
127159 }
128160
129161 private function addHerselfAccount (array $ return , string $ search ): array {
@@ -132,7 +164,14 @@ private function addHerselfAccount(array $return, string $search): array {
132164 return $ return ;
133165 }
134166 $ user = $ this ->userSession ->getUser ();
135- if (!str_contains ($ user ->getUID (), $ search ) && !str_contains (strtolower ($ user ->getDisplayName ()), $ search )) {
167+ $ search = strtolower ($ search );
168+ if (!str_contains ($ user ->getUID (), $ search )
169+ && !str_contains (strtolower ($ user ->getDisplayName ()), $ search )
170+ && (
171+ $ user ->getEMailAddress () === null
172+ || ($ user ->getEMailAddress () !== null && !str_contains ($ user ->getEMailAddress (), $ search ))
173+ )
174+ ) {
136175 return $ return ;
137176 }
138177 $ filtered = array_filter ($ return , fn ($ i ) => $ i ['id ' ] === $ user ->getUID ());
@@ -145,7 +184,7 @@ private function addHerselfAccount(array $return, string $search): array {
145184 'displayName ' => $ user ->getDisplayName (),
146185 'subname ' => $ user ->getEMailAddress (),
147186 'icon ' => 'icon-user ' ,
148- 'shareType ' => IShare:: TYPE_USER ,
187+ 'method ' => ' account ' ,
149188 ];
150189 return $ return ;
151190 }
@@ -159,7 +198,9 @@ private function addHerselfEmail(array $return, string $search): array {
159198 if (empty ($ user ->getEMailAddress ())) {
160199 return $ return ;
161200 }
162- if (!str_contains ($ user ->getEMailAddress (), $ search ) && !str_contains ($ user ->getDisplayName (), $ search )) {
201+ if (!str_contains ($ user ->getEMailAddress (), $ search )
202+ && !str_contains ($ user ->getDisplayName (), $ search )
203+ ) {
163204 return $ return ;
164205 }
165206 $ filtered = array_filter ($ return , fn ($ i ) => $ i ['id ' ] === $ user ->getUID ());
@@ -172,7 +213,7 @@ private function addHerselfEmail(array $return, string $search): array {
172213 'displayName ' => $ user ->getDisplayName (),
173214 'subname ' => $ user ->getEMailAddress (),
174215 'icon ' => 'icon-mail ' ,
175- 'shareType ' => IShare:: TYPE_EMAIL ,
216+ 'method ' => ' email ' ,
176217 ];
177218 return $ return ;
178219 }
@@ -182,7 +223,21 @@ private function excludeEmptyShareWith(array $list): array {
182223 }
183224
184225 private function excludeNotAllowed (array $ list ): array {
185- $ shareTypes = $ this ->getShareTypes ();
186- return array_filter ($ list , fn ($ result ) => in_array ($ result ['shareType ' ], $ shareTypes ));
226+ return array_filter ($ list , fn ($ result ) => isset ($ result ['method ' ]) && !empty ($ result ['method ' ]));
227+ }
228+
229+ private function replaceShareTypeByMethod (array $ list ): array {
230+ foreach ($ list as $ key => $ item ) {
231+ if (isset ($ item ['method ' ]) && !empty ($ item ['method ' ])) {
232+ continue ;
233+ }
234+ $ list [$ key ]['method ' ] = match ($ item ['shareType ' ]) {
235+ IShare::TYPE_EMAIL => 'email ' ,
236+ IShare::TYPE_USER => 'account ' ,
237+ default => '' ,
238+ };
239+ unset($ list [$ key ]['shareType ' ]);
240+ }
241+ return $ list ;
187242 }
188243}
0 commit comments