Skip to content

Commit e58d179

Browse files
authored
Merge pull request #23 from evolvedbinary/hotfix/enable-disable-user
When updating a user you can now change their enabled/disabled status
2 parents e4b69c8 + 536739b commit e58d179

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

src/main/xar-resources/modules/security.xqm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module namespace sec = "http://fusiondb.com/ns/studio/api/security";
2222
declare namespace array = "http://www.w3.org/2005/xpath-functions/array";
2323

2424
import module namespace sm = "http://exist-db.org/xquery/securitymanager";
25+
import module namespace util = "http://exist-db.org/xquery/util";
2526

2627

2728
declare function sec:list-users() as array(xs:string) {
@@ -105,6 +106,12 @@ function sec:update-user($username, $user-data as map(xs:string, item())) as xs:
105106
return ()
106107
)
107108
else (),
109+
110+
(: change enabled/disabled? :)
111+
if (not(empty($user-data?enabled)))
112+
then
113+
sm:set-account-enabled($username, $user-data?enabled)
114+
else(),
108115

109116
true() (: success :)
110117
)

src/test/java/com/fusiondb/studio/api/API.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,16 @@ static <K, V> Map<K, V> mapOf(final Tuple2<K, V>... entries) {
140140
}
141141
return map;
142142
}
143+
144+
static <K, V> Map<K, V>[] arrayOf(final Map<K, V>... entries) {
145+
if (entries == null) {
146+
return new Map[0];
147+
}
148+
149+
final Map<K, V>[] arrayOfMaps = new Map[entries.length];
150+
for (int i = 0; i < entries.length; i++) {
151+
arrayOfMaps[i] = entries[i];
152+
}
153+
return arrayOfMaps;
154+
}
143155
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Fusion Studio API - API for Fusion Studio
3+
* Copyright © 2017 Evolved Binary ([email protected])
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Affero General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Affero General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Affero General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package com.fusiondb.studio.api;
19+
20+
import io.restassured.response.ExtractableResponse;
21+
import io.restassured.response.Response;
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.util.Map;
25+
26+
import static com.evolvedbinary.j8fu.tuple.Tuple.Tuple;
27+
import static com.fusiondb.studio.api.API.*;
28+
import static io.restassured.RestAssured.given;
29+
import static io.restassured.http.ContentType.JSON;
30+
import static org.apache.http.HttpStatus.*;
31+
import static org.junit.jupiter.api.Assertions.assertFalse;
32+
33+
public class UserIT {
34+
35+
@Test
36+
public void createUser() {
37+
createUser("123");
38+
}
39+
40+
@Test
41+
public void disableUser() {
42+
final String userId = "456";
43+
44+
// 1. create the user
45+
createUser(userId);
46+
47+
// 2. update the user
48+
final Map<String, Object> requestBody = mapOf(
49+
Tuple("userName", "user" + userId),
50+
Tuple("enabled", false)
51+
);
52+
given().
53+
auth().preemptive().basic(DEFAULT_ADMIN_USERNAME, DEFAULT_ADMIN_PASSWORD).
54+
contentType(JSON).
55+
body(requestBody).
56+
when().
57+
put(getApiBaseUri() + "/user/user" + userId).
58+
then().
59+
statusCode(SC_NO_CONTENT);
60+
61+
// get user
62+
final ExtractableResponse<Response> userResponse = getUser(userId);
63+
64+
// check they are now disabled
65+
assertFalse(userResponse.jsonPath().getBoolean("enabled"));
66+
}
67+
68+
private ExtractableResponse<Response> getUser(final String userId) {
69+
return
70+
given().
71+
auth().preemptive().basic(DEFAULT_ADMIN_USERNAME, DEFAULT_ADMIN_PASSWORD).
72+
contentType(JSON).
73+
when().
74+
get(getApiBaseUri() + "/user/user" + userId).
75+
then().
76+
statusCode(SC_OK)
77+
.extract();
78+
}
79+
80+
private void createUser(final String userId) {
81+
final Map<String, Object> requestBody = mapOf(
82+
Tuple("userName", "user" + userId),
83+
Tuple("password", "user" + userId),
84+
Tuple("metadata", arrayOf(
85+
mapOf(
86+
Tuple("key", "http://axschema.org/namePerson"),
87+
Tuple("value", "User " + userId)
88+
),
89+
mapOf(
90+
Tuple("key", "http://axschema.org/pref/language"),
91+
Tuple("value", "en")
92+
)
93+
))
94+
);
95+
96+
given().
97+
auth().preemptive().basic(DEFAULT_ADMIN_USERNAME, DEFAULT_ADMIN_PASSWORD).
98+
contentType(JSON).
99+
body(requestBody).
100+
when().
101+
put(getApiBaseUri() + "/user/user" + userId).
102+
then().
103+
statusCode(SC_NO_CONTENT);
104+
}
105+
}

0 commit comments

Comments
 (0)