Commit a2eb2304 authored by Kevin Lyda's avatar Kevin Lyda
Browse files

Allow show user to work for non-admins

parent 25b96def
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -86,6 +86,10 @@ func ActionUserDelete(cmd *dclish.Command) error {
		fmt.Println("ERROR: User not found.")
		return nil
	}
	if u.Login == "SYSTEM" {
		fmt.Println("ERROR: SYSTEM user can't be deleted.")
		return nil
	}
	ctx := storage.Context()
	err = this.Q.DeleteUser(ctx, u.Login)
	if err != nil {
+7 −2
Original line number Diff line number Diff line
@@ -1607,8 +1607,13 @@ have done this.`,
				Action:  ActionShowUser,
				Flags: dclish.Flags{
					"/ALL": {
						Description: `  Specifies that information for all users is to be displayed. This is a
  privileged command.`,
						Description: `  Specifies that information for all users is to be displayed.  Without
  /FULL, only enabled users and last activity are shown.`,
					},
					"/FULL": {
						Description: `  Shows full user details including name, admin/moderator/disabled/
  suspended flags, last activity, and last login.  This is a privileged
  command.`,
					},
					"/LOGIN": {
						Description: `/[NO]LOGIN
+48 −4
Original line number Diff line number Diff line
@@ -109,17 +109,62 @@ func ActionShowPrivileges(_ *dclish.Command) error {

// ActionShowUser handles the `SHOW USER` command.
func ActionShowUser(cmd *dclish.Command) error {
	ctx := storage.Context()
	isAdmin := this.User.Admin == 1

	// /FULL: admin-only, shows all users with complete details.
	if cmd.Flags["/FULL"].Set {
		if !isAdmin {
			fmt.Println("ERROR: You are not an admin.")
			return nil
		}
		rows, err := this.Q.ListUsers(ctx)
		if err != nil {
			fmt.Printf("ERROR: Failed to get list (%s).\n", err)
			return nil
		}
		fmt.Printf("%-12s  %-25s  A M D S  %-19s  %s\n",
			"User", "Name", "Last Activity", "Last Login")
		for _, r := range rows {
			fmt.Printf("%-12s  %-25s  %s %s %s %s  %s  %s\n",
				r.Login, r.Name,
				ask.YesNo(r.Admin)[:1], ask.YesNo(r.Moderator)[:1],
				ask.YesNo(r.Disabled)[:1], ask.YesNo(r.Suspended)[:1],
				r.LastActivity.Format("2006-01-02 15:04:05"),
				r.LastLogin.Format("2006-01-02 15:04:05"))
		}
		return nil
	}

	// /ALL without privileged flags: available to all users, shows only
	// enabled users and their last activity.
	if cmd.Flags["/ALL"].Set && !cmd.Flags["/LOGIN"].Set && !cmd.Flags["/FOLDER"].Set && !cmd.Flags["/SINCE"].Set {
		rows, err := this.Q.ListUsers(ctx)
		if err != nil {
			fmt.Printf("ERROR: Failed to get list (%s).\n", err)
			return nil
		}
		fmt.Printf("%-12s  %s\n", "User", "Last Activity")
		for _, r := range rows {
			if r.Disabled == 0 {
				fmt.Printf("%-12s  %s\n", r.Login,
					r.LastActivity.Format("2006-01-02 15:04:05"))
			}
		}
		return nil
	}

	// Parse the options.
	login := this.User.Login
	if len(cmd.Args) == 1 {
		if this.User.Admin == 0 {
		if !isAdmin {
			fmt.Println("ERROR: You are not an admin.")
			return nil
		}
		login = strings.ToUpper(cmd.Args[0])
	}
	if cmd.Flags["/ALL"].Set {
		if this.User.Admin == 0 {
		if !isAdmin {
			fmt.Println("ERROR: You are not an admin.")
			return nil
		}
@@ -130,7 +175,7 @@ func ActionShowUser(cmd *dclish.Command) error {
	}
	var showDisabled int64
	if cmd.Flags["/LOGIN"].Value == "true" {
		if this.User.Admin == 0 {
		if !isAdmin {
			fmt.Println("ERROR: You are not an admin.")
			return nil
		}
@@ -169,7 +214,6 @@ func ActionShowUser(cmd *dclish.Command) error {
	}

	// Actually do the thing.
	ctx := storage.Context()
	if cmd.Flags["/LOGIN"].Set && cmd.Flags["/FOLDER"].Set {
		rows, err := this.Q.GetLastReadByEnabled(ctx, folder.Name, showDisabled)
		if err != nil {