Adds a mechanism to delete CPA values for a given user (#30330)
* Adds a mechanism to delete CPA values for a given user This requires improving the Property Value service to enable delete all values for a given target, so a new method was created that allows to delete filtering by targetType and targetID (required) and optionally for a specific groupID in case the caller wants to affect all values for a target (useful in case you remove a post for example and want to delete all values pointing to that post regardless of the feature they belong to) or only those that belong to a specific feature. * Fix property value tests * Fix after merge and update method name * Fix linter --------- Co-authored-by: Miguel de la Cruz <miguel@ctrlz.es> Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
3ab0da1648
Коммит
ca9fd45408
@@ -272,3 +272,21 @@ func (a *App) PatchCPAValues(userID string, fieldValueMap map[string]json.RawMes
|
||||
|
||||
return updatedValues, nil
|
||||
}
|
||||
|
||||
func (a *App) DeleteCPAValues(userID string) *model.AppError {
|
||||
groupID, err := a.CpaGroupID()
|
||||
if err != nil {
|
||||
return model.NewAppError("DeleteCPAValues", "app.custom_profile_attributes.cpa_group_id.app_error", nil, "", http.StatusInternalServerError).Wrap(err)
|
||||
}
|
||||
|
||||
if err := a.Srv().propertyService.DeletePropertyValuesForTarget(groupID, "user", userID); err != nil {
|
||||
return model.NewAppError("DeleteCPAValues", "app.custom_profile_attributes.delete_property_values_for_user.app_error", nil, "", http.StatusInternalServerError).Wrap(err)
|
||||
}
|
||||
|
||||
message := model.NewWebSocketEvent(model.WebsocketEventCPAValuesUpdated, "", "", "", nil, "")
|
||||
message.Add("user_id", userID)
|
||||
message.Add("values", map[string]json.RawMessage{})
|
||||
a.Publish(message)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -698,3 +698,74 @@ func TestPatchCPAValue(t *testing.T) {
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestDeleteCPAValues(t *testing.T) {
|
||||
os.Setenv("MM_FEATUREFLAGS_CUSTOMPROFILEATTRIBUTES", "true")
|
||||
defer os.Unsetenv("MM_FEATUREFLAGS_CUSTOMPROFILEATTRIBUTES")
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
cpaGroupID, cErr := th.App.CpaGroupID()
|
||||
require.NoError(t, cErr)
|
||||
|
||||
userID := model.NewId()
|
||||
otherUserID := model.NewId()
|
||||
|
||||
// Create multiple fields and values for the user
|
||||
var createdFields []*model.PropertyField
|
||||
for i := 1; i <= 3; i++ {
|
||||
field, err := model.NewCPAFieldFromPropertyField(&model.PropertyField{
|
||||
GroupID: cpaGroupID,
|
||||
Name: fmt.Sprintf("Field %d", i),
|
||||
Type: model.PropertyFieldTypeText,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
createdField, appErr := th.App.CreateCPAField(field)
|
||||
require.Nil(t, appErr)
|
||||
createdFields = append(createdFields, createdField)
|
||||
|
||||
// Create a value for this field
|
||||
value, appErr := th.App.PatchCPAValue(userID, createdField.ID, json.RawMessage(fmt.Sprintf(`"Value %d"`, i)))
|
||||
require.Nil(t, appErr)
|
||||
require.NotNil(t, value)
|
||||
}
|
||||
|
||||
// Verify values exist before deletion
|
||||
values, appErr := th.App.ListCPAValues(userID)
|
||||
require.Nil(t, appErr)
|
||||
require.Len(t, values, 3)
|
||||
|
||||
// Test deleting values for user
|
||||
t.Run("should delete all values for a user", func(t *testing.T) {
|
||||
appErr := th.App.DeleteCPAValues(userID)
|
||||
require.Nil(t, appErr)
|
||||
|
||||
// Verify values are gone
|
||||
values, appErr := th.App.ListCPAValues(userID)
|
||||
require.Nil(t, appErr)
|
||||
require.Empty(t, values)
|
||||
})
|
||||
|
||||
t.Run("should handle deleting values for a user with no values", func(t *testing.T) {
|
||||
appErr := th.App.DeleteCPAValues(otherUserID)
|
||||
require.Nil(t, appErr)
|
||||
})
|
||||
|
||||
t.Run("should not affect values for other users", func(t *testing.T) {
|
||||
// Create values for another user
|
||||
for _, field := range createdFields {
|
||||
value, appErr := th.App.PatchCPAValue(otherUserID, field.ID, json.RawMessage(`"Other user value"`))
|
||||
require.Nil(t, appErr)
|
||||
require.NotNil(t, value)
|
||||
}
|
||||
|
||||
// Delete values for original user
|
||||
appErr := th.App.DeleteCPAValues(userID)
|
||||
require.Nil(t, appErr)
|
||||
|
||||
// Verify other user's values still exist
|
||||
values, appErr := th.App.ListCPAValues(otherUserID)
|
||||
require.Nil(t, appErr)
|
||||
require.Len(t, values, 3)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -56,3 +56,7 @@ func (ps *PropertyService) UpsertPropertyValues(values []*model.PropertyValue) (
|
||||
func (ps *PropertyService) DeletePropertyValue(groupID, id string) error {
|
||||
return ps.valueStore.Delete(groupID, id)
|
||||
}
|
||||
|
||||
func (ps *PropertyService) DeletePropertyValuesForTarget(groupID string, targetType string, targetID string) error {
|
||||
return ps.valueStore.DeleteForTarget(groupID, targetType, targetID)
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user