2022-09-02 21:30:50 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/mail"
|
2022-09-30 00:42:19 +02:00
|
|
|
"path"
|
2022-09-28 09:01:17 +02:00
|
|
|
"strings"
|
2022-09-02 21:30:50 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
2022-11-22 22:14:34 +01:00
|
|
|
"github.com/ulule/deepcopier"
|
2022-09-02 21:30:50 +02:00
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
2022-09-28 09:01:17 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2022-09-02 21:30:50 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2023-03-08 23:30:39 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/authn"
|
2022-09-02 21:30:50 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2023-02-20 15:54:33 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/list"
|
2022-09-02 21:30:50 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
2022-10-19 22:23:12 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2022-09-02 21:30:50 +02:00
|
|
|
)
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// User identifier prefixes.
|
|
|
|
const (
|
2022-09-30 19:15:10 +02:00
|
|
|
UserUID = byte('u')
|
|
|
|
UserPrefix = "user"
|
|
|
|
OwnerUnknown = ""
|
2022-09-28 09:01:17 +02:00
|
|
|
)
|
|
|
|
|
2022-11-22 22:14:34 +01:00
|
|
|
// UsernameLength specifies the minimum length of the username in characters.
|
|
|
|
var UsernameLength = 1
|
2022-09-28 09:01:17 +02:00
|
|
|
|
2022-11-22 22:14:34 +01:00
|
|
|
// PasswordLength specifies the minimum length of the password in characters.
|
|
|
|
var PasswordLength = 4
|
2022-09-02 21:30:50 +02:00
|
|
|
|
2023-03-14 21:47:14 +01:00
|
|
|
// UsersPath is the relative path for user assets.
|
|
|
|
var UsersPath = "users"
|
|
|
|
|
2022-09-02 21:30:50 +02:00
|
|
|
// Users represents a list of users.
|
|
|
|
type Users []User
|
|
|
|
|
|
|
|
// User represents a person that may optionally log in as user.
|
|
|
|
type User struct {
|
2023-03-09 15:12:10 +01:00
|
|
|
ID int `gorm:"primary_key" json:"ID" yaml:"-"`
|
2022-09-30 19:15:10 +02:00
|
|
|
UUID string `gorm:"type:VARBINARY(64);column:user_uuid;index;" json:"UUID,omitempty" yaml:"UUID,omitempty"`
|
2022-10-02 11:38:30 +02:00
|
|
|
UserUID string `gorm:"type:VARBINARY(42);column:user_uid;unique_index;" json:"UID" yaml:"UID"`
|
2023-03-08 23:30:39 +01:00
|
|
|
AuthProvider string `gorm:"type:VARBINARY(128);default:'';" json:"AuthProvider" yaml:"AuthProvider,omitempty"`
|
|
|
|
AuthID string `gorm:"type:VARBINARY(255);index;default:'';" json:"AuthID" yaml:"AuthID,omitempty"`
|
2023-01-24 06:05:31 +01:00
|
|
|
UserName string `gorm:"size:255;index;" json:"Name" yaml:"Name,omitempty"`
|
2022-09-28 09:01:17 +02:00
|
|
|
DisplayName string `gorm:"size:200;" json:"DisplayName" yaml:"DisplayName,omitempty"`
|
|
|
|
UserEmail string `gorm:"size:255;index;" json:"Email" yaml:"Email,omitempty"`
|
|
|
|
BackupEmail string `gorm:"size:255;" json:"BackupEmail,omitempty" yaml:"BackupEmail,omitempty"`
|
2023-03-08 23:30:39 +01:00
|
|
|
UserRole string `gorm:"size:64;default:'';" json:"Role" yaml:"Role,omitempty"`
|
|
|
|
UserAttr string `gorm:"size:1024;" json:"Attr" yaml:"Attr,omitempty"`
|
|
|
|
SuperAdmin bool `json:"SuperAdmin" yaml:"SuperAdmin,omitempty"`
|
|
|
|
CanLogin bool `json:"CanLogin" yaml:"CanLogin,omitempty"`
|
|
|
|
LoginAt *time.Time `json:"LoginAt" yaml:"LoginAt,omitempty"`
|
2022-10-02 11:38:30 +02:00
|
|
|
ExpiresAt *time.Time `sql:"index" json:"ExpiresAt,omitempty" yaml:"ExpiresAt,omitempty"`
|
2023-03-08 23:30:39 +01:00
|
|
|
WebDAV bool `gorm:"column:webdav;" json:"WebDAV" yaml:"WebDAV,omitempty"`
|
|
|
|
BasePath string `gorm:"type:VARBINARY(1024);" json:"BasePath" yaml:"BasePath,omitempty"`
|
|
|
|
UploadPath string `gorm:"type:VARBINARY(1024);" json:"UploadPath" yaml:"UploadPath,omitempty"`
|
|
|
|
CanInvite bool `json:"CanInvite" yaml:"CanInvite,omitempty"`
|
2022-09-28 09:01:17 +02:00
|
|
|
InviteToken string `gorm:"type:VARBINARY(64);index;" json:"-" yaml:"-"`
|
|
|
|
InvitedBy string `gorm:"size:64;" json:"-" yaml:"-"`
|
|
|
|
VerifyToken string `gorm:"type:VARBINARY(64);" json:"-" yaml:"-"`
|
|
|
|
VerifiedAt *time.Time `json:"VerifiedAt,omitempty" yaml:"VerifiedAt,omitempty"`
|
|
|
|
ConsentAt *time.Time `json:"ConsentAt,omitempty" yaml:"ConsentAt,omitempty"`
|
|
|
|
BornAt *time.Time `sql:"index" json:"BornAt,omitempty" yaml:"BornAt,omitempty"`
|
|
|
|
UserDetails *UserDetails `gorm:"PRELOAD:true;foreignkey:UserUID;association_foreignkey:UserUID;" json:"Details,omitempty" yaml:"Details,omitempty"`
|
|
|
|
UserSettings *UserSettings `gorm:"PRELOAD:true;foreignkey:UserUID;association_foreignkey:UserUID;" json:"Settings,omitempty" yaml:"Settings,omitempty"`
|
2022-10-04 00:54:39 +02:00
|
|
|
UserShares UserShares `gorm:"-" json:"Shares,omitempty" yaml:"Shares,omitempty"`
|
2022-09-28 09:01:17 +02:00
|
|
|
ResetToken string `gorm:"type:VARBINARY(64);" json:"-" yaml:"-"`
|
|
|
|
PreviewToken string `gorm:"type:VARBINARY(64);column:preview_token;" json:"-" yaml:"-"`
|
|
|
|
DownloadToken string `gorm:"type:VARBINARY(64);column:download_token;" json:"-" yaml:"-"`
|
2022-10-17 19:07:38 +02:00
|
|
|
Thumb string `gorm:"type:VARBINARY(128);index;default:'';" json:"Thumb" yaml:"Thumb,omitempty"`
|
|
|
|
ThumbSrc string `gorm:"type:VARBINARY(8);default:'';" json:"ThumbSrc" yaml:"ThumbSrc,omitempty"`
|
2022-09-28 09:01:17 +02:00
|
|
|
RefID string `gorm:"type:VARBINARY(16);" json:"-" yaml:"-"`
|
|
|
|
CreatedAt time.Time `json:"CreatedAt" yaml:"-"`
|
|
|
|
UpdatedAt time.Time `json:"UpdatedAt" yaml:"-"`
|
|
|
|
DeletedAt *time.Time `sql:"index" json:"DeletedAt,omitempty" yaml:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// TableName returns the entity table name.
|
2022-09-02 21:30:50 +02:00
|
|
|
func (User) TableName() string {
|
2022-10-02 22:09:02 +02:00
|
|
|
return "auth_users"
|
2022-09-28 09:01:17 +02:00
|
|
|
}
|
|
|
|
|
2023-03-09 15:12:10 +01:00
|
|
|
// NewUser creates a new user entity with defaults.
|
2022-09-28 09:01:17 +02:00
|
|
|
func NewUser() (m *User) {
|
|
|
|
uid := rnd.GenerateUID(UserUID)
|
|
|
|
|
|
|
|
return &User{
|
2022-10-13 22:11:02 +02:00
|
|
|
UserUID: uid,
|
|
|
|
UserDetails: NewUserDetails(uid),
|
|
|
|
UserSettings: NewUserSettings(uid),
|
|
|
|
PreviewToken: GenerateToken(),
|
|
|
|
DownloadToken: GenerateToken(),
|
|
|
|
RefID: rnd.RefID(UserPrefix),
|
2022-09-28 09:01:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-09 15:12:10 +01:00
|
|
|
// LdapUser creates an LDAP user entity.
|
|
|
|
func LdapUser(username, dn string) User {
|
|
|
|
return User{
|
|
|
|
UserName: clean.Username(username),
|
|
|
|
AuthID: dn,
|
|
|
|
AuthProvider: authn.ProviderLDAP.String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 22:11:02 +02:00
|
|
|
// FindUser returns the matching user or nil if it was not found.
|
|
|
|
func FindUser(find User) *User {
|
|
|
|
m := &User{}
|
|
|
|
|
|
|
|
// Build query.
|
|
|
|
stmt := UnscopedDb()
|
2023-03-09 15:59:08 +01:00
|
|
|
if find.ID != 0 && find.UserName != "" {
|
|
|
|
stmt = stmt.Where("id = ? OR user_name = ?", find.ID, find.UserName)
|
|
|
|
} else if find.ID != 0 {
|
2022-10-13 22:11:02 +02:00
|
|
|
stmt = stmt.Where("id = ?", find.ID)
|
|
|
|
} else if rnd.IsUID(find.UserUID, UserUID) {
|
|
|
|
stmt = stmt.Where("user_uid = ?", find.UserUID)
|
2023-03-14 21:47:14 +01:00
|
|
|
} else if find.AuthProvider != "" && find.AuthID != "" && find.UserName != "" {
|
|
|
|
stmt = stmt.Where("auth_provider = ? AND auth_id = ? OR user_name = ?", find.AuthProvider, find.AuthID, find.UserName)
|
2022-10-13 22:11:02 +02:00
|
|
|
} else if find.UserName != "" {
|
|
|
|
stmt = stmt.Where("user_name = ?", find.UserName)
|
|
|
|
} else if find.UserEmail != "" {
|
|
|
|
stmt = stmt.Where("user_email = ?", find.UserEmail)
|
2022-11-22 22:14:34 +01:00
|
|
|
} else if find.AuthProvider != "" && find.AuthID != "" {
|
|
|
|
stmt = stmt.Where("auth_provider = ? AND auth_id = ?", find.AuthProvider, find.AuthID)
|
2022-10-13 22:11:02 +02:00
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find matching record.
|
|
|
|
if err := stmt.First(m).Error; err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch related records.
|
|
|
|
return m.LoadRelated()
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// FirstOrCreateUser returns an existing record, inserts a new record, or returns nil in case of an error.
|
|
|
|
func FirstOrCreateUser(m *User) *User {
|
2022-10-13 22:11:02 +02:00
|
|
|
if m == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-28 09:01:17 +02:00
|
|
|
|
2022-10-13 22:11:02 +02:00
|
|
|
if found := FindUser(*m); found != nil {
|
|
|
|
return found
|
|
|
|
} else if err := m.Create(); err != nil {
|
2022-09-28 09:01:17 +02:00
|
|
|
event.AuditErr([]string{"user", "failed to create", "%s"}, err)
|
|
|
|
return nil
|
2022-10-13 22:11:02 +02:00
|
|
|
} else {
|
|
|
|
return m
|
2022-09-28 09:01:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 22:11:02 +02:00
|
|
|
// FindUserByName returns the matching user or nil if it was not found.
|
2023-03-08 23:30:39 +01:00
|
|
|
func FindUserByName(userName string) *User {
|
|
|
|
userName = clean.Username(userName)
|
2022-12-29 23:41:43 +01:00
|
|
|
|
2023-03-08 23:30:39 +01:00
|
|
|
if userName == "" {
|
2022-09-28 09:01:17 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-08 23:30:39 +01:00
|
|
|
return FindUser(User{UserName: userName})
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindLocalUser returns the matching local user or nil if it was not found.
|
|
|
|
func FindLocalUser(userName string) *User {
|
2023-03-09 15:12:10 +01:00
|
|
|
name := clean.Username(userName)
|
2023-03-08 23:30:39 +01:00
|
|
|
|
2023-03-09 15:12:10 +01:00
|
|
|
if name == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
m := &User{}
|
|
|
|
providers := authn.LocalProviders
|
|
|
|
|
|
|
|
// Build query.
|
|
|
|
if err := UnscopedDb().
|
|
|
|
Where("user_name = ? AND auth_provider IN (?)", name, providers).
|
|
|
|
First(m).Error; err != nil {
|
2023-03-08 23:30:39 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-09 15:12:10 +01:00
|
|
|
// Return with related records.
|
|
|
|
return m.LoadRelated()
|
2022-09-28 09:01:17 +02:00
|
|
|
}
|
|
|
|
|
2022-10-13 22:11:02 +02:00
|
|
|
// FindUserByUID returns the matching user or nil if it was not found.
|
2022-09-28 09:01:17 +02:00
|
|
|
func FindUserByUID(uid string) *User {
|
2022-10-02 11:38:30 +02:00
|
|
|
if rnd.InvalidUID(uid, UserUID) {
|
2022-09-28 09:01:17 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-13 22:11:02 +02:00
|
|
|
return FindUser(User{UserUID: uid})
|
2022-09-28 09:01:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// UID returns the unique id as string.
|
|
|
|
func (m *User) UID() string {
|
2022-10-02 11:38:30 +02:00
|
|
|
if m == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
return m.UserUID
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
// SameUID checks if the given uid matches the own uid.
|
|
|
|
func (m *User) SameUID(uid string) bool {
|
|
|
|
if m == nil {
|
|
|
|
return false
|
|
|
|
} else if m.UserUID == "" || rnd.InvalidUID(uid, UserUID) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.UserUID == uid
|
|
|
|
}
|
|
|
|
|
2022-09-02 21:30:50 +02:00
|
|
|
// InitAccount sets the name and password of the initial admin account.
|
2022-10-13 22:11:02 +02:00
|
|
|
func (m *User) InitAccount(initName, initPasswd string) (updated bool) {
|
|
|
|
// User must exist and the password must not be empty.
|
|
|
|
initPasswd = strings.TrimSpace(initPasswd)
|
|
|
|
if rnd.InvalidUID(m.UserUID, UserUID) || initPasswd == "" {
|
2022-09-02 21:30:50 +02:00
|
|
|
return false
|
2022-10-13 22:11:02 +02:00
|
|
|
} else if !m.CanLogIn() {
|
|
|
|
log.Warnf("users: %s account is not allowed to log in", m.String())
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
2022-10-13 22:11:02 +02:00
|
|
|
// Abort if user has a password.
|
|
|
|
existingPasswd := FindPassword(m.UserUID)
|
2022-09-02 21:30:50 +02:00
|
|
|
|
2022-10-13 22:11:02 +02:00
|
|
|
if existingPasswd != nil {
|
2022-09-02 21:30:50 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-13 22:11:02 +02:00
|
|
|
// Set initial password.
|
|
|
|
initialPasswd := NewPassword(m.UserUID, initPasswd)
|
2022-09-02 21:30:50 +02:00
|
|
|
|
2022-09-02 22:55:57 +02:00
|
|
|
// Save password.
|
2022-10-13 22:11:02 +02:00
|
|
|
if err := initialPasswd.Save(); err != nil {
|
2022-10-02 11:38:30 +02:00
|
|
|
event.AuditErr([]string{"user %s", "failed to change password", "%s"}, m.RefID, err)
|
2022-09-02 21:30:50 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-13 22:11:02 +02:00
|
|
|
// Change username if needed.
|
|
|
|
if initName != "" && initName != m.UserName {
|
2023-03-08 23:30:39 +01:00
|
|
|
if err := m.UpdateUsername(initName); err != nil {
|
2022-10-13 22:11:02 +02:00
|
|
|
event.AuditErr([]string{"user %s", "failed to change username to %s", "%s"}, m.RefID, clean.Log(initName), err)
|
|
|
|
}
|
2022-09-02 22:55:57 +02:00
|
|
|
}
|
|
|
|
|
2022-09-02 21:30:50 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create new entity in the database.
|
2022-09-28 09:01:17 +02:00
|
|
|
func (m *User) Create() (err error) {
|
|
|
|
err = Db().Create(m).Error
|
2022-09-02 21:30:50 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if err == nil {
|
|
|
|
m.SaveRelated()
|
|
|
|
}
|
2022-09-02 21:30:50 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
return err
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
// Save updates the record in the database or inserts a new record if it does not already exist.
|
2022-09-28 09:01:17 +02:00
|
|
|
func (m *User) Save() (err error) {
|
2022-10-13 22:11:02 +02:00
|
|
|
m.GenerateTokens(false)
|
|
|
|
|
2023-02-20 20:24:04 +01:00
|
|
|
err = UnscopedDb().Save(m).Error
|
2022-09-02 21:30:50 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if err == nil {
|
|
|
|
m.SaveRelated()
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-09-02 21:30:50 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Delete marks the entity as deleted.
|
2023-02-20 20:24:04 +01:00
|
|
|
func (m *User) Delete() (err error) {
|
2022-09-28 09:01:17 +02:00
|
|
|
if m.ID <= 1 {
|
|
|
|
return fmt.Errorf("cannot delete system user")
|
2023-02-20 20:24:04 +01:00
|
|
|
} else if m.UserUID == "" {
|
|
|
|
return fmt.Errorf("uid is required to delete user")
|
2022-09-28 09:01:17 +02:00
|
|
|
}
|
2022-09-02 21:30:50 +02:00
|
|
|
|
2023-02-20 20:24:04 +01:00
|
|
|
if err = UnscopedDb().Delete(Session{}, "user_uid = ?", m.UserUID).Error; err != nil {
|
|
|
|
event.AuditErr([]string{"user %s", "delete", "failed to remove sessions", "%s"}, m.RefID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = Db().Delete(m).Error
|
|
|
|
|
|
|
|
FlushSessionCache()
|
|
|
|
|
|
|
|
return err
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Deleted checks if the user account has been deleted.
|
|
|
|
func (m *User) Deleted() bool {
|
|
|
|
if m.DeletedAt == nil {
|
|
|
|
return false
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
return !m.DeletedAt.IsZero()
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// LoadRelated loads related settings and details.
|
|
|
|
func (m *User) LoadRelated() *User {
|
|
|
|
m.Settings()
|
|
|
|
m.Details()
|
2022-09-02 21:30:50 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
return m
|
|
|
|
}
|
2022-09-02 21:30:50 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// SaveRelated saves related settings and details.
|
|
|
|
func (m *User) SaveRelated() *User {
|
|
|
|
if err := m.Settings().Save(); err != nil {
|
2022-10-02 11:38:30 +02:00
|
|
|
event.AuditErr([]string{"user %s", "failed to save settings", "%s"}, m.RefID, err)
|
2022-09-28 09:01:17 +02:00
|
|
|
}
|
|
|
|
if err := m.Details().Save(); err != nil {
|
2022-10-02 11:38:30 +02:00
|
|
|
event.AuditErr([]string{"user %s", "failed to save details", "%s"}, m.RefID, err)
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Updates multiple properties in the database.
|
|
|
|
func (m *User) Updates(values interface{}) error {
|
|
|
|
return UnscopedDb().Model(m).Updates(values).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// BeforeCreate sets a random UID if needed before inserting a new row to the database.
|
|
|
|
func (m *User) BeforeCreate(scope *gorm.Scope) error {
|
|
|
|
if m.UserSettings != nil {
|
|
|
|
m.UserSettings.UserUID = m.UserUID
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if m.UserDetails != nil {
|
|
|
|
m.UserDetails.UserUID = m.UserUID
|
|
|
|
}
|
2022-09-02 21:30:50 +02:00
|
|
|
|
2022-10-13 22:11:02 +02:00
|
|
|
m.GenerateTokens(false)
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if rnd.InvalidRefID(m.RefID) {
|
|
|
|
m.RefID = rnd.RefID(UserPrefix)
|
2022-10-02 11:38:30 +02:00
|
|
|
Log("user", "set ref id", scope.SetColumn("RefID", m.RefID))
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if rnd.IsUnique(m.UserUID, UserUID) {
|
2022-09-02 21:30:50 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
m.UserUID = rnd.GenerateUID(UserUID)
|
|
|
|
return scope.SetColumn("UserUID", m.UserUID)
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Expired checks if the user account has expired.
|
|
|
|
func (m *User) Expired() bool {
|
|
|
|
if m.ExpiresAt == nil {
|
|
|
|
return false
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
return m.ExpiresAt.Before(time.Now())
|
|
|
|
}
|
2022-09-02 21:30:50 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Disabled checks if the user account has been deleted or has expired.
|
|
|
|
func (m *User) Disabled() bool {
|
2022-10-13 22:11:02 +02:00
|
|
|
return m.Deleted() || m.Expired() && !m.SuperAdmin
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
2023-03-08 23:30:39 +01:00
|
|
|
// UpdateLoginTime updates the login timestamp and returns it if successful.
|
|
|
|
func (m *User) UpdateLoginTime() *time.Time {
|
|
|
|
if m == nil {
|
|
|
|
return nil
|
|
|
|
} else if m.Deleted() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
timeStamp := TimePointer()
|
|
|
|
|
|
|
|
if err := Db().Model(m).UpdateColumn("LoginAt", timeStamp).Error; err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
m.LoginAt = timeStamp
|
|
|
|
|
|
|
|
return timeStamp
|
|
|
|
}
|
|
|
|
|
2022-10-13 22:11:02 +02:00
|
|
|
// CanLogIn checks if the user is allowed to log in and use the web UI.
|
|
|
|
func (m *User) CanLogIn() bool {
|
2023-02-20 20:24:04 +01:00
|
|
|
if m == nil {
|
|
|
|
return false
|
2023-03-10 13:20:16 +01:00
|
|
|
} else if m.Deleted() || m.HasProvider(authn.ProviderNone) {
|
2023-02-20 20:24:04 +01:00
|
|
|
return false
|
|
|
|
} else if !m.CanLogin && !m.SuperAdmin || m.ID <= 0 || m.UserName == "" {
|
2022-10-13 22:11:02 +02:00
|
|
|
return false
|
|
|
|
} else if role := m.AclRole(); m.Disabled() || role == acl.RoleUnknown {
|
2022-09-28 09:01:17 +02:00
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
return acl.Resources.Allow(acl.ResourceConfig, role, acl.AccessOwn)
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 22:11:02 +02:00
|
|
|
// CanUseWebDAV checks whether the user is allowed to use WebDAV to synchronize files.
|
|
|
|
func (m *User) CanUseWebDAV() bool {
|
2023-03-10 13:20:16 +01:00
|
|
|
if m == nil {
|
|
|
|
return false
|
|
|
|
} else if m.Deleted() || m.HasProvider(authn.ProviderNone) {
|
|
|
|
return false
|
|
|
|
} else if role := m.AclRole(); m.Disabled() || !m.WebDAV || m.ID <= 0 || m.UserName == "" || role == acl.RoleUnknown {
|
2022-09-02 21:30:50 +02:00
|
|
|
return false
|
2022-09-28 09:01:17 +02:00
|
|
|
} else {
|
|
|
|
return acl.Resources.Allow(acl.ResourcePhotos, role, acl.ActionUpload)
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 22:11:02 +02:00
|
|
|
// CanUpload checks if the user is allowed to upload files.
|
|
|
|
func (m *User) CanUpload() bool {
|
2023-03-10 13:20:16 +01:00
|
|
|
if m == nil {
|
|
|
|
return false
|
|
|
|
} else if m.Deleted() || m.HasProvider(authn.ProviderNone) {
|
|
|
|
return false
|
|
|
|
} else if role := m.AclRole(); m.Disabled() || role == acl.RoleUnknown {
|
2022-09-30 00:42:19 +02:00
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
return acl.Resources.Allow(acl.ResourcePhotos, role, acl.ActionUpload)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-13 17:52:48 +01:00
|
|
|
// DefaultBasePath returns the default base path of the user based on the user name.
|
|
|
|
func (m *User) DefaultBasePath() string {
|
2023-03-14 16:36:30 +01:00
|
|
|
if s := m.Handle(); s == "" {
|
2023-03-13 17:52:48 +01:00
|
|
|
return ""
|
2023-03-14 16:36:30 +01:00
|
|
|
} else {
|
2023-03-14 21:47:14 +01:00
|
|
|
return path.Join(UsersPath, s)
|
2023-03-13 17:52:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-20 15:54:33 +01:00
|
|
|
// GetBasePath returns the user's relative base path.
|
|
|
|
func (m *User) GetBasePath() string {
|
2023-03-13 17:52:48 +01:00
|
|
|
if m.BasePath == "" && m.HasRole("contributor") {
|
|
|
|
m.BasePath = m.DefaultBasePath()
|
|
|
|
}
|
|
|
|
|
2023-02-20 15:54:33 +01:00
|
|
|
return m.BasePath
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBasePath changes the user's relative base path.
|
2022-09-30 00:42:19 +02:00
|
|
|
func (m *User) SetBasePath(dir string) *User {
|
2023-02-20 15:54:33 +01:00
|
|
|
if list.Contains(list.List{"", ".", "./", "/", "\\"}, dir) {
|
|
|
|
m.BasePath = ""
|
2023-03-13 17:36:56 +01:00
|
|
|
} else if dir == "~" && m.UserName != "" {
|
2023-03-13 17:52:48 +01:00
|
|
|
m.BasePath = m.DefaultBasePath()
|
2023-02-20 15:54:33 +01:00
|
|
|
} else {
|
|
|
|
m.BasePath = clean.UserPath(dir)
|
|
|
|
}
|
2022-09-30 00:42:19 +02:00
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2023-02-20 15:54:33 +01:00
|
|
|
// GetUploadPath returns the user's relative upload path.
|
|
|
|
func (m *User) GetUploadPath() string {
|
|
|
|
basePath := m.GetBasePath()
|
|
|
|
|
|
|
|
if list.Contains(list.List{"", ".", "./"}, m.UploadPath) {
|
|
|
|
return basePath
|
|
|
|
} else if basePath != "" && strings.HasPrefix(m.UploadPath, basePath+"/") {
|
|
|
|
return m.UploadPath
|
2023-03-13 17:36:56 +01:00
|
|
|
} else if basePath == "" && m.UploadPath == "~" && m.UserName != "" {
|
2023-03-13 17:52:48 +01:00
|
|
|
return m.DefaultBasePath()
|
2023-02-20 15:54:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return path.Join(basePath, m.UploadPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetUploadPath changes the user's relative upload path.
|
2022-09-30 00:42:19 +02:00
|
|
|
func (m *User) SetUploadPath(dir string) *User {
|
2023-02-20 15:54:33 +01:00
|
|
|
basePath := m.GetBasePath()
|
|
|
|
|
|
|
|
if list.Contains(list.List{"", ".", "./", "/", "\\"}, dir) {
|
|
|
|
m.UploadPath = ""
|
2023-03-13 17:36:56 +01:00
|
|
|
} else if basePath == "" && dir == "~" && m.UserName != "" {
|
2023-03-13 17:52:48 +01:00
|
|
|
m.UploadPath = m.DefaultBasePath()
|
2022-09-30 00:42:19 +02:00
|
|
|
} else {
|
2023-02-20 15:54:33 +01:00
|
|
|
m.UploadPath = clean.UserPath(dir)
|
2022-09-30 00:42:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2022-09-02 21:30:50 +02:00
|
|
|
// String returns an identifier that can be used in logs.
|
|
|
|
func (m *User) String() string {
|
2023-03-08 23:30:39 +01:00
|
|
|
if n := m.Username(); n != "" {
|
2023-02-20 20:24:04 +01:00
|
|
|
return clean.LogQuote(n)
|
2022-09-28 09:01:17 +02:00
|
|
|
} else if n = m.FullName(); n != "" {
|
2023-02-20 20:24:04 +01:00
|
|
|
return clean.LogQuote(n)
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return clean.Log(m.UserUID)
|
|
|
|
}
|
|
|
|
|
2023-03-08 23:30:39 +01:00
|
|
|
// Provider returns the authentication provider name.
|
2023-03-09 15:12:10 +01:00
|
|
|
func (m *User) Provider() authn.ProviderType {
|
2023-03-08 23:30:39 +01:00
|
|
|
if m.AuthProvider != "" {
|
2023-03-09 15:12:10 +01:00
|
|
|
return authn.ProviderType(m.AuthProvider)
|
2023-03-08 23:30:39 +01:00
|
|
|
} else if m.ID == Visitor.ID {
|
2023-03-13 16:04:37 +01:00
|
|
|
return authn.ProviderLink
|
2023-03-08 23:30:39 +01:00
|
|
|
} else if m.ID == 1 {
|
|
|
|
return authn.ProviderLocal
|
|
|
|
} else if m.UserName != "" && m.ID > 0 {
|
|
|
|
return authn.ProviderDefault
|
|
|
|
}
|
|
|
|
|
|
|
|
return authn.ProviderNone
|
|
|
|
}
|
|
|
|
|
2023-03-10 13:20:16 +01:00
|
|
|
// HasProvider checks if the user has the given auth provider.
|
|
|
|
func (m *User) HasProvider(t authn.ProviderType) bool {
|
|
|
|
return t.String() == m.Provider().String()
|
|
|
|
}
|
|
|
|
|
2023-03-08 23:30:39 +01:00
|
|
|
// SetProvider set the authentication provider.
|
2023-03-09 15:12:10 +01:00
|
|
|
func (m *User) SetProvider(t authn.ProviderType) *User {
|
2023-03-08 23:30:39 +01:00
|
|
|
if m == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-09 15:12:10 +01:00
|
|
|
m.AuthProvider = t.String()
|
2023-03-08 23:30:39 +01:00
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// Username returns the user's login name as sanitized string.
|
|
|
|
func (m *User) Username() string {
|
2022-09-28 09:01:17 +02:00
|
|
|
return clean.Username(m.UserName)
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
2023-03-08 23:30:39 +01:00
|
|
|
// SetUsername sets the login username to the specified string.
|
|
|
|
func (m *User) SetUsername(login string) (err error) {
|
2022-10-19 05:09:09 +02:00
|
|
|
if m.ID < 0 {
|
|
|
|
return fmt.Errorf("system users cannot be modified")
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
login = clean.Username(login)
|
2022-09-02 21:30:50 +02:00
|
|
|
|
|
|
|
// Empty?
|
|
|
|
if login == "" {
|
2022-10-19 05:09:09 +02:00
|
|
|
return fmt.Errorf("username is empty")
|
|
|
|
} else if m.UserName == login {
|
|
|
|
return nil
|
|
|
|
} else if m.UserName != "" && m.ID != 1 {
|
|
|
|
return fmt.Errorf("username cannot be changed")
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update username and slug.
|
2022-09-28 09:01:17 +02:00
|
|
|
m.UserName = login
|
2022-09-02 21:30:50 +02:00
|
|
|
|
|
|
|
// Update display name.
|
2022-10-19 05:09:09 +02:00
|
|
|
if m.DisplayName == "" || m.DisplayName == AdminDisplayName && m.ID == 1 {
|
2023-03-09 15:12:10 +01:00
|
|
|
m.DisplayName = m.FullName()
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-09 19:56:07 +01:00
|
|
|
// UpdateUsername changes the login name in the database.
|
2023-03-08 23:30:39 +01:00
|
|
|
func (m *User) UpdateUsername(login string) (err error) {
|
2023-03-09 19:56:07 +01:00
|
|
|
// Check if the name already exists or has not changed.
|
|
|
|
if m.UserName == login || m.ID <= 0 {
|
|
|
|
return nil
|
|
|
|
} else if user := FindUserByName(login); user != nil {
|
|
|
|
return fmt.Errorf("user %s already exists", clean.LogQuote(login))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set new username.
|
2023-03-08 23:30:39 +01:00
|
|
|
if err = m.SetUsername(login); err != nil {
|
2022-09-02 21:30:50 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save to database.
|
|
|
|
return m.Updates(Values{
|
2022-09-28 09:01:17 +02:00
|
|
|
"UserName": m.UserName,
|
2022-09-02 21:30:50 +02:00
|
|
|
"DisplayName": m.DisplayName,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Email returns the user's login email for authentication.
|
|
|
|
func (m *User) Email() string {
|
|
|
|
return clean.Email(m.UserEmail)
|
|
|
|
}
|
|
|
|
|
2023-02-20 15:54:33 +01:00
|
|
|
// Handle returns the user's login handle.
|
|
|
|
func (m *User) Handle() string {
|
2023-03-14 16:36:30 +01:00
|
|
|
return clean.Handle(m.UserName)
|
2023-02-20 15:54:33 +01:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// FullName returns the name of the user for display purposes.
|
|
|
|
func (m *User) FullName() string {
|
2023-03-09 15:12:10 +01:00
|
|
|
if m.DisplayName != "" {
|
2022-09-28 09:01:17 +02:00
|
|
|
return m.DisplayName
|
|
|
|
}
|
2023-03-09 15:12:10 +01:00
|
|
|
|
|
|
|
if n := m.Details().DisplayName(); n != "" {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
return clean.NameCapitalized(strings.ReplaceAll(m.Handle(), ".", " "))
|
2022-09-28 09:01:17 +02:00
|
|
|
}
|
|
|
|
|
2023-03-13 16:04:37 +01:00
|
|
|
// SetRole sets the user role specified as string.
|
|
|
|
func (m *User) SetRole(role string) *User {
|
|
|
|
role = clean.Role(role)
|
|
|
|
|
|
|
|
switch role {
|
|
|
|
case "", "0", "false", "nil", "null", "nan":
|
|
|
|
m.UserRole = acl.RoleUnknown.String()
|
|
|
|
default:
|
|
|
|
m.UserRole = acl.ValidRoles[role].String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2023-03-13 17:52:48 +01:00
|
|
|
// HasRole checks the user role specified as string.
|
|
|
|
func (m *User) HasRole(role string) bool {
|
|
|
|
return m.AclRole().String() == acl.ValidRoles[clean.Role(role)].String()
|
|
|
|
}
|
|
|
|
|
2022-09-02 21:30:50 +02:00
|
|
|
// AclRole returns the user role for ACL permission checks.
|
|
|
|
func (m *User) AclRole() acl.Role {
|
|
|
|
role := clean.Role(m.UserRole)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case m.SuperAdmin:
|
|
|
|
return acl.RoleAdmin
|
|
|
|
case role == "":
|
2022-10-13 22:11:02 +02:00
|
|
|
return acl.RoleUnknown
|
2022-09-28 09:01:17 +02:00
|
|
|
case m.UserName == "":
|
|
|
|
return acl.RoleVisitor
|
2022-09-02 21:30:50 +02:00
|
|
|
default:
|
2022-09-28 09:01:17 +02:00
|
|
|
return acl.ValidRoles[role]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Settings returns the user settings and initializes them if necessary.
|
|
|
|
func (m *User) Settings() *UserSettings {
|
|
|
|
if m.UserSettings != nil {
|
|
|
|
m.UserSettings.UserUID = m.UserUID
|
|
|
|
return m.UserSettings
|
|
|
|
} else if m.UID() == "" {
|
|
|
|
m.UserSettings = &UserSettings{}
|
|
|
|
return m.UserSettings
|
|
|
|
} else if err := CreateUserSettings(m); err != nil {
|
|
|
|
m.UserSettings = NewUserSettings(m.UserUID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.UserSettings
|
|
|
|
}
|
|
|
|
|
|
|
|
// Details returns user profile information and initializes it if needed.
|
|
|
|
func (m *User) Details() *UserDetails {
|
|
|
|
if m.UserDetails != nil {
|
|
|
|
m.UserDetails.UserUID = m.UserUID
|
|
|
|
return m.UserDetails
|
|
|
|
} else if m.UID() == "" {
|
|
|
|
m.UserDetails = &UserDetails{}
|
|
|
|
return m.UserDetails
|
|
|
|
} else if err := CreateUserDetails(m); err != nil {
|
|
|
|
m.UserDetails = NewUserDetails(m.UserUID)
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
2022-09-28 09:01:17 +02:00
|
|
|
|
|
|
|
return m.UserDetails
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attr returns optional user account attributes as sanitized string.
|
|
|
|
// Example: https://learn.microsoft.com/en-us/troubleshoot/windows-server/identity/useraccountcontrol-manipulate-account-properties
|
|
|
|
func (m *User) Attr() string {
|
|
|
|
return clean.Attr(m.UserAttr)
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsRegistered checks if the user is registered e.g. has a username.
|
|
|
|
func (m *User) IsRegistered() bool {
|
2022-10-02 11:38:30 +02:00
|
|
|
if m == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.UserName != "" && rnd.IsUID(m.UserUID, UserUID) && !m.IsVisitor()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotRegistered checks if the user is not registered with an own account.
|
|
|
|
func (m *User) NotRegistered() bool {
|
|
|
|
return !m.IsRegistered()
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
2023-03-08 23:30:39 +01:00
|
|
|
// Equal returns true if the user specified matches.
|
|
|
|
func (m *User) Equal(u *User) bool {
|
|
|
|
if m == nil || u == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.UserUID == u.UserUID
|
|
|
|
}
|
|
|
|
|
2022-09-02 21:30:50 +02:00
|
|
|
// IsAdmin checks if the user is an admin with username.
|
|
|
|
func (m *User) IsAdmin() bool {
|
2022-10-02 11:38:30 +02:00
|
|
|
if m == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-11-22 22:14:34 +01:00
|
|
|
return m.IsSuperAdmin() || m.IsRegistered() && m.AclRole() == acl.RoleAdmin
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsSuperAdmin checks if the user is a super admin.
|
|
|
|
func (m *User) IsSuperAdmin() bool {
|
|
|
|
if m == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.SuperAdmin
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// IsVisitor checks if the user is a sharing link visitor.
|
|
|
|
func (m *User) IsVisitor() bool {
|
|
|
|
return m.AclRole() == acl.RoleVisitor || m.ID == Visitor.ID
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// IsUnknown checks if the user is unknown.
|
|
|
|
func (m *User) IsUnknown() bool {
|
|
|
|
return !rnd.IsUID(m.UserUID, UserUID) || m.ID == UnknownUser.ID || m.UserUID == UnknownUser.UserUID
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// DeleteSessions deletes all active user sessions except those passed as argument.
|
|
|
|
func (m *User) DeleteSessions(omit []string) (deleted int) {
|
|
|
|
if m.UserUID == "" {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find all user sessions except the session ids passed as argument.
|
|
|
|
stmt := Db().Where("user_uid = ? AND id NOT IN (?)", m.UserUID, omit)
|
|
|
|
sess := Sessions{}
|
|
|
|
|
|
|
|
if err := stmt.Find(&sess).Error; err != nil {
|
2022-10-02 11:38:30 +02:00
|
|
|
event.AuditErr([]string{"user %s", "failed to invalidate sessions", "%s"}, m.RefID, err)
|
2022-09-28 09:01:17 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// This will also remove the session from the cache.
|
|
|
|
for _, s := range sess {
|
|
|
|
if err := s.Delete(); err != nil {
|
2022-10-02 11:38:30 +02:00
|
|
|
event.AuditWarn([]string{"user %s", "failed to invalidate session %s", "%s"}, m.RefID, clean.Log(s.RefID), err)
|
2022-09-28 09:01:17 +02:00
|
|
|
} else {
|
|
|
|
deleted++
|
|
|
|
}
|
|
|
|
}
|
2022-09-02 21:30:50 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Return number of deleted sessions for logs.
|
|
|
|
return deleted
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetPassword sets a new password stored as hash.
|
|
|
|
func (m *User) SetPassword(password string) error {
|
|
|
|
if !m.IsRegistered() {
|
2022-10-11 22:44:11 +02:00
|
|
|
return fmt.Errorf("only registered users can change their password")
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
2022-11-22 22:14:34 +01:00
|
|
|
if len(password) < PasswordLength {
|
|
|
|
return fmt.Errorf("password must have at least %d characters", PasswordLength)
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pw := NewPassword(m.UserUID, password)
|
|
|
|
|
2022-10-13 22:11:02 +02:00
|
|
|
if err := pw.Save(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.RegenerateTokens()
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
2022-10-11 22:44:11 +02:00
|
|
|
// HasPassword checks if the user has the specified password and the account is registered.
|
|
|
|
func (m *User) HasPassword(s string) bool {
|
|
|
|
return !m.WrongPassword(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WrongPassword checks if the given password is incorrect or the account is not registered.
|
|
|
|
func (m *User) WrongPassword(s string) bool {
|
2022-09-28 09:01:17 +02:00
|
|
|
// Registered user?
|
2022-09-02 21:30:50 +02:00
|
|
|
if !m.IsRegistered() {
|
2022-10-11 22:44:11 +02:00
|
|
|
log.Warn("only registered users can log in")
|
2022-09-02 21:30:50 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Empty password?
|
2022-10-11 22:44:11 +02:00
|
|
|
if s == "" {
|
2022-09-02 21:30:50 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Fetch password.
|
2022-09-02 21:30:50 +02:00
|
|
|
pw := FindPassword(m.UserUID)
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Found?
|
2022-09-02 21:30:50 +02:00
|
|
|
if pw == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Invalid?
|
2022-10-11 22:44:11 +02:00
|
|
|
if pw.IsWrong(s) {
|
2022-09-02 21:30:50 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Validate checks if username, email and role are valid and returns an error otherwise.
|
|
|
|
func (m *User) Validate() (err error) {
|
|
|
|
// Empty name?
|
2023-03-08 23:30:39 +01:00
|
|
|
if m.Username() == "" {
|
2022-10-11 22:44:11 +02:00
|
|
|
return errors.New("username must not be empty")
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Name too short?
|
2023-03-08 23:30:39 +01:00
|
|
|
if len(m.Username()) < UsernameLength {
|
2022-11-22 22:14:34 +01:00
|
|
|
return fmt.Errorf("username must have at least %d characters", UsernameLength)
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Validate user role.
|
|
|
|
if acl.ValidRoles[m.UserRole] == "" {
|
|
|
|
return fmt.Errorf("role %s is invalid", clean.LogQuote(m.UserRole))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the username is unique.
|
|
|
|
var duplicate = User{}
|
2022-09-02 21:30:50 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if err = Db().
|
|
|
|
Where("user_name = ? AND id <> ?", m.UserName, m.ID).
|
|
|
|
First(&duplicate).Error; err == nil {
|
2023-03-09 15:59:08 +01:00
|
|
|
return fmt.Errorf("user %s already exists", clean.LogQuote(m.UserName))
|
2022-09-02 21:30:50 +02:00
|
|
|
} else if err != gorm.ErrRecordNotFound {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Skip email check?
|
|
|
|
if m.UserEmail == "" {
|
2022-09-02 21:30:50 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Parse and validate email address.
|
|
|
|
if a, err := mail.ParseAddress(m.UserEmail); err != nil {
|
|
|
|
return fmt.Errorf("email %s is invalid", clean.LogQuote(m.UserEmail))
|
|
|
|
} else if email := a.Address; !strings.ContainsRune(email, '.') {
|
|
|
|
return fmt.Errorf("email %s does not have a fully qualified domain", clean.LogQuote(m.UserEmail))
|
2022-09-02 21:30:50 +02:00
|
|
|
} else {
|
2022-09-28 09:01:17 +02:00
|
|
|
m.UserEmail = email
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Check if the email is unique.
|
|
|
|
if err = Db().
|
|
|
|
Where("user_email = ? AND id <> ?", m.UserEmail, m.ID).
|
|
|
|
First(&duplicate).Error; err == nil {
|
|
|
|
return fmt.Errorf("email %s already exists", clean.Log(m.UserEmail))
|
2022-09-02 21:30:50 +02:00
|
|
|
} else if err != gorm.ErrRecordNotFound {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// SetFormValues sets the values specified in the form.
|
|
|
|
func (m *User) SetFormValues(frm form.User) *User {
|
2023-03-08 23:30:39 +01:00
|
|
|
m.UserName = frm.Username()
|
2023-03-09 15:12:10 +01:00
|
|
|
m.SetProvider(frm.Provider())
|
2022-09-28 09:01:17 +02:00
|
|
|
m.UserEmail = frm.Email()
|
|
|
|
m.DisplayName = frm.DisplayName
|
|
|
|
m.SuperAdmin = frm.SuperAdmin
|
|
|
|
m.CanLogin = frm.CanLogin
|
2022-10-13 22:11:02 +02:00
|
|
|
m.WebDAV = frm.WebDAV
|
2023-03-13 16:04:37 +01:00
|
|
|
m.SetRole(frm.Role())
|
2022-09-28 09:01:17 +02:00
|
|
|
m.UserAttr = frm.Attr()
|
2022-09-30 00:42:19 +02:00
|
|
|
m.SetBasePath(frm.BasePath)
|
|
|
|
m.SetUploadPath(frm.UploadPath)
|
2022-09-02 21:30:50 +02:00
|
|
|
|
2023-03-09 15:12:10 +01:00
|
|
|
// Set display name default if empty.
|
|
|
|
if m.DisplayName == "" || m.DisplayName == AdminDisplayName && m.ID == 1 {
|
|
|
|
m.DisplayName = m.FullName()
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
return m
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
2022-10-02 11:38:30 +02:00
|
|
|
|
2022-10-13 22:11:02 +02:00
|
|
|
// GenerateTokens generates preview and download tokens as needed.
|
|
|
|
func (m *User) GenerateTokens(force bool) *User {
|
|
|
|
if m.ID < 0 {
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.PreviewToken == "" || force {
|
|
|
|
m.PreviewToken = GenerateToken()
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.DownloadToken == "" || force {
|
|
|
|
m.DownloadToken = GenerateToken()
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegenerateTokens replaces the existing preview and download tokens.
|
|
|
|
func (m *User) RegenerateTokens() error {
|
|
|
|
if m.ID < 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
m.GenerateTokens(true)
|
|
|
|
|
|
|
|
return m.Updates(Values{"PreviewToken": m.PreviewToken, "DownloadToken": m.DownloadToken})
|
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
// RefreshShares updates the list of shares.
|
|
|
|
func (m *User) RefreshShares() *User {
|
2022-10-04 00:54:39 +02:00
|
|
|
m.UserShares = FindUserShares(m.UID())
|
2022-10-02 11:38:30 +02:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// NoShares checks if the user has no shares yet.
|
|
|
|
func (m *User) NoShares() bool {
|
|
|
|
if !m.IsRegistered() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-10-04 00:54:39 +02:00
|
|
|
return m.UserShares.Empty()
|
2022-10-02 11:38:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// HasShares checks if the user has any shares.
|
|
|
|
func (m *User) HasShares() bool {
|
|
|
|
return !m.NoShares()
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasShare if a uid was shared with the user.
|
|
|
|
func (m *User) HasShare(uid string) bool {
|
|
|
|
if !m.IsRegistered() || m.NoShares() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-04 00:54:39 +02:00
|
|
|
return m.UserShares.Contains(uid)
|
2022-10-02 11:38:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SharedUIDs returns shared entity UIDs.
|
|
|
|
func (m *User) SharedUIDs() UIDs {
|
2022-10-04 00:54:39 +02:00
|
|
|
if m.IsRegistered() && m.UserShares.Empty() {
|
2022-10-02 11:38:30 +02:00
|
|
|
m.RefreshShares()
|
|
|
|
}
|
|
|
|
|
2022-10-04 00:54:39 +02:00
|
|
|
return m.UserShares.UIDs()
|
2022-10-02 11:38:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RedeemToken updates shared entity UIDs using the specified token.
|
|
|
|
func (m *User) RedeemToken(token string) (n int) {
|
|
|
|
if !m.IsRegistered() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find links.
|
|
|
|
links := FindValidLinks(token, "")
|
|
|
|
|
|
|
|
// Found?
|
|
|
|
if n = len(links); n == 0 {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find shares.
|
|
|
|
for _, link := range links {
|
2022-10-04 00:54:39 +02:00
|
|
|
if found := FindUserShare(UserShare{UserUID: m.UID(), ShareUID: link.ShareUID}); found == nil {
|
|
|
|
share := NewUserShare(m.UID(), link.ShareUID, link.Perm, link.ExpiresAt())
|
2022-10-02 11:38:30 +02:00
|
|
|
share.LinkUID = link.LinkUID
|
|
|
|
share.Comment = link.Comment
|
|
|
|
|
|
|
|
if err := share.Save(); err != nil {
|
|
|
|
event.AuditErr([]string{"user %s", "token %s", "failed to redeem shares", "%s"}, m.RefID, clean.Log(token), err)
|
|
|
|
} else {
|
|
|
|
link.Redeem()
|
|
|
|
}
|
|
|
|
} else if err := found.UpdateLink(link); err != nil {
|
|
|
|
event.AuditErr([]string{"user %s", "token %s", "failed to update shares", "%s"}, m.RefID, clean.Log(token), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
2022-10-17 19:07:38 +02:00
|
|
|
|
2022-10-19 05:09:09 +02:00
|
|
|
// Form returns a populated user form to perform changes.
|
|
|
|
func (m *User) Form() (form.User, error) {
|
|
|
|
frm := form.User{UserDetails: &form.UserDetails{}}
|
|
|
|
|
|
|
|
if err := deepcopier.Copy(m).To(&frm); err != nil {
|
|
|
|
return frm, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := deepcopier.Copy(m.UserDetails).To(frm.UserDetails); err != nil {
|
|
|
|
return frm, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return frm, nil
|
|
|
|
}
|
|
|
|
|
2022-10-17 19:07:38 +02:00
|
|
|
// SaveForm updates the entity using form data and stores it in the database.
|
2023-03-08 23:30:39 +01:00
|
|
|
func (m *User) SaveForm(f form.User, updateRights bool) error {
|
2022-10-17 19:07:38 +02:00
|
|
|
if m.UserName == "" || m.ID <= 0 {
|
2023-03-08 23:30:39 +01:00
|
|
|
return fmt.Errorf("system users cannot be modified")
|
|
|
|
} else if (m.ID == 1 || f.SuperAdmin) && acl.RoleAdmin.NotEqual(f.Role()) {
|
|
|
|
return fmt.Errorf("super admin must not have a non-admin role")
|
|
|
|
} else if f.BasePath != "" && clean.UserPath(f.BasePath) == "" {
|
|
|
|
return fmt.Errorf("invalid base folder")
|
|
|
|
} else if f.UploadPath != "" && clean.UserPath(f.UploadPath) == "" {
|
|
|
|
return fmt.Errorf("invalid upload folder")
|
2022-10-17 19:07:38 +02:00
|
|
|
}
|
|
|
|
|
2022-10-19 05:09:09 +02:00
|
|
|
// Ignore details if not set.
|
|
|
|
if f.UserDetails == nil {
|
|
|
|
// Ignore.
|
|
|
|
} else if err := deepcopier.Copy(f.UserDetails).To(m.UserDetails); err != nil {
|
2022-10-17 19:07:38 +02:00
|
|
|
return err
|
2022-10-19 05:09:09 +02:00
|
|
|
} else {
|
2023-03-10 10:21:46 +01:00
|
|
|
m.UserDetails.UserAbout = txt.Clip(m.UserDetails.UserAbout, txt.ClipComment)
|
|
|
|
m.UserDetails.UserBio = txt.Clip(m.UserDetails.UserBio, txt.ClipText)
|
2022-10-17 19:07:38 +02:00
|
|
|
}
|
|
|
|
|
2022-10-19 05:09:09 +02:00
|
|
|
// Sanitize display name.
|
2022-10-17 19:07:38 +02:00
|
|
|
if n := clean.Name(f.DisplayName); n != "" && n != m.DisplayName {
|
2023-03-08 23:30:39 +01:00
|
|
|
m.SetDisplayName(n, SrcManual)
|
2022-10-17 19:07:38 +02:00
|
|
|
}
|
|
|
|
|
2023-03-09 15:12:10 +01:00
|
|
|
// Set display name default if empty.
|
|
|
|
if m.DisplayName == "" || m.DisplayName == AdminDisplayName && m.ID == 1 {
|
|
|
|
m.DisplayName = m.FullName()
|
|
|
|
}
|
|
|
|
|
2022-10-19 05:09:09 +02:00
|
|
|
// Sanitize email address.
|
2023-03-10 10:21:46 +01:00
|
|
|
if email := f.Email(); email != "" && email != m.UserEmail {
|
2022-10-17 19:07:38 +02:00
|
|
|
m.UserEmail = email
|
|
|
|
m.VerifiedAt = nil
|
|
|
|
m.VerifyToken = GenerateToken()
|
|
|
|
}
|
|
|
|
|
2023-03-08 23:30:39 +01:00
|
|
|
// Update user rights only if explicitly requested.
|
|
|
|
if updateRights {
|
2023-03-13 16:04:37 +01:00
|
|
|
m.SetRole(f.Role())
|
2023-03-08 23:30:39 +01:00
|
|
|
m.SuperAdmin = f.SuperAdmin
|
2023-03-09 15:12:10 +01:00
|
|
|
|
2023-03-08 23:30:39 +01:00
|
|
|
m.CanLogin = f.CanLogin
|
|
|
|
m.WebDAV = f.WebDAV
|
2023-03-10 10:21:46 +01:00
|
|
|
m.UserAttr = f.Attr()
|
2023-03-08 23:30:39 +01:00
|
|
|
|
2023-03-09 15:12:10 +01:00
|
|
|
m.SetProvider(f.Provider())
|
2023-03-08 23:30:39 +01:00
|
|
|
m.SetBasePath(f.BasePath)
|
|
|
|
m.SetUploadPath(f.UploadPath)
|
|
|
|
}
|
|
|
|
|
2023-03-09 15:12:10 +01:00
|
|
|
// Ensure super admins never have a non-admin role.
|
|
|
|
if m.SuperAdmin {
|
2023-03-13 16:04:37 +01:00
|
|
|
m.SetRole(acl.RoleAdmin.String())
|
2023-03-09 15:12:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that the initial admin user cannot lock itself out.
|
|
|
|
if m.ID == Admin.ID && (m.AclRole() != acl.RoleAdmin || !m.SuperAdmin || !m.CanLogin) {
|
2023-03-13 16:04:37 +01:00
|
|
|
m.SetRole(acl.RoleAdmin.String())
|
2023-03-09 15:12:10 +01:00
|
|
|
m.SuperAdmin = true
|
|
|
|
m.CanLogin = true
|
|
|
|
}
|
|
|
|
|
2022-10-17 19:07:38 +02:00
|
|
|
return m.Save()
|
|
|
|
}
|
|
|
|
|
2022-10-19 05:09:09 +02:00
|
|
|
// SetDisplayName sets a new display name and, if possible, splits it into its components.
|
2023-03-08 23:30:39 +01:00
|
|
|
func (m *User) SetDisplayName(name, src string) *User {
|
2022-10-19 05:09:09 +02:00
|
|
|
name = clean.Name(name)
|
|
|
|
|
|
|
|
d := m.Details()
|
2023-03-09 15:12:10 +01:00
|
|
|
priority := SrcPriority[src] >= SrcPriority[d.NameSrc]
|
2022-10-19 05:09:09 +02:00
|
|
|
|
2023-03-09 15:12:10 +01:00
|
|
|
if name == "" || !priority && m.DisplayName != "" {
|
2022-10-19 05:09:09 +02:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2023-02-20 15:54:33 +01:00
|
|
|
m.DisplayName = name
|
2023-03-09 15:12:10 +01:00
|
|
|
|
|
|
|
if !priority {
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2023-03-08 23:30:39 +01:00
|
|
|
d.NameSrc = src
|
2023-02-20 15:54:33 +01:00
|
|
|
|
2022-10-19 05:09:09 +02:00
|
|
|
// Try to parse name into components.
|
|
|
|
n := txt.ParseName(name)
|
|
|
|
|
|
|
|
d.NameTitle = n.Title
|
|
|
|
d.GivenName = n.Given
|
|
|
|
d.MiddleName = n.Middle
|
|
|
|
d.FamilyName = n.Family
|
|
|
|
d.NameSuffix = n.Suffix
|
|
|
|
d.NickName = n.Nick
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2023-02-20 15:54:33 +01:00
|
|
|
// SetGivenName updates the user's given name.
|
|
|
|
func (m *User) SetGivenName(name string) *User {
|
|
|
|
m.Details().SetGivenName(name)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFamilyName updates the user's family name.
|
|
|
|
func (m *User) SetFamilyName(name string) *User {
|
|
|
|
m.Details().SetFamilyName(name)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2022-10-17 19:07:38 +02:00
|
|
|
// SetAvatar updates the user avatar image.
|
|
|
|
func (m *User) SetAvatar(thumb, thumbSrc string) error {
|
|
|
|
if m.UserName == "" || m.ID <= 0 {
|
|
|
|
return fmt.Errorf("system user avatars cannot be changed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if SrcPriority[thumbSrc] < SrcPriority[m.ThumbSrc] && m.Thumb != "" {
|
|
|
|
return fmt.Errorf("no permission to change avatar")
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Thumb = thumb
|
|
|
|
m.ThumbSrc = thumbSrc
|
|
|
|
|
|
|
|
return m.Updates(Values{"Thumb": m.Thumb, "ThumbSrc": m.ThumbSrc})
|
|
|
|
}
|