2024-10-31 14:08:51 +00:00
|
|
|
package authkit
|
|
|
|
|
|
|
|
import (
|
2024-11-02 04:48:17 +00:00
|
|
|
"context"
|
2024-10-31 14:08:51 +00:00
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/nex"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/proto"
|
|
|
|
"github.com/samber/lo"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetRealm(nx *nex.Conn, id uint) (models.Realm, error) {
|
|
|
|
var realm models.Realm
|
|
|
|
conn, err := nx.GetClientGrpcConn(nex.ServiceTypeAuth)
|
|
|
|
if err != nil {
|
|
|
|
return realm, err
|
|
|
|
}
|
2024-11-02 04:48:17 +00:00
|
|
|
resp, err := proto.NewRealmServiceClient(conn).GetRealm(context.Background(), &proto.LookupRealmRequest{
|
2024-10-31 14:08:51 +00:00
|
|
|
Id: lo.ToPtr(uint64(id)),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return realm, err
|
|
|
|
}
|
|
|
|
return models.NewRealmFromProto(resp), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetRealmByAlias(nx *nex.Conn, alias string) (models.Realm, error) {
|
|
|
|
var realm models.Realm
|
|
|
|
conn, err := nx.GetClientGrpcConn(nex.ServiceTypeAuth)
|
|
|
|
if err != nil {
|
|
|
|
return realm, err
|
|
|
|
}
|
2024-11-02 04:48:17 +00:00
|
|
|
resp, err := proto.NewRealmServiceClient(conn).GetRealm(context.Background(), &proto.LookupRealmRequest{
|
2024-10-31 14:08:51 +00:00
|
|
|
Alias: &alias,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return realm, err
|
|
|
|
}
|
|
|
|
return models.NewRealmFromProto(resp), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ListRealm(nx *nex.Conn, id []uint) ([]models.Realm, error) {
|
|
|
|
var realms []models.Realm
|
|
|
|
conn, err := nx.GetClientGrpcConn(nex.ServiceTypeAuth)
|
|
|
|
if err != nil {
|
|
|
|
return realms, err
|
|
|
|
}
|
2024-11-02 04:48:17 +00:00
|
|
|
resp, err := proto.NewRealmServiceClient(conn).ListRealm(context.Background(), &proto.ListRealmRequest{
|
2024-10-31 14:08:51 +00:00
|
|
|
Id: lo.Map(id, func(item uint, _ int) uint64 {
|
|
|
|
return uint64(item)
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return realms, err
|
|
|
|
}
|
|
|
|
for _, realm := range resp.GetData() {
|
|
|
|
realms = append(realms, models.NewRealmFromProto(realm))
|
|
|
|
}
|
|
|
|
return realms, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetRealmMember(nx *nex.Conn, realmID, userID uint) (models.RealmMember, error) {
|
|
|
|
var member models.RealmMember
|
|
|
|
conn, err := nx.GetClientGrpcConn(nex.ServiceTypeAuth)
|
|
|
|
if err != nil {
|
|
|
|
return member, err
|
|
|
|
}
|
2024-11-02 04:48:17 +00:00
|
|
|
resp, err := proto.NewRealmServiceClient(conn).GetRealmMember(context.Background(), &proto.RealmMemberLookupRequest{
|
2024-10-31 14:08:51 +00:00
|
|
|
RealmId: lo.ToPtr(uint64(realmID)),
|
|
|
|
UserId: lo.ToPtr(uint64(userID)),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return member, err
|
|
|
|
}
|
|
|
|
return models.NewRealmMemberFromProto(resp), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ListRealmMember(nx *nex.Conn, realmID uint) ([]models.RealmMember, error) {
|
|
|
|
var members []models.RealmMember
|
|
|
|
conn, err := nx.GetClientGrpcConn(nex.ServiceTypeAuth)
|
|
|
|
if err != nil {
|
|
|
|
return members, err
|
|
|
|
}
|
2024-11-02 04:48:17 +00:00
|
|
|
resp, err := proto.NewRealmServiceClient(conn).ListRealmMember(context.Background(), &proto.RealmMemberLookupRequest{
|
2024-10-31 14:08:51 +00:00
|
|
|
RealmId: lo.ToPtr(uint64(realmID)),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return members, err
|
|
|
|
}
|
|
|
|
for _, member := range resp.GetData() {
|
|
|
|
members = append(members, models.NewRealmMemberFromProto(member))
|
|
|
|
}
|
|
|
|
return members, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CheckRealmMemberPerm(nx *nex.Conn, realmID uint, userID, power int) bool {
|
|
|
|
conn, err := nx.GetClientGrpcConn(nex.ServiceTypeAuth)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2024-11-02 04:48:17 +00:00
|
|
|
resp, err := proto.NewRealmServiceClient(conn).CheckRealmMemberPerm(context.Background(), &proto.CheckRealmPermRequest{
|
2024-10-31 14:08:51 +00:00
|
|
|
RealmId: uint64(realmID),
|
|
|
|
UserId: uint64(userID),
|
|
|
|
PowerLevel: int32(power),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return resp.GetIsSuccess()
|
|
|
|
}
|