summaryrefslogtreecommitdiffstats
path: root/cmd/admin_user_must_change_password.go
blob: 2794414259ac4721257a9835491b037a69b0c7e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// 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"

	"github.com/urfave/cli/v2"
)

var microcmdUserMustChangePassword = &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(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
	}

	n, err := user_model.SetMustChangePassword(ctx, all, mustChangePassword, c.Args().Slice(), exclude)
	if err != nil {
		return err
	}

	fmt.Printf("Updated %d users setting MustChangePassword to %t\n", n, mustChangePassword)
	return nil
}