town/town.go

42 lines
789 B
Go

package town
import (
"bytes"
"fmt"
"os/exec"
"os/user"
)
const adminGroup = "admin"
func IsAdmin(u os.User) (bool, error) {
adminGroup, err := user.LookupGroup(adminGroup)
if err != nil {
return false, fmt.Errorf("failed to get admin group info: %w", err)
}
groupIds, err := u.GroupIds()
if err != nil {
return false, fmt.Errorf("failed to get groups info: %w", err)
}
for _, groupId := range groupIds {
if groupId == adminGroup.Gid {
return true, nil
}
}
return false, nil
}
func SendLocalEmail(username, subject, body string) error {
cmd := exec.Command("/usr/sbin/sendmail", "-s", subject, username)
cmd.Stdin = bytes.NewBufferString(body)
if err := cmd.Run(); err != nil {
return fmt.Errof("failed to send local email: %w", err)
}
return nil
}