Skip to content

Commit b19dfb2

Browse files
authored
Avoid spamming the server for auditories when we don't have the rights (#4283)
1 parent bf48780 commit b19dfb2

4 files changed

Lines changed: 31 additions & 16 deletions

File tree

app/Http/Resources/Models/UserResource.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class UserResource extends Data
2121
public ?string $username;
2222
public ?string $email;
2323
public bool $is_ldap;
24+
public bool $may_administrate;
2425
public UserSharedAlbumsVisibility $shared_albums_visibility;
2526

2627
public function __construct(?User $user)
@@ -30,6 +31,7 @@ public function __construct(?User $user)
3031
$this->username = $user?->username;
3132
$this->email = $user?->email;
3233
$this->is_ldap = $user?->is_ldap ?? false;
34+
$this->may_administrate = $user?->may_administrate ?? false;
3335
$this->shared_albums_visibility = $user?->shared_albums_visibility ?? UserSharedAlbumsVisibility::DEFAULT;
3436
}
3537
}

resources/js/composables/modals/useAdvisoryModal.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import { ref } from "vue";
88
import SecurityAdvisoriesService from "@/services/security-advisories-service";
9+
import { useUserStore } from "@/stores/UserState";
910

1011
const DISMISSED_KEY = "advisory_dismissed";
1112

@@ -21,25 +22,33 @@ const advisories = ref<App.Http.Resources.Models.SecurityAdvisoryResource[]>([])
2122
* modal for admin users after login.
2223
*
2324
* The modal is shown at most once per browser session (controlled via
24-
* sessionStorage). Non-admin users receive a 403 from the endpoint, which
25-
* is caught and silently ignored.
25+
* sessionStorage). The advisory endpoint is only queried when the current
26+
* user has the `may_administrate` flag set on their user resource.
2627
*/
2728
export function useAdvisoryModal() {
2829
function advisoryCheck() {
29-
if (sessionStorage.getItem(DISMISSED_KEY) !== null) {
30-
return;
31-
}
32-
33-
SecurityAdvisoriesService.getAdvisories()
34-
.then((response) => {
35-
if (response.data.length > 0) {
36-
advisories.value = response.data;
37-
isAdvisoriesVisible.value = true;
38-
}
39-
})
40-
.catch(() => {
41-
// 401/403 for non-admins or network errors: silently ignore.
42-
});
30+
const userStore = useUserStore();
31+
32+
userStore.load().then(() => {
33+
if (!userStore.isAdmin) {
34+
return;
35+
}
36+
37+
if (sessionStorage.getItem(DISMISSED_KEY) !== null) {
38+
return;
39+
}
40+
41+
SecurityAdvisoriesService.getAdvisories()
42+
.then((response) => {
43+
if (response.data.length > 0) {
44+
advisories.value = response.data;
45+
isAdvisoriesVisible.value = true;
46+
}
47+
})
48+
.catch(() => {
49+
// Network errors: silently ignore.
50+
});
51+
});
4352
}
4453

4554
function advisoryDismiss() {

resources/js/lychee.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,7 @@ declare namespace App.Http.Resources.Models {
832832
username: string | null;
833833
email: string | null;
834834
is_ldap: boolean;
835+
may_administrate: boolean;
835836
shared_albums_visibility: App.Enum.UserSharedAlbumsVisibility;
836837
};
837838
export type WebAuthnResource = {

resources/js/stores/UserState.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,8 @@ export const useUserStore = defineStore("user-store", {
7373
isGuest(): boolean {
7474
return this.user !== undefined && this.user.id === null;
7575
},
76+
isAdmin(): boolean {
77+
return this.user?.may_administrate === true;
78+
},
7679
},
7780
});

0 commit comments

Comments
 (0)