2022-10-02 11:38:30 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import "github.com/photoprism/photoprism/internal/event"
|
|
|
|
|
2022-10-02 22:09:02 +02:00
|
|
|
// AlbumUser represents the user and group ownership of an Album and the corresponding permissions.
|
|
|
|
type AlbumUser struct {
|
2022-10-02 11:38:30 +02:00
|
|
|
UID string `gorm:"type:VARBINARY(42);primary_key;auto_increment:false" json:"UID" yaml:"UID"`
|
2022-10-02 22:09:02 +02:00
|
|
|
UserUID string `gorm:"type:VARBINARY(42);primary_key;auto_increment:false;index" json:"UserUID,omitempty" yaml:"UserUID,omitempty"`
|
|
|
|
TeamUID string `gorm:"type:VARBINARY(42);index" json:"TeamUID,omitempty" yaml:"TeamUID,omitempty"`
|
2022-10-02 11:38:30 +02:00
|
|
|
Perm uint `json:"Perm,omitempty" yaml:"Perm,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// TableName returns the database table name.
|
2022-10-02 22:09:02 +02:00
|
|
|
func (AlbumUser) TableName() string {
|
|
|
|
return "albums_users"
|
2022-10-02 11:38:30 +02:00
|
|
|
}
|
|
|
|
|
2022-10-02 22:09:02 +02:00
|
|
|
// NewAlbumUser creates a new entity model.
|
|
|
|
func NewAlbumUser(uid, userUid, teamUid string, perm uint) *AlbumUser {
|
|
|
|
result := &AlbumUser{
|
2022-10-02 11:38:30 +02:00
|
|
|
UID: uid,
|
|
|
|
UserUID: userUid,
|
|
|
|
TeamUID: teamUid,
|
|
|
|
Perm: perm,
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create inserts a new record into the database.
|
2022-10-02 22:09:02 +02:00
|
|
|
func (m *AlbumUser) Create() error {
|
2022-10-02 11:38:30 +02:00
|
|
|
return Db().Create(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save updates the record in the database or inserts a new record if it does not already exist.
|
2022-10-02 22:09:02 +02:00
|
|
|
func (m *AlbumUser) Save() error {
|
2022-10-02 11:38:30 +02:00
|
|
|
return Db().Save(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// FirstOrCreateAlbumUser returns the existing record or inserts a new record if it does not already exist.
|
2022-10-02 22:09:02 +02:00
|
|
|
func FirstOrCreateAlbumUser(m *AlbumUser) *AlbumUser {
|
|
|
|
found := AlbumUser{}
|
2022-10-02 11:38:30 +02:00
|
|
|
|
|
|
|
if err := Db().Where("uid = ?", m.UID).First(&found).Error; err == nil {
|
|
|
|
return &found
|
|
|
|
} else if err = m.Create(); err != nil {
|
2022-10-02 22:09:02 +02:00
|
|
|
event.AuditErr([]string{"album %s", "failed to set owner and permissions", "%s"}, m.UID, err)
|
2022-10-02 11:38:30 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|