✨ Authkit can get user
This commit is contained in:
64
pkg/authkit/user.go
Normal file
64
pkg/authkit/user.go
Normal file
@ -0,0 +1,64 @@
|
||||
package authkit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/proto"
|
||||
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func GetUser(nx *nex.Conn, userId uint) (models.Account, error) {
|
||||
conn, err := nx.GetClientGrpcConn(nex.ServiceTypeAuth)
|
||||
if err != nil {
|
||||
return models.Account{}, err
|
||||
}
|
||||
raw, _ := proto.NewUserServiceClient(conn).GetUser(context.Background(), &proto.GetUserRequest{
|
||||
UserId: lo.ToPtr(uint64(userId)),
|
||||
})
|
||||
return GetAccountFromUserInfo(&sec.UserInfo{
|
||||
ID: uint(raw.GetId()),
|
||||
Name: raw.GetName(),
|
||||
PermNodes: nex.DecodeMap(raw.GetPermNodes()),
|
||||
Metadata: nex.DecodeMap(raw.GetMetadata()),
|
||||
}), nil
|
||||
}
|
||||
|
||||
func GetUserByName(nx *nex.Conn, name string) (models.Account, error) {
|
||||
conn, err := nx.GetClientGrpcConn(nex.ServiceTypeAuth)
|
||||
if err != nil {
|
||||
return models.Account{}, err
|
||||
}
|
||||
raw, _ := proto.NewUserServiceClient(conn).GetUser(context.Background(), &proto.GetUserRequest{
|
||||
Name: &name,
|
||||
})
|
||||
return GetAccountFromUserInfo(&sec.UserInfo{
|
||||
ID: uint(raw.GetId()),
|
||||
Name: raw.GetName(),
|
||||
PermNodes: nex.DecodeMap(raw.GetPermNodes()),
|
||||
Metadata: nex.DecodeMap(raw.GetMetadata()),
|
||||
}), nil
|
||||
}
|
||||
|
||||
func ListUser(nx *nex.Conn, userId []uint) ([]models.Account, error) {
|
||||
conn, err := nx.GetClientGrpcConn(nex.ServiceTypeAuth)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
raw, _ := proto.NewUserServiceClient(conn).ListUser(context.Background(), &proto.ListUserRequest{
|
||||
UserId: lo.Map(userId, func(item uint, index int) uint64 {
|
||||
return uint64(item)
|
||||
}),
|
||||
})
|
||||
var out []models.Account
|
||||
for _, item := range raw.GetData() {
|
||||
out = append(out, GetAccountFromUserInfo(&sec.UserInfo{
|
||||
ID: uint(item.GetId()),
|
||||
Name: item.GetName(),
|
||||
PermNodes: nex.DecodeMap(item.GetPermNodes()),
|
||||
Metadata: nex.DecodeMap(item.GetMetadata()),
|
||||
}))
|
||||
}
|
||||
return out, nil
|
||||
}
|
@ -77,6 +77,12 @@ func (v *App) NotifyUserBatch(_ context.Context, in *proto.NotifyUserBatchReques
|
||||
notifications = append(notifications, notification)
|
||||
}
|
||||
|
||||
if len(notifications) == 0 {
|
||||
return &proto.NotifyResponse{
|
||||
IsSuccess: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
log.Debug().Str("topic", notifications[0].Topic).Any("uid", lo.Keys(checklist)).Msg("Notifying users...")
|
||||
|
||||
if in.GetNotify().GetUnsaved() {
|
||||
|
@ -12,8 +12,16 @@ import (
|
||||
)
|
||||
|
||||
func (v *App) GetUser(ctx context.Context, request *proto.GetUserRequest) (*proto.UserInfo, error) {
|
||||
tx := database.C
|
||||
if request.UserId != nil {
|
||||
tx = tx.Where("id = ?", uint(request.GetUserId()))
|
||||
}
|
||||
if request.Name != nil {
|
||||
tx = tx.Where("name = ?", request.GetName())
|
||||
}
|
||||
|
||||
var account models.Account
|
||||
if err := database.C.Where("id = ?", uint(request.GetUserId())).First(&account).Error; err != nil {
|
||||
if err := tx.First(&account).Error; err != nil {
|
||||
return nil, status.Errorf(codes.NotFound, fmt.Sprintf("requested user with id %d was not found", request.GetUserId()))
|
||||
}
|
||||
return account.EncodeToUserInfo(), nil
|
||||
|
Reference in New Issue
Block a user