navigation
This commit is contained in:
@@ -26,7 +26,7 @@ func NewPasswordAuthenticator(
|
||||
func (s *PasswordAuthenticator) Authenticate(ctx context.Context, creds auth.Credentials) (*auth.Session, error) {
|
||||
|
||||
user, err := s.userRepository.FindUserByEmail(creds.Email)
|
||||
if err != nil {
|
||||
if err != nil || !user.Enabled {
|
||||
return nil, fmt.Errorf("Unauthorized user %s : %s", creds.Email, err)
|
||||
}
|
||||
|
||||
@@ -61,15 +61,28 @@ func (s *PasswordAuthenticator) Register(ctx context.Context, registration auth.
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
user, err := s.userRepository.CreateUser(&domain.User{
|
||||
|
||||
user := &domain.User{
|
||||
Email: registration.Email,
|
||||
Firstname: registration.Firstname,
|
||||
Lastname: registration.Lastname,
|
||||
Password: hashedPassord,
|
||||
})
|
||||
Enabled: true,
|
||||
}
|
||||
|
||||
count, err := s.userRepository.CountUsers()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_ = user
|
||||
return nil, nil
|
||||
if count == 0 {
|
||||
user.Administrator = true
|
||||
}
|
||||
|
||||
user, err = s.userRepository.CreateUser(user)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &auth.Session{User: user}, nil
|
||||
}
|
||||
|
||||
@@ -9,8 +9,9 @@ import (
|
||||
)
|
||||
|
||||
type Database interface {
|
||||
SettingsReporitory() domain.SettingsRepository
|
||||
UserReporitory() domain.UserRepository
|
||||
SettingsRepository() domain.SettingsRepository
|
||||
UserRepository() domain.UserRepository
|
||||
FileRepository() domain.FileRepository
|
||||
Migrate() error
|
||||
Close()
|
||||
}
|
||||
|
||||
@@ -31,18 +31,6 @@ func NewTursoDB(cfg *config.Config) (*TursoDB, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (db *TursoDB) UserReporitory() domain.UserRepository {
|
||||
return &TursoUserRepository{
|
||||
DB: db.DB,
|
||||
}
|
||||
}
|
||||
|
||||
func (db *TursoDB) SettingsReporitory() domain.SettingsRepository {
|
||||
return &TursoSettingsRepository{
|
||||
DB: db.DB,
|
||||
}
|
||||
}
|
||||
|
||||
func (db *TursoDB) Close() {
|
||||
db.DB.Close()
|
||||
}
|
||||
@@ -67,3 +55,15 @@ func (db *TursoDB) Migrate() error {
|
||||
m.Up()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *TursoDB) UserRepository() domain.UserRepository {
|
||||
return &TursoUserRepository{DB: db.DB}
|
||||
}
|
||||
|
||||
func (db *TursoDB) SettingsRepository() domain.SettingsRepository {
|
||||
return &TursoSettingsRepository{DB: db.DB}
|
||||
}
|
||||
|
||||
func (db *TursoDB) FileRepository() domain.FileRepository {
|
||||
return &TursoFileRepository{DB: db.DB}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
package turso
|
||||
|
||||
import (
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"trankilou.fr/lassistanoque/backend/internal/adapter/database/dberrors"
|
||||
"trankilou.fr/lassistanoque/backend/internal/domain"
|
||||
"trankilou.fr/lassistanoque/backend/internal/utility"
|
||||
)
|
||||
|
||||
type TursoFileRepository struct {
|
||||
DB *sqlx.DB
|
||||
}
|
||||
|
||||
func (fr *TursoFileRepository) FindByID(id string) (*domain.File, error) {
|
||||
var file domain.File
|
||||
err := fr.DB.Get(&file, "select id, name, content_type, storage_path, storage_filename, date_created, date_updated, _version from files where id=$1", id)
|
||||
return &file, err
|
||||
}
|
||||
|
||||
func (fr *TursoFileRepository) Create(file *domain.File) (*domain.File, error) {
|
||||
file.ID = utility.GenID()
|
||||
file.VersionId = utility.GenID()
|
||||
file.DateCreated = time.Now()
|
||||
file.DateUpdated = time.Now()
|
||||
_, err := fr.DB.NamedExec(
|
||||
`insert into files (id, name, content_type, storage_path, storage_filename, date_created, date_updated, _version)
|
||||
values (:id, :name, :content_type, :storage_path, :storage_filename, :date_created, :date_updated, :_version)`,
|
||||
file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func (fr *TursoFileRepository) Update(file *domain.File) (*domain.File, error) {
|
||||
newVersion := utility.GenID()
|
||||
file.DateUpdated = time.Now()
|
||||
res, err := fr.DB.Exec(
|
||||
`update files
|
||||
set name=$1,
|
||||
content_type=$2,
|
||||
storage_path=$3,
|
||||
storage_filename=$4,
|
||||
date_updated=$5,
|
||||
_version=$6
|
||||
where id=$7 and _version=$8`,
|
||||
file.Name, file.ContentType, file.StoragePath, file.StorageFilename, file.DateUpdated,
|
||||
newVersion, file.ID, file.VersionId,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if count, _ := res.RowsAffected(); count == 0 {
|
||||
return nil, dberrors.ErrNoRowUpdated
|
||||
}
|
||||
|
||||
file.VersionId = newVersion
|
||||
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func (fr *TursoFileRepository) Delete(id string) error {
|
||||
res, err := fr.DB.Exec("delete from files where id=$1", id)
|
||||
if count, _ := res.RowsAffected(); count == 0 {
|
||||
return dberrors.ErrNoRowUpdated
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (fr *TursoFileRepository) Upload(reader io.Reader, path string, name string, contentType string, replace bool) (*domain.File, error) {
|
||||
|
||||
bytes, err := io.ReadAll(reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
file := &domain.File{
|
||||
ID: utility.GenID(),
|
||||
Name: name,
|
||||
StoragePath: path,
|
||||
StorageFilename: name,
|
||||
ContentType: contentType,
|
||||
VersionId: utility.GenID(),
|
||||
DateCreated: time.Now(),
|
||||
DateUpdated: time.Now(),
|
||||
Content: bytes,
|
||||
}
|
||||
|
||||
_, err = fr.DB.NamedExec(
|
||||
`insert into files (id, name, content_type, storage_path, storage_filename, date_created, date_updated, _version, content)
|
||||
values (:id, :name, :content_type, :storage_path, :storage_filename, :date_created, :date_updated, :_version, :content)`,
|
||||
file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func (fr *TursoFileRepository) Download(id string) (*domain.File, error) {
|
||||
var file domain.File
|
||||
err := fr.DB.Get(&file, "select * from files where id=$1", id)
|
||||
return &file, err
|
||||
}
|
||||
@@ -24,72 +24,72 @@ create table settings (
|
||||
);
|
||||
|
||||
create table oidc (
|
||||
id string not null primary key,
|
||||
label string,
|
||||
domain string,
|
||||
client_id string,
|
||||
client_secret string,
|
||||
wellknown_url string,
|
||||
id text not null primary key,
|
||||
label text,
|
||||
domain text,
|
||||
client_id text,
|
||||
client_secret text,
|
||||
wellknown_url text,
|
||||
_version text
|
||||
);
|
||||
|
||||
create table users (
|
||||
id string not null primary key,
|
||||
firstname string not null default '',
|
||||
lastname string not null default '',
|
||||
password string not null default '',
|
||||
email string not null unique,
|
||||
picture string not null default '',
|
||||
id text not null primary key,
|
||||
firstname text not null default '',
|
||||
lastname text not null default '',
|
||||
password text not null default '',
|
||||
email text not null unique,
|
||||
picture text not null default '',
|
||||
enabled numeric default true,
|
||||
administrator numeric default false,
|
||||
_version text not null
|
||||
);
|
||||
|
||||
create table user_addresses (
|
||||
id string not null primary key,
|
||||
user_id string,
|
||||
type string,
|
||||
address string,
|
||||
id text not null primary key,
|
||||
user_id text,
|
||||
type text,
|
||||
address text,
|
||||
_version text
|
||||
);
|
||||
|
||||
create table channels (
|
||||
id string not null primary key,
|
||||
name string,
|
||||
type string,
|
||||
id text not null primary key,
|
||||
name text,
|
||||
type text,
|
||||
enabled numeric,
|
||||
configuration string,
|
||||
configuration text,
|
||||
_version text
|
||||
);
|
||||
|
||||
create table tools (
|
||||
id string not null primary key,
|
||||
name string,
|
||||
type string,
|
||||
id text not null primary key,
|
||||
name text,
|
||||
type text,
|
||||
enabled numeric,
|
||||
configuration string,
|
||||
configuration text,
|
||||
_version text
|
||||
);
|
||||
|
||||
create table tasks (
|
||||
id string not null primary key,
|
||||
owner_id string,
|
||||
model_id string,
|
||||
label string,
|
||||
prompt string,
|
||||
cron string,
|
||||
status string,
|
||||
id text not null primary key,
|
||||
owner_id text,
|
||||
model_id text,
|
||||
label text,
|
||||
prompt text,
|
||||
cron text,
|
||||
status text,
|
||||
next_datetime numeric,
|
||||
_version text
|
||||
);
|
||||
|
||||
create table history (
|
||||
id string not null primary key,
|
||||
task_id string,
|
||||
id text not null primary key,
|
||||
task_id text,
|
||||
start_datetimle numeric,
|
||||
end_datetime numeric,
|
||||
prompt string,
|
||||
log string,
|
||||
response string,
|
||||
prompt text,
|
||||
log text,
|
||||
response text,
|
||||
_version text
|
||||
);
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
drop table files;
|
||||
@@ -0,0 +1,11 @@
|
||||
create table files (
|
||||
id text not null primary key,
|
||||
name text not null default '',
|
||||
content_type text not null default '',
|
||||
storage_path text not null default '',
|
||||
storage_filename text not null default '',
|
||||
date_created numeric not null default current_timestamp,
|
||||
date_updated numeric not null default 0,
|
||||
content blob,
|
||||
_version text not null
|
||||
);
|
||||
@@ -37,21 +37,18 @@ func (ur *TursoUserRepository) ListUsers() ([]*domain.User, error) {
|
||||
|
||||
func (ur *TursoUserRepository) CreateUser(user *domain.User) (*domain.User, error) {
|
||||
|
||||
count, err := ur.CountUsers()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if count == 0 {
|
||||
user.Administrator = true
|
||||
}
|
||||
user.ID = utility.GenID()
|
||||
user.VersionId = utility.GenID()
|
||||
|
||||
ur.DB.NamedExec(
|
||||
_, err := ur.DB.NamedExec(
|
||||
`insert into users (id, email, firstname, lastname, enabled, password, administrator, _version)
|
||||
values (:id, :email, :firstname, :lastname, :enabled, :password, :administrator, :_version)`,
|
||||
user)
|
||||
return nil, nil
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (ur *TursoUserRepository) UpdateUser(user *domain.User) (*domain.User, error) {
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"trankilou.fr/lassistanoque/backend/internal/domain"
|
||||
)
|
||||
|
||||
type DatabaseStorageProvider struct {
|
||||
repo domain.FileRepository
|
||||
}
|
||||
|
||||
func NewDatabaseStorageProvider(repo domain.FileRepository) *DatabaseStorageProvider {
|
||||
return &DatabaseStorageProvider{repo}
|
||||
}
|
||||
|
||||
func (p *DatabaseStorageProvider) EnsureBucket(
|
||||
ctx context.Context,
|
||||
bucketName string,
|
||||
) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *DatabaseStorageProvider) PutObject(
|
||||
ctx context.Context,
|
||||
reader io.Reader,
|
||||
bucketName string,
|
||||
objectName string,
|
||||
filePath string,
|
||||
contentType string,
|
||||
) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *DatabaseStorageProvider) GetObject(
|
||||
ctx context.Context,
|
||||
bucketName string,
|
||||
objectName string,
|
||||
filePath string,
|
||||
) (io.ReadCloser, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (p *DatabaseStorageProvider) DeleteObject(
|
||||
ctx context.Context,
|
||||
bucketName string,
|
||||
objectName string,
|
||||
filePath string,
|
||||
) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *DatabaseStorageProvider) ExistsObject(
|
||||
ctx context.Context,
|
||||
bucketName string,
|
||||
objectName string,
|
||||
filePath string,
|
||||
) (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
func (p *DatabaseStorageProvider) GetPresignedURL(
|
||||
ctx context.Context,
|
||||
bucketName string,
|
||||
objectName string,
|
||||
filePath string,
|
||||
Expiry time.Time,
|
||||
) (*url.URL, error) {
|
||||
parsed, _ := url.Parse("https://localhost")
|
||||
return parsed, nil
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"trankilou.fr/lassistanoque/backend/internal/adapter/file/database"
|
||||
"trankilou.fr/lassistanoque/backend/internal/config"
|
||||
"trankilou.fr/lassistanoque/backend/internal/domain"
|
||||
"trankilou.fr/lassistanoque/backend/internal/service/storage"
|
||||
)
|
||||
|
||||
func GetStorageProvider(repository domain.FileRepository) (storage.StorageProvider, error) {
|
||||
cfg := config.GetConfig()
|
||||
switch cfg.StorageType {
|
||||
case "database":
|
||||
return database.NewDatabaseStorageProvider(repository), nil
|
||||
default:
|
||||
}
|
||||
return nil, fmt.Errorf("Storage %s not implemented", cfg.StorageType)
|
||||
}
|
||||
Reference in New Issue
Block a user