add static map to add users to scenarios based on their groups

This commit is contained in:
Steffen Vogel 2021-04-15 17:32:55 +02:00
parent edb361f150
commit 7049ba6eaa
2 changed files with 31 additions and 6 deletions

View file

@ -33,6 +33,10 @@ import (
// Global configuration
var GlobalConfig *config.Config = nil
var ScenarioGroupMap = map[string][]int{
"moodle-l2pmanager@13306": {1, 2, 3},
}
func InitConfig() error {
if GlobalConfig != nil {
return nil

View file

@ -22,6 +22,7 @@
package user
import (
"log"
"net/http"
"strings"
"time"
@ -231,21 +232,41 @@ func authenticateExternal(c *gin.Context) *User {
// preferred_username := c.Request.Header.Get("X-Forwarded-Preferred-Username")
var user User
if err := user.ByUsername(username); err == nil {
// There is already a user by this name
return &user
} else {
if err := user.ByUsername(username); err != nil {
role := "User"
if _, found := helper.Find(groups, "admin"); found {
role = "Admin"
}
newUser, err := NewUser(username, "", email, role, true)
user, err := NewUser(username, "", email, role, true)
if err != nil {
helper.UnauthorizedAbort(c, "Authentication failed (failed to create new user: "+err.Error()+")")
return nil
}
return newUser
return user
}
// Add users to scenarios based on static map
for _, group := range groups {
if soIDs, ok := configuration.ScenarioGroupMap[group]; ok {
for soID := range soIDs {
db := database.GetDB()
var so database.Scenario
err := db.Find(so, soID).Error
if err != nil {
log.Printf("Failed to add user %s (id=%d) to scenario %d: Scenario does not exist\n", user.Username, user.ID, soID)
continue
}
err = db.Model(so).Association("Users").Append(user).Error
if err != nil {
log.Printf("Failed to add user %s (id=%d) to scenario %d: %s\n", user.Username, user.ID, soID, err)
}
}
}
}
return &user
}