2022-08-31 12:38:31 +02:00
|
|
|
package daos
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/pocketbase/dbx"
|
|
|
|
"github.com/pocketbase/pocketbase/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ExternalAuthQuery returns a new ExternalAuth select query.
|
|
|
|
func (dao *Dao) ExternalAuthQuery() *dbx.SelectQuery {
|
|
|
|
return dao.ModelQuery(&models.ExternalAuth{})
|
|
|
|
}
|
|
|
|
|
2023-02-23 21:51:42 +02:00
|
|
|
// FindAllExternalAuthsByRecord returns all ExternalAuth models
|
|
|
|
// linked to the provided auth record.
|
2022-10-30 10:28:14 +02:00
|
|
|
func (dao *Dao) FindAllExternalAuthsByRecord(authRecord *models.Record) ([]*models.ExternalAuth, error) {
|
2022-08-31 12:38:31 +02:00
|
|
|
auths := []*models.ExternalAuth{}
|
|
|
|
|
|
|
|
err := dao.ExternalAuthQuery().
|
2022-10-30 10:28:14 +02:00
|
|
|
AndWhere(dbx.HashExp{
|
|
|
|
"collectionId": authRecord.Collection().Id,
|
|
|
|
"recordId": authRecord.Id,
|
|
|
|
}).
|
2022-08-31 12:38:31 +02:00
|
|
|
OrderBy("created ASC").
|
|
|
|
All(&auths)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return auths, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindExternalAuthByProvider returns the first available
|
|
|
|
// ExternalAuth model for the specified provider and providerId.
|
|
|
|
func (dao *Dao) FindExternalAuthByProvider(provider, providerId string) (*models.ExternalAuth, error) {
|
|
|
|
model := &models.ExternalAuth{}
|
|
|
|
|
|
|
|
err := dao.ExternalAuthQuery().
|
2022-09-02 11:05:00 +02:00
|
|
|
AndWhere(dbx.Not(dbx.HashExp{"providerId": ""})). // exclude empty providerIds
|
2022-08-31 12:38:31 +02:00
|
|
|
AndWhere(dbx.HashExp{
|
|
|
|
"provider": provider,
|
|
|
|
"providerId": providerId,
|
|
|
|
}).
|
|
|
|
Limit(1).
|
|
|
|
One(model)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return model, nil
|
|
|
|
}
|
|
|
|
|
2022-10-30 10:28:14 +02:00
|
|
|
// FindExternalAuthByRecordAndProvider returns the first available
|
|
|
|
// ExternalAuth model for the specified record data and provider.
|
|
|
|
func (dao *Dao) FindExternalAuthByRecordAndProvider(authRecord *models.Record, provider string) (*models.ExternalAuth, error) {
|
2022-08-31 12:38:31 +02:00
|
|
|
model := &models.ExternalAuth{}
|
|
|
|
|
|
|
|
err := dao.ExternalAuthQuery().
|
|
|
|
AndWhere(dbx.HashExp{
|
2022-10-30 10:28:14 +02:00
|
|
|
"collectionId": authRecord.Collection().Id,
|
|
|
|
"recordId": authRecord.Id,
|
|
|
|
"provider": provider,
|
2022-08-31 12:38:31 +02:00
|
|
|
}).
|
|
|
|
Limit(1).
|
|
|
|
One(model)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return model, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveExternalAuth upserts the provided ExternalAuth model.
|
|
|
|
func (dao *Dao) SaveExternalAuth(model *models.ExternalAuth) error {
|
2022-09-02 11:05:00 +02:00
|
|
|
// extra check the model data in case the provider's API response
|
2022-09-02 11:45:59 +02:00
|
|
|
// has changed and no longer returns the expected fields
|
2022-10-30 10:28:14 +02:00
|
|
|
if model.CollectionId == "" || model.RecordId == "" || model.Provider == "" || model.ProviderId == "" {
|
2022-09-02 11:05:00 +02:00
|
|
|
return errors.New("Missing required ExternalAuth fields.")
|
|
|
|
}
|
|
|
|
|
2022-08-31 12:38:31 +02:00
|
|
|
return dao.Save(model)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteExternalAuth deletes the provided ExternalAuth model.
|
|
|
|
func (dao *Dao) DeleteExternalAuth(model *models.ExternalAuth) error {
|
|
|
|
return dao.Delete(model)
|
|
|
|
}
|