2020-06-25 01:20:58 +02:00
|
|
|
package session
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
)
|
2020-06-25 01:20:58 +02:00
|
|
|
|
|
|
|
type Saved struct {
|
2020-06-25 14:54:04 +02:00
|
|
|
User string `json:"user"`
|
2020-06-25 01:20:58 +02:00
|
|
|
Tokens []string `json:"tokens"`
|
|
|
|
Expiration int64 `json:"expiration"`
|
|
|
|
}
|
|
|
|
|
2021-11-12 05:09:17 +01:00
|
|
|
// UIDs represents a slice of unique ID strings.
|
2020-06-25 14:54:04 +02:00
|
|
|
type UIDs []string
|
|
|
|
|
2021-11-12 05:09:17 +01:00
|
|
|
// String returns all UIDs as comma separated string.
|
|
|
|
func (u UIDs) String() string {
|
|
|
|
return u.Join(",")
|
2020-12-31 12:34:06 +01:00
|
|
|
}
|
|
|
|
|
2021-11-12 05:09:17 +01:00
|
|
|
// Join returns all UIDs as custom separated string.
|
|
|
|
func (u UIDs) Join(s string) string {
|
|
|
|
return strings.Join(u, s)
|
2020-06-25 14:54:04 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 01:20:58 +02:00
|
|
|
type Data struct {
|
2020-10-03 13:50:30 +02:00
|
|
|
User entity.User `json:"user"` // Session user, guest or anonymous person.
|
|
|
|
Tokens []string `json:"tokens"` // Slice of secret share tokens.
|
|
|
|
Shares UIDs `json:"shares"` // Slice of shared entity UIDs.
|
2020-06-25 14:54:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s Data) Saved() Saved {
|
2020-10-03 13:50:30 +02:00
|
|
|
return Saved{User: s.User.UserUID, Tokens: s.Tokens}
|
2020-06-25 14:54:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s Data) Invalid() bool {
|
2020-10-03 13:50:30 +02:00
|
|
|
return s.User.ID == 0 || s.User.UserUID == "" || (s.Guest() && s.NoShares())
|
2020-06-25 01:20:58 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
func (s Data) Valid() bool {
|
|
|
|
return !s.Invalid()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Data) Guest() bool {
|
|
|
|
return s.User.Guest()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Data) NoShares() bool {
|
|
|
|
return len(s.Shares) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Data) HasShare(uid string) bool {
|
|
|
|
for _, share := range s.Shares {
|
|
|
|
if share == uid {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2020-06-25 01:20:58 +02:00
|
|
|
}
|