Files
EdgeAPI/internal/rpc/services/service_authority_key_plus.go

89 lines
2.5 KiB
Go
Raw Normal View History

2021-04-13 20:01:21 +08:00
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
2021-07-25 15:46:12 +08:00
// +build plus
2021-04-13 20:01:21 +08:00
package services
import (
"context"
"encoding/json"
2021-04-13 21:23:26 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/db/models/authority"
2021-04-13 20:01:21 +08:00
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
2021-07-22 18:42:57 +08:00
plusutils "github.com/TeaOSLab/EdgePlus/pkg/utils"
2021-04-13 20:01:21 +08:00
)
// AuthorityKeyService 版本认证
type AuthorityKeyService struct {
BaseService
}
// UpdateAuthorityKey 设置Key
func (this *AuthorityKeyService) UpdateAuthorityKey(ctx context.Context, req *pb.UpdateAuthorityKeyRequest) (*pb.RPCSuccess, error) {
2021-07-11 18:05:57 +08:00
_, _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAuthority)
2021-04-13 20:01:21 +08:00
if err != nil {
return nil, err
}
var tx = this.NullTx()
2021-04-14 20:02:21 +08:00
err = authority.SharedAuthorityKeyDAO.UpdateKey(tx, req.Value, req.DayFrom, req.DayTo, req.Hostname, req.MacAddresses, req.Company)
2021-04-13 20:01:21 +08:00
if err != nil {
return nil, err
}
return this.Success()
}
// ReadAuthorityKey 读取Key
func (this *AuthorityKeyService) ReadAuthorityKey(ctx context.Context, req *pb.ReadAuthorityKeyRequest) (*pb.ReadAuthorityKeyResponse, error) {
2021-07-11 18:05:57 +08:00
_, _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin, rpcutils.UserTypeMonitor, rpcutils.UserTypeProvider, rpcutils.UserTypeDNS)
2021-04-13 20:01:21 +08:00
if err != nil {
return nil, err
}
var tx = this.NullTx()
2021-04-13 21:23:26 +08:00
key, err := authority.SharedAuthorityKeyDAO.ReadKey(tx)
2021-04-13 20:01:21 +08:00
if err != nil {
return nil, err
}
if key == nil {
return &pb.ReadAuthorityKeyResponse{AuthorityKey: nil}, nil
}
2021-07-22 18:42:57 +08:00
if len(key.Value) == 0 {
return &pb.ReadAuthorityKeyResponse{AuthorityKey: nil}, nil
}
m, err := plusutils.Decode([]byte(key.Value))
if err != nil {
return nil, err
}
2021-04-13 20:01:21 +08:00
macAddresses := []string{}
if len(key.MacAddresses) > 0 {
err = json.Unmarshal([]byte(key.MacAddresses), &macAddresses)
if err != nil {
return nil, err
}
}
return &pb.ReadAuthorityKeyResponse{AuthorityKey: &pb.AuthorityKey{
Value: key.Value,
2021-07-22 18:42:57 +08:00
DayFrom: m.GetString("dayFrom"),
DayTo: m.GetString("dayTo"),
2021-04-13 20:01:21 +08:00
Hostname: key.Hostname,
MacAddresses: macAddresses,
2021-04-14 20:02:21 +08:00
Company: key.Company,
2021-04-13 20:01:21 +08:00
UpdatedAt: int64(key.UpdatedAt),
}}, nil
}
// ResetAuthorityKey 重置Key
func (this *AuthorityKeyService) ResetAuthorityKey(ctx context.Context, req *pb.ResetAuthorityKeyRequest) (*pb.RPCSuccess, error) {
_, err := this.ValidateAdmin(ctx, 0)
if err != nil {
return nil, err
}
2021-04-13 21:23:26 +08:00
err = authority.SharedAuthorityKeyDAO.ResetKey(nil)
2021-04-13 20:01:21 +08:00
if err != nil {
return nil, err
}
return this.Success()
}