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 .TestUtils ;
2750import org .exist .security .*;
2851import org .exist .security .SecurityManager ;
29- import org .exist .security .internal .aider .GroupAider ;
30- import org .exist .security .internal .aider .UserAider ;
3152import org .exist .storage .BrokerPool ;
3253import org .exist .storage .DBBroker ;
3354import org .exist .storage .txn .Txn ;
3455import org .exist .test .ExistEmbeddedServer ;
3556import org .exist .xquery .XPathException ;
36- import org .exist .xquery .XQuery ;
37- import org .exist .xquery .value .Sequence ;
3857import org .junit .Before ;
3958import org .junit .Rule ;
4059import org .junit .Test ;
4160
4261import java .util .Optional ;
4362
63+ import static org .exist .xquery .functions .securitymanager .SecurityManagerTestUtil .*;
4464import static org .junit .Assert .*;
4565
4666public class GroupMembershipFunctionRemoveGroupMemberTest {
@@ -59,9 +79,9 @@ public void cannotRemoveAllGroupsFromUserAsOwner() throws XPathException, Permis
5979 final BrokerPool pool = existWebServer .getBrokerPool ();
6080 final Subject owner = pool .getSecurityManager ().authenticate (USER1_NAME , USER1_NAME );
6181 extractPermissionDenied (() -> {
62- xqueryRemoveUserFromGroup (USER1_NAME , OTHER_GROUP2_NAME , Optional .of (owner ));
63- xqueryRemoveUserFromGroup (USER1_NAME , OTHER_GROUP1_NAME , Optional .of (owner ));
64- xqueryRemoveUserFromGroup (USER1_NAME , USER1_NAME , Optional .of (owner ));
82+ xqueryRemoveUserFromGroup (pool , USER1_NAME , OTHER_GROUP2_NAME , Optional .of (owner ));
83+ xqueryRemoveUserFromGroup (pool , USER1_NAME , OTHER_GROUP1_NAME , Optional .of (owner ));
84+ xqueryRemoveUserFromGroup (pool , USER1_NAME , USER1_NAME , Optional .of (owner ));
6585 });
6686 }
6787
@@ -70,9 +90,9 @@ public void cannotRemoveAllGroupsFromUserAsDBA() throws XPathException, Permissi
7090 final BrokerPool pool = existWebServer .getBrokerPool ();
7191 final Subject admin = pool .getSecurityManager ().authenticate (TestUtils .ADMIN_DB_USER , TestUtils .ADMIN_DB_PWD );
7292 extractPermissionDenied (() -> {
73- xqueryRemoveUserFromGroup (USER1_NAME , OTHER_GROUP2_NAME , Optional .of (admin ));
74- xqueryRemoveUserFromGroup (USER1_NAME , OTHER_GROUP1_NAME , Optional .of (admin ));
75- xqueryRemoveUserFromGroup (USER1_NAME , USER1_NAME , Optional .of (admin ));
93+ xqueryRemoveUserFromGroup (pool , USER1_NAME , OTHER_GROUP2_NAME , Optional .of (admin ));
94+ xqueryRemoveUserFromGroup (pool , USER1_NAME , OTHER_GROUP1_NAME , Optional .of (admin ));
95+ xqueryRemoveUserFromGroup (pool , USER1_NAME , USER1_NAME , Optional .of (admin ));
7696 });
7797 }
7898
@@ -88,11 +108,11 @@ public void setup() throws EXistException, PermissionDeniedException, XPathExcep
88108
89109 final Group otherGroup1 = createGroup (broker , sm , OTHER_GROUP1_NAME );
90110 addUserToGroup (sm , user1 , otherGroup1 );
91- addUserAsGroupManager ( USER1_NAME , OTHER_GROUP1_NAME );
111+ xqueryAddUserAsGroupManager ( pool , USER1_NAME , OTHER_GROUP1_NAME );
92112
93113 final Group otherGroup2 = createGroup (broker , sm , OTHER_GROUP2_NAME );
94114 addUserToGroup (sm , user1 , otherGroup2 );
95- addUserAsGroupManager ( USER1_NAME , OTHER_GROUP2_NAME );
115+ xqueryAddUserAsGroupManager ( pool , USER1_NAME , OTHER_GROUP2_NAME );
96116
97117 transaction .commit ();
98118 }
@@ -110,75 +130,4 @@ public void setup() throws EXistException, PermissionDeniedException, XPathExcep
110130 transaction .commit ();
111131 }
112132 }
113-
114- private Sequence xqueryRemoveUserFromGroup (final String username , final String groupname ) throws XPathException , PermissionDeniedException , EXistException {
115- final BrokerPool pool = existWebServer .getBrokerPool ();
116- final Optional <Subject > asUser = Optional .of (pool .getSecurityManager ().getSystemSubject ());
117- return xqueryRemoveUserFromGroup (username , groupname , asUser );
118- }
119-
120- private Sequence xqueryRemoveUserFromGroup (final String username , final String groupname , final Optional <Subject > asUser ) throws EXistException , PermissionDeniedException , XPathException {
121- final BrokerPool pool = existWebServer .getBrokerPool ();
122-
123- final String query =
124- "import module namespace sm = 'http://exist-db.org/xquery/securitymanager';\n " +
125- "sm:remove-group-member('" + groupname + "', '" + username + "')" ;
126-
127- try (final DBBroker broker = pool .get (asUser )) {
128- final XQuery xquery = existWebServer .getBrokerPool ().getXQueryService ();
129- final Sequence result = xquery .execute (broker , query , null );
130- return result ;
131- }
132- }
133-
134- private Sequence addUserAsGroupManager (final String username , final String groupname ) throws EXistException , PermissionDeniedException , XPathException {
135- final BrokerPool pool = existWebServer .getBrokerPool ();
136-
137- final String query =
138- "import module namespace sm = 'http://exist-db.org/xquery/securitymanager';\n " +
139- "sm:add-group-manager('" + groupname + "', '" + username + "')" ;
140-
141- try (final DBBroker broker = pool .get (Optional .of (pool .getSecurityManager ().getSystemSubject ()))) {
142- final XQuery xquery = existWebServer .getBrokerPool ().getXQueryService ();
143- final Sequence result = xquery .execute (broker , query , null );
144- return result ;
145- }
146- }
147-
148- private static Account createUser (final DBBroker broker , final SecurityManager sm , final String username , final String password ) throws PermissionDeniedException , EXistException {
149- Group userGroup = new GroupAider (username );
150- sm .addGroup (broker , userGroup );
151- final Account user = new UserAider (username );
152- user .setPassword (password );
153- user .setPrimaryGroup (userGroup );
154- sm .addAccount (user );
155-
156- userGroup = sm .getGroup (username );
157- userGroup .addManager (sm .getAccount (username ));
158- sm .updateGroup (userGroup );
159-
160- return user ;
161- }
162-
163- private static Group createGroup (final DBBroker broker , final SecurityManager sm , final String groupName ) throws PermissionDeniedException , EXistException {
164- final Group otherGroup = new GroupAider (groupName );
165- return sm .addGroup (broker , otherGroup );
166- }
167-
168- private static void addUserToGroup (final SecurityManager sm , final Account user , final Group group ) throws PermissionDeniedException , EXistException {
169- user .addGroup (group .getName ());
170- sm .updateAccount (user );
171- }
172-
173- private static void extractPermissionDenied (final Runnable3E <XPathException , PermissionDeniedException , EXistException > runnable ) throws XPathException , PermissionDeniedException , EXistException {
174- try {
175- runnable .run ();
176- } catch (final XPathException e ) {
177- if (e .getCause () != null && e .getCause () instanceof PermissionDeniedException ) {
178- throw (PermissionDeniedException )e .getCause ();
179- } else {
180- throw e ;
181- }
182- }
183- }
184133}
0 commit comments