mirror of
				https://codeberg.org/forgejo/forgejo.git
				synced 2025-11-04 00:11:04 +00:00 
			
		
		
		
	- Massive replacement of changing `code.gitea.io/gitea` to `forgejo.org`. - Resolves forgejo/discussions#258 Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7337 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Reviewed-by: Michael Kriese <michael.kriese@gmx.de> Reviewed-by: Beowulf <beowulf@beocode.eu> Reviewed-by: Panagiotis "Ivory" Vasilopoulos <git@n0toose.net> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
		
			
				
	
	
		
			60 lines
		
	
	
	
		
			1.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			60 lines
		
	
	
	
		
			1.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
// Copyright 2023 The Gitea Authors. All rights reserved.
 | 
						|
// SPDX-License-Identifier: MIT
 | 
						|
 | 
						|
package cmd
 | 
						|
 | 
						|
import (
 | 
						|
	"errors"
 | 
						|
	"fmt"
 | 
						|
 | 
						|
	user_model "forgejo.org/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
 | 
						|
}
 |