[MM-62552] Custom Profile Attributes: use json.RawMessage for the value. (#29989)
* refactor: Move property value sanitization to model layer * feat: Add value sanitization for custom profile attributes * refactor: Update custom profile attributes to use json.RawMessage * refactor: Update patchCustomProfileAttribute to handle json.RawMessage directly * refactor: Refactor custom profile attributes handler with improved validation * refactor: Rename `patchCustomProfileAttribute` to `patchCPAValues` * refactor: Replace ReturnJSON with json.NewEncoder and add error logging * feat: Add encoding/json import to property_value.go * refactor: Update property value tests to use json.RawMessage * fix: Convert string value to json.RawMessage in property value test * fix: Convert string literals to json.RawMessage in property value tests * fix: Add missing encoding/json import in custom_profile_attributes.go * fix: Preserve JSON RawMessage type in listCPAValues function * fix: Update custom profile attributes test to use json.RawMessage * feat: Add json import to custom_profile_attributes_test.go * refactor: Update ListCPAValues and PatchCPAValues to use json.RawMessage * refactor: Rename `actualValue` to `updatedValue` in custom profile attributes test * refactor: Improve user permission and audit logging for custom profile attributes patch * refactor: Optimize CPA field lookup by using ListCPAFields() and map * fix: Correct user ID reference in custom profile attributes patch endpoint * refactor: Change patchCPAValues to use map[string]json.RawMessage for results * refactor: format and fix tests * test: Add comprehensive unit tests for sanitizePropertyValue function * test: Add test case for invalid property value type * feat: Use `model.NewId()` to generate valid IDs in custom profile attributes tests * refactor: Replace hardcoded IDs with dynamic variables in custom profile attributes test * refactor: restore variable name * refactor: drop undesired changes * chore: refresh app layers * feat: Update API definition to support string or string array values for custom profile attributes * test: Add test cases for multiselect custom profile attribute values * test: Add tests for multiselect custom profile attribute values * test: Isolate array value test in separate t.Run * test: Add test case for multiselect array values in custom profile attributes * refactor: Move array value test from TestCreateCPAField to TestPatchCPAValue * test: Update custom profile attributes test assertions * test: add test case for handling array values in GetCPAValue * test: Add array value tests for property value store * refactor(store): no need to convert to json the rawmessage * chore: lint * i18n * use model to interface with sqlx * fix: Allow empty strings for text, date, and select profile attributes * refactor: Filter out empty strings in multiselect and multiuser fields * refactor: Update multiuser field sanitization to validate and error on invalid IDs * refactor: Simplify sanitizePropertyValue function with reduced code duplication * fix: Allow empty user ID in custom profile attribute sanitization * refactor: Convert comment-based subtests to nested t.Run in TestSanitizePropertyValue * refactor: Convert comment-based subtests to nested t.Run tests in TestSanitizePropertyValue --------- Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
@@ -4,8 +4,6 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
sq "github.com/mattermost/squirrel"
|
||||
@@ -15,89 +13,6 @@ import (
|
||||
"github.com/mattermost/mattermost/server/v8/channels/store"
|
||||
)
|
||||
|
||||
func (s *SqlPropertyValueStore) propertyValueToInsertMap(value *model.PropertyValue) (map[string]any, error) {
|
||||
valueJSON, err := json.Marshal(value.Value)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "property_value_to_insert_map_marshal_value")
|
||||
}
|
||||
if s.IsBinaryParamEnabled() {
|
||||
valueJSON = AppendBinaryFlag(valueJSON)
|
||||
}
|
||||
|
||||
return map[string]any{
|
||||
"ID": value.ID,
|
||||
"TargetID": value.TargetID,
|
||||
"TargetType": value.TargetType,
|
||||
"GroupID": value.GroupID,
|
||||
"FieldID": value.FieldID,
|
||||
"Value": valueJSON,
|
||||
"CreateAt": value.CreateAt,
|
||||
"UpdateAt": value.UpdateAt,
|
||||
"DeleteAt": value.DeleteAt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *SqlPropertyValueStore) propertyValueToUpdateMap(value *model.PropertyValue) (map[string]any, error) {
|
||||
valueJSON, err := json.Marshal(value.Value)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "property_value_to_udpate_map_marshal_value")
|
||||
}
|
||||
if s.IsBinaryParamEnabled() {
|
||||
valueJSON = AppendBinaryFlag(valueJSON)
|
||||
}
|
||||
|
||||
return map[string]any{
|
||||
"Value": valueJSON,
|
||||
"UpdateAt": value.UpdateAt,
|
||||
"DeleteAt": value.DeleteAt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func propertyValuesFromRows(rows *sql.Rows) ([]*model.PropertyValue, error) {
|
||||
results := []*model.PropertyValue{}
|
||||
|
||||
for rows.Next() {
|
||||
var value model.PropertyValue
|
||||
var valueJSON string
|
||||
|
||||
err := rows.Scan(
|
||||
&value.ID,
|
||||
&value.TargetID,
|
||||
&value.TargetType,
|
||||
&value.GroupID,
|
||||
&value.FieldID,
|
||||
&valueJSON,
|
||||
&value.CreateAt,
|
||||
&value.UpdateAt,
|
||||
&value.DeleteAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := json.Unmarshal([]byte(valueJSON), &value.Value); err != nil {
|
||||
return nil, errors.Wrap(err, "property_values_from_rows_unmarshal_value")
|
||||
}
|
||||
|
||||
results = append(results, &value)
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func propertyValueFromRows(rows *sql.Rows) (*model.PropertyValue, error) {
|
||||
values, err := propertyValuesFromRows(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(values) > 0 {
|
||||
return values[0], nil
|
||||
}
|
||||
|
||||
return nil, sql.ErrNoRows
|
||||
}
|
||||
|
||||
type SqlPropertyValueStore struct {
|
||||
*SqlStore
|
||||
|
||||
@@ -125,15 +40,15 @@ func (s *SqlPropertyValueStore) Create(value *model.PropertyValue) (*model.Prope
|
||||
return nil, errors.Wrap(err, "property_value_create_isvalid")
|
||||
}
|
||||
|
||||
insertMap, err := s.propertyValueToInsertMap(value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
valueJSON := value.Value
|
||||
if s.IsBinaryParamEnabled() {
|
||||
valueJSON = AppendBinaryFlag(valueJSON)
|
||||
}
|
||||
|
||||
builder := s.getQueryBuilder().
|
||||
Insert("PropertyValues").
|
||||
SetMap(insertMap)
|
||||
|
||||
Columns("ID", "TargetID", "TargetType", "GroupID", "FieldID", "Value", "CreateAt", "UpdateAt", "DeleteAt").
|
||||
Values(value.ID, value.TargetID, value.TargetType, value.GroupID, value.FieldID, valueJSON, value.CreateAt, value.UpdateAt, value.DeleteAt)
|
||||
if _, err := s.GetMaster().ExecBuilder(builder); err != nil {
|
||||
return nil, errors.Wrap(err, "property_value_create_insert")
|
||||
}
|
||||
@@ -142,45 +57,23 @@ func (s *SqlPropertyValueStore) Create(value *model.PropertyValue) (*model.Prope
|
||||
}
|
||||
|
||||
func (s *SqlPropertyValueStore) Get(id string) (*model.PropertyValue, error) {
|
||||
queryString, args, err := s.tableSelectQuery.
|
||||
Where(sq.Eq{"id": id}).
|
||||
ToSql()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "property_value_get_tosql")
|
||||
}
|
||||
builder := s.tableSelectQuery.Where(sq.Eq{"id": id})
|
||||
|
||||
rows, err := s.GetReplica().Query(queryString, args...)
|
||||
if err != nil {
|
||||
var value model.PropertyValue
|
||||
if err := s.GetReplica().GetBuilder(&value, builder); err != nil {
|
||||
return nil, errors.Wrap(err, "property_value_get_select")
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
value, err := propertyValueFromRows(rows)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "property_value_get_propertyvaluefromrows")
|
||||
}
|
||||
|
||||
return value, nil
|
||||
return &value, nil
|
||||
}
|
||||
|
||||
func (s *SqlPropertyValueStore) GetMany(ids []string) ([]*model.PropertyValue, error) {
|
||||
queryString, args, err := s.tableSelectQuery.
|
||||
Where(sq.Eq{"id": ids}).
|
||||
ToSql()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "property_value_get_many_tosql")
|
||||
}
|
||||
builder := s.tableSelectQuery.Where(sq.Eq{"id": ids})
|
||||
|
||||
rows, err := s.GetReplica().Query(queryString, args...)
|
||||
if err != nil {
|
||||
var values []*model.PropertyValue
|
||||
if err := s.GetReplica().SelectBuilder(&values, builder); err != nil {
|
||||
return nil, errors.Wrap(err, "property_value_get_many_query")
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
values, err := propertyValuesFromRows(rows)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "property_value_get_many_propertyvaluesfromrows")
|
||||
}
|
||||
|
||||
if len(values) < len(ids) {
|
||||
return nil, fmt.Errorf("missmatch results: got %d results of the %d ids passed", len(values), len(ids))
|
||||
@@ -198,46 +91,35 @@ func (s *SqlPropertyValueStore) SearchPropertyValues(opts model.PropertyValueSea
|
||||
return nil, errors.New("per page must be positive integer greater than zero")
|
||||
}
|
||||
|
||||
query := s.tableSelectQuery.
|
||||
builder := s.tableSelectQuery.
|
||||
OrderBy("CreateAt ASC").
|
||||
Offset(uint64(opts.Page * opts.PerPage)).
|
||||
Limit(uint64(opts.PerPage))
|
||||
|
||||
if !opts.IncludeDeleted {
|
||||
query = query.Where(sq.Eq{"DeleteAt": 0})
|
||||
builder = builder.Where(sq.Eq{"DeleteAt": 0})
|
||||
}
|
||||
|
||||
if opts.GroupID != "" {
|
||||
query = query.Where(sq.Eq{"GroupID": opts.GroupID})
|
||||
builder = builder.Where(sq.Eq{"GroupID": opts.GroupID})
|
||||
}
|
||||
|
||||
if opts.TargetType != "" {
|
||||
query = query.Where(sq.Eq{"TargetType": opts.TargetType})
|
||||
builder = builder.Where(sq.Eq{"TargetType": opts.TargetType})
|
||||
}
|
||||
|
||||
if opts.TargetID != "" {
|
||||
query = query.Where(sq.Eq{"TargetID": opts.TargetID})
|
||||
builder = builder.Where(sq.Eq{"TargetID": opts.TargetID})
|
||||
}
|
||||
|
||||
if opts.FieldID != "" {
|
||||
query = query.Where(sq.Eq{"FieldID": opts.FieldID})
|
||||
builder = builder.Where(sq.Eq{"FieldID": opts.FieldID})
|
||||
}
|
||||
|
||||
queryString, args, err := query.ToSql()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "property_value_search_tosql")
|
||||
}
|
||||
|
||||
rows, err := s.GetReplica().Query(queryString, args...)
|
||||
if err != nil {
|
||||
var values []*model.PropertyValue
|
||||
if err := s.GetReplica().SelectBuilder(&values, builder); err != nil {
|
||||
return nil, errors.Wrap(err, "property_value_search_query")
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
values, err := propertyValuesFromRows(rows)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "property_value_search_propertyvaluesfromrows")
|
||||
}
|
||||
|
||||
return values, nil
|
||||
}
|
||||
@@ -261,14 +143,16 @@ func (s *SqlPropertyValueStore) Update(values []*model.PropertyValue) (_ []*mode
|
||||
return nil, errors.Wrap(err, "property_value_update_isvalid")
|
||||
}
|
||||
|
||||
updateMap, err := s.propertyValueToUpdateMap(value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
valueJSON := value.Value
|
||||
if s.IsBinaryParamEnabled() {
|
||||
valueJSON = AppendBinaryFlag(valueJSON)
|
||||
}
|
||||
|
||||
queryString, args, err := s.getQueryBuilder().
|
||||
Update("PropertyValues").
|
||||
SetMap(updateMap).
|
||||
Set("Value", valueJSON).
|
||||
Set("UpdateAt", value.UpdateAt).
|
||||
Set("DeleteAt", value.DeleteAt).
|
||||
Where(sq.Eq{"id": value.ID}).
|
||||
ToSql()
|
||||
if err != nil {
|
||||
|
||||
Ссылка в новой задаче
Block a user