2022-09-28 09:01:17 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
2023-03-09 18:45:04 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2022-09-28 09:01:17 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2022-10-15 21:54:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/get"
|
2022-09-28 09:01:17 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2023-03-09 18:45:04 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
2022-09-28 09:01:17 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// DeleteSession deletes an existing client session (logout).
|
|
|
|
//
|
|
|
|
// DELETE /api/v1/session/:id
|
|
|
|
func DeleteSession(router *gin.RouterGroup) {
|
|
|
|
router.DELETE("/session/:id", func(c *gin.Context) {
|
|
|
|
id := clean.ID(c.Param("id"))
|
|
|
|
|
2023-03-09 18:45:04 +01:00
|
|
|
// Abort if ID is missing.
|
2022-09-28 09:01:17 +02:00
|
|
|
if id == "" {
|
|
|
|
AbortBadRequest(c)
|
|
|
|
return
|
2022-10-15 21:54:11 +02:00
|
|
|
} else if get.Config().Public() {
|
2022-09-28 09:01:17 +02:00
|
|
|
c.JSON(http.StatusOK, gin.H{"status": "authentication disabled", "id": id})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-09 18:45:04 +01:00
|
|
|
// Find session by reference ID.
|
|
|
|
if !rnd.IsRefID(id) {
|
|
|
|
// Do nothing.
|
|
|
|
} else if s := Session(SessionID(c)); s == nil {
|
|
|
|
entity.SessionStatusUnauthorized().Abort(c)
|
|
|
|
return
|
|
|
|
} else if !acl.Resources.AllowAll(acl.ResourceUsers, s.User().AclRole(), acl.Permissions{acl.AccessAll, acl.ActionManage}) {
|
|
|
|
s.Abort(c)
|
|
|
|
return
|
|
|
|
} else if ref := entity.FindSessionByRefID(id); ref == nil {
|
|
|
|
AbortNotFound(c)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
id = ref.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete session by ID.
|
2022-10-15 21:54:11 +02:00
|
|
|
if err := get.Session().Delete(id); err != nil {
|
2022-09-28 09:01:17 +02:00
|
|
|
event.AuditErr([]string{ClientIP(c), "session %s"}, err)
|
|
|
|
} else {
|
|
|
|
event.AuditDebug([]string{ClientIP(c), "session deleted"})
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"status": "ok", "id": id})
|
|
|
|
})
|
|
|
|
}
|