MM-56881 Validate and ensure valid CustomStatus is stored (#26287)

* don't allow invalid CustomStatus

* allow empty emoji in custom status

* lint fix

* add english translation

* update for review comments.

* fix bad fix

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Scott Bishel
2024-03-04 15:53:55 -07:00
коммит произвёл GitHub
родитель 5740b43922
Коммит 30454f241d
7 изменённых файлов: 78 добавлений и 8 удалений

Просмотреть файл

@@ -399,6 +399,13 @@ func (u *User) IsValid() *AppError {
map[string]any{"Limit": UserRolesMaxLength}, "user_id="+u.Id+" roles_limit="+u.Roles, http.StatusBadRequest)
}
if u.Props != nil {
if !u.ValidateCustomStatus() {
return NewAppError("User.IsValid", "model.user.is_valid.invalidProperty.app_error",
map[string]any{"Props": u.Props}, "user_id="+u.Id, http.StatusBadRequest)
}
}
return nil
}
@@ -472,6 +479,12 @@ func (u *User) PreSave() {
if u.Password != "" {
u.Password = HashPassword(u.Password)
}
cs := u.GetCustomStatus()
if cs != nil {
cs.PreSave()
u.SetCustomStatus(cs)
}
}
// PreUpdate should be run before updating the user in the db.
@@ -508,6 +521,14 @@ func (u *User) PreUpdate() {
}
u.NotifyProps[MentionKeysNotifyProp] = strings.Join(goodKeys, ",")
}
if u.Props != nil {
cs := u.GetCustomStatus()
if cs != nil {
cs.PreSave()
u.SetCustomStatus(cs)
}
}
}
func (u *User) SetDefaultNotifications() {
@@ -711,6 +732,17 @@ func (u *User) ClearCustomStatus() {
u.Props[UserPropsKeyCustomStatus] = ""
}
func (u *User) ValidateCustomStatus() bool {
status, exists := u.Props[UserPropsKeyCustomStatus]
if exists && status != "" {
cs := u.GetCustomStatus()
if cs == nil {
return false
}
}
return true
}
func (u *User) GetFullName() string {
if u.FirstName != "" && u.LastName != "" {
return u.FirstName + " " + u.LastName