Skip to content

Commit 920beb1

Browse files
Change button style and fix pytest
Small pytest fix Small pytest fix Small pytest fix Small pytest fix Small pytest fix
1 parent c172b04 commit 920beb1

File tree

6 files changed

+40
-62
lines changed

6 files changed

+40
-62
lines changed

pydatalab/src/pydatalab/routes/v0_1/admin.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,10 @@ def save_role(user_id):
9999

100100
@ADMIN.route("/items/<refcode>/invalidate-access-token", methods=["POST"])
101101
def invalidate_access_token(refcode: str):
102-
request_json = request.get_json(silent=True) or {}
103-
104102
if len(refcode.split(":")) != 2:
105103
refcode = f"{CONFIG.IDENTIFIER_PREFIX}:{refcode}"
106104

107-
if request_json.get("token") == "admin-invalidation":
108-
query = {"refcode": refcode, "active": True, "type": "access_token"}
109-
else:
110-
query = {"refcode": refcode, "active": True, "type": "access_token"}
105+
query = {"refcode": refcode, "active": True, "type": "access_token"}
111106

112107
response = flask_mongo.db.api_keys.update_one(
113108
query,
@@ -155,7 +150,7 @@ def list_access_tokens():
155150
"active": 1,
156151
"created_at": 1,
157152
"invalidated_at": 1,
158-
"token": {"$substr": ["$token", 0, 16]},
153+
"token": "$token",
159154
"item_name": {
160155
"$cond": {
161156
"if": {"$gt": [{"$size": "$item_info"}, 0]},

pydatalab/src/pydatalab/routes/v0_1/items.py

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,6 @@ def update_item_permissions(refcode: str):
754754

755755

756756
@ITEMS.route("/items/<refcode>/issue-access-token", methods=["POST"])
757-
@active_users_or_get_only
758757
def issue_physical_token(refcode: str):
759758
"""Issue a token that will give semi-permanent access to an
760759
item with this refcode. This should be used when generating
@@ -872,14 +871,9 @@ def get_item_data(item_id: str | None = None, refcode: str | None = None):
872871
redirect_url += f"?at={access_token}"
873872
return redirect(redirect_url, code=307)
874873

875-
valid_access_token: bool = False
874+
valid_access_token = False
876875
if refcode and access_token:
877876
valid_access_token = check_access_token(refcode, access_token)
878-
if not valid_access_token:
879-
return (
880-
jsonify({"status": "error", "message": "Invalid access token"}),
881-
401,
882-
)
883877

884878
if item_id:
885879
match = {"item_id": item_id}
@@ -932,24 +926,6 @@ def get_item_data(item_id: str | None = None, refcode: str | None = None):
932926
404,
933927
)
934928

935-
if valid_access_token:
936-
pass
937-
938-
elif (
939-
not current_user.is_authenticated
940-
and not CONFIG.TESTING
941-
and doc["type"] != "starting_materials"
942-
):
943-
return (
944-
jsonify(
945-
{
946-
"status": "error",
947-
"message": "Authentication required or invalid access token.",
948-
}
949-
),
950-
401,
951-
)
952-
953929
# determine the item type and validate according to the appropriate schema
954930
try:
955931
ItemModel = ITEM_MODELS[doc["type"]]

pydatalab/tests/server/test_permissions.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,25 +89,25 @@ def test_basic_permissions_update(admin_client, admin_user_id, client, user_id):
8989
def test_access_token_permissions(client, unauthenticated_client, admin_client, database):
9090
response = client.post("/new-sample/", json={"type": "samples", "item_id": "private-sample"})
9191
assert response.status_code == 201
92-
response = response.json()
92+
response = response.json
9393

9494
refcode = response["sample_list_entry"]["refcode"]
9595
assert refcode
9696

97-
response = client.get(f"/items/{refcode}/issue-access-token")
98-
response = response.json()
97+
response = client.post(f"/items/{refcode}/issue-access-token")
98+
response = response.json
9999
assert response["status"] == "success"
100100
token = response["token"]
101101
assert token
102102

103103
response = unauthenticated_client.get(f"/items/{refcode}")
104-
assert response.status_code == 404
104+
assert response.status_code == 401
105105

106106
response = unauthenticated_client.get(f"/items/{refcode}?at={token}")
107107
assert response.status_code == 200
108108

109109
response = unauthenticated_client.get(f"/items/{refcode}?at={token}123")
110-
assert response.status_code == 200
110+
assert response.status_code == 401
111111

112112
response = admin_client.get(f"/items/{refcode}")
113113
assert response.status_code == 200

webapp/src/components/QRCode.vue

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,22 @@
4040

4141
<div v-else-if="!isPublicMode" class="mt-3">
4242
<div class="alert alert-info">
43-
<strong>Generate Public QR Code:</strong><br />
44-
This QR code requires authentication to access. You can generate a public QR code that allows
45-
access without login.
43+
<strong v-if="hasExistingToken">Public QR Code Available:</strong>
44+
<strong v-else>Generate Public QR Code:</strong>
45+
<br />
46+
<span v-if="hasExistingToken">
47+
A public QR code already exists for this item. You can view it or generate a new one if
48+
needed.
49+
</span>
50+
<span v-else>
51+
This QR code requires authentication to access. You can generate a public QR code that
52+
allows access without login.
53+
</span>
4654
</div>
4755

4856
<button
49-
class="btn btn-warning w-100"
57+
class="btn w-100"
58+
:class="hasExistingToken ? 'btn-info' : 'btn-warning'"
5059
:disabled="isGenerating"
5160
@click.prevent="hasExistingToken ? switchToPublic() : generatePublicQRCode()"
5261
>
@@ -68,17 +77,21 @@
6877
</div>
6978
</div>
7079

71-
<div class="d-flex justify-content-center">
72-
<button class="btn btn-sm btn-outline-primary mr-2" @click="switchToPrivate">
73-
<i class="fas fa-eye me-1"></i>View Private QRCode
74-
</button>
75-
<button
76-
class="btn btn-sm btn-outline-danger"
77-
:disabled="isInvalidating"
78-
@click.stop.prevent="invalidateToken"
79-
>
80-
<i class="fas fa-trash me-1"></i>Delete Token
81-
</button>
80+
<div class="row g-2">
81+
<div class="col-6">
82+
<button class="btn btn-outline-info w-100" @click="switchToPrivate">
83+
<i class="fas fa-eye me-1"></i>View Private QRCode
84+
</button>
85+
</div>
86+
<div class="col-6">
87+
<button
88+
class="btn btn-outline-danger w-100"
89+
:disabled="isInvalidating"
90+
@click.stop.prevent="invalidateToken"
91+
>
92+
<i class="fas fa-trash me-1"></i>Delete Token
93+
</button>
94+
</div>
8295
</div>
8396
</div>
8497

@@ -164,7 +177,7 @@ export default {
164177
}
165178
},
166179
hasExistingToken() {
167-
return this.tokenInfo && this.publicToken && this.publicToken !== "existing-token";
180+
return this.tokenInfo && this.publicToken === "existing-token";
168181
},
169182
},
170183
mounted() {
@@ -196,8 +209,7 @@ export default {
196209
if (response.ok && data.status === "success") {
197210
if (data.has_token) {
198211
this.tokenInfo = data.token_info;
199-
this.isPublicMode = true;
200-
this.publicToken = data.token_info.token;
212+
this.publicToken = "existing-token";
201213
}
202214
} else if (response.status === 404) {
203215
console.debug("No access to item or item not found");

webapp/src/components/TokenTable.vue

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
<tbody>
1515
<tr v-if="isLoading">
1616
<td colspan="6" class="text-center">
17-
<div class="spinner-border spinner-border-sm" role="status">
18-
<span class="visually-hidden">Loading...</span>
19-
</div>
17+
<div class="spinner-border spinner-border-sm" role="status"></div>
2018
Loading tokens...
2119
</td>
2220
</tr>

webapp/src/views/EditPage.vue

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
</div>
9292
</div>
9393

94-
<FileSelectModal v-if="isAuthenticated" :item_id="item_id" />
94+
<FileSelectModal :item_id="item_id" />
9595
</div>
9696
</template>
9797

@@ -212,9 +212,6 @@ export default {
212212
itemApiUrl() {
213213
return API_URL + "/items/" + this.refcode;
214214
},
215-
isAuthenticated() {
216-
return this.$store.state.currentUserID != null;
217-
},
218215
},
219216
watch: {
220217
// add a warning before leaving page if unsaved

0 commit comments

Comments
 (0)