package user

import (
	"fmt"
	"os/user"
)

const adminGroup = "admin"

func IsAdmin(u *user.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
}
