11/*
2+ * Elemental
3+ * Copyright (C) 2024, Evolved Binary Ltd
4+ *
5+ 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 {
@@ -55,17 +75,23 @@ public class GroupManagementFunctionRemoveGroupTest {
5575
5676 @ Test (expected = PermissionDeniedException .class )
5777 public void cannotDeleteDbaGroup () throws XPathException , PermissionDeniedException , EXistException {
58- extractPermissionDenied (() -> xqueryRemoveGroup (SecurityManager .DBA_GROUP ));
78+ extractPermissionDenied (() -> {
79+ xqueryRemoveGroup (existWebServer .getBrokerPool (), SecurityManager .DBA_GROUP );
80+ });
5981 }
6082
6183 @ Test (expected = PermissionDeniedException .class )
6284 public void cannotDeleteGuestGroup () throws XPathException , PermissionDeniedException , EXistException {
63- extractPermissionDenied (() -> xqueryRemoveGroup (SecurityManager .GUEST_GROUP ));
85+ extractPermissionDenied (() -> {
86+ xqueryRemoveGroup (existWebServer .getBrokerPool (), SecurityManager .GUEST_GROUP );
87+ });
6488 }
6589
6690 @ Test (expected = PermissionDeniedException .class )
6791 public void cannotDeleteUnknownGroup () throws XPathException , PermissionDeniedException , EXistException {
68- extractPermissionDenied (() -> xqueryRemoveGroup (SecurityManager .UNKNOWN_GROUP ));
92+ extractPermissionDenied (() -> {
93+ xqueryRemoveGroup (existWebServer .getBrokerPool (), SecurityManager .UNKNOWN_GROUP );
94+ });
6995 }
7096
7197 @ Test
@@ -258,69 +284,4 @@ public void deleteUsersSharingPersonalPrimaryGroup() throws PermissionDeniedExce
258284 transaction .commit ();
259285 }
260286 }
261-
262- private static Account createUser (final DBBroker broker , final SecurityManager sm , final String username , final String password ) throws PermissionDeniedException , EXistException {
263- Group userGroup = new GroupAider (username );
264- sm .addGroup (broker , userGroup );
265- final Account user = new UserAider (username );
266- user .setPassword (password );
267- user .setPrimaryGroup (userGroup );
268- sm .addAccount (user );
269-
270- userGroup = sm .getGroup (username );
271- userGroup .addManager (sm .getAccount (username ));
272- sm .updateGroup (userGroup );
273-
274- return user ;
275- }
276-
277- private static Group createGroup (final DBBroker broker , final SecurityManager sm , final String groupName ) throws PermissionDeniedException , EXistException {
278- final Group otherGroup = new GroupAider (groupName );
279- return sm .addGroup (broker , otherGroup );
280- }
281-
282- private static void addUserToGroup (final SecurityManager sm , final Account user , final Group group ) throws PermissionDeniedException , EXistException {
283- user .addGroup (group .getName ());
284- sm .updateAccount (user );
285- }
286-
287- private static void setPrimaryGroup (final SecurityManager sm , final Account user , final Group group ) throws PermissionDeniedException , EXistException {
288- user .setPrimaryGroup (group );
289- sm .updateAccount (user );
290- }
291-
292- private static void removeUser (final SecurityManager sm , final String username ) throws PermissionDeniedException , EXistException {
293- sm .deleteAccount (username );
294- removeGroup (sm , username );
295- }
296-
297- private static void removeGroup (final SecurityManager sm , final String groupname ) throws PermissionDeniedException , EXistException {
298- sm .deleteGroup (groupname );
299- }
300-
301- private Sequence xqueryRemoveGroup (final String groupname ) throws EXistException , PermissionDeniedException , XPathException {
302- final BrokerPool pool = existWebServer .getBrokerPool ();
303-
304- final String query =
305- "import module namespace sm = 'http://exist-db.org/xquery/securitymanager';\n " +
306- "sm:remove-group('" + groupname + "')" ;
307-
308- try (final DBBroker broker = pool .get (Optional .of (pool .getSecurityManager ().getSystemSubject ()))) {
309- final XQuery xquery = existWebServer .getBrokerPool ().getXQueryService ();
310- final Sequence result = xquery .execute (broker , query , null );
311- return result ;
312- }
313- }
314-
315- private static void extractPermissionDenied (final Runnable3E <XPathException , PermissionDeniedException , EXistException > runnable ) throws XPathException , PermissionDeniedException , EXistException {
316- try {
317- runnable .run ();
318- } catch (final XPathException e ) {
319- if (e .getCause () != null && e .getCause () instanceof PermissionDeniedException ) {
320- throw (PermissionDeniedException )e .getCause ();
321- } else {
322- throw e ;
323- }
324- }
325- }
326287}
0 commit comments