diff --git a/server/channels/api4/custom_profile_attributes.go b/server/channels/api4/custom_profile_attributes.go index 487a1727aa0..2746bb502d6 100644 --- a/server/channels/api4/custom_profile_attributes.go +++ b/server/channels/api4/custom_profile_attributes.go @@ -278,10 +278,13 @@ func listCPAValues(c *Context, w http.ResponseWriter, r *http.Request) { } userID := c.Params.UserId - canSee, err := c.App.UserCanSeeOtherUser(c.AppContext, c.AppContext.Session().UserId, userID) - if err != nil || !canSee { - c.SetPermissionError(model.PermissionViewMembers) - return + // we check unrestricted sessions to allow local mode requests to go through + if !c.AppContext.Session().IsUnrestricted() { + canSee, err := c.App.UserCanSeeOtherUser(c.AppContext, c.AppContext.Session().UserId, userID) + if err != nil || !canSee { + c.SetPermissionError(model.PermissionViewMembers) + return + } } values, appErr := c.App.ListCPAValues(userID) diff --git a/server/channels/api4/custom_profile_attributes_local.go b/server/channels/api4/custom_profile_attributes_local.go index 42c4f0ce8ad..91a114cd070 100644 --- a/server/channels/api4/custom_profile_attributes_local.go +++ b/server/channels/api4/custom_profile_attributes_local.go @@ -11,8 +11,8 @@ func (api *API) InitCustomProfileAttributesLocal() { api.BaseRoutes.CustomProfileAttributesFields.Handle("", api.APILocal(createCPAField)).Methods(http.MethodPost) api.BaseRoutes.CustomProfileAttributesField.Handle("", api.APILocal(patchCPAField)).Methods(http.MethodPatch) api.BaseRoutes.CustomProfileAttributesField.Handle("", api.APILocal(deleteCPAField)).Methods(http.MethodDelete) - api.BaseRoutes.User.Handle("/custom_profile_attributes", api.APISessionRequired(listCPAValues)).Methods(http.MethodGet) - api.BaseRoutes.CustomProfileAttributesValues.Handle("", api.APISessionRequired(patchCPAValues)).Methods(http.MethodPatch) - api.BaseRoutes.User.Handle("/custom_profile_attributes", api.APISessionRequired(patchCPAValuesForUser)).Methods(http.MethodPatch) + api.BaseRoutes.User.Handle("/custom_profile_attributes", api.APILocal(listCPAValues)).Methods(http.MethodGet) + api.BaseRoutes.CustomProfileAttributesValues.Handle("", api.APILocal(patchCPAValues)).Methods(http.MethodPatch) + api.BaseRoutes.User.Handle("/custom_profile_attributes", api.APILocal(patchCPAValuesForUser)).Methods(http.MethodPatch) } } diff --git a/server/channels/api4/custom_profile_attributes_test.go b/server/channels/api4/custom_profile_attributes_test.go index 351f0e0cd8e..f90e05c9046 100644 --- a/server/channels/api4/custom_profile_attributes_test.go +++ b/server/channels/api4/custom_profile_attributes_test.go @@ -1150,12 +1150,16 @@ func TestPatchCPAValuesForUser(t *testing.T) { require.Equal(t, "Regular Value", actualValue) }) - t.Run("system admin can update managed field", func(t *testing.T) { + th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) { + // Set initial value through the app layer that we will be replacing during the test + _, appErr := th.App.PatchCPAValue(th.SystemAdminUser.Id, createdManagedField.ID, json.RawMessage(`"Initial Admin Value"`), true) + require.Nil(t, appErr) + values := map[string]json.RawMessage{ createdManagedField.ID: json.RawMessage(`"Admin Updated Value"`), } - patchedValues, resp, err := th.SystemAdminClient.PatchCPAValuesForUser(context.Background(), th.SystemAdminUser.Id, values) + patchedValues, resp, err := client.PatchCPAValuesForUser(context.Background(), th.SystemAdminUser.Id, values) CheckOKStatus(t, resp) require.NoError(t, err) require.NotEmpty(t, patchedValues) @@ -1163,9 +1167,9 @@ func TestPatchCPAValuesForUser(t *testing.T) { var actualValue string require.NoError(t, json.Unmarshal(patchedValues[createdManagedField.ID], &actualValue)) require.Equal(t, "Admin Updated Value", actualValue) - }) + }, "system admin can update managed field") - t.Run("system admin can update managed field values for other users", func(t *testing.T) { + th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) { values := map[string]json.RawMessage{ createdManagedField.ID: json.RawMessage(`"Admin Updated Managed Value For Other User"`), } @@ -1188,7 +1192,7 @@ func TestPatchCPAValuesForUser(t *testing.T) { var storedValue string require.NoError(t, json.Unmarshal(userValues[createdManagedField.ID], &storedValue)) require.Equal(t, "Admin Updated Managed Value For Other User", storedValue) - }) + }, "system admin can update managed field values for other users") t.Run("a user should not be able to update other user's field values", func(t *testing.T) { values := map[string]json.RawMessage{ @@ -1256,7 +1260,7 @@ func TestPatchCPAValuesForUser(t *testing.T) { require.True(t, managedFieldHasOriginalValue, "Managed field should retain its original value") }) - t.Run("batch update with managed fields succeeds for admin", func(t *testing.T) { + th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) { values := map[string]json.RawMessage{ createdManagedField.ID: json.RawMessage(`"Admin Managed Batch"`), createdRegularField.ID: json.RawMessage(`"Admin Regular Batch"`), @@ -1272,6 +1276,6 @@ func TestPatchCPAValuesForUser(t *testing.T) { require.NoError(t, json.Unmarshal(patchedValues[createdRegularField.ID], ®ularValue)) require.Equal(t, "Admin Managed Batch", managedValue) require.Equal(t, "Admin Regular Batch", regularValue) - }) + }, "batch update with managed fields succeeds for admin") }) }