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
|
|
|
// PhotoUser represents the user and group ownership of a Photo and the corresponding permissions.
|
|
|
|
type PhotoUser 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 (PhotoUser) TableName() string {
|
|
|
|
return "photos_users"
|
2022-10-02 11:38:30 +02:00
|
|
|
}
|
|
|
|
|
2022-10-02 22:09:02 +02:00
|
|
|
// NewPhotoUser creates a new entity model.
|
|
|
|
func NewPhotoUser(uid, userUid, teamUid string, perm uint) *PhotoUser {
|
|
|
|
result := &PhotoUser{
|
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 *PhotoUser) 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 *PhotoUser) Save() error {
|
2022-10-02 11:38:30 +02:00
|
|
|
return Db().Save(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// FirstOrCreatePhotoUser returns the existing record or inserts a new record if it does not already exist.
|
2022-10-02 22:09:02 +02:00
|
|
|
func FirstOrCreatePhotoUser(m *PhotoUser) *PhotoUser {
|
|
|
|
found := PhotoUser{}
|
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 {
|
|
|
|
event.AuditErr([]string{"photo %s", "failed to set owner and permissions", "%s"}, m.UID, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|