Skip to content

Commit 14cb424

Browse files
committed
增加认证节点管理
1 parent 7db07d6 commit 14cb424

15 files changed

Lines changed: 524 additions & 72 deletions

internal/db/models/authority_key_dao.go renamed to internal/db/models/authority/authority_key_dao.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package models
1+
package authority
22

33
import (
44
"encoding/json"

internal/db/models/authority_key_dao_test.go renamed to internal/db/models/authority/authority_key_dao_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package models
1+
package authority
22

33
import (
44
_ "github.com/go-sql-driver/mysql"

internal/db/models/authority_key_model.go renamed to internal/db/models/authority/authority_key_model.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package models
1+
package authority
22

33
// AuthorityKey 企业版认证信息
44
type AuthorityKey struct {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package authority
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
package authority
2+
3+
import (
4+
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
5+
"github.com/TeaOSLab/EdgeAPI/internal/errors"
6+
_ "github.com/go-sql-driver/mysql"
7+
"github.com/iwind/TeaGo/Tea"
8+
"github.com/iwind/TeaGo/dbs"
9+
"github.com/iwind/TeaGo/rands"
10+
"github.com/iwind/TeaGo/types"
11+
)
12+
13+
const (
14+
AuthorityNodeStateEnabled = 1 // 已启用
15+
AuthorityNodeStateDisabled = 0 // 已禁用
16+
)
17+
18+
type AuthorityNodeDAO dbs.DAO
19+
20+
func NewAuthorityNodeDAO() *AuthorityNodeDAO {
21+
return dbs.NewDAO(&AuthorityNodeDAO{
22+
DAOObject: dbs.DAOObject{
23+
DB: Tea.Env,
24+
Table: "edgeAuthorityNodes",
25+
Model: new(AuthorityNode),
26+
PkName: "id",
27+
},
28+
}).(*AuthorityNodeDAO)
29+
}
30+
31+
var SharedAuthorityNodeDAO *AuthorityNodeDAO
32+
33+
func init() {
34+
dbs.OnReady(func() {
35+
SharedAuthorityNodeDAO = NewAuthorityNodeDAO()
36+
})
37+
}
38+
39+
// EnableAuthorityNode 启用条目
40+
func (this *AuthorityNodeDAO) EnableAuthorityNode(tx *dbs.Tx, id int64) error {
41+
_, err := this.Query(tx).
42+
Pk(id).
43+
Set("state", AuthorityNodeStateEnabled).
44+
Update()
45+
return err
46+
}
47+
48+
// DisableAuthorityNode 禁用条目
49+
func (this *AuthorityNodeDAO) DisableAuthorityNode(tx *dbs.Tx, id int64) error {
50+
_, err := this.Query(tx).
51+
Pk(id).
52+
Set("state", AuthorityNodeStateDisabled).
53+
Update()
54+
return err
55+
}
56+
57+
// FindEnabledAuthorityNode 查找启用中的条目
58+
func (this *AuthorityNodeDAO) FindEnabledAuthorityNode(tx *dbs.Tx, id int64) (*AuthorityNode, error) {
59+
result, err := this.Query(tx).
60+
Pk(id).
61+
Attr("state", AuthorityNodeStateEnabled).
62+
Find()
63+
if result == nil {
64+
return nil, err
65+
}
66+
return result.(*AuthorityNode), err
67+
}
68+
69+
// FindAuthorityNodeName 根据主键查找名称
70+
func (this *AuthorityNodeDAO) FindAuthorityNodeName(tx *dbs.Tx, id int64) (string, error) {
71+
return this.Query(tx).
72+
Pk(id).
73+
Result("name").
74+
FindStringCol("")
75+
}
76+
77+
// FindAllEnabledAuthorityNodes 列出所有可用认证节点
78+
func (this *AuthorityNodeDAO) FindAllEnabledAuthorityNodes(tx *dbs.Tx) (result []*AuthorityNode, err error) {
79+
_, err = this.Query(tx).
80+
State(AuthorityNodeStateEnabled).
81+
Desc("order").
82+
AscPk().
83+
Slice(&result).
84+
FindAll()
85+
return
86+
}
87+
88+
// CountAllEnabledAuthorityNodes 计算认证节点数量
89+
func (this *AuthorityNodeDAO) CountAllEnabledAuthorityNodes(tx *dbs.Tx) (int64, error) {
90+
return this.Query(tx).
91+
State(AuthorityNodeStateEnabled).
92+
Count()
93+
}
94+
95+
// ListEnabledAuthorityNodes 列出单页的认证节点
96+
func (this *AuthorityNodeDAO) ListEnabledAuthorityNodes(tx *dbs.Tx, offset int64, size int64) (result []*AuthorityNode, err error) {
97+
_, err = this.Query(tx).
98+
State(AuthorityNodeStateEnabled).
99+
Offset(offset).
100+
Limit(size).
101+
Desc("order").
102+
DescPk().
103+
Slice(&result).
104+
FindAll()
105+
return
106+
}
107+
108+
// CreateAuthorityNode 创建认证节点
109+
func (this *AuthorityNodeDAO) CreateAuthorityNode(tx *dbs.Tx, name string, description string, isOn bool) (nodeId int64, err error) {
110+
uniqueId, err := this.GenUniqueId(tx)
111+
if err != nil {
112+
return 0, err
113+
}
114+
secret := rands.String(32)
115+
err = models.NewApiTokenDAO().CreateAPIToken(tx, uniqueId, secret, models.NodeRoleAuthority)
116+
if err != nil {
117+
return
118+
}
119+
120+
op := NewAuthorityNodeOperator()
121+
op.IsOn = isOn
122+
op.UniqueId = uniqueId
123+
op.Secret = secret
124+
op.Name = name
125+
op.Description = description
126+
op.State = AuthorityNodeStateEnabled
127+
err = this.Save(tx, op)
128+
if err != nil {
129+
return
130+
}
131+
132+
return types.Int64(op.Id), nil
133+
}
134+
135+
// UpdateAuthorityNode 修改认证节点
136+
func (this *AuthorityNodeDAO) UpdateAuthorityNode(tx *dbs.Tx, nodeId int64, name string, description string, isOn bool) error {
137+
if nodeId <= 0 {
138+
return errors.New("invalid nodeId")
139+
}
140+
141+
op := NewAuthorityNodeOperator()
142+
op.Id = nodeId
143+
op.Name = name
144+
op.Description = description
145+
op.IsOn = isOn
146+
err := this.Save(tx, op)
147+
return err
148+
}
149+
150+
// FindEnabledAuthorityNodeWithUniqueId 根据唯一ID获取节点信息
151+
func (this *AuthorityNodeDAO) FindEnabledAuthorityNodeWithUniqueId(tx *dbs.Tx, uniqueId string) (*AuthorityNode, error) {
152+
result, err := this.Query(tx).
153+
Attr("uniqueId", uniqueId).
154+
Attr("state", AuthorityNodeStateEnabled).
155+
Find()
156+
if result == nil {
157+
return nil, err
158+
}
159+
return result.(*AuthorityNode), err
160+
}
161+
162+
// FindEnabledAuthorityNodeIdWithUniqueId 根据唯一ID获取节点ID
163+
func (this *AuthorityNodeDAO) FindEnabledAuthorityNodeIdWithUniqueId(tx *dbs.Tx, uniqueId string) (int64, error) {
164+
return this.Query(tx).
165+
Attr("uniqueId", uniqueId).
166+
Attr("state", AuthorityNodeStateEnabled).
167+
ResultPk().
168+
FindInt64Col(0)
169+
}
170+
171+
// GenUniqueId 生成唯一ID
172+
func (this *AuthorityNodeDAO) GenUniqueId(tx *dbs.Tx) (string, error) {
173+
for {
174+
uniqueId := rands.HexString(32)
175+
ok, err := this.Query(tx).
176+
Attr("uniqueId", uniqueId).
177+
Exist()
178+
if err != nil {
179+
return "", err
180+
}
181+
if ok {
182+
continue
183+
}
184+
return uniqueId, nil
185+
}
186+
}
187+
188+
// UpdateNodeStatus 更改节点状态
189+
func (this *AuthorityNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, statusJSON []byte) error {
190+
if statusJSON == nil {
191+
return nil
192+
}
193+
_, err := this.Query(tx).
194+
Pk(nodeId).
195+
Set("status", string(statusJSON)).
196+
Update()
197+
return err
198+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package authority
2+
3+
import (
4+
_ "github.com/go-sql-driver/mysql"
5+
_ "github.com/iwind/TeaGo/bootstrap"
6+
)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package authority
2+
3+
// AuthorityNode 监控节点
4+
type AuthorityNode struct {
5+
Id uint32 `field:"id"` // ID
6+
IsOn uint8 `field:"isOn"` // 是否启用
7+
UniqueId string `field:"uniqueId"` // 唯一ID
8+
Secret string `field:"secret"` // 密钥
9+
Name string `field:"name"` // 名称
10+
Description string `field:"description"` // 描述
11+
Order uint32 `field:"order"` // 排序
12+
State uint8 `field:"state"` // 状态
13+
CreatedAt uint64 `field:"createdAt"` // 创建时间
14+
AdminId uint32 `field:"adminId"` // 管理员ID
15+
Weight uint32 `field:"weight"` // 权重
16+
Status string `field:"status"` // 运行状态
17+
}
18+
19+
type AuthorityNodeOperator struct {
20+
Id interface{} // ID
21+
IsOn interface{} // 是否启用
22+
UniqueId interface{} // 唯一ID
23+
Secret interface{} // 密钥
24+
Name interface{} // 名称
25+
Description interface{} // 描述
26+
Order interface{} // 排序
27+
State interface{} // 状态
28+
CreatedAt interface{} // 创建时间
29+
AdminId interface{} // 管理员ID
30+
Weight interface{} // 权重
31+
Status interface{} // 运行状态
32+
}
33+
34+
func NewAuthorityNodeOperator() *AuthorityNodeOperator {
35+
return &AuthorityNodeOperator{}
36+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package authority

internal/db/models/authority_key_model_ext.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)