Skip to content

Commit

Permalink
[fix] #74
Browse files Browse the repository at this point in the history
  • Loading branch information
yoneyan committed Mar 19, 2021
1 parent eaa218c commit 2c8a065
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 93 deletions.
5 changes: 2 additions & 3 deletions pkg/api/core/auth/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ type UserResult struct {
}

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

type AdminStruct struct {
Expand Down
44 changes: 16 additions & 28 deletions pkg/api/core/auth/v0/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ import (
"fmt"
"github.com/homenoc/dsbd-backend/pkg/api/core"
"github.com/homenoc/dsbd-backend/pkg/api/core/auth"
"github.com/homenoc/dsbd-backend/pkg/api/core/group"
"github.com/homenoc/dsbd-backend/pkg/api/core/token"
"github.com/homenoc/dsbd-backend/pkg/api/core/user"
dbGroup "github.com/homenoc/dsbd-backend/pkg/api/store/group/v0"
dbToken "github.com/homenoc/dsbd-backend/pkg/api/store/token/v0"
dbUser "github.com/homenoc/dsbd-backend/pkg/api/store/user/v0"
"github.com/jinzhu/gorm"
"log"
"time"
Expand All @@ -23,17 +19,14 @@ func UserAuthentication(data core.Token) auth.UserResult {
if resultToken.Err != nil {
return auth.UserResult{Err: fmt.Errorf("db error")}
}
resultUser := dbUser.Get(user.ID, &core.User{Model: gorm.Model{ID: resultToken.Token[0].UserID}})
if resultUser.Err != nil {
return auth.UserResult{Err: fmt.Errorf("db error")}
}
if 0 < *resultUser.User[0].ExpiredStatus {

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

renewProcess(resultToken.Token[0])
go renewProcess(resultToken.Token[0])

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

// errorType 0: 未審査の場合でもエラーを返す 1: 未審査の場合エラーを返さない
Expand All @@ -45,38 +38,33 @@ func GroupAuthentication(errorType uint, data core.Token) auth.GroupResult {
if resultToken.Err != nil {
return auth.GroupResult{Err: fmt.Errorf("error: no token")}
}
resultUser := dbUser.Get(user.ID, &core.User{Model: gorm.Model{ID: resultToken.Token[0].UserID}})
if resultUser.Err != nil {
return auth.GroupResult{Err: fmt.Errorf("db error")}
}
if 0 < *resultUser.User[0].ExpiredStatus {
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, &core.Group{Model: gorm.Model{ID: resultUser.User[0].GroupID}})
if resultGroup.Err != nil {
return auth.GroupResult{Err: fmt.Errorf("db error")}
}

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

renewProcess(resultToken.Token[0])
go renewProcess(resultToken.Token[0])

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

func renewProcess(t core.Token) {
Expand Down
12 changes: 6 additions & 6 deletions pkg/api/core/group/connection/v0/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ func Add(c *gin.Context) {
}

// status check for group
if !*result.Group.Pass {
if !*result.User.Group.Pass {
c.JSON(http.StatusForbidden, common.Error{Error: "error: Your group has not yet been reviewed."})
return
}

if !(*result.Group.Status == 3 && *result.Group.ExpiredStatus == 0) {
if !(*result.User.Group.Status == 3 && *result.User.Group.ExpiredStatus == 0) {
c.JSON(http.StatusUnauthorized, common.Error{Error: "error: failed group status"})
return
}
Expand Down Expand Up @@ -114,7 +114,7 @@ func Add(c *gin.Context) {
}

// GroupIDが一致しない場合はエラーを返す
if resultService.Service[0].GroupID != result.Group.ID {
if resultService.Service[0].GroupID != result.User.Group.ID {
c.JSON(http.StatusBadRequest, common.Error{Error: "error: GroupID does not match."})
return
}
Expand Down Expand Up @@ -159,7 +159,7 @@ func Add(c *gin.Context) {
attachment := slack.Attachment{}
attachment.AddField(slack.Field{Title: "Title", Value: "接続情報登録"}).
AddField(slack.Field{Title: "申請者", Value: strconv.Itoa(int(result.User.ID)) + ":" + result.User.Name}).
AddField(slack.Field{Title: "GroupID", Value: strconv.Itoa(int(result.Group.ID)) + ":" + result.Group.Org}).
AddField(slack.Field{Title: "GroupID", Value: strconv.Itoa(int(result.User.Group.ID)) + ":" + result.User.Group.Org}).
AddField(slack.Field{Title: "サービスコード", Value: resultService.Service[0].ServiceTemplate.Type +
fmt.Sprintf("%03d", resultService.Service[0].ServiceNumber)}).
AddField(slack.Field{Title: "接続コード(新規発番)", Value: resultConnectionTemplate.Connections[0].Type +
Expand All @@ -168,7 +168,7 @@ func Add(c *gin.Context) {
notification.SendSlack(notification.Slack{Attachment: attachment, ID: "main", Status: true})

if err = dbGroup.Update(group.UpdateStatus, core.Group{
Model: gorm.Model{ID: result.Group.ID},
Model: gorm.Model{ID: result.User.Group.ID},
Status: &[]uint{4}[0],
}); err != nil {
c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()})
Expand All @@ -186,7 +186,7 @@ func Add(c *gin.Context) {
attachment = slack.Attachment{}
attachment.AddField(slack.Field{Title: "Title", Value: "ステータス変更"}).
AddField(slack.Field{Title: "申請者", Value: "System"}).
AddField(slack.Field{Title: "GroupID", Value: strconv.Itoa(int(result.Group.ID)) + ":" + result.Group.Org}).
AddField(slack.Field{Title: "GroupID", Value: strconv.Itoa(int(result.User.Group.ID)) + ":" + result.User.Group.Org}).
AddField(slack.Field{Title: "現在ステータス情報", Value: "開通作業中"}).
AddField(slack.Field{Title: "ステータス履歴", Value: "3[接続情報記入段階(User)] =>4[開通作業中] "})
notification.SendSlack(notification.Slack{Attachment: attachment, ID: "main", Status: true})
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/core/group/info/v0/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Get(c *gin.Context) {
return
}

resultService := dbService.Get(service.Open, &core.Service{GroupID: result.Group.ID})
resultService := dbService.Get(service.Open, &core.Service{GroupID: result.User.GroupID})
if resultService.Err != nil {
c.JSON(http.StatusInternalServerError, common.Error{Error: resultService.Err.Error()})
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/core/group/service/jpnicAdmin/v0/jpnicAdmin.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func Add(c *gin.Context) {
return
}

networkResult := dbService.Get(service.GID, &core.Service{GroupID: result.Group.ID})
networkResult := dbService.Get(service.GID, &core.Service{GroupID: result.User.GroupID})
if networkResult.Err != nil {
c.JSON(http.StatusInternalServerError, common.Error{Error: networkResult.Err.Error()})
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/core/group/service/jpnicTech/v0/jpnicTech.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func Add(c *gin.Context) {
return
}

networkResult := dbService.Get(service.GID, &core.Service{GroupID: result.Group.ID})
networkResult := dbService.Get(service.GID, &core.Service{GroupID: result.User.GroupID})
if networkResult.Err != nil {
c.JSON(http.StatusInternalServerError, common.Error{Error: networkResult.Err.Error()})
return
Expand Down
16 changes: 8 additions & 8 deletions pkg/api/core/group/service/v0/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func Add(c *gin.Context) {
}

// status check for group
if !(*result.Group.Status == 1 && *result.Group.ExpiredStatus == 0 && *result.Group.Pass) {
if !(*result.User.Group.Status == 1 && *result.User.Group.ExpiredStatus == 0 && *result.User.Group.Pass) {
c.JSON(http.StatusUnauthorized, common.Error{Error: "error: failed group status"})
return
}
Expand Down Expand Up @@ -118,7 +118,7 @@ func Add(c *gin.Context) {
return
}

resultNetwork := dbService.Get(service.SearchNewNumber, &core.Service{GroupID: result.Group.ID})
resultNetwork := dbService.Get(service.SearchNewNumber, &core.Service{GroupID: result.User.Group.ID})
if resultNetwork.Err != nil {
c.JSON(http.StatusBadRequest, common.Error{Error: resultNetwork.Err.Error()})
return
Expand All @@ -137,7 +137,7 @@ func Add(c *gin.Context) {

// db create for network
net, err := dbService.Create(&core.Service{
GroupID: result.Group.ID,
GroupID: result.User.Group.ID,
ServiceTemplateID: &input.ServiceTemplateID,
ServiceComment: input.ServiceComment,
ServiceNumber: number,
Expand Down Expand Up @@ -169,14 +169,14 @@ func Add(c *gin.Context) {
attachment := slack.Attachment{}
attachment.AddField(slack.Field{Title: "Title", Value: "ネットワーク情報登録"}).
AddField(slack.Field{Title: "申請者", Value: strconv.Itoa(int(result.User.ID)) + ":" + result.User.Name}).
AddField(slack.Field{Title: "GroupID", Value: strconv.Itoa(int(result.Group.ID)) + ":" + result.Group.Org}).
AddField(slack.Field{Title: "GroupID", Value: strconv.Itoa(int(result.User.Group.ID)) + ":" + result.User.Group.Org}).
AddField(slack.Field{Title: "サービスコード(新規発番)", Value: resultServiceTemplate.Services[0].Type + fmt.Sprintf("%03d", number)}).
AddField(slack.Field{Title: "サービスコード(補足情報)", Value: input.ServiceComment})
notification.SendSlack(notification.Slack{Attachment: attachment, ID: "main", Status: true})

// ---------ここまで処理が通っている場合、DBへの書き込みにすべて成功している
// GroupのStatusをAfterStatusにする
if err = dbGroup.Update(group.UpdateStatus, core.Group{Model: gorm.Model{ID: result.Group.ID},
if err = dbGroup.Update(group.UpdateStatus, core.Group{Model: gorm.Model{ID: result.User.Group.ID},
Status: &[]uint{2}[0]}); err != nil {
c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()})
return
Expand All @@ -185,7 +185,7 @@ func Add(c *gin.Context) {
attachment = slack.Attachment{}
attachment.AddField(slack.Field{Title: "Title", Value: "ステータス変更"}).
AddField(slack.Field{Title: "申請者", Value: "System"}).
AddField(slack.Field{Title: "GroupID", Value: strconv.Itoa(int(result.Group.ID)) + ":" + result.Group.Org}).
AddField(slack.Field{Title: "GroupID", Value: strconv.Itoa(int(result.User.Group.ID)) + ":" + result.User.Group.Org}).
AddField(slack.Field{Title: "現在ステータス情報", Value: "審査中"}).
AddField(slack.Field{Title: "ステータス履歴", Value: "1[ネットワーク情報記入段階(User)] =>2[審査中] "})
notification.SendSlack(notification.Slack{Attachment: attachment, ID: "main", Status: true})
Expand Down Expand Up @@ -227,7 +227,7 @@ func Update(c *gin.Context) {
c.JSON(http.StatusInternalServerError, common.Error{Error: "failed Service ID"})
return
}
if resultNetwork.Service[0].GroupID != result.Group.ID {
if resultNetwork.Service[0].GroupID != result.User.Group.ID {
c.JSON(http.StatusInternalServerError, common.Error{Error: "Authentication failure"})
return
}
Expand Down Expand Up @@ -256,7 +256,7 @@ func GetAddAllow(c *gin.Context) {
return
}

if resultService := dbService.Get(service.GIDAndAddAllow, &core.Service{GroupID: result.Group.ID}); resultService.Err != nil {
if resultService := dbService.Get(service.GIDAndAddAllow, &core.Service{GroupID: result.User.Group.ID}); resultService.Err != nil {
log.Println(resultService.Err)
c.JSON(http.StatusInternalServerError, common.Error{Error: resultService.Err.Error()})
} else {
Expand Down
76 changes: 44 additions & 32 deletions pkg/api/core/group/v0/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,21 @@ func Update(c *gin.Context) {
c.JSON(http.StatusUnauthorized, common.Error{Error: "error: failed user level"})
return
}
if *authResult.Group.Lock {
if *authResult.User.Group.Lock {
c.JSON(http.StatusUnauthorized, common.Error{Error: "error: This group is locked"})
return
}

data := authResult.Group
data := authResult.User.Group

if data.Org != input.Org {
data.Org = input.Org
}

if err = dbGroup.Update(group.UpdateInfo, data); err != nil {
c.JSON(http.StatusInternalServerError, common.Error{Error: authResult.Err.Error()})
return
}
//if err = dbGroup.Update(group.UpdateInfo, data); err != nil {
// c.JSON(http.StatusInternalServerError, common.Error{Error: authResult.Err.Error()})
// return
//}
c.JSON(http.StatusOK, common.Result{})

}
Expand All @@ -154,9 +154,15 @@ func Get(c *gin.Context) {
return
}

resultUser := dbUser.Get(user.ID, &core.User{Model: gorm.Model{ID: result.User.ID}})
if resultUser.Err != nil {
c.JSON(http.StatusUnauthorized, common.Error{Error: resultUser.Err.Error()})
return
}

var responseUser []user.User
if result.User.Level == 1 || result.User.Level == 2 {
for _, tmpUser := range result.Group.Users {
for _, tmpUser := range resultUser.User {
responseUser = append(responseUser, user.User{
ID: tmpUser.ID,
Name: tmpUser.Name,
Expand All @@ -181,20 +187,20 @@ func Get(c *gin.Context) {
}
}

log.Println(result.Group.Users)
log.Println(result.User)

c.JSON(http.StatusOK, group.Result{Group: group.Group{
ID: result.Group.ID,
Agree: result.Group.Agree,
Question: result.Group.Question,
Org: result.Group.Org,
Status: *result.Group.Status,
Contract: result.Group.Contract,
Student: result.Group.Student,
Open: result.Group.Open,
Pass: result.Group.Pass,
Lock: result.Group.Lock,
ExpiredStatus: *result.Group.ExpiredStatus,
ID: result.User.Group.ID,
Agree: result.User.Group.Agree,
Question: result.User.Group.Question,
Org: result.User.Group.Org,
Status: *result.User.Group.Status,
Contract: result.User.Group.Contract,
Student: result.User.Group.Student,
Open: result.User.Group.Open,
Pass: result.User.Group.Pass,
Lock: result.User.Group.Lock,
ExpiredStatus: *result.User.Group.ExpiredStatus,
//User: users,
}})
}
Expand All @@ -214,10 +220,16 @@ func GetAll(c *gin.Context) {
return
}

resultGroup := dbGroup.Get(group.ID, &core.Group{Model: gorm.Model{ID: result.User.GroupID}})
if resultGroup.Err != nil {
c.JSON(http.StatusInternalServerError, common.Error{Error: result.Err.Error()})
return
}

var users []user.User
var services []service.Service

for _, tmpUser := range result.Group.Users {
for _, tmpUser := range resultGroup.Group[0].Users {
users = append(users, user.User{
ID: tmpUser.ID,
Name: tmpUser.Name,
Expand All @@ -229,7 +241,7 @@ func GetAll(c *gin.Context) {
})
}

for _, tmpService := range result.Group.Services {
for _, tmpService := range resultGroup.Group[0].Services {
var tmpConnections *[]connection.Connection = nil
var tmpJPNICAdmin *service.JPNIC = nil
var tmpJPNICTech *[]service.JPNIC = nil
Expand Down Expand Up @@ -338,17 +350,17 @@ func GetAll(c *gin.Context) {
}

c.JSON(http.StatusOK, group.Result{Group: group.Group{
ID: result.Group.ID,
Agree: result.Group.Agree,
Question: result.Group.Question,
Org: result.Group.Org,
Status: *result.Group.Status,
Contract: result.Group.Contract,
Student: result.Group.Student,
Open: result.Group.Open,
Pass: result.Group.Pass,
Lock: result.Group.Lock,
ExpiredStatus: *result.Group.ExpiredStatus,
ID: resultGroup.Group[0].ID,
Agree: resultGroup.Group[0].Agree,
Question: resultGroup.Group[0].Question,
Org: resultGroup.Group[0].Org,
Status: *resultGroup.Group[0].Status,
Contract: resultGroup.Group[0].Contract,
Student: resultGroup.Group[0].Student,
Open: resultGroup.Group[0].Open,
Pass: resultGroup.Group[0].Pass,
Lock: resultGroup.Group[0].Lock,
ExpiredStatus: *resultGroup.Group[0].ExpiredStatus,
User: users,
Service: &services,
}})
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/core/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type User struct {
gorm.Model
Tokens []*Token `json:"tokens"`
Notice []*Notice `json:"notice"`
Group *Group `json:"group"`
GroupID uint `json:"group_id"`
Name string `json:"name"`
NameEn string `json:"name_en"`
Expand Down Expand Up @@ -269,6 +270,7 @@ type Token struct {
gorm.Model
ExpiredAt time.Time `json:"expired_at"`
UserID uint `json:"user_id"`
User User `json:"user"`
Status uint `json:"status"` //0: initToken(30m) 1: 30m 2:6h 3: 12h 10: 30d 11:180d
Admin *bool `json:"admin"`
UserToken string `json:"user_token"`
Expand Down
Loading

0 comments on commit 2c8a065

Please sign in to comment.