Skip to content

Commit

Permalink
Merge pull request #152 from Viva-con-Agua/tk/hotfix_message_permission
Browse files Browse the repository at this point in the history
Tk/hotfix message permission
  • Loading branch information
deinelieblings authored Oct 19, 2023
2 parents 9c1029f + f52d32c commit da1a1e3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 26 deletions.
6 changes: 6 additions & 0 deletions dao/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ var (
ActitityUserPipe = vmdb.NewPipeline()
UserPipe = vmdb.NewPipeline()
UpdateCollection *vmdb.Collection

TestLogin bool
)

func InitialDatabase() {
Expand Down Expand Up @@ -230,3 +232,7 @@ func InitialIDjango() {
IDjango.Key = vcago.Settings.String("IDJANGO_KEY", "n", "")
IDjango.Export = vcago.Settings.Bool("IDJANGO_EXPORT", "n", false)
}

func InitialTestLogin() {
TestLogin = vcago.Settings.Bool("API_TEST_LOGIN", "n", false)
}
4 changes: 3 additions & 1 deletion dao/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ func MessageInsert(ctx context.Context, i *models.MessageCreate, token *vcapool.
event := new(models.Event)
EventCollection.FindOne(ctx, bson.D{{Key: "_id", Value: i.RecipientGroup.EventID}}, event)

result = i.MessageSub(token).PermittedCreate(token, crew, event)
if result, err = models.PermittedMessageCreate(token, i.MessageSub(token), crew, event); err != nil {
return
}
if err = MessageCollection.InsertOne(ctx, result); err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion handlers/token/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var HydraClient = vcago.NewHydraClient()
func (i *LoginHandler) Routes(group *echo.Group) {
group.Use(i.Context)
group.POST("/callback", i.Callback)
if vcago.Settings.Bool("API_TEST_LOGIN", "n", false) {
if dao.TestLogin {
group.POST("/testlogin", i.LoginAPI)
}
group.GET("/refresh", i.Refresh, refreshCookie)
Expand Down
36 changes: 15 additions & 21 deletions models/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,30 +193,24 @@ func (i *RecipientGroup) FilterEvent() bson.D {
return filter.Bson()
}

func (i *Message) PermittedCreate(token *vcapool.AccessToken, crew *Crew, event *Event) *Message {
if !(token.Roles.Validate("employee;admin") || token.PoolRoles.Validate("network;operation;education")) {
if !(i.RecipientGroup.Type == "event" || token.ID == event.EventASPID) { // USER
i.MailboxID = token.MailboxID
i.From = token.Email
} else { // EVENT ASP
i.MailboxID = crew.MailboxID
if !(i.From == token.Email || i.From == crew.Email) {
i.From = token.Email
}
}
} else if !(token.Roles.Validate("employee;admin")) { // ASP
if i.MailboxID == crew.MailboxID {
i.RecipientGroup.CrewID = crew.ID
if !(i.From == token.Email || i.From == crew.Email) {
i.From = token.Email
}
func PermittedMessageCreate(token *vcapool.AccessToken, i *Message, crew *Crew, event *Event) (message *Message, err error) {
message = i
if !token.Roles.Validate("employee;admin") {
if i.RecipientGroup.Type == "event" && token.ID == event.EventASPID {
// IF IS EVENT ASP -> Force Mailbox and From to CrewMailbox and CrewEmail
message.MailboxID = crew.MailboxID
message.From = crew.Email
} else if token.PoolRoles.Validate("network;operation;education") {
// IF IS CREW ASP -> Force Mailbox and From to CrewMailbox and CrewEmail
message.MailboxID = crew.MailboxID
message.From = crew.Email
} else {
i.MailboxID = token.MailboxID
i.From = token.Email
return nil, vcago.NewBadRequest(MessageCollection, "Not allwed to create a message")
// i.MailboxID = token.MailboxID
// i.From = token.Email
}
}
// ADMIN
return i
return
}

func (i *Message) Inbox() *[]interface{} {
Expand Down
10 changes: 7 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"pool-backend/dao"
"pool-backend/handlers/admin"
"pool-backend/handlers/key"
"pool-backend/handlers/token"

Expand All @@ -13,6 +14,7 @@ func main() {
dao.InitialDatabase()
dao.InitialNats()
dao.InitialIDjango()
dao.InitialTestLogin()
dao.FixDatabase()
dao.UpdateDatabase()
dao.UpdateTicker()
Expand Down Expand Up @@ -56,9 +58,11 @@ func main() {

key.Import.Routes(api.Group("/import"))

//admin.Crew.Routes(e.Group("/admin/crews"))
//admin.Role.Routes(e.Group("/admin/users/role"))
//admin.User.Routes(e.Group("/admin/users"))
if dao.TestLogin {
admin.Crew.Routes(e.Group("/admin/crews"))
admin.Role.Routes(e.Group("/admin/users/role"))
admin.User.Routes(e.Group("/admin/users"))
}
//server
e.Run()
}

0 comments on commit da1a1e3

Please sign in to comment.