11/*
2+ * Elemental
3+ * Copyright (C) 2024, Evolved Binary Ltd
4+ *
5+ * admin@evolvedbinary.com
6+ * https://www.evolvedbinary.com | https://www.elemental.xyz
7+ *
8+ * This library is free software; you can redistribute it and/or
9+ * modify it under the terms of the GNU Lesser General Public
10+ * License as published by the Free Software Foundation; version 2.1.
11+ *
12+ * This library is distributed in the hope that it will be useful,
13+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+ * Lesser General Public License for more details.
16+ *
17+ * You should have received a copy of the GNU Lesser General Public
18+ * License along with this library; if not, write to the Free Software
19+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+ *
21+ * NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+ * The original license header is included below.
23+ *
24+ * =====================================================================
25+ *
226 * eXist-db Open Source Native XML Database
327 * Copyright (C) 2001 The eXist-db Authors
428 *
2145 */
2246package org .exist .xquery .functions .securitymanager ;
2347
24- import com .evolvedbinary .j8fu .function .Runnable3E ;
2548import org .exist .EXistException ;
2649import org .exist .security .*;
2750import org .exist .security .SecurityManager ;
28- import org .exist .security .internal .aider .GroupAider ;
29- import org .exist .security .internal .aider .UserAider ;
3051import org .exist .storage .BrokerPool ;
3152import org .exist .storage .DBBroker ;
3253import org .exist .storage .txn .Txn ;
3354import org .exist .test .ExistEmbeddedServer ;
3455import org .exist .xquery .XPathException ;
35- import org .exist .xquery .XQuery ;
36- import org .exist .xquery .value .Sequence ;
3756import org .junit .*;
3857
3958import java .util .Optional ;
4059
60+ import static org .exist .xquery .functions .securitymanager .SecurityManagerTestUtil .*;
4161import static org .junit .Assert .*;
4262
4363public class GroupManagementFunctionRemoveGroupTest {
@@ -56,21 +76,21 @@ public class GroupManagementFunctionRemoveGroupTest {
5676 @ Test (expected = PermissionDeniedException .class )
5777 public void cannotDeleteDbaGroup () throws XPathException , PermissionDeniedException , EXistException {
5878 extractPermissionDenied (() -> {
59- xqueryRemoveGroup (SecurityManager .DBA_GROUP );
79+ xqueryRemoveGroup (existWebServer . getBrokerPool (), SecurityManager .DBA_GROUP );
6080 });
6181 }
6282
6383 @ Test (expected = PermissionDeniedException .class )
6484 public void cannotDeleteGuestGroup () throws XPathException , PermissionDeniedException , EXistException {
6585 extractPermissionDenied (() -> {
66- xqueryRemoveGroup (SecurityManager .GUEST_GROUP );
86+ xqueryRemoveGroup (existWebServer . getBrokerPool (), SecurityManager .GUEST_GROUP );
6787 });
6888 }
6989
7090 @ Test (expected = PermissionDeniedException .class )
7191 public void cannotDeleteUnknownGroup () throws XPathException , PermissionDeniedException , EXistException {
7292 extractPermissionDenied (() -> {
73- xqueryRemoveGroup (SecurityManager .UNKNOWN_GROUP );
93+ xqueryRemoveGroup (existWebServer . getBrokerPool (), SecurityManager .UNKNOWN_GROUP );
7494 });
7595 }
7696
@@ -264,69 +284,4 @@ public void deleteUsersSharingPersonalPrimaryGroup() throws PermissionDeniedExce
264284 transaction .commit ();
265285 }
266286 }
267-
268- private static Account createUser (final DBBroker broker , final SecurityManager sm , final String username , final String password ) throws PermissionDeniedException , EXistException {
269- Group userGroup = new GroupAider (username );
270- sm .addGroup (broker , userGroup );
271- final Account user = new UserAider (username );
272- user .setPassword (password );
273- user .setPrimaryGroup (userGroup );
274- sm .addAccount (user );
275-
276- userGroup = sm .getGroup (username );
277- userGroup .addManager (sm .getAccount (username ));
278- sm .updateGroup (userGroup );
279-
280- return user ;
281- }
282-
283- private static Group createGroup (final DBBroker broker , final SecurityManager sm , final String groupName ) throws PermissionDeniedException , EXistException {
284- final Group otherGroup = new GroupAider (groupName );
285- return sm .addGroup (broker , otherGroup );
286- }
287-
288- private static void addUserToGroup (final SecurityManager sm , final Account user , final Group group ) throws PermissionDeniedException , EXistException {
289- user .addGroup (group .getName ());
290- sm .updateAccount (user );
291- }
292-
293- private static void setPrimaryGroup (final SecurityManager sm , final Account user , final Group group ) throws PermissionDeniedException , EXistException {
294- user .setPrimaryGroup (group );
295- sm .updateAccount (user );
296- }
297-
298- private static void removeUser (final SecurityManager sm , final String username ) throws PermissionDeniedException , EXistException {
299- sm .deleteAccount (username );
300- removeGroup (sm , username );
301- }
302-
303- private static void removeGroup (final SecurityManager sm , final String groupname ) throws PermissionDeniedException , EXistException {
304- sm .deleteGroup (groupname );
305- }
306-
307- private Sequence xqueryRemoveGroup (final String groupname ) throws EXistException , PermissionDeniedException , XPathException {
308- final BrokerPool pool = existWebServer .getBrokerPool ();
309-
310- final String query =
311- "import module namespace sm = 'http://exist-db.org/xquery/securitymanager';\n " +
312- "sm:remove-group('" + groupname + "')" ;
313-
314- try (final DBBroker broker = pool .get (Optional .of (pool .getSecurityManager ().getSystemSubject ()))) {
315- final XQuery xquery = existWebServer .getBrokerPool ().getXQueryService ();
316- final Sequence result = xquery .execute (broker , query , null );
317- return result ;
318- }
319- }
320-
321- private static void extractPermissionDenied (final Runnable3E <XPathException , PermissionDeniedException , EXistException > runnable ) throws XPathException , PermissionDeniedException , EXistException {
322- try {
323- runnable .run ();
324- } catch (final XPathException e ) {
325- if (e .getCause () != null && e .getCause () instanceof PermissionDeniedException ) {
326- throw (PermissionDeniedException )e .getCause ();
327- } else {
328- throw e ;
329- }
330- }
331- }
332287}
0 commit comments