From 40a040352174a6adc4a0cb04e87ae6e092d1e328 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 2 Nov 2024 12:48:32 +0800 Subject: [PATCH] :sparkles: Authkit support check user related permission --- pkg/authkit/auth.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 pkg/authkit/auth.go diff --git a/pkg/authkit/auth.go b/pkg/authkit/auth.go new file mode 100644 index 0000000..45ad239 --- /dev/null +++ b/pkg/authkit/auth.go @@ -0,0 +1,26 @@ +package authkit + +import ( + "context" + "fmt" + "git.solsynth.dev/hypernet/nexus/pkg/nex" + "git.solsynth.dev/hypernet/nexus/pkg/proto" + "github.com/samber/lo" +) + +func EnsureUserPermGranted(nx *nex.Conn, userId, otherId uint, key string, val any) error { + conn, err := nx.GetClientGrpcConn(nex.ServiceTypeAuth) + if err != nil { + return fmt.Errorf("failed to get auth service client: %v", err) + } + resp, err := proto.NewAuthServiceClient(conn).EnsureUserPermGranted(context.Background(), &proto.CheckUserPermRequest{ + UserId: uint64(userId), + OtherId: uint64(otherId), + Key: key, + Value: nex.EncodeMap(val), + }) + if err != nil { + return err + } + return lo.Ternary(resp.GetIsValid(), nil, fmt.Errorf("missing permission: %v", key)) +}