2020-07-29 19:02:28 +08:00
|
|
|
package services
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
|
|
|
|
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
2020-09-13 20:37:28 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
2020-07-29 19:02:28 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type NodeGrantService struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *NodeGrantService) CreateNodeGrant(ctx context.Context, req *pb.CreateNodeGrantRequest) (*pb.CreateNodeGrantResponse, error) {
|
|
|
|
|
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
grantId, err := models.SharedNodeGrantDAO.CreateGrant(req.Name, req.Method, req.Username, req.Password, req.PrivateKey, req.Description, req.NodeId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &pb.CreateNodeGrantResponse{
|
|
|
|
|
GrantId: grantId,
|
|
|
|
|
}, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 18:22:22 +08:00
|
|
|
func (this *NodeGrantService) UpdateNodeGrant(ctx context.Context, req *pb.UpdateNodeGrantRequest) (*pb.RPCSuccess, error) {
|
2020-07-29 19:02:28 +08:00
|
|
|
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if req.GrantId <= 0 {
|
|
|
|
|
return nil, errors.New("wrong grantId")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = models.SharedNodeGrantDAO.UpdateGrant(req.GrantId, req.Name, req.Method, req.Username, req.Password, req.PrivateKey, req.Description, req.NodeId)
|
2020-11-13 18:22:22 +08:00
|
|
|
return &pb.RPCSuccess{}, err
|
2020-07-29 19:02:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *NodeGrantService) DisableNodeGrant(ctx context.Context, req *pb.DisableNodeGrantRequest) (*pb.DisableNodeGrantResponse, error) {
|
|
|
|
|
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = models.SharedNodeGrantDAO.DisableNodeGrant(req.GrantId)
|
|
|
|
|
return &pb.DisableNodeGrantResponse{}, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-12 14:41:28 +08:00
|
|
|
func (this *NodeGrantService) CountAllEnabledNodeGrants(ctx context.Context, req *pb.CountAllEnabledNodeGrantsRequest) (*pb.RPCCountResponse, error) {
|
2020-07-29 19:02:28 +08:00
|
|
|
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
count, err := models.SharedNodeGrantDAO.CountAllEnabledGrants()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-11-12 14:41:28 +08:00
|
|
|
return &pb.RPCCountResponse{Count: count}, nil
|
2020-07-29 19:02:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *NodeGrantService) ListEnabledNodeGrants(ctx context.Context, req *pb.ListEnabledNodeGrantsRequest) (*pb.ListEnabledNodeGrantsResponse, error) {
|
|
|
|
|
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
grants, err := models.SharedNodeGrantDAO.ListEnabledGrants(req.Offset, req.Size)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
result := []*pb.NodeGrant{}
|
|
|
|
|
for _, grant := range grants {
|
|
|
|
|
result = append(result, &pb.NodeGrant{
|
|
|
|
|
Id: int64(grant.Id),
|
|
|
|
|
Name: grant.Name,
|
|
|
|
|
Method: grant.Method,
|
|
|
|
|
Password: grant.Password,
|
|
|
|
|
Su: grant.Su == 1,
|
|
|
|
|
PrivateKey: grant.PrivateKey,
|
|
|
|
|
Description: grant.Description,
|
|
|
|
|
NodeId: int64(grant.NodeId),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &pb.ListEnabledNodeGrantsResponse{Grants: result}, nil
|
|
|
|
|
}
|
2020-07-30 22:41:49 +08:00
|
|
|
|
|
|
|
|
// 列出所有认证信息
|
|
|
|
|
func (this *NodeGrantService) FindAllEnabledNodeGrants(ctx context.Context, req *pb.FindAllEnabledNodeGrantsRequest) (*pb.FindAllEnabledNodeGrantsResponse, error) {
|
|
|
|
|
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
grants, err := models.SharedNodeGrantDAO.FindAllEnabledGrants()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
result := []*pb.NodeGrant{}
|
|
|
|
|
for _, grant := range grants {
|
|
|
|
|
result = append(result, &pb.NodeGrant{
|
|
|
|
|
Id: int64(grant.Id),
|
|
|
|
|
Name: grant.Name,
|
|
|
|
|
Method: grant.Method,
|
|
|
|
|
Password: grant.Password,
|
|
|
|
|
Su: grant.Su == 1,
|
|
|
|
|
PrivateKey: grant.PrivateKey,
|
|
|
|
|
Description: grant.Description,
|
|
|
|
|
NodeId: int64(grant.NodeId),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &pb.FindAllEnabledNodeGrantsResponse{Grants: result}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *NodeGrantService) FindEnabledGrant(ctx context.Context, req *pb.FindEnabledGrantRequest) (*pb.FindEnabledGrantResponse, error) {
|
|
|
|
|
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
grant, err := models.SharedNodeGrantDAO.FindEnabledNodeGrant(req.GrantId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if grant == nil {
|
|
|
|
|
return &pb.FindEnabledGrantResponse{}, nil
|
|
|
|
|
}
|
|
|
|
|
return &pb.FindEnabledGrantResponse{Grant: &pb.NodeGrant{
|
|
|
|
|
Id: int64(grant.Id),
|
|
|
|
|
Name: grant.Name,
|
|
|
|
|
Method: grant.Method,
|
|
|
|
|
Username: grant.Username,
|
|
|
|
|
Password: grant.Password,
|
|
|
|
|
Su: grant.Su == 1,
|
|
|
|
|
PrivateKey: grant.PrivateKey,
|
|
|
|
|
Description: grant.Description,
|
|
|
|
|
NodeId: int64(grant.NodeId),
|
|
|
|
|
}}, nil
|
|
|
|
|
}
|