Documentation
¶
Overview ¶
Package user provides the user subcommand for the medic CLI.
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var UserCmd = &cobra.Command{ Use: "user", Short: "Manage users within a mediator control plane", Long: `The medic user subcommands allows the management of users within a mediator controlplane.`, RunE: func(cmd *cobra.Command, args []string) error { return cmd.Usage() }, }
UserCmd is the root command for the user subcommands.
View Source
var User_createCmd = &cobra.Command{ Use: "create", Short: "Create a user within a mediator control plane", Long: `The medic user create subcommand lets you create new users for a role within a mediator control plane.`, PreRun: func(cmd *cobra.Command, args []string) { if err := viper.BindPFlags(cmd.Flags()); err != nil { fmt.Fprintf(os.Stderr, "Error binding flags: %s\n", err) } }, Run: func(cmd *cobra.Command, args []string) { email := util.GetConfigValue("email", "email", cmd, nil) username := util.GetConfigValue("username", "username", cmd, nil) password := util.GetConfigValue("password", "password", cmd, nil) firstName := util.GetConfigValue("firstname", "firstname", cmd, nil) lastName := util.GetConfigValue("lastname", "lastname", cmd, nil) isProtected := util.GetConfigValue("is-protected", "is-protected", cmd, false).(bool) force := util.GetConfigValue("force", "force", cmd, false).(bool) needsPasswordChange := util.GetConfigValue("needs-password-change", "needs-password-change", cmd, true).(bool) org := util.GetConfigValue("org-id", "org-id", cmd, nil).(int) // convert string of roles to array var roles []int32 roleField := util.GetConfigValue("roles", "roles", cmd, "").(string) if roleField != "" { roleStr := strings.Split(roleField, ",") for _, str := range roleStr { if str != "" { number, _ := strconv.ParseInt(str, 10, 32) roles = append(roles, int32(number)) } } } groupField := util.GetConfigValue("groups", "groups", cmd, "").(string) var groups []int32 if groupField != "" { groupStr := strings.Split(groupField, ",") for _, str := range groupStr { if str != "" { number, _ := strconv.ParseInt(str, 10, 32) groups = append(groups, int32(number)) } } } if username == nil { fmt.Fprintf(os.Stderr, "Error: username is required\n") os.Exit(1) } if (len(roles) == 0 && len(groups) > 0) || (len(roles) > 0 && len(groups) == 0) { fmt.Fprintf(os.Stderr, "Error: if you specify roles, you need to specify groups as well\n") os.Exit(1) } if org == 0 && !force { confirmed := askForConfirmation("You didn't specify any org id. Do you want to create an self enrolled user?") if !confirmed { fmt.Println("User creation cancelled") os.Exit(0) } } if (org != 0 && len(groups) == 0 && len(roles) == 0) && !force { confirmed := askForConfirmation("You didn't specify either groups or roles for the user. Are you sure to continue?") if !confirmed { fmt.Println("User creation cancelled") os.Exit(0) } } conn, err := util.GrpcForCommand(cmd) util.ExitNicelyOnError(err, "Error getting grpc connection") defer conn.Close() ctx, cancel := util.GetAppContext() defer cancel() if org == 0 { client := pb.NewOrganizationServiceClient(conn) resp, err := client.CreateOrganization(ctx, &pb.CreateOrganizationRequest{ Name: username.(string) + "-org", Company: username.(string) + " - Self enrolled", }) util.ExitNicelyOnError(err, "Error creating organization") clientg := pb.NewGroupServiceClient(conn) respg, err := clientg.CreateGroup(ctx, &pb.CreateGroupRequest{OrganizationId: resp.Id, Name: username.(string) + "-group"}) util.ExitNicelyOnError(err, "Error creating group") clientr := pb.NewRoleServiceClient(conn) isAdmin := true groupId := respg.GroupId respo, err := clientr.CreateRoleByOrganization(ctx, &pb.CreateRoleByOrganizationRequest{OrganizationId: resp.Id, Name: username.(string) + "-admin-org", IsAdmin: &isAdmin}) util.ExitNicelyOnError(err, "Error creating role") roles = append(roles, respo.Id) respr, err := clientr.CreateRoleByGroup(ctx, &pb.CreateRoleByGroupRequest{OrganizationId: resp.Id, GroupId: groupId, Name: username.(string) + "-admin-role", IsAdmin: &isAdmin}) util.ExitNicelyOnError(err, "Error creating role") roles = append(roles, respr.Id) } protectedPtr := &isProtected var emailPtr *string if email == nil || email == "" { emailPtr = nil } else { temp := email.(string) emailPtr = &temp } var passwordPtr *string if password == nil || password == "" { passwordPtr = nil } else { temp := password.(string) passwordPtr = &temp } var firstNamePtr *string if firstName == nil || firstName == "" { firstNamePtr = nil } else { temp := firstName.(string) firstNamePtr = &temp } var lastNamePtr *string if lastName == nil || lastName == "" { lastNamePtr = nil } else { temp := lastName.(string) lastNamePtr = &temp } needsPasswordChangePtr := &needsPasswordChange client := pb.NewUserServiceClient(conn) resp, err := client.CreateUser(ctx, &pb.CreateUserRequest{ OrganizationId: int32(org), Email: emailPtr, Username: username.(string), Password: passwordPtr, FirstName: firstNamePtr, LastName: lastNamePtr, IsProtected: protectedPtr, NeedsPasswordChange: needsPasswordChangePtr, RoleIds: roles, GroupIds: groups, }) util.ExitNicelyOnError(err, "Error creating user") out, err := util.GetJsonFromProto(resp) util.ExitNicelyOnError(err, "Error getting json from proto") fmt.Println(out) }, }
User_createCmd is the command for creating an user
View Source
var User_updateCmd = &cobra.Command{ Use: "update", Short: "Update a user within a mediator control plane", Long: `The medic user update subcommand allows to modify the details of an user.`, PreRun: func(cmd *cobra.Command, args []string) { if err := viper.BindPFlags(cmd.Flags()); err != nil { fmt.Fprintf(os.Stderr, "Error binding flags: %s\n", err) } }, Run: func(cmd *cobra.Command, args []string) { password := util.GetConfigValue("password", "password", cmd, "").(string) password_confirmation := util.GetConfigValue("password_confirmation", "password_confirmation", cmd, "").(string) email := util.GetConfigValue("email", "email", cmd, "").(string) first_name := util.GetConfigValue("firstname", "firstname", cmd, "").(string) last_name := util.GetConfigValue("lastname", "lastname", cmd, "").(string) if password == "" && email == "" && first_name == "" && last_name == "" { fmt.Fprint(os.Stderr, "Error: Must provide at least one of the following: password, email, first_name, last_name") os.Exit(1) } conn, err := util.GrpcForCommand(cmd) util.ExitNicelyOnError(err, "Error getting grpc connection") defer conn.Close() client := pb.NewUserServiceClient(conn) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() if (password != "" && password_confirmation == "") || (password == "" && password_confirmation != "") { fmt.Fprint(os.Stderr, "Error: Must provide both password and password_confirmation") os.Exit(1) } if password != "" && (email != "" || first_name != "" || last_name != "") { fmt.Fprint(os.Stderr, "Error: Cannot update both password and profile fields") os.Exit(1) } if password != "" && password_confirmation != "" { _, err = client.UpdatePassword(ctx, &pb.UpdatePasswordRequest{ Password: password, PasswordConfirmation: password_confirmation, }) util.ExitNicelyOnError(err, "Error updating user password") cmd.Println("Password updated successfully, please authenticate again with your new credentials.") } else { req := pb.UpdateProfileRequest{} if email != "" { req.Email = &email } if first_name != "" { req.FirstName = &first_name } if last_name != "" { req.LastName = &last_name } _, err = client.UpdateProfile(ctx, &req) if err != nil { fmt.Fprintf(os.Stderr, "Error updating user profile: %s\n", err) os.Exit(1) } cmd.Println("Profile updated successfully.") } }, }
User_updateCmd is the command for creating an user
Functions ¶
This section is empty.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.