Some checks failed
cache-seeder / prune (push) Has been skipped
cache-seeder / frontend (push) Failing after 3s
cache-seeder / python (push) Failing after 3s
cache-seeder / gobuild (push) Failing after 4s
cache-seeder / lint (bindata, lint-backend) (push) Failing after 3s
giteabot backport / giteabot (push) Has been skipped
giteabot / giteabot (push) Has been skipped
cache-prune / prune (push) Has been skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Has been cancelled
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Has been cancelled
release-nightly / nightly-binary (push) Has been cancelled
release-nightly / nightly-container (push) Has been cancelled
cron-renovate / cron-renovate (push) Has been skipped
64 lines
1.6 KiB
Go
64 lines
1.6 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
user_model "gitea.dev/models/user"
|
|
"gitea.dev/modules/setting"
|
|
|
|
"github.com/urfave/cli/v3"
|
|
)
|
|
|
|
func microcmdUserMustChangePassword() *cli.Command {
|
|
return &cli.Command{
|
|
Name: "must-change-password",
|
|
Usage: "Set the must change password flag for the provided users or all users",
|
|
Action: runMustChangePassword,
|
|
Flags: []cli.Flag{
|
|
&cli.BoolFlag{
|
|
Name: "all",
|
|
Aliases: []string{"A"},
|
|
Usage: "All users must change password, except those explicitly excluded with --exclude",
|
|
},
|
|
&cli.StringSliceFlag{
|
|
Name: "exclude",
|
|
Aliases: []string{"e"},
|
|
Usage: "Do not change the must-change-password flag for these users",
|
|
},
|
|
&cli.BoolFlag{
|
|
Name: "unset",
|
|
Usage: "Instead of setting the must-change-password flag, unset it",
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func runMustChangePassword(ctx context.Context, c *cli.Command) error {
|
|
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 !setting.IsInTesting {
|
|
if err := initDB(ctx); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
n, err := user_model.SetMustChangePassword(ctx, all, mustChangePassword, c.Args().Slice(), exclude)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// codeql[disable-next-line=go/clear-text-logging]
|
|
cprintf(c, "Updated %d users setting MustChangePassword to %t\n", n, mustChangePassword)
|
|
return nil
|
|
}
|