2023-03-12 16:00:57 +01:00
|
|
|
// GoToSocial
|
|
|
|
// Copyright (C) GoToSocial Authors admin@gotosocial.org
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
//
|
|
|
|
// 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/>.
|
2021-05-22 14:26:45 +02:00
|
|
|
|
|
|
|
package account
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-03-27 16:02:26 +02:00
|
|
|
"os"
|
|
|
|
"text/tabwriter"
|
2021-05-22 14:26:45 +02:00
|
|
|
"time"
|
|
|
|
|
2021-12-07 13:31:39 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/cmd/gotosocial/action"
|
2021-05-22 14:26:45 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
2021-08-25 15:34:33 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db/bundb"
|
2023-07-23 12:33:17 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2022-12-08 18:35:14 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/state"
|
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
|
|
|
)
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
func initState(ctx context.Context) (*state.State, error) {
|
2022-12-08 18:35:14 +01:00
|
|
|
var state state.State
|
|
|
|
state.Caches.Init()
|
2023-07-23 12:33:17 +02:00
|
|
|
state.Caches.Start()
|
2023-02-13 19:40:48 +01:00
|
|
|
state.Workers.Start()
|
2022-12-08 18:35:14 +01:00
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
// Set the state DB connection
|
2022-12-08 18:35:14 +01:00
|
|
|
dbConn, err := bundb.NewBunDBService(ctx, &state)
|
2021-05-22 14:26:45 +02:00
|
|
|
if err != nil {
|
2023-07-23 12:33:17 +02:00
|
|
|
return nil, fmt.Errorf("error creating dbConn: %w", err)
|
2021-05-22 14:26:45 +02:00
|
|
|
}
|
2022-12-08 18:35:14 +01:00
|
|
|
state.DB = dbConn
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
return &state, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func stopState(ctx context.Context, state *state.State) error {
|
|
|
|
if err := state.DB.Stop(ctx); err != nil {
|
|
|
|
return fmt.Errorf("error stopping dbConn: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Workers.Stop()
|
|
|
|
state.Caches.Stop()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates a new account and user
|
|
|
|
// in the database using the provided flags.
|
|
|
|
var Create action.GTSAction = func(ctx context.Context) error {
|
|
|
|
state, err := initState(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-05-22 14:26:45 +02:00
|
|
|
}
|
2023-07-23 12:33:17 +02:00
|
|
|
|
|
|
|
username := config.GetAdminAccountUsername()
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
usernameAvailable, err := state.DB.IsUsernameAvailable(ctx, username)
|
2022-10-11 12:16:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-23 12:33:17 +02:00
|
|
|
|
2022-10-11 12:16:34 +02:00
|
|
|
if !usernameAvailable {
|
|
|
|
return fmt.Errorf("username %s is already in use", username)
|
|
|
|
}
|
|
|
|
|
2022-05-30 14:41:24 +02:00
|
|
|
email := config.GetAdminAccountEmail()
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
emailAvailable, err := state.DB.IsEmailAvailable(ctx, email)
|
2022-10-11 12:16:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-23 12:33:17 +02:00
|
|
|
|
2022-10-11 12:16:34 +02:00
|
|
|
if !emailAvailable {
|
|
|
|
return fmt.Errorf("email address %s is already in use", email)
|
|
|
|
}
|
|
|
|
|
2022-05-30 14:41:24 +02:00
|
|
|
password := config.GetAdminAccountPassword()
|
2023-07-23 12:33:17 +02:00
|
|
|
if err := validate.Password(password); err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
if _, err := state.DB.NewSignup(ctx, gtsmodel.NewSignup{
|
|
|
|
Username: username,
|
|
|
|
Email: email,
|
|
|
|
Password: password,
|
|
|
|
EmailVerified: true, // Assume cli user wants email marked as verified already.
|
|
|
|
PreApproved: true, // Assume cli user wants account marked as approved already.
|
|
|
|
}); err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
return stopState(ctx, state)
|
2021-05-22 14:26:45 +02:00
|
|
|
}
|
|
|
|
|
2023-03-27 16:02:26 +02:00
|
|
|
// List returns all existing local accounts.
|
|
|
|
var List action.GTSAction = func(ctx context.Context) error {
|
2023-07-23 12:33:17 +02:00
|
|
|
state, err := initState(ctx)
|
2023-03-27 16:02:26 +02:00
|
|
|
if err != nil {
|
2023-07-23 12:33:17 +02:00
|
|
|
return err
|
2023-03-27 16:02:26 +02:00
|
|
|
}
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
users, err := state.DB.GetAllUsers(ctx)
|
2023-03-27 16:02:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmtBool := func(b *bool) string {
|
|
|
|
if b == nil {
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
if *b {
|
|
|
|
return "yes"
|
|
|
|
}
|
|
|
|
return "no"
|
|
|
|
}
|
|
|
|
|
|
|
|
fmtDate := func(t time.Time) string {
|
|
|
|
if t.Equal(time.Time{}) {
|
|
|
|
return "no"
|
|
|
|
}
|
|
|
|
return "yes"
|
|
|
|
}
|
|
|
|
|
|
|
|
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
|
|
|
|
fmt.Fprintln(w, "user\taccount\tapproved\tadmin\tmoderator\tsuspended\tconfirmed")
|
|
|
|
for _, u := range users {
|
|
|
|
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", u.Account.Username, u.AccountID, fmtBool(u.Approved), fmtBool(u.Admin), fmtBool(u.Moderator), fmtDate(u.Account.SuspendedAt), fmtDate(u.ConfirmedAt))
|
|
|
|
}
|
|
|
|
w.Flush()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
// Confirm sets a user to Approved, sets Email to the current
|
|
|
|
// UnconfirmedEmail value, and sets ConfirmedAt to now.
|
2021-12-07 13:31:39 +01:00
|
|
|
var Confirm action.GTSAction = func(ctx context.Context) error {
|
2023-07-23 12:33:17 +02:00
|
|
|
state, err := initState(ctx)
|
2021-05-22 14:26:45 +02:00
|
|
|
if err != nil {
|
2023-07-23 12:33:17 +02:00
|
|
|
return err
|
2021-05-22 14:26:45 +02:00
|
|
|
}
|
|
|
|
|
2022-05-30 14:41:24 +02:00
|
|
|
username := config.GetAdminAccountUsername()
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
account, err := state.DB.GetAccountByUsernameDomain(ctx, username, "")
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
user, err := state.DB.GetUserByAccountID(ctx, account.ID)
|
2022-10-03 10:46:11 +02:00
|
|
|
if err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
user.Approved = func() *bool { a := true; return &a }()
|
|
|
|
user.Email = user.UnconfirmedEmail
|
|
|
|
user.ConfirmedAt = time.Now()
|
|
|
|
if err := state.DB.UpdateUser(
|
|
|
|
ctx, user,
|
|
|
|
"approved", "email", "confirmed_at",
|
|
|
|
); err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
return stopState(ctx, state)
|
2021-05-22 14:26:45 +02:00
|
|
|
}
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
// Promote sets admin + moderator flags on a user to true.
|
2021-12-07 13:31:39 +01:00
|
|
|
var Promote action.GTSAction = func(ctx context.Context) error {
|
2023-07-23 12:33:17 +02:00
|
|
|
state, err := initState(ctx)
|
2021-05-22 14:26:45 +02:00
|
|
|
if err != nil {
|
2023-07-23 12:33:17 +02:00
|
|
|
return err
|
2021-05-22 14:26:45 +02:00
|
|
|
}
|
|
|
|
|
2022-05-30 14:41:24 +02:00
|
|
|
username := config.GetAdminAccountUsername()
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
account, err := state.DB.GetAccountByUsernameDomain(ctx, username, "")
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
user, err := state.DB.GetUserByAccountID(ctx, account.ID)
|
2022-10-03 10:46:11 +02:00
|
|
|
if err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-08-15 12:35:05 +02:00
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
user.Admin = func() *bool { a := true; return &a }()
|
|
|
|
user.Moderator = func() *bool { a := true; return &a }()
|
|
|
|
if err := state.DB.UpdateUser(
|
|
|
|
ctx, user,
|
|
|
|
"admin", "moderator",
|
|
|
|
); err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
return stopState(ctx, state)
|
2021-05-22 14:26:45 +02:00
|
|
|
}
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
// Demote sets admin + moderator flags on a user to false.
|
2021-12-07 13:31:39 +01:00
|
|
|
var Demote action.GTSAction = func(ctx context.Context) error {
|
2023-07-23 12:33:17 +02:00
|
|
|
state, err := initState(ctx)
|
2021-05-22 14:26:45 +02:00
|
|
|
if err != nil {
|
2023-07-23 12:33:17 +02:00
|
|
|
return err
|
2021-05-22 14:26:45 +02:00
|
|
|
}
|
|
|
|
|
2022-05-30 14:41:24 +02:00
|
|
|
username := config.GetAdminAccountUsername()
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
a, err := state.DB.GetAccountByUsernameDomain(ctx, username, "")
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
user, err := state.DB.GetUserByAccountID(ctx, a.ID)
|
2022-10-03 10:46:11 +02:00
|
|
|
if err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-08-15 12:35:05 +02:00
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
user.Admin = func() *bool { a := false; return &a }()
|
|
|
|
user.Moderator = func() *bool { a := false; return &a }()
|
|
|
|
if err := state.DB.UpdateUser(
|
|
|
|
ctx, user,
|
|
|
|
"admin", "moderator",
|
|
|
|
); err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
return stopState(ctx, state)
|
2021-05-22 14:26:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Disable sets Disabled to true on a user.
|
2021-12-07 13:31:39 +01:00
|
|
|
var Disable action.GTSAction = func(ctx context.Context) error {
|
2023-07-23 12:33:17 +02:00
|
|
|
state, err := initState(ctx)
|
2021-05-22 14:26:45 +02:00
|
|
|
if err != nil {
|
2023-07-23 12:33:17 +02:00
|
|
|
return err
|
2021-05-22 14:26:45 +02:00
|
|
|
}
|
|
|
|
|
2022-05-30 14:41:24 +02:00
|
|
|
username := config.GetAdminAccountUsername()
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
account, err := state.DB.GetAccountByUsernameDomain(ctx, username, "")
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
user, err := state.DB.GetUserByAccountID(ctx, account.ID)
|
2022-10-03 10:46:11 +02:00
|
|
|
if err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-08-15 12:35:05 +02:00
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
user.Disabled = func() *bool { d := true; return &d }()
|
|
|
|
if err := state.DB.UpdateUser(
|
|
|
|
ctx, user,
|
|
|
|
"disabled",
|
|
|
|
); err != nil {
|
2021-05-22 14:26:45 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
return stopState(ctx, state)
|
2021-05-22 14:26:45 +02:00
|
|
|
}
|
|
|
|
|
2021-07-31 13:57:23 +02:00
|
|
|
// Password sets the password of target account.
|
2021-12-07 13:31:39 +01:00
|
|
|
var Password action.GTSAction = func(ctx context.Context) error {
|
2023-07-23 12:33:17 +02:00
|
|
|
state, err := initState(ctx)
|
2021-07-31 13:57:23 +02:00
|
|
|
if err != nil {
|
2023-07-23 12:33:17 +02:00
|
|
|
return err
|
2021-07-31 13:57:23 +02:00
|
|
|
}
|
|
|
|
|
2022-05-30 14:41:24 +02:00
|
|
|
username := config.GetAdminAccountUsername()
|
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
|
|
|
|
}
|
|
|
|
|
2022-05-30 14:41:24 +02:00
|
|
|
password := config.GetAdminAccountPassword()
|
2023-07-23 12:33:17 +02:00
|
|
|
if err := validate.Password(password); err != nil {
|
2021-07-31 13:57:23 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
account, err := state.DB.GetAccountByUsernameDomain(ctx, username, "")
|
2021-08-20 12:26:56 +02:00
|
|
|
if err != nil {
|
2021-07-31 13:57:23 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
user, err := state.DB.GetUserByAccountID(ctx, account.ID)
|
2022-10-03 10:46:11 +02:00
|
|
|
if err != nil {
|
2021-07-31 13:57:23 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
encryptedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
2021-07-31 13:57:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error hashing password: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-07-23 12:33:17 +02:00
|
|
|
user.EncryptedPassword = string(encryptedPassword)
|
|
|
|
if err := state.DB.UpdateUser(
|
|
|
|
ctx, user,
|
|
|
|
"encrypted_password",
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return stopState(ctx, state)
|
2021-07-31 13:57:23 +02:00
|
|
|
}
|