mirror of
https://github.com/photoprism/photoprism.git
synced 2025-12-12 00:34:13 +01:00
Docs: Improve code comments in internal/entity/auth*.go
Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
@@ -25,10 +25,10 @@ const (
|
|||||||
ClientUID = byte('c')
|
ClientUID = byte('c')
|
||||||
)
|
)
|
||||||
|
|
||||||
// Clients represents a list of client applications.
|
// Clients is a convenience alias for slices of Client.
|
||||||
type Clients []Client
|
type Clients []Client
|
||||||
|
|
||||||
// Client represents a client application.
|
// Client represents an OAuth/OpenID client registered with PhotoPrism.
|
||||||
type Client struct {
|
type Client struct {
|
||||||
ClientUID string `gorm:"type:VARBINARY(42);primary_key;auto_increment:false;" json:"-" yaml:"ClientUID"`
|
ClientUID string `gorm:"type:VARBINARY(42);primary_key;auto_increment:false;" json:"-" yaml:"ClientUID"`
|
||||||
NodeUUID string `gorm:"type:VARBINARY(64);index;default:'';" json:"NodeUUID,omitempty" yaml:"NodeUUID,omitempty"`
|
NodeUUID string `gorm:"type:VARBINARY(64);index;default:'';" json:"NodeUUID,omitempty" yaml:"NodeUUID,omitempty"`
|
||||||
@@ -60,7 +60,7 @@ func (Client) TableName() string {
|
|||||||
return "auth_clients"
|
return "auth_clients"
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient returns a new client application instance.
|
// NewClient returns a new client application instance with default ACL role and authentication settings.
|
||||||
func NewClient() *Client {
|
func NewClient() *Client {
|
||||||
return &Client{
|
return &Client{
|
||||||
UserUID: "",
|
UserUID: "",
|
||||||
|
|||||||
@@ -30,10 +30,10 @@ const (
|
|||||||
UnknownIP = limiter.DefaultIP
|
UnknownIP = limiter.DefaultIP
|
||||||
)
|
)
|
||||||
|
|
||||||
// Sessions represents a list of sessions.
|
// Sessions is a convenience alias for slices of Session.
|
||||||
type Sessions []Session
|
type Sessions []Session
|
||||||
|
|
||||||
// Session represents a User session.
|
// Session represents an authenticated user or client session persisted in the database.
|
||||||
type Session struct {
|
type Session struct {
|
||||||
ID string `gorm:"type:VARBINARY(2048);primary_key;auto_increment:false;" json:"-" yaml:"ID"`
|
ID string `gorm:"type:VARBINARY(2048);primary_key;auto_increment:false;" json:"-" yaml:"ID"`
|
||||||
authToken string `gorm:"-" yaml:"-"`
|
authToken string `gorm:"-" yaml:"-"`
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
"github.com/photoprism/photoprism/pkg/time/unix"
|
"github.com/photoprism/photoprism/pkg/time/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DeleteSession permanently deletes a session.
|
// DeleteSession permanently deletes the session and any sessions that were derived from it.
|
||||||
func DeleteSession(s *Session) error {
|
func DeleteSession(s *Session) error {
|
||||||
if s == nil {
|
if s == nil {
|
||||||
return nil
|
return nil
|
||||||
@@ -38,7 +38,7 @@ func DeleteSession(s *Session) error {
|
|||||||
return UnscopedDb().Delete(s).Error
|
return UnscopedDb().Delete(s).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteChildSessions deletes any other sessions that were authenticated with the specified session.
|
// DeleteChildSessions deletes sessions that authenticated via the provided parent session ID.
|
||||||
func DeleteChildSessions(s *Session) (deleted int) {
|
func DeleteChildSessions(s *Session) (deleted int) {
|
||||||
if s == nil {
|
if s == nil {
|
||||||
return 0
|
return 0
|
||||||
@@ -64,7 +64,7 @@ func DeleteChildSessions(s *Session) (deleted int) {
|
|||||||
return deleted
|
return deleted
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteClientSessions deletes client sessions above the specified limit.
|
// DeleteClientSessions deletes sessions for the client beyond the configured retention limit.
|
||||||
func DeleteClientSessions(client *Client, authMethod authn.MethodType, limit int64) (deleted int) {
|
func DeleteClientSessions(client *Client, authMethod authn.MethodType, limit int64) (deleted int) {
|
||||||
if limit < 0 {
|
if limit < 0 {
|
||||||
return 0
|
return 0
|
||||||
@@ -110,7 +110,7 @@ func DeleteClientSessions(client *Client, authMethod authn.MethodType, limit int
|
|||||||
return deleted
|
return deleted
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteExpiredSessions deletes all expired sessions.
|
// DeleteExpiredSessions removes sessions whose expiration timestamp has passed.
|
||||||
func DeleteExpiredSessions() (deleted int) {
|
func DeleteExpiredSessions() (deleted int) {
|
||||||
found := Sessions{}
|
found := Sessions{}
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import (
|
|||||||
"github.com/photoprism/photoprism/pkg/txt"
|
"github.com/photoprism/photoprism/pkg/txt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Auth checks if the credentials are valid and returns the user and authentication provider.
|
// Auth validates login credentials and returns the authenticated user plus provider/method metadata.
|
||||||
var Auth = func(frm form.Login, s *Session, c *gin.Context) (user *User, provider authn.ProviderType, method authn.MethodType, err error) {
|
var Auth = func(frm form.Login, s *Session, c *gin.Context) (user *User, provider authn.ProviderType, method authn.MethodType, err error) {
|
||||||
// Get sanitized username from login form.
|
// Get sanitized username from login form.
|
||||||
nameName := frm.CleanUsername()
|
nameName := frm.CleanUsername()
|
||||||
@@ -39,7 +39,7 @@ var Auth = func(frm form.Login, s *Session, c *gin.Context) (user *User, provide
|
|||||||
return user, provider, method, err
|
return user, provider, method, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// AuthSession returns the client session that belongs to the auth token provided, or returns nil if it was not found.
|
// AuthSession resolves an existing session from an app password token embedded in the login form.
|
||||||
func AuthSession(frm form.Login, c *gin.Context) (sess *Session, user *User, err error) {
|
func AuthSession(frm form.Login, c *gin.Context) (sess *Session, user *User, err error) {
|
||||||
if frm.Password == "" {
|
if frm.Password == "" {
|
||||||
// Abort authentication if no token was provided.
|
// Abort authentication if no token was provided.
|
||||||
@@ -68,7 +68,7 @@ func AuthSession(frm form.Login, c *gin.Context) (sess *Session, user *User, err
|
|||||||
return sess, sess.GetUser(), nil
|
return sess, sess.GetUser(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AuthLocal authenticates against the local user database with the specified username and password.
|
// AuthLocal authenticates against the local user database, handling OTP and app-password flows.
|
||||||
func AuthLocal(user *User, frm form.Login, s *Session, c *gin.Context) (provider authn.ProviderType, method authn.MethodType, err error) {
|
func AuthLocal(user *User, frm form.Login, s *Session, c *gin.Context) (provider authn.ProviderType, method authn.MethodType, err error) {
|
||||||
// Set defaults.
|
// Set defaults.
|
||||||
provider = authn.ProviderNone
|
provider = authn.ProviderNone
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"github.com/photoprism/photoprism/pkg/rnd"
|
"github.com/photoprism/photoprism/pkg/rnd"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Reserved token names used for configuration and public access.
|
||||||
const TokenConfig = "__config__"
|
const TokenConfig = "__config__"
|
||||||
const TokenPublic = "public"
|
const TokenPublic = "public"
|
||||||
|
|
||||||
@@ -11,7 +12,7 @@ var PreviewToken = NewStringMap(Strings{})
|
|||||||
var DownloadToken = NewStringMap(Strings{})
|
var DownloadToken = NewStringMap(Strings{})
|
||||||
var ValidateTokens = true
|
var ValidateTokens = true
|
||||||
|
|
||||||
// GenerateToken returns a random string token.
|
// GenerateToken returns a short random token for previews or downloads.
|
||||||
func GenerateToken() string {
|
func GenerateToken() string {
|
||||||
return rnd.Base36(8)
|
return rnd.Base36(8)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,10 +40,10 @@ var PasswordLength = PasswordLengthDefault
|
|||||||
// UsersPath is the relative path for user assets.
|
// UsersPath is the relative path for user assets.
|
||||||
var UsersPath = "users"
|
var UsersPath = "users"
|
||||||
|
|
||||||
// Users represents a list of users.
|
// Users is a convenience alias for slices of User.
|
||||||
type Users []User
|
type Users []User
|
||||||
|
|
||||||
// User represents a person that may optionally log in as user.
|
// User represents an account that can authenticate with PhotoPrism.
|
||||||
type User struct {
|
type User struct {
|
||||||
ID int `gorm:"primary_key" json:"ID" yaml:"-"`
|
ID int `gorm:"primary_key" json:"ID" yaml:"-"`
|
||||||
UUID string `gorm:"type:VARBINARY(64);column:user_uuid;index;" json:"UUID,omitempty" yaml:"UUID,omitempty"`
|
UUID string `gorm:"type:VARBINARY(64);column:user_uuid;index;" json:"UUID,omitempty" yaml:"UUID,omitempty"`
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"github.com/photoprism/photoprism/pkg/clean"
|
"github.com/photoprism/photoprism/pkg/clean"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SetValuesFromCli updates the entity values from a CLI context and validates them.
|
// SetValuesFromCli updates user fields based on CLI flags and invalidates sessions after privilege changes.
|
||||||
func (m *User) SetValuesFromCli(ctx *cli.Context) error {
|
func (m *User) SetValuesFromCli(ctx *cli.Context) error {
|
||||||
frm := form.NewUserFromCli(ctx)
|
frm := form.NewUserFromCli(ctx)
|
||||||
|
|
||||||
@@ -102,7 +102,7 @@ func (m *User) SetValuesFromCli(ctx *cli.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RestoreFromCli restored the account from a CLI context.
|
// RestoreFromCli restores a deleted account from CLI input and optionally sets a new password.
|
||||||
func (m *User) RestoreFromCli(ctx *cli.Context, newPassword string) (err error) {
|
func (m *User) RestoreFromCli(ctx *cli.Context, newPassword string) (err error) {
|
||||||
m.DeletedAt = nil
|
m.DeletedAt = nil
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"github.com/photoprism/photoprism/pkg/authn"
|
"github.com/photoprism/photoprism/pkg/authn"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Role defaults.
|
// Role defaults referenced when creating built-in users.
|
||||||
const (
|
const (
|
||||||
AdminUserName = "admin"
|
AdminUserName = "admin"
|
||||||
AdminDisplayName = "Admin"
|
AdminDisplayName = "Admin"
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ const (
|
|||||||
GenderOther = "other"
|
GenderOther = "other"
|
||||||
)
|
)
|
||||||
|
|
||||||
// UserDetails represents user profile information.
|
// UserDetails stores extended profile information for a user account.
|
||||||
type UserDetails struct {
|
type UserDetails struct {
|
||||||
UserUID string `gorm:"type:VARBINARY(42);primary_key;auto_increment:false;" json:"-" yaml:"-"`
|
UserUID string `gorm:"type:VARBINARY(42);primary_key;auto_increment:false;" json:"-" yaml:"-"`
|
||||||
SubjUID string `gorm:"type:VARBINARY(42);index;" json:"SubjUID,omitempty" yaml:"SubjUID,omitempty"`
|
SubjUID string `gorm:"type:VARBINARY(42);index;" json:"SubjUID,omitempty" yaml:"SubjUID,omitempty"`
|
||||||
@@ -58,12 +58,12 @@ func (UserDetails) TableName() string {
|
|||||||
return "auth_users_details"
|
return "auth_users_details"
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewUserDetails creates new user details.
|
// NewUserDetails allocates an empty profile record for the user UID.
|
||||||
func NewUserDetails(uid string) *UserDetails {
|
func NewUserDetails(uid string) *UserDetails {
|
||||||
return &UserDetails{UserUID: uid}
|
return &UserDetails{UserUID: uid}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateUserDetails creates new user details or returns nil on error.
|
// CreateUserDetails ensures a profile record exists for the user, creating one when absent.
|
||||||
func CreateUserDetails(user *User) error {
|
func CreateUserDetails(user *User) error {
|
||||||
if user == nil {
|
if user == nil {
|
||||||
return fmt.Errorf("user is nil")
|
return fmt.Errorf("user is nil")
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
"github.com/photoprism/photoprism/pkg/rnd"
|
"github.com/photoprism/photoprism/pkg/rnd"
|
||||||
)
|
)
|
||||||
|
|
||||||
// UserSettings represents user preferences.
|
// UserSettings stores per-user UI, indexing, and workflow preferences.
|
||||||
type UserSettings struct {
|
type UserSettings struct {
|
||||||
UserUID string `gorm:"type:VARBINARY(42);primary_key;auto_increment:false;" json:"-" yaml:"UserUID"`
|
UserUID string `gorm:"type:VARBINARY(42);primary_key;auto_increment:false;" json:"-" yaml:"UserUID"`
|
||||||
UITheme string `gorm:"type:VARBINARY(32);column:ui_theme;" json:"UITheme,omitempty" yaml:"UITheme,omitempty"`
|
UITheme string `gorm:"type:VARBINARY(32);column:ui_theme;" json:"UITheme,omitempty" yaml:"UITheme,omitempty"`
|
||||||
@@ -38,12 +38,12 @@ func (UserSettings) TableName() string {
|
|||||||
return "auth_users_settings"
|
return "auth_users_settings"
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewUserSettings creates new user preferences.
|
// NewUserSettings allocates a settings record bound to the provided user UID.
|
||||||
func NewUserSettings(uid string) *UserSettings {
|
func NewUserSettings(uid string) *UserSettings {
|
||||||
return &UserSettings{UserUID: uid}
|
return &UserSettings{UserUID: uid}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateUserSettings creates new user settings or returns nil on error.
|
// CreateUserSettings ensures settings exist for the given user, creating them if missing.
|
||||||
func CreateUserSettings(user *User) error {
|
func CreateUserSettings(user *User) error {
|
||||||
if user == nil {
|
if user == nil {
|
||||||
return fmt.Errorf("user is nil")
|
return fmt.Errorf("user is nil")
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ const (
|
|||||||
SharePrefix = "share"
|
SharePrefix = "share"
|
||||||
)
|
)
|
||||||
|
|
||||||
// UserShares represents shared content.
|
// UserShares groups share relationships granted to a user.
|
||||||
type UserShares []UserShare
|
type UserShares []UserShare
|
||||||
|
|
||||||
// UIDs returns shared UIDs.
|
// UIDs returns shared UIDs.
|
||||||
@@ -61,7 +61,7 @@ func (m UserShares) Contains(uid string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// UserShare represents content shared with a user.
|
// UserShare represents a share relationship granting a user access to another entity.
|
||||||
type UserShare struct {
|
type UserShare struct {
|
||||||
UserUID string `gorm:"type:VARBINARY(42);primary_key;auto_increment:false;" json:"-" yaml:"UserUID"`
|
UserUID string `gorm:"type:VARBINARY(42);primary_key;auto_increment:false;" json:"-" yaml:"UserUID"`
|
||||||
ShareUID string `gorm:"type:VARBINARY(42);primary_key;index;" json:"ShareUID" yaml:"ShareUID"`
|
ShareUID string `gorm:"type:VARBINARY(42);primary_key;index;" json:"ShareUID" yaml:"ShareUID"`
|
||||||
@@ -79,7 +79,7 @@ func (UserShare) TableName() string {
|
|||||||
return "auth_users_shares"
|
return "auth_users_shares"
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewUserShare creates a new entity model.
|
// NewUserShare constructs a share relationship with optional expiration.
|
||||||
func NewUserShare(userUID, shareUid string, perm uint, expires *time.Time) *UserShare {
|
func NewUserShare(userUID, shareUid string, perm uint, expires *time.Time) *UserShare {
|
||||||
result := &UserShare{
|
result := &UserShare{
|
||||||
UserUID: userUID,
|
UserUID: userUID,
|
||||||
|
|||||||
Reference in New Issue
Block a user