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

Better user management

parent e064bcf3
Loading
Loading
Loading
Loading
+36 −0
Original line number Diff line number Diff line
@@ -136,6 +136,42 @@ func ActionUserDisable(cmd *dclish.Command) error {
	return actionUserEnable(cmd, 1, "disable")
}

func actionUserSuspend(cmd *dclish.Command, suspended int64, doing string) error {
	if this.User.Admin == 0 {
		fmt.Println("ERROR: You are not an admin.")
		return nil
	}
	u, err := users.ValidExistingLogin(this.Q, cmd.Args[0])
	if err != nil || u.Login == "" {
		fmt.Println("ERROR: User not found.")
		return nil
	}
	if u.Suspended == suspended {
		fmt.Printf("User already %sd.\n", doing)
		return nil
	}
	ctx := storage.Context()
	err = this.Q.UpdateUserSuspended(ctx, suspended, u.Login)
	if err != nil {
		fmt.Printf("ERROR: Failed to %s user (%s).\n", doing, err)
		return nil
	}
	fmt.Printf("User %sd.\n", doing)
	return nil
}

// ActionUserSuspend handles the `USER SUSPEND` command.  This suspends
// a user to read-only access.  Only the admin can use this command.
func ActionUserSuspend(cmd *dclish.Command) error {
	return actionUserSuspend(cmd, 1, "suspend")
}

// ActionUserUnsuspend handles the `USER UNSUSPEND` command.  This
// restores full access to a suspended user.  Only the admin can use this.
func ActionUserUnsuspend(cmd *dclish.Command) error {
	return actionUserSuspend(cmd, 0, "unsuspend")
}

func actionUserAdmin(cmd *dclish.Command, admin int64, doing string) error {
	if this.User.Admin == 0 {
		fmt.Println("ERROR: You are not an admin.")
+26 −5
Original line number Diff line number Diff line
@@ -481,6 +481,7 @@ another user. You will be prompted for the recipient.
        HELP  topic`,
		MaxArgs:   2,
		Action:    ActionHelp,
		Completer: completeHelpTopics,
	},
	"INDEX": {
		Description: `Gives directory  listing of  all folders in  alphabetical order.  If the
@@ -957,7 +958,7 @@ message.`,
The following actions are available:

  ADD        ADMIN      DELETE     DISABLE    ENABLE     LIST
  MOD        NAME       NOADMIN    NOMOD
  MOD        NAME       NOADMIN    NOMOD      SUSPEND    UNSUSPEND
`,
		Action: ActionUser,
		Commands: dclish.Commands{
@@ -1023,8 +1024,8 @@ The following actions are available:
			"LIST": {
				Description: `  Lists all the users.  Must be an admin.

  Each user is shown with flags [a, m, d, p] where:
    a = admin, m = moderator, d = disabled, p = prompt
  Each user is shown with flags [a, m, d, s, p] where:
    a = admin, m = moderator, d = disabled, s = suspended, p = prompt
  A value of 1 means the flag is set, 0 means it is not.`,
				Action: ActionUserList,
			},
@@ -1059,6 +1060,26 @@ The following actions are available:
				MaxArgs: 1,
				Action:  ActionUserNomod,
			},
			"SUSPEND": {
				Description: `  Suspends a user, restricting them to read-only access.  A suspended
  user can still log in and read messages but cannot post, change, delete,
  move messages, or send mail.  Must be an admin.

  Format:
    USER SUSPEND login`,
				MinArgs: 1,
				MaxArgs: 1,
				Action:  ActionUserSuspend,
			},
			"UNSUSPEND": {
				Description: `  Restores full access to a previously suspended user.  Must be an admin.

  Format:
    USER UNSUSPEND login`,
				MinArgs: 1,
				MaxArgs: 1,
				Action:  ActionUserUnsuspend,
			},
		},
	},
	"SSH": {
+10 −0
Original line number Diff line number Diff line
@@ -191,6 +191,16 @@ func findHelp(hmap map[string]string, args []string, fullcmd string) string {
	return helptext
}

// completeHelpTopics returns the list of help topics for tab completion.
func completeHelpTopics() []string {
	topics := make([]string, 0, len(helpmap))
	for topic := range helpmap {
		topics = append(topics, topic)
	}
	sort.Strings(topics)
	return topics
}

// ActionHelp handles the `HELP` command.  This provides help taxt
// for each command.
//
+12 −0
Original line number Diff line number Diff line
@@ -82,6 +82,10 @@ func MailLoop() error {
// If called with no args, enters the mail sub-app REPL.
// If called with args (recipient names), sends mail directly.
func ActionMail(cmd *dclish.Command) error {
	if this.User.Suspended == 1 {
		fmt.Println("ERROR: Your account is suspended (read-only).")
		return nil
	}
	if len(cmd.Args) == 0 {
		fmt.Println("Entering mail sub-system. Type EXIT to return to BULLETIN.")
		return MailLoop()
@@ -143,6 +147,10 @@ func ActionMail(cmd *dclish.Command) error {
//
// Forwards the current bulletin message as mail to a user.
func ActionForward(_ *dclish.Command) error {
	if this.User.Suspended == 1 {
		fmt.Println("ERROR: Your account is suspended (read-only).")
		return nil
	}
	if this.MsgID == 0 {
		return errors.New("no current message to forward")
	}
@@ -187,6 +195,10 @@ func ActionForward(_ *dclish.Command) error {
//
// Sends a private mail reply to the author of the current bulletin message.
func ActionRespond(cmd *dclish.Command) error {
	if this.User.Suspended == 1 {
		fmt.Println("ERROR: Your account is suspended (read-only).")
		return nil
	}
	if this.MsgID == 0 {
		return errors.New("no current message to respond to")
	}
+15 −2
Original line number Diff line number Diff line
@@ -87,6 +87,10 @@ func ActionDirectory(cmd *dclish.Command) error {
//
// This originally existed as the subroutine ADD in bulletin.for.
func ActionAdd(cmd *dclish.Command) error {
	if this.User.Suspended == 1 {
		fmt.Println("ERROR: Your account is suspended (read-only).")
		return nil
	}
	ctx := storage.Context()

	optAll := 0
@@ -281,8 +285,13 @@ func ActionBack(_ *dclish.Command) error {
//
// This originally existed as... not sure.
func ActionChange(cmd *dclish.Command) error {
	if this.User.Suspended == 1 {
		fmt.Println("ERROR: Your account is suspended (read-only).")
		return nil
	}
	var err error
	isAdmin := this.User.Admin == 1
	isMod := this.User.Moderator == 1

	optAll := false
	if cmd.Flags["/ALL"].Value == "true" {
@@ -413,7 +422,7 @@ func ActionChange(cmd *dclish.Command) error {
		if msg.ID == 0 {
			return fmt.Errorf("failed to retrieve message number %d", msgids[i])
		}
		if !(isAdmin || isFolderOwner || msg.Author == this.User.Login) {
		if !(isAdmin || isMod || isFolderOwner || msg.Author == this.User.Login) {
			return errors.New("only admin or folder owner or author can change message")
		}
		if optSystem {
@@ -690,6 +699,10 @@ func ActionUnseen(cmd *dclish.Command) error {
//
// This originally existed as the subroutine DELETE_MSG in bulletin0.for.
func ActionDelete(cmd *dclish.Command) error {
	if this.User.Suspended == 1 {
		fmt.Println("ERROR: Your account is suspended (read-only).")
		return nil
	}
	var err error

	all := false
@@ -718,7 +731,7 @@ func ActionDelete(cmd *dclish.Command) error {
			return err
		}
	}
	if this.User.Admin == 0 && !folders.WroteAllMessages(this.User.Login, msgids) {
	if this.User.Admin == 0 && this.User.Moderator == 0 && !folders.WroteAllMessages(this.User.Login, msgids) {
		return errors.New("can't delete messages you haven't written")
	}
	err = folders.DeleteMessages(msgids)
Loading