github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/admin-user-list.go (about) 1 // Copyright (c) 2015-2022 MinIO, Inc. 2 // 3 // This file is part of MinIO Object Storage stack 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package cmd 19 20 import ( 21 "strings" 22 23 "github.com/fatih/color" 24 "github.com/minio/cli" 25 "github.com/minio/mc/pkg/probe" 26 "github.com/minio/pkg/v2/console" 27 ) 28 29 var adminUserListCmd = cli.Command{ 30 Name: "list", 31 ShortName: "ls", 32 Usage: "list all users", 33 Action: mainAdminUserList, 34 OnUsageError: onUsageError, 35 Before: setGlobalsFromContext, 36 Flags: globalFlags, 37 CustomHelpTemplate: `NAME: 38 {{.HelpName}} - {{.Usage}} 39 40 USAGE: 41 {{.HelpName}} TARGET 42 43 FLAGS: 44 {{range .VisibleFlags}}{{.}} 45 {{end}} 46 EXAMPLES: 47 1. List all users on MinIO server. 48 {{.Prompt}} {{.HelpName}} myminio 49 `, 50 } 51 52 // checkAdminUserListSyntax - validate all the passed arguments 53 func checkAdminUserListSyntax(ctx *cli.Context) { 54 if len(ctx.Args()) != 1 { 55 showCommandHelpAndExit(ctx, 1) // last argument is exit code 56 } 57 } 58 59 // mainAdminUserList is the handle for "mc admin user list" command. 60 func mainAdminUserList(ctx *cli.Context) error { 61 checkAdminUserListSyntax(ctx) 62 63 // Additional command speific theme customization. 64 console.SetColor("UserMessage", color.New(color.FgGreen)) 65 console.SetColor("AccessKey", color.New(color.FgBlue)) 66 console.SetColor("PolicyName", color.New(color.FgYellow)) 67 console.SetColor("UserStatus", color.New(color.FgCyan)) 68 69 // Get the alias parameter from cli 70 args := ctx.Args() 71 aliasedURL := args.Get(0) 72 73 // Create a new MinIO Admin Client 74 client, err := newAdminClient(aliasedURL) 75 fatalIf(err, "Unable to initialize admin connection.") 76 77 users, e := client.ListUsers(globalContext) 78 fatalIf(probe.NewError(e).Trace(args...), "Unable to list user") 79 80 for k, v := range users { 81 memberOf := []userGroup{} 82 for _, group := range v.MemberOf { 83 gd, e := client.GetGroupDescription(globalContext, group) 84 fatalIf(probe.NewError(e).Trace(args...), "Unable to fetch group info") 85 policies := []string{} 86 if gd.Policy != "" { 87 policies = strings.Split(gd.Policy, ",") 88 } 89 memberOf = append(memberOf, userGroup{ 90 Name: gd.Name, 91 Policies: policies, 92 }) 93 } 94 printMsg(userMessage{ 95 op: ctx.Command.Name, 96 AccessKey: k, 97 PolicyName: v.PolicyName, 98 MemberOf: memberOf, 99 UserStatus: string(v.Status), 100 }) 101 } 102 return nil 103 }