Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

Commit

Permalink
[fix] #15, #16
Browse files Browse the repository at this point in the history
  • Loading branch information
yoneyan committed Apr 5, 2021
1 parent 6c3737e commit 85cb445
Show file tree
Hide file tree
Showing 85 changed files with 1,888 additions and 1,451 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/cmd/backend/con.conf
/cmd/backend/con.json
/cmd/backend/template.conf
6 changes: 3 additions & 3 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func AdminRestAPI() error {
// Controller
//
v1.POST("/controller/chat", controller.ReceiveChatAdmin)
v1.POST("/controller/node", controller.ReceiveNode)
//v1.POST("/controller/node", controller.ReceiveNode)

// Notice
//
Expand Down Expand Up @@ -194,7 +194,7 @@ func UserRestAPI() {
// Controller
//
v1.POST("/controller/chat", controller.ReceiveChatUser)
v1.POST("/controller/node", controller.ReceiveNode)
//v1.POST("/controller/node", controller.ReceiveNode)

//
// User
Expand Down Expand Up @@ -233,7 +233,7 @@ func UserRestAPI() {
// Support
//
v1.POST("/support", ticket.Create)
v1.GET("/support", ticket.GetTitle)
//v1.GET("/support", ticket.GetTitle)
v1.GET("/support/:id", ticket.Get)

//
Expand Down
9 changes: 4 additions & 5 deletions pkg/api/core/auth/interface.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package auth

import (
"github.com/vmmgr/controller/pkg/api/core/group"
"github.com/vmmgr/controller/pkg/api/core/user"
"github.com/vmmgr/controller/pkg/api/core"
)

type UserResult struct {
User user.User
User core.User
Err error
}

type GroupResult struct {
Group group.Group
User user.User
Group core.Group
User core.User
Err error
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/api/core/auth/v0/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v0

import (
"fmt"
"github.com/vmmgr/controller/pkg/api/core"
"github.com/vmmgr/controller/pkg/api/core/auth"
"github.com/vmmgr/controller/pkg/api/core/token"
"github.com/vmmgr/controller/pkg/api/core/tool/config"
Expand All @@ -18,7 +19,7 @@ func AdminRadiusAuthentication(data auth.AdminStruct) auth.AdminResult {
}

func AdminAuthentication(accessToken string) auth.AdminResult {
tokenResult := dbToken.Get(token.AdminToken, &token.Token{AccessToken: accessToken})
tokenResult := dbToken.Get(token.AdminToken, &core.Token{AccessToken: accessToken})
if tokenResult.Err != nil {
return auth.AdminResult{Err: tokenResult.Err}
}
Expand Down
76 changes: 49 additions & 27 deletions pkg/api/core/auth/v0/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,79 @@ package v0
import (
"fmt"
"github.com/jinzhu/gorm"
"github.com/vmmgr/controller/pkg/api/core"
"github.com/vmmgr/controller/pkg/api/core/auth"
"github.com/vmmgr/controller/pkg/api/core/group"
"github.com/vmmgr/controller/pkg/api/core/token"
"github.com/vmmgr/controller/pkg/api/core/user"
dbGroup "github.com/vmmgr/controller/pkg/api/store/group/v0"
dbToken "github.com/vmmgr/controller/pkg/api/store/token/v0"
dbUser "github.com/vmmgr/controller/pkg/api/store/user/v0"
"log"
"time"
)

func UserAuthentication(data token.Token) auth.UserResult {
func UserAuthentication(data core.Token) auth.UserResult {
resultToken := dbToken.Get(token.UserTokenAndAccessToken, &data)
if len(resultToken.Token) == 0 {
return auth.UserResult{Err: fmt.Errorf("auth failed")}
}
if resultToken.Err != nil {
return auth.UserResult{Err: fmt.Errorf("db error")}
}
resultUser := dbUser.Get(user.ID, &user.User{Model: gorm.Model{ID: resultToken.Token[0].UserID}})
if resultUser.Err != nil {
return auth.UserResult{Err: fmt.Errorf("db error")}
}
if 100 <= resultUser.User[0].Status {

if 0 < *resultToken.Token[0].User.ExpiredStatus {
return auth.UserResult{Err: fmt.Errorf("deleted this user")}
}
return auth.UserResult{User: resultUser.User[0], Err: nil}

go renewProcess(resultToken.Token[0])

return auth.UserResult{User: resultToken.Token[0].User, Err: nil}
}

func GroupAuthentication(data token.Token) auth.GroupResult {
// errorType 0: 未審査の場合でもエラーを返す 1: 未審査の場合エラーを返さない
func GroupAuthentication(errorType uint, data core.Token) auth.GroupResult {
resultToken := dbToken.Get(token.UserTokenAndAccessToken, &data)
if len(resultToken.Token) == 0 {
return auth.GroupResult{Err: fmt.Errorf("auth failed")}
}
if resultToken.Err != nil {
return auth.GroupResult{Err: fmt.Errorf("db error")}
}
resultUser := dbUser.Get(user.ID, &user.User{Model: gorm.Model{ID: resultToken.Token[0].UserID}})
if resultUser.Err != nil {
return auth.GroupResult{Err: fmt.Errorf("db error")}
return auth.GroupResult{Err: fmt.Errorf("error: no token")}
}
if resultUser.User[0].Status == 0 || 100 <= resultUser.User[0].Status {
return auth.GroupResult{Err: fmt.Errorf("user status error")}

if 0 < *resultToken.Token[0].User.ExpiredStatus {
return auth.GroupResult{Err: fmt.Errorf("deleted this user")}
}
if resultUser.User[0].GroupID == 0 {

if resultToken.Token[0].User.GroupID == 0 {
return auth.GroupResult{Err: fmt.Errorf("no group")}
}
resultGroup := dbGroup.Get(group.ID, &group.Group{Model: gorm.Model{ID: resultUser.User[0].GroupID}})
if resultGroup.Err != nil {
return auth.GroupResult{Err: fmt.Errorf("db error")}
}
if resultGroup.Group[0].Status < 2 || 1000 <= resultGroup.Group[0].Status {
return auth.GroupResult{Err: fmt.Errorf("error: group status")}

//// 未審査+errorType = 0の場合
//if !*resultcore.Token[0].User.Group.Pass && errorType == 0 {
// return auth.GroupResult{Err: fmt.Errorf("error: unexamined")}
//}
//// アカウント失効時の動作
//if *resultcore.Token[0].User.Group.ExpiredStatus == 1 {
// return auth.GroupResult{Err: fmt.Errorf("error: discontinued by Master Account")}
//}
//if *resultcore.Token[0].User.Group.ExpiredStatus == 2 {
// return auth.GroupResult{Err: fmt.Errorf("error: discontinuation by the steering committee")}
//}
//if *resultcore.Token[0].User.Group.ExpiredStatus == 3 {
// return auth.GroupResult{Err: fmt.Errorf("error: discontinuation due to failed review")}
//}

go renewProcess(resultToken.Token[0])

return auth.GroupResult{User: resultToken.Token[0].User, Err: nil}
}

func renewProcess(t core.Token) {
if t.ExpiredAt.UTC().Unix() < time.Now().Add(10*time.Minute).UTC().Unix() {
if err := dbToken.Update(token.UpdateToken, &core.Token{
Model: gorm.Model{ID: t.ID},
ExpiredAt: t.ExpiredAt.Add(10 * time.Minute),
}); err != nil {
log.Println(err)
} else {
log.Println("Success!!")
}
}
return auth.GroupResult{User: resultUser.User[0], Group: resultGroup.Group[0], Err: nil}
}
9 changes: 9 additions & 0 deletions pkg/api/core/common/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package common

type Error struct {
Error string `json:"error"`
}

type Result struct {
Result string `json:"result"`
}
11 changes: 1 addition & 10 deletions pkg/api/core/controller/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,8 @@ type Chat struct {
Err string `json:"error"`
CreatedAt time.Time `json:"created_at"`
UserID uint `json:"user_id"`
UserName string `json:"user_name"`
GroupID uint `json:"group_id"`
Admin bool `json:"admin"`
Message string `json:"message"`
}

type Node struct {
GroupID uint `json:"group_id"`
UUID string `json:"uuid"`
FilePath string `json:"file_path"`
Code uint `json:"code"` //1: Image Copy 2:VM Create
Progress uint `json:"progress"`
Status bool `json:"status ,string"`
Comment string `json:"comment"`
}
22 changes: 17 additions & 5 deletions pkg/api/core/controller/v0/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ func SendChatAdmin(data controller.Chat) {
client := &http.Client{}
client.Timeout = time.Second * 5

body, _ := json.Marshal(controller.Chat{Err: data.Err, CreatedAt: data.CreatedAt, UserID: data.UserID,
GroupID: data.GroupID, Admin: data.Admin, Message: data.Message})
body, _ := json.Marshal(controller.Chat{
Err: data.Err,
CreatedAt: data.CreatedAt,
UserID: data.UserID,
UserName: data.UserName,
GroupID: data.GroupID,
Admin: data.Admin,
Message: data.Message,
})

//Header部分
header := http.Header{}
Expand Down Expand Up @@ -62,8 +69,13 @@ func ReceiveChatAdmin(c *gin.Context) {
}

var input controller.Chat
c.BindJSON(&input)
log.Println(c.BindJSON(&input))

support.Broadcast <- support.WebSocketResult{CreatedAt: input.CreatedAt,
UserID: input.UserID, GroupID: input.GroupID, Admin: input.Admin, Message: input.Message}
support.Broadcast <- support.WebSocketResult{
CreatedAt: time.Now(),
UserID: input.UserID,
GroupID: input.GroupID,
Admin: input.Admin,
Message: input.Message,
}
}
38 changes: 0 additions & 38 deletions pkg/api/core/controller/v0/node.go

This file was deleted.

12 changes: 8 additions & 4 deletions pkg/api/core/controller/v0/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,13 @@ func ReceiveChatUser(c *gin.Context) {
}

var input controller.Chat
c.BindJSON(&input)

support.Broadcast <- support.WebSocketResult{CreatedAt: input.CreatedAt,
UserID: input.UserID, GroupID: input.GroupID, Admin: input.Admin, Message: input.Message}
log.Println(c.BindJSON(&input))

support.Broadcast <- support.WebSocketResult{
CreatedAt: time.Now(),
UserID: input.UserID,
GroupID: input.GroupID,
Admin: input.Admin,
Message: input.Message,
}
}
34 changes: 11 additions & 23 deletions pkg/api/core/group/interface.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package group

import (
"github.com/jinzhu/gorm"
"github.com/vmmgr/controller/pkg/api/core"
)

const (
Expand All @@ -16,37 +16,25 @@ const (
UpdateAll = 110
)

type Group struct {
gorm.Model
Org string `json:"org"`
Status uint `json:"status"`
Comment string `json:"comment"`
Vlan uint `json:"vlan"`
Lock bool `json:"lock"`
MaxVM uint `json:"max_VM"`
MaxCPU uint `json:"max_cpu"`
MaxMemory uint `json:"max_memory"`
}

type Result struct {
Status bool `json:"status"`
Error string `json:"error"`
Group []Group `json:"group"`
Status bool `json:"status"`
Error string `json:"error"`
Group []core.Group `json:"group"`
}

type ResultOne struct {
Status bool `json:"status"`
Error string `json:"error"`
Group Group `json:"group"`
Status bool `json:"status"`
Error string `json:"error"`
Group core.Group `json:"group"`
}

type ResultAll struct {
Status bool `json:"status"`
Error string `json:"error"`
Group Group `json:"group"`
Status bool `json:"status"`
Error string `json:"error"`
Group core.Group `json:"group"`
}

type ResultDatabase struct {
Err error
Group []Group
Group []core.Group
}
Loading

0 comments on commit 85cb445

Please sign in to comment.