2022-09-02 21:30:50 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
2022-10-13 22:11:02 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2023-03-08 23:30:39 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/authn"
|
2022-09-02 21:30:50 +02:00
|
|
|
)
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Role defaults.
|
|
|
|
const (
|
2022-10-02 11:38:30 +02:00
|
|
|
AdminUserName = "admin"
|
|
|
|
AdminDisplayName = "Admin"
|
|
|
|
VisitorDisplayName = "Visitor"
|
2022-10-13 22:11:02 +02:00
|
|
|
UnknownDisplayName = "Unknown"
|
2022-09-28 09:01:17 +02:00
|
|
|
)
|
2022-09-02 21:30:50 +02:00
|
|
|
|
|
|
|
// Admin is the default admin user.
|
|
|
|
var Admin = User{
|
2022-10-13 22:11:02 +02:00
|
|
|
ID: 1,
|
|
|
|
UserName: AdminUserName,
|
2023-03-09 15:12:10 +01:00
|
|
|
AuthProvider: authn.ProviderLocal.String(),
|
2022-10-13 22:11:02 +02:00
|
|
|
UserRole: acl.RoleAdmin.String(),
|
|
|
|
DisplayName: AdminDisplayName,
|
|
|
|
SuperAdmin: true,
|
|
|
|
CanLogin: true,
|
|
|
|
WebDAV: true,
|
|
|
|
CanInvite: true,
|
|
|
|
InviteToken: GenerateToken(),
|
|
|
|
PreviewToken: GenerateToken(),
|
|
|
|
DownloadToken: GenerateToken(),
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnknownUser is an anonymous, public user without own account.
|
|
|
|
var UnknownUser = User{
|
2022-10-13 22:11:02 +02:00
|
|
|
ID: -1,
|
|
|
|
UserUID: "u000000000000001",
|
|
|
|
UserName: "",
|
2023-03-09 15:12:10 +01:00
|
|
|
AuthProvider: authn.ProviderNone.String(),
|
2022-10-13 22:11:02 +02:00
|
|
|
UserRole: acl.RoleUnknown.String(),
|
|
|
|
CanLogin: false,
|
|
|
|
WebDAV: false,
|
|
|
|
CanInvite: false,
|
|
|
|
DisplayName: UnknownDisplayName,
|
|
|
|
InviteToken: "",
|
|
|
|
PreviewToken: "",
|
|
|
|
DownloadToken: "",
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// Visitor is a user without own account e.g. for link sharing.
|
|
|
|
var Visitor = User{
|
2022-10-13 22:11:02 +02:00
|
|
|
ID: -2,
|
|
|
|
UserUID: "u000000000000002",
|
|
|
|
UserName: "",
|
2023-03-09 15:12:10 +01:00
|
|
|
AuthProvider: authn.ProviderToken.String(),
|
2022-10-13 22:11:02 +02:00
|
|
|
UserRole: acl.RoleVisitor.String(),
|
|
|
|
DisplayName: VisitorDisplayName,
|
|
|
|
CanLogin: false,
|
|
|
|
WebDAV: false,
|
|
|
|
CanInvite: false,
|
|
|
|
InviteToken: "",
|
|
|
|
PreviewToken: "",
|
|
|
|
DownloadToken: "",
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateDefaultUsers initializes the database with default user accounts.
|
|
|
|
func CreateDefaultUsers() {
|
2022-10-13 22:11:02 +02:00
|
|
|
if admin := FindUser(Admin); admin != nil {
|
|
|
|
Admin = *admin
|
|
|
|
} else {
|
|
|
|
// Set legacy values.
|
|
|
|
if leg := FindLegacyUser(Admin); leg != nil {
|
|
|
|
Admin.UserUID = leg.UserUID
|
2022-11-03 11:02:36 +01:00
|
|
|
if leg.UserName != "" {
|
|
|
|
Admin.UserName = leg.UserName
|
|
|
|
}
|
|
|
|
if leg.PrimaryEmail != "" {
|
|
|
|
Admin.UserEmail = leg.PrimaryEmail
|
|
|
|
}
|
|
|
|
if leg.FullName != "" {
|
|
|
|
Admin.DisplayName = leg.FullName
|
|
|
|
}
|
|
|
|
if leg.LoginAt != nil {
|
|
|
|
Admin.LoginAt = leg.LoginAt
|
|
|
|
}
|
2022-10-13 22:11:02 +02:00
|
|
|
log.Infof("users: migrating %s account", Admin.UserName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set default values.
|
|
|
|
Admin.SuperAdmin = true
|
|
|
|
Admin.CanLogin = true
|
|
|
|
Admin.WebDAV = true
|
|
|
|
|
|
|
|
// Username is required.
|
|
|
|
if Admin.UserName == "" {
|
|
|
|
Admin.UserName = "admin"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add initial admin account.
|
|
|
|
if err := Admin.Create(); err != nil {
|
|
|
|
event.AuditErr([]string{"user", "failed to create", "%s"}, err)
|
|
|
|
}
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if user := FirstOrCreateUser(&UnknownUser); user != nil {
|
|
|
|
UnknownUser = *user
|
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if user := FirstOrCreateUser(&Visitor); user != nil {
|
|
|
|
Visitor = *user
|
2022-09-02 21:30:50 +02:00
|
|
|
}
|
|
|
|
}
|