2020-06-29 21:14:34 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2020-07-07 10:51:55 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
2020-06-30 08:50:44 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
2020-06-29 21:14:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// PUT /api/v1/users/:uid/password
|
|
|
|
func ChangePassword(router *gin.RouterGroup) {
|
|
|
|
router.PUT("/users/:uid/password", func(c *gin.Context) {
|
2020-06-30 08:50:44 +02:00
|
|
|
conf := service.Config()
|
|
|
|
|
|
|
|
if conf.Public() {
|
2020-07-07 10:51:55 +02:00
|
|
|
Abort(c, http.StatusForbidden, i18n.ErrPublic)
|
2020-06-30 08:50:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-29 21:14:34 +02:00
|
|
|
s := Auth(SessionID(c), acl.ResourcePeople, acl.ActionUpdateSelf)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
2020-07-04 12:54:35 +02:00
|
|
|
AbortUnauthorized(c)
|
2020-06-29 21:14:34 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
uid := c.Param("uid")
|
|
|
|
m := entity.FindPersonByUID(uid)
|
|
|
|
|
|
|
|
if m == nil {
|
2020-07-07 10:51:55 +02:00
|
|
|
Abort(c, http.StatusNotFound, i18n.ErrUserNotFound)
|
2020-06-29 21:14:34 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
f := form.ChangePassword{}
|
|
|
|
|
|
|
|
if err := c.BindJSON(&f); err != nil {
|
2020-07-07 10:51:55 +02:00
|
|
|
Error(c, http.StatusBadRequest, err, i18n.ErrInvalidPassword)
|
2020-06-29 21:14:34 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.InvalidPassword(f.OldPassword) {
|
2020-07-07 10:51:55 +02:00
|
|
|
Abort(c, http.StatusBadRequest, i18n.ErrInvalidPassword)
|
2020-06-29 21:14:34 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.SetPassword(f.NewPassword); err != nil {
|
2020-07-07 10:51:55 +02:00
|
|
|
Error(c, http.StatusBadRequest, err, i18n.ErrInvalidPassword)
|
2020-06-29 21:14:34 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-07 10:51:55 +02:00
|
|
|
c.JSON(http.StatusOK, i18n.NewResponse(http.StatusOK, i18n.MsgPasswordChanged))
|
2020-06-29 21:14:34 +02:00
|
|
|
})
|
|
|
|
}
|