2022-10-17 19:07:38 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
2022-10-19 05:09:09 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2022-10-17 19:07:38 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/get"
|
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
2022-10-19 05:09:09 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2022-10-17 19:07:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// UpdateUser updates the profile information of the currently authenticated user.
|
|
|
|
//
|
|
|
|
// PUT /api/v1/users/:uid
|
|
|
|
func UpdateUser(router *gin.RouterGroup) {
|
|
|
|
router.PUT("/users/:uid", func(c *gin.Context) {
|
|
|
|
conf := get.Config()
|
|
|
|
|
2023-03-08 23:30:39 +01:00
|
|
|
if conf.Public() || conf.DisableSettings() {
|
2022-10-17 19:07:38 +02:00
|
|
|
AbortForbidden(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-08 23:30:39 +01:00
|
|
|
// Check if the session user is allowed to manage all accounts or update his/her own account.
|
2022-10-17 19:07:38 +02:00
|
|
|
s := AuthAny(c, acl.ResourceUsers, acl.Permissions{acl.ActionManage, acl.AccessOwn, acl.ActionUpdate})
|
|
|
|
|
|
|
|
if s.Abort(c) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
uid := clean.UID(c.Param("uid"))
|
|
|
|
|
|
|
|
m := entity.FindUserByUID(uid)
|
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrUserNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-19 05:09:09 +02:00
|
|
|
// Init form with model values.
|
|
|
|
f, err := m.Form()
|
2022-10-17 19:07:38 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
AbortSaveFailed(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-19 05:09:09 +02:00
|
|
|
// Update form with values from request.
|
2022-10-17 19:07:38 +02:00
|
|
|
if err = c.BindJSON(&f); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
AbortBadRequest(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-08 23:30:39 +01:00
|
|
|
// Check if the session user is has user management privileges.
|
|
|
|
isPrivileged := acl.Resources.AllowAll(acl.ResourceUsers, s.User().AclRole(), acl.Permissions{acl.AccessAll, acl.ActionManage})
|
|
|
|
|
|
|
|
// Prevent super admins from locking themselves out.
|
|
|
|
if u := s.User(); u.IsSuperAdmin() && u.Equal(m) && !f.CanLogin {
|
|
|
|
f.CanLogin = true
|
|
|
|
}
|
|
|
|
|
2022-10-19 05:09:09 +02:00
|
|
|
// Save model with values from form.
|
2023-03-08 23:30:39 +01:00
|
|
|
if err = m.SaveForm(f, isPrivileged); err != nil {
|
2022-10-17 19:07:38 +02:00
|
|
|
log.Error(err)
|
|
|
|
AbortSaveFailed(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the session cache, as it contains user information.
|
|
|
|
s.ClearCache()
|
|
|
|
|
|
|
|
m = entity.FindUserByUID(uid)
|
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
AbortEntityNotFound(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, m)
|
|
|
|
})
|
|
|
|
}
|