2023-02-15 01:12:19 +03:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
|
2023-07-21 12:28:19 +03:00
|
|
|
"github.com/urfave/cli/v2"
|
2023-02-15 01:12:19 +03:00
|
|
|
)
|
|
|
|
|
2023-07-21 12:28:19 +03:00
|
|
|
var microcmdUserMustChangePassword = &cli.Command{
|
2023-02-15 01:12:19 +03:00
|
|
|
Name: "must-change-password",
|
|
|
|
Usage: "Set the must change password flag for the provided users or all users",
|
|
|
|
Action: runMustChangePassword,
|
|
|
|
Flags: []cli.Flag{
|
2023-07-21 12:28:19 +03:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "all",
|
|
|
|
Aliases: []string{"A"},
|
|
|
|
Usage: "All users must change password, except those explicitly excluded with --exclude",
|
2023-02-15 01:12:19 +03:00
|
|
|
},
|
2023-07-21 12:28:19 +03:00
|
|
|
&cli.StringSliceFlag{
|
|
|
|
Name: "exclude",
|
|
|
|
Aliases: []string{"e"},
|
|
|
|
Usage: "Do not change the must-change-password flag for these users",
|
2023-02-15 01:12:19 +03:00
|
|
|
},
|
2023-07-21 12:28:19 +03:00
|
|
|
&cli.BoolFlag{
|
2023-02-15 01:12:19 +03:00
|
|
|
Name: "unset",
|
|
|
|
Usage: "Instead of setting the must-change-password flag, unset it",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func runMustChangePassword(c *cli.Context) error {
|
|
|
|
ctx, cancel := installSignals()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if c.NArg() == 0 && !c.IsSet("all") {
|
|
|
|
return errors.New("either usernames or --all must be provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
mustChangePassword := !c.Bool("unset")
|
|
|
|
all := c.Bool("all")
|
|
|
|
exclude := c.StringSlice("exclude")
|
|
|
|
|
|
|
|
if err := initDB(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-21 12:28:19 +03:00
|
|
|
n, err := user_model.SetMustChangePassword(ctx, all, mustChangePassword, c.Args().Slice(), exclude)
|
2023-02-15 01:12:19 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Updated %d users setting MustChangePassword to %t\n", n, mustChangePassword)
|
|
|
|
return nil
|
|
|
|
}
|