52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
package proxmoxaas_common_lib
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
)
|
|
|
|
func (g Groupname) ToString() string {
|
|
return fmt.Sprintf("%s-%s", g.GroupID, g.Realm)
|
|
}
|
|
|
|
func (u Username) ToString() string {
|
|
return fmt.Sprintf("%s-%s", u.UserID, u.Realm)
|
|
}
|
|
|
|
// returns an error if the groupname format was not correct
|
|
func ParseGroupname(groupname string) (Groupname, error) {
|
|
g := Groupname{}
|
|
// <groupid>-<realm>
|
|
m1 := regexp.MustCompilePOSIX("([[:alnum:]]+)-([[:alnum:]]+)")
|
|
// <groupid> (implied pve realm)
|
|
m2 := regexp.MustCompilePOSIX("([[:alnum:]]+)")
|
|
x1 := m1.FindStringSubmatch(groupname)
|
|
x2 := m2.FindStringSubmatch(groupname)
|
|
if len(x1) == 3 {
|
|
g.GroupID = x1[1]
|
|
g.Realm = x1[2]
|
|
return g, nil
|
|
|
|
} else if len(x2) == 2 {
|
|
g.GroupID = groupname
|
|
g.Realm = "pve"
|
|
return g, nil
|
|
} else {
|
|
return g, fmt.Errorf("groupid did not follow the format <groupid> or <groupid>-<realm>")
|
|
}
|
|
}
|
|
|
|
// returns an error if the username format was not correct
|
|
func ParseUsername(username string) (Username, error) {
|
|
u := Username{}
|
|
m := regexp.MustCompilePOSIX("([[:alnum:]]+)@([[:alnum:]]+)")
|
|
x := m.FindStringSubmatch(username)
|
|
if len(x) == 3 {
|
|
u.UserID = x[1]
|
|
u.Realm = x[2]
|
|
return u, nil
|
|
} else {
|
|
return u, fmt.Errorf("userid did not follow the format <userid>@<realm>")
|
|
}
|
|
}
|