Skip to content

Commit 231bb70

Browse files
committed
🔨 Init check b3 key
1 parent f371561 commit 231bb70

4 files changed

Lines changed: 68 additions & 28 deletions

File tree

console/package-lock.json

Lines changed: 17 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

controller/initctl.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ package controller
2020
import (
2121
"fmt"
2222
"net/http"
23+
"strings"
2324
"text/template"
25+
"time"
2426

2527
"github.com/b3log/pipe/model"
2628
"github.com/b3log/pipe/service"
2729
"github.com/b3log/pipe/util"
2830
"github.com/gin-gonic/gin"
31+
"github.com/parnurzeal/gorequest"
2932
)
3033

3134
func showInitPageAction(c *gin.Context) {
@@ -52,9 +55,51 @@ func initAction(c *gin.Context) {
5255
return
5356
}
5457

58+
arg := map[string]interface{}{}
59+
if err := c.BindJSON(&arg); nil != err {
60+
result.Code = -1
61+
result.Msg = "parses init request failed"
62+
63+
return
64+
}
65+
b3key := strings.TrimSpace(arg["b3key"].(string))
66+
if "" == b3key {
67+
result.Code = -1
68+
result.Msg = "B3 key cant' be empty"
69+
70+
return
71+
}
72+
if 20 < len(b3key) {
73+
result.Code = -1
74+
result.Msg = "B3 key should less then 20 characters"
75+
76+
return
77+
}
78+
79+
checkResult := util.NewResult()
80+
request := gorequest.New()
81+
_, _, errs := request.Post(util.HacPaiURL+"/apis/check-b3key").Send(map[string]interface{}{
82+
"userName": session.UName,
83+
"userB3Key": b3key,
84+
}).Set("user-agent", util.UserAgent).Timeout(30 * time.Second).EndStruct(checkResult)
85+
if nil != errs {
86+
logger.Errorf("check b3 key failed: %s", errs)
87+
result.Code = -1
88+
result.Msg = "check b3 key failed"
89+
90+
return
91+
}
92+
93+
if 0 != checkResult.Code {
94+
result.Code = -1
95+
result.Msg = "B3 key is not match"
96+
97+
return
98+
}
99+
55100
platformAdmin := &model.User{
56101
Name: session.UName,
57-
B3Key: session.UB3Key,
102+
B3Key: b3key,
58103
AvatarURL: session.UAvatar,
59104
}
60105

controller/statusctl.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ func getStatusAction(c *gin.Context) {
7878

7979
if model.UserRoleNoLogin != session.URole && platformStatus.Inited {
8080
ownBlog := service.User.GetOwnBlog(user.ID)
81-
data.BlogTitle = ownBlog.Title
82-
data.BlogURL = ownBlog.URL
81+
if nil != ownBlog {
82+
data.BlogTitle = ownBlog.Title
83+
data.BlogURL = ownBlog.URL
84+
}
8385
data.Blogs = service.User.GetUserBlogs(user.ID)
8486
}
8587
}

service/initsrv.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,7 @@ func (srv *initService) InitBlog(blogAdmin *model.User) error {
124124
defer srv.mutex.Unlock()
125125

126126
user := User.GetUserByName(blogAdmin.Name)
127-
if nil != user {
128-
return nil
129-
}
130-
131-
adminCount := 0
132-
db.Model(&model.Correlation{}).Where("id2 = ? AND type = ? AND int1 = ?", blogAdmin.ID, model.CorrelationBlogUser, model.UserRoleBlogAdmin).
133-
Count(&adminCount)
134-
if 0 < adminCount {
127+
if nil != user && nil != User.GetOwnBlog(user.ID) {
135128
return nil
136129
}
137130

0 commit comments

Comments
 (0)