2021-05-22 14:26:45 +02:00
|
|
|
/*
|
|
|
|
GoToSocial
|
|
|
|
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package account
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-05-30 13:12:00 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/cliactions"
|
2021-05-22 14:26:45 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2021-08-25 15:34:33 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db/bundb"
|
2021-05-22 14:26:45 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2021-09-01 18:29:25 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/validate"
|
2021-07-31 13:57:23 +02:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2021-05-22 14:26:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Create creates a new account in the database using the provided flags.
|
2021-05-30 13:12:00 +02:00
|
|
|
var Create cliactions.GTSAction = func(ctx context.Context, c *config.Config, log *logrus.Logger) error {
|
2021-08-25 15:34:33 +02:00
|
|
|
dbConn, err := bundb.NewBunDBService(ctx, c, log)
|
2021-05-22 14:26:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating dbservice: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
username, ok := c.AccountCLIFlags[config.UsernameFlag]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("no username set")
|
|
|
|
}
|
2021-09-01 18:29:25 +02:00
|
|
|
if err := validate.Username(username); err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
email, ok := c.AccountCLIFlags[config.EmailFlag]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("no email set")
|
|
|
|
}
|
2021-09-01 18:29:25 +02:00
|
|
|
if err := validate.Email(email); err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
password, ok := c.AccountCLIFlags[config.PasswordFlag]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("no password set")
|
|
|
|
}
|
2021-09-01 18:29:25 +02:00
|
|
|
if err := validate.NewPassword(password); err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
_, err = dbConn.NewSignup(ctx, username, "", false, email, password, nil, "", "", false, false)
|
2021-05-22 14:26:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbConn.Stop(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Confirm sets a user to Approved, sets Email to the current UnconfirmedEmail value, and sets ConfirmedAt to now.
|
2021-05-30 13:12:00 +02:00
|
|
|
var Confirm cliactions.GTSAction = func(ctx context.Context, c *config.Config, log *logrus.Logger) error {
|
2021-08-25 15:34:33 +02:00
|
|
|
dbConn, err := bundb.NewBunDBService(ctx, c, log)
|
2021-05-22 14:26:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating dbservice: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
username, ok := c.AccountCLIFlags[config.UsernameFlag]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("no username set")
|
|
|
|
}
|
2021-09-01 18:29:25 +02:00
|
|
|
if err := validate.Username(username); err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
a, err := dbConn.GetLocalAccountByUsername(ctx, username)
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
u := >smodel.User{}
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := dbConn.GetWhere(ctx, []db.Where{{Key: "account_id", Value: a.ID}}, u); err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
u.Approved = true
|
|
|
|
u.Email = u.UnconfirmedEmail
|
|
|
|
u.ConfirmedAt = time.Now()
|
2021-09-10 14:36:10 +02:00
|
|
|
if err := dbConn.UpdateByPrimaryKey(ctx, u); err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbConn.Stop(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Promote sets a user to admin.
|
2021-05-30 13:12:00 +02:00
|
|
|
var Promote cliactions.GTSAction = func(ctx context.Context, c *config.Config, log *logrus.Logger) error {
|
2021-08-25 15:34:33 +02:00
|
|
|
dbConn, err := bundb.NewBunDBService(ctx, c, log)
|
2021-05-22 14:26:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating dbservice: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
username, ok := c.AccountCLIFlags[config.UsernameFlag]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("no username set")
|
|
|
|
}
|
2021-09-01 18:29:25 +02:00
|
|
|
if err := validate.Username(username); err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
a, err := dbConn.GetLocalAccountByUsername(ctx, username)
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
u := >smodel.User{}
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := dbConn.GetWhere(ctx, []db.Where{{Key: "account_id", Value: a.ID}}, u); err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
u.Admin = true
|
2021-09-10 14:36:10 +02:00
|
|
|
if err := dbConn.UpdateByPrimaryKey(ctx, u); err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbConn.Stop(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Demote sets admin on a user to false.
|
2021-05-30 13:12:00 +02:00
|
|
|
var Demote cliactions.GTSAction = func(ctx context.Context, c *config.Config, log *logrus.Logger) error {
|
2021-08-25 15:34:33 +02:00
|
|
|
dbConn, err := bundb.NewBunDBService(ctx, c, log)
|
2021-05-22 14:26:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating dbservice: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
username, ok := c.AccountCLIFlags[config.UsernameFlag]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("no username set")
|
|
|
|
}
|
2021-09-01 18:29:25 +02:00
|
|
|
if err := validate.Username(username); err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
a, err := dbConn.GetLocalAccountByUsername(ctx, username)
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
u := >smodel.User{}
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := dbConn.GetWhere(ctx, []db.Where{{Key: "account_id", Value: a.ID}}, u); err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
u.Admin = false
|
2021-09-10 14:36:10 +02:00
|
|
|
if err := dbConn.UpdateByPrimaryKey(ctx, u); err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbConn.Stop(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable sets Disabled to true on a user.
|
2021-05-30 13:12:00 +02:00
|
|
|
var Disable cliactions.GTSAction = func(ctx context.Context, c *config.Config, log *logrus.Logger) error {
|
2021-08-25 15:34:33 +02:00
|
|
|
dbConn, err := bundb.NewBunDBService(ctx, c, log)
|
2021-05-22 14:26:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating dbservice: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
username, ok := c.AccountCLIFlags[config.UsernameFlag]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("no username set")
|
|
|
|
}
|
2021-09-01 18:29:25 +02:00
|
|
|
if err := validate.Username(username); err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
a, err := dbConn.GetLocalAccountByUsername(ctx, username)
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
u := >smodel.User{}
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := dbConn.GetWhere(ctx, []db.Where{{Key: "account_id", Value: a.ID}}, u); err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
u.Disabled = true
|
2021-09-10 14:36:10 +02:00
|
|
|
if err := dbConn.UpdateByPrimaryKey(ctx, u); err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbConn.Stop(ctx)
|
|
|
|
}
|
|
|
|
|
2021-05-30 13:12:00 +02:00
|
|
|
// Suspend suspends the target account, cleanly removing all of its media, followers, following, likes, statuses, etc.
|
|
|
|
var Suspend cliactions.GTSAction = func(ctx context.Context, c *config.Config, log *logrus.Logger) error {
|
2021-05-22 14:26:45 +02:00
|
|
|
// TODO
|
|
|
|
return nil
|
|
|
|
}
|
2021-07-31 13:57:23 +02:00
|
|
|
|
|
|
|
// Password sets the password of target account.
|
|
|
|
var Password cliactions.GTSAction = func(ctx context.Context, c *config.Config, log *logrus.Logger) error {
|
2021-08-25 15:34:33 +02:00
|
|
|
dbConn, err := bundb.NewBunDBService(ctx, c, log)
|
2021-07-31 13:57:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating dbservice: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
username, ok := c.AccountCLIFlags[config.UsernameFlag]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("no username set")
|
|
|
|
}
|
2021-09-01 18:29:25 +02:00
|
|
|
if err := validate.Username(username); err != nil {
|
2021-07-31 13:57:23 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
password, ok := c.AccountCLIFlags[config.PasswordFlag]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("no password set")
|
|
|
|
}
|
2021-09-01 18:29:25 +02:00
|
|
|
if err := validate.NewPassword(password); err != nil {
|
2021-07-31 13:57:23 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
a, err := dbConn.GetLocalAccountByUsername(ctx, username)
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2021-07-31 13:57:23 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
u := >smodel.User{}
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := dbConn.GetWhere(ctx, []db.Where{{Key: "account_id", Value: a.ID}}, u); err != nil {
|
2021-07-31 13:57:23 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pw, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error hashing password: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
u.EncryptedPassword = string(pw)
|
|
|
|
|
2021-09-10 14:36:10 +02:00
|
|
|
if err := dbConn.UpdateByPrimaryKey(ctx, u); err != nil {
|
2021-07-31 13:57:23 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|