netbirdio / dashboard

NetBird Management Service Web UI Panel
https://app.netbird.io
BSD 3-Clause "New" or "Revised" License
273 stars 63 forks source link

Bug (&fix) - PUT requests from Settings tabs overwrite all other values #404

Closed taylorwilsdon closed 4 months ago

taylorwilsdon commented 4 months ago

Issue:

Solution:

https://github.com/user-attachments/assets/aec673c8-e7c6-43ea-a822-d9b54a2cdbec

https://github.com/user-attachments/assets/cb96112a-b0c5-4f9f-b42d-e7946694bb1b

I seem to lack the appropriate permissions to open a PR for this repo, but the diff that fixes this is included below:


diff --git a/src/modules/settings/AuthenticationTab.tsx b/src/modules/settings/AuthenticationTab.tsx
index 11f0235..333590f 100644
--- a/src/modules/settings/AuthenticationTab.tsx
+++ b/src/modules/settings/AuthenticationTab.tsx
@@ -22,6 +22,7 @@ import { useSWRConfig } from "swr";
 import SettingsIcon from "@/assets/icons/SettingsIcon";
 import { useHasChanges } from "@/hooks/useHasChanges";
 import { Account } from "@/interfaces/Account";
+import { isEmpty } from "lodash";

 type Props = {
   account: Account;
@@ -41,6 +42,26 @@ export default function AuthenticationTab({ account }: Props) {
     }
   });

+    /**
+   * Group Propagation
+   */
+    const [groupsPropagation, setGroupsPropagation] = useState<boolean>(
+      account.settings.groups_propagation_enabled,
+    );
+
+    /**
+     * JWT Group Sync
+     */
+    const [jwtGroupSync, setJwtGroupSync] = useState<boolean>(
+      account.settings.jwt_groups_enabled,
+    );
+    const [jwtGroupsClaimName, setJwtGroupsClaimName] = useState(
+      account.settings.jwt_groups_claim_name,
+    );
+    const [jwtAllowGroups, setJwtAllowGroups] = useState<string[]>(
+      account.settings.jwt_allow_groups,
+    );
+
   /**
    * Login expiration enabled
    */
@@ -87,6 +108,9 @@ export default function AuthenticationTab({ account }: Props) {
   ]);

   const saveChanges = async () => {
+    const jwtGroupsEntered =
+    jwtAllowGroups.filter((g) => !isEmpty(g)).length > 0;
+
     const expiration =
       expireInterval === "days"
         ? Number(expiresIn) * 86400
@@ -99,6 +123,12 @@ export default function AuthenticationTab({ account }: Props) {
         .put({
           id: account.id,
           settings: {
+            groups_propagation_enabled: groupsPropagation,
+            jwt_groups_enabled: jwtGroupSync,
+            jwt_groups_claim_name: isEmpty(jwtGroupsClaimName)
+              ? undefined
+              : jwtGroupsClaimName,
+            jwt_allow_groups: jwtGroupsEntered ? jwtAllowGroups : undefined,
             peer_login_expiration_enabled: loginExpiration,
             peer_login_expiration: loginExpiration ? expiration : 86400,
             extra: {