184 lines
3.9 KiB
Go
184 lines
3.9 KiB
Go
/*
|
|
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
|
*
|
|
* This work is published and licensed under a Creative Commons
|
|
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
|
|
*
|
|
* Distribution of this work is permitted, but commercial use and
|
|
* modifications are strictly prohibited.
|
|
*/
|
|
|
|
package maindb
|
|
|
|
import (
|
|
"context"
|
|
|
|
"mstore/pkg/auxpwd"
|
|
"mstore/pkg/auxtool"
|
|
"mstore/pkg/auxuuid"
|
|
"mstore/pkg/descr"
|
|
"mstore/pkg/terms"
|
|
)
|
|
|
|
func (db *Database) WriteAnonymous(ctx context.Context) error {
|
|
var err error
|
|
|
|
now := auxtool.TimeNow()
|
|
password := auxtool.RandomString(64)
|
|
passhash := auxpwd.MakeSHA256Hash([]byte(password))
|
|
accountDescr := &descr.Account{
|
|
ID: terms.AnonymousID,
|
|
Username: terms.AnonimousUsername,
|
|
Passhash: passhash,
|
|
Disabled: false,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
CreatedBy: terms.ServerID,
|
|
UpdatedBy: terms.ServerID,
|
|
}
|
|
err = db.InsertAccount(ctx, accountDescr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
grantDescr := &descr.Grant{
|
|
ID: auxuuid.NewUUID(),
|
|
AccountID: accountDescr.ID,
|
|
Right: terms.RightReadFiles,
|
|
Pattern: ".*",
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
CreatedBy: terms.ServerID,
|
|
UpdatedBy: terms.ServerID,
|
|
}
|
|
err = db.InsertGrant(ctx, grantDescr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
grantDescr = &descr.Grant{
|
|
ID: auxuuid.NewUUID(),
|
|
AccountID: accountDescr.ID,
|
|
Right: terms.RightReadImages,
|
|
Pattern: ".*",
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
CreatedBy: terms.ServerID,
|
|
UpdatedBy: terms.ServerID,
|
|
}
|
|
err = db.InsertGrant(ctx, grantDescr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
func (db *Database) WriteInituser(ctx context.Context) error {
|
|
var err error
|
|
now := auxtool.TimeNow()
|
|
passhash := auxpwd.MakeSHA256Hash([]byte(terms.InitUsername))
|
|
accountDescr := &descr.Account{
|
|
ID: terms.InitID,
|
|
Username: terms.InitUsername,
|
|
Passhash: passhash,
|
|
Disabled: false,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
CreatedBy: terms.ServerID,
|
|
UpdatedBy: terms.ServerID,
|
|
}
|
|
err = db.InsertAccount(ctx, accountDescr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// Files
|
|
grantDescr := &descr.Grant{
|
|
ID: auxuuid.NewUUID(),
|
|
AccountID: accountDescr.ID,
|
|
Right: terms.RightReadFiles,
|
|
Pattern: ".*",
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
CreatedBy: terms.ServerID,
|
|
UpdatedBy: terms.ServerID,
|
|
}
|
|
err = db.InsertGrant(ctx, grantDescr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
grantDescr = &descr.Grant{
|
|
ID: auxuuid.NewUUID(),
|
|
AccountID: accountDescr.ID,
|
|
Right: terms.RightWriteFiles,
|
|
Pattern: ".*",
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
CreatedBy: terms.ServerID,
|
|
UpdatedBy: terms.ServerID,
|
|
}
|
|
err = db.InsertGrant(ctx, grantDescr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// Accounts
|
|
grantDescr = &descr.Grant{
|
|
ID: auxuuid.NewUUID(),
|
|
AccountID: accountDescr.ID,
|
|
Right: terms.RightReadAccounts,
|
|
Pattern: ".*",
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
CreatedBy: terms.ServerID,
|
|
UpdatedBy: terms.ServerID,
|
|
}
|
|
err = db.InsertGrant(ctx, grantDescr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
grantDescr = &descr.Grant{
|
|
ID: auxuuid.NewUUID(),
|
|
AccountID: accountDescr.ID,
|
|
Right: terms.RightWriteAccounts,
|
|
Pattern: ".*",
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
CreatedBy: terms.ServerID,
|
|
UpdatedBy: terms.ServerID,
|
|
}
|
|
err = db.InsertGrant(ctx, grantDescr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// Images
|
|
grantDescr = &descr.Grant{
|
|
ID: auxuuid.NewUUID(),
|
|
AccountID: accountDescr.ID,
|
|
Right: terms.RightReadImages,
|
|
Pattern: ".*",
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
CreatedBy: terms.ServerID,
|
|
UpdatedBy: terms.ServerID,
|
|
}
|
|
err = db.InsertGrant(ctx, grantDescr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
grantDescr = &descr.Grant{
|
|
ID: auxuuid.NewUUID(),
|
|
AccountID: accountDescr.ID,
|
|
Right: terms.RightWriteImages,
|
|
Pattern: ".*",
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
CreatedBy: terms.ServerID,
|
|
UpdatedBy: terms.ServerID,
|
|
}
|
|
err = db.InsertGrant(ctx, grantDescr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return err
|
|
|
|
}
|