feat(misc): allows user edition, templating of the home, and more
All checks were successful
build / build (push) Successful in 2m9s

This commit is contained in:
Thomas Maurice 2024-02-14 18:50:35 +01:00
parent 09d3149932
commit d08f82ba66
Signed by: thomas
GPG key ID: 1D577F50583032A6
8 changed files with 144 additions and 15 deletions

View file

@ -1,7 +1,9 @@
package cmd
import (
"bytes"
"fmt"
"text/template"
"git.maurice.fr/thomas/mailout/pkg/database"
"git.maurice.fr/thomas/mailout/pkg/models"
@ -12,7 +14,12 @@ import (
)
var (
flagUserActive bool
flagUserActive bool
flagUserHome string
flagUserQuota int64
flagUserPassword string
flagUserGID int
flagUserUID int
)
var UserCmd = &cobra.Command{
@ -73,6 +80,24 @@ var UserAddCmd = &cobra.Command{
Active: flagUserActive,
Password: fmt.Sprintf("{BLF-CRYPT}%s", string(passwordHash)),
}
if flagUserHome == "" {
flagUserHome = cfg.Defaults.HomeTemplate
}
tmpl, err := template.New("").Parse(flagUserHome)
if err != nil {
logrus.WithError(err).Fatal("could not parse the default home template")
}
buf := bytes.NewBufferString("")
err = tmpl.Execute(buf, user)
if err != nil {
logrus.WithError(err).Fatal("could not render template")
}
user.Home = buf.String()
err = db.Save(&user).Error
if err != nil {
@ -100,10 +125,10 @@ var UserListCmd = &cobra.Command{
}
tData := pterm.TableData{
{"id", "username", "domain", "active", "uid", "gid", "created_at", "updated_at"},
{"id", "username", "domain", "active", "uid", "gid", "home"},
}
for _, u := range qRes {
tData = append(tData, []string{u.ID.String(), u.Username, u.Domain, fmt.Sprintf("%v", u.Active), fmt.Sprintf("%d", u.UID), fmt.Sprintf("%d", u.GID), u.CreatedAt.String(), u.UpdatedAt.String()})
tData = append(tData, []string{u.ID.String(), u.Username, u.Domain, fmt.Sprintf("%v", u.Active), fmt.Sprintf("%d", u.UID), fmt.Sprintf("%d", u.GID), u.Home})
}
pterm.DefaultTable.WithHasHeader().WithData(tData).Render()
@ -182,12 +207,90 @@ var UserDeactivateCmd = &cobra.Command{
},
}
var UserEditCmd = &cobra.Command{
Use: "edit",
Short: "edites a user",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
db, err := database.NewDB(cfg)
if err != nil {
logrus.WithError(err).Fatal("could not connect to the database")
}
userQuery, err := buildUserQuery(args[0])
if err != nil {
logrus.WithError(err).Fatal("unable to determine user")
}
qRes := make([]models.User, 0)
err = db.Model(&models.User{}).Find(&qRes, userQuery).Error
if err != nil {
logrus.WithError(err).Fatal("could not preload user")
}
if len(qRes) == 0 {
logrus.Fatal("no such user")
}
user := qRes[0]
if flagUserHome != "" {
tmpl, err := template.New("").Parse(flagUserHome)
if err != nil {
logrus.WithError(err).Fatal("could not parse the default home template")
}
buf := bytes.NewBufferString("")
err = tmpl.Execute(buf, user)
if err != nil {
logrus.WithError(err).Fatal("could not render home template")
}
user.Home = buf.String()
}
if flagUserPassword != "" {
passwordHash, err := bcrypt.GenerateFromPassword([]byte(args[1]), bcrypt.DefaultCost)
if err != nil {
logrus.WithError(err).Fatal("could not compute user password hash")
}
user.Password = fmt.Sprintf("{BLF-CRYPT}%s", string(passwordHash))
}
if flagUserGID != -1 {
user.GID = flagUserGID
}
if flagUserUID != -1 {
user.UID = flagUserUID
}
err = db.Save(&user).Error
if err != nil {
logrus.WithError(err).Fatal("could not update user")
}
logrus.Infof("edited user %s", args[0])
},
}
func InitUserCmd() {
UserCmd.AddCommand(UserAddCmd)
UserCmd.AddCommand(UserEditCmd)
UserCmd.AddCommand(UserListCmd)
UserCmd.AddCommand(UserDeleteCmd)
UserCmd.AddCommand(UserActivateCmd)
UserCmd.AddCommand(UserDeactivateCmd)
UserAddCmd.PersistentFlags().BoolVarP(&flagUserActive, "active", "a", true, "whether or not the created user is active")
UserAddCmd.PersistentFlags().StringVarP(&flagUserHome, "home", "", "", "template to use for user's home directories")
UserEditCmd.PersistentFlags().StringVarP(&flagUserPassword, "password", "p", "", "User password")
UserEditCmd.PersistentFlags().StringVarP(&flagUserHome, "home", "", "", "home (user's mailbox)")
UserEditCmd.PersistentFlags().Int64VarP(&flagUserQuota, "quota", "q", -1, "Quota in bytes for the user")
UserEditCmd.PersistentFlags().IntVarP(&flagUserUID, "uid", "u", -1, "user's uid")
UserEditCmd.PersistentFlags().IntVarP(&flagUserGID, "gid", "g", -1, "user's gid")
}