mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00
add static map to add users to scenarios based on their groups
This commit is contained in:
parent
edb361f150
commit
7049ba6eaa
2 changed files with 31 additions and 6 deletions
|
@ -33,6 +33,10 @@ import (
|
||||||
// Global configuration
|
// Global configuration
|
||||||
var GlobalConfig *config.Config = nil
|
var GlobalConfig *config.Config = nil
|
||||||
|
|
||||||
|
var ScenarioGroupMap = map[string][]int{
|
||||||
|
"moodle-l2pmanager@13306": {1, 2, 3},
|
||||||
|
}
|
||||||
|
|
||||||
func InitConfig() error {
|
func InitConfig() error {
|
||||||
if GlobalConfig != nil {
|
if GlobalConfig != nil {
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
package user
|
package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -231,21 +232,41 @@ func authenticateExternal(c *gin.Context) *User {
|
||||||
// preferred_username := c.Request.Header.Get("X-Forwarded-Preferred-Username")
|
// preferred_username := c.Request.Header.Get("X-Forwarded-Preferred-Username")
|
||||||
|
|
||||||
var user User
|
var user User
|
||||||
if err := user.ByUsername(username); err == nil {
|
if err := user.ByUsername(username); err != nil {
|
||||||
// There is already a user by this name
|
|
||||||
return &user
|
|
||||||
} else {
|
|
||||||
role := "User"
|
role := "User"
|
||||||
if _, found := helper.Find(groups, "admin"); found {
|
if _, found := helper.Find(groups, "admin"); found {
|
||||||
role = "Admin"
|
role = "Admin"
|
||||||
}
|
}
|
||||||
|
|
||||||
newUser, err := NewUser(username, "", email, role, true)
|
user, err := NewUser(username, "", email, role, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
helper.UnauthorizedAbort(c, "Authentication failed (failed to create new user: "+err.Error()+")")
|
helper.UnauthorizedAbort(c, "Authentication failed (failed to create new user: "+err.Error()+")")
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue