Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework scenario duplication #144

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
b193e01
[WIP] add usergroups endpoints
iripiri Aug 15, 2024
69d898b
[WIP] add usergroup endpoints & tests
iripiri Aug 30, 2024
55f0610
Added endpoints for usergroup, duplicate scenario(s) when adding user…
iripiri Sep 2, 2024
6dcb7f3
Merge branch 'master' into rework_scenario_duplication
iripiri Sep 2, 2024
f9a504b
corrections for staticcheck
iripiri Sep 3, 2024
c9016ac
Rewrote deleteUserFromUserGroup handler
SystemsPurge Oct 15, 2024
7197fac
Rewrote add user to usergroup test
SystemsPurge Oct 15, 2024
e83236c
Rewrote test accounting for multiple users
SystemsPurge Oct 15, 2024
5972c68
Added delete user from usergroup test
SystemsPurge Oct 15, 2024
c4d632f
Fixed missing registration of scenario endpoints
SystemsPurge Oct 16, 2024
8da1b1f
Added scenario ID existence checks on usergroup add
SystemsPurge Oct 16, 2024
15ff90f
Added duplicate scenario deletion on usergroup delete and its test
SystemsPurge Oct 16, 2024
2282b51
Rewrote some scenario duplication checks to account for the MtoM rela…
SystemsPurge Oct 16, 2024
08ba939
Made remove duplicate also remove duplicated ics
SystemsPurge Oct 16, 2024
d710627
Made remove duplicate send action over amqp
SystemsPurge Oct 17, 2024
0bc0162
Added updateUserGroup test
SystemsPurge Oct 17, 2024
4310cb6
Fixed mistake in test
SystemsPurge Oct 17, 2024
2a2c5ca
Removed unused code
SystemsPurge Oct 17, 2024
b014a1e
Fix staticcheck
SystemsPurge Oct 17, 2024
a932c7c
Fixed mistake in ic deletion when clearing usergroup
SystemsPurge Oct 18, 2024
e5b8df3
Loosened restrictions on usergroup delete/user delete from usergroup
SystemsPurge Oct 31, 2024
81a0255
More checks on job metadata naming
SystemsPurge Nov 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ go run start.go --help
```
to get a list of available parameters and default values

### Generating Docs
```bash
$(go env GOPATH)/bin/swag init --generalInfo start.go --output ./doc/api --parseDependency
```

## Environment variables

| Variable | Description |
Expand Down
45 changes: 0 additions & 45 deletions configuration/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@ package configuration
import (
"flag"
"fmt"
"io"
"log"
"os"
"sort"
"strings"

"gopkg.in/yaml.v3"

"github.com/zpatrick/go-config"
)

Expand Down Expand Up @@ -192,45 +189,3 @@ func InitConfig() error {
}
return nil
}

func remove(arr []GroupedScenario, index int) []GroupedScenario {
arr[index] = arr[len(arr)-1]
return arr[:len(arr)-1]
}

func ReadGroupsFile(path string) error {
_, err := os.Stat(path)
if err != nil {
return err
}

yamlFile, err := os.Open(path)
if err != nil {
return fmt.Errorf("error opening yaml file for groups: %v", err)
}
log.Println("Successfully opened yaml groups file", path)

defer yamlFile.Close()

byteValue, _ := io.ReadAll(yamlFile)

err = yaml.Unmarshal(byteValue, &ScenarioGroupMap)
if err != nil {
return fmt.Errorf("error unmarshalling yaml into ScenarioGroupMap: %v", err)
}

for _, group := range ScenarioGroupMap {
for i, scenario := range group {
// remove invalid values that might have been introduced by typos
// (Unmarshal sets default values when it doesn't find a field)
if scenario.Scenario == 0 {
log.Println("Removing entry from ScenarioGroupMap, check for typos in the yaml!")
remove(group, i)
}
}
}

log.Println("ScenarioGroupMap", ScenarioGroupMap)

return nil
}
4 changes: 4 additions & 0 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ func DropTables() {
DBpool.DropTableIfExists(&File{})
DBpool.DropTableIfExists(&Scenario{})
DBpool.DropTableIfExists(&User{})
DBpool.DropTableIfExists(&UserGroup{})
DBpool.DropTableIfExists(&ScenarioMapping{})
DBpool.DropTableIfExists(&Dashboard{})
DBpool.DropTableIfExists(&Widget{})
DBpool.DropTableIfExists(&Result{})
Expand All @@ -120,6 +122,8 @@ func MigrateModels() {
DBpool.AutoMigrate(&File{})
DBpool.AutoMigrate(&Scenario{})
DBpool.AutoMigrate(&User{})
DBpool.AutoMigrate(&UserGroup{})
DBpool.AutoMigrate(&ScenarioMapping{})
DBpool.AutoMigrate(&Dashboard{})
DBpool.AutoMigrate(&Widget{})
DBpool.AutoMigrate(&Result{})
Expand Down
24 changes: 24 additions & 0 deletions database/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@ type User struct {
Active bool `json:"active" gorm:"default:true"`
// Scenarios to which user has access
Scenarios []*Scenario `json:"-" gorm:"many2many:user_scenarios;"`
// Groups of user
UserGroups []*UserGroup `json:"-" gorm:"many2many:user_groups_users;"`
}

// ScenarioMapping data model
type ScenarioMapping struct {
Model
ScenarioID uint `json:"scenarioID" ` // Foreign key to Scenario
Scenario Scenario `json:"-" gorm:"foreignkey:ScenarioID"`
UserGroupID uint `json:"-"` // Foreign key to UserGroup
UserGroup UserGroup `json:"-" gorm:"foreignkey:UserGroupID"`
// Whether to duplicate Scenario or add users to existing Scenario
Duplicate bool `json:"duplicate"`
}

// UserGroup data model
type UserGroup struct {
Model
// Name of user group
Name string `json:"name" gorm:"unique;not null"`
// Scenarios that belong to the user group
ScenarioMappings []ScenarioMapping `json:"scenarioMappings" gorm:"foreignkey:UserGroupID"`
// Users that belong to the user group
Users []*User `json:"users" gorm:"many2many:user_groups_users;"`
}

// Scenario data model
Expand Down
29 changes: 29 additions & 0 deletions database/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,35 @@ func CheckSignalPermissions(c *gin.Context, operation CRUD) (bool, Signal) {

}

func CheckUserGroupPermissions(c *gin.Context, operation CRUD, userGroupIDSource string, usergroupIDBody int) (bool, UserGroup) {

var usrgrp UserGroup

err := ValidateRole(c, ModelUserGroup, operation)
if err != nil {
helper.UnprocessableEntityError(c, fmt.Sprintf("Access denied (role validation failed): %v", err.Error()))
return false, usrgrp
}

if operation == Create {
return true, usrgrp
}

groupID, err := helper.GetIDOfElement(c, "userGroupID", userGroupIDSource, usergroupIDBody)
if err != nil {
return false, usrgrp
}

db := GetDB()
err = db.Preload("ScenarioMappings.Scenario").First(&usrgrp, groupID).Error

if helper.DBNotFoundError(c, err, strconv.Itoa(groupID), "UserGroup") {
return false, usrgrp
}

return true, usrgrp
}

func CheckDashboardPermissions(c *gin.Context, operation CRUD, dabIDSource string, dabIDBody int) (bool, Dashboard) {

var dab Dashboard
Expand Down
5 changes: 5 additions & 0 deletions database/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package database

import (
"fmt"

"github.com/gin-gonic/gin"
)

Expand All @@ -33,6 +34,7 @@ type ModelName string
const ModelUser = ModelName("user")
const ModelUsers = ModelName("users")
const ModelScenario = ModelName("scenario")
const ModelUserGroup = ModelName("usergroup")
const ModelInfrastructureComponent = ModelName("ic")
const ModelInfrastructureComponentAction = ModelName("icaction")
const ModelDashboard = ModelName("dashboard")
Expand Down Expand Up @@ -73,6 +75,7 @@ var Roles = RoleActions{
ModelUser: crud,
ModelUsers: crud,
ModelScenario: crud,
ModelUserGroup: crud,
ModelComponentConfiguration: crud,
ModelInfrastructureComponent: crud,
ModelInfrastructureComponentAction: crud,
Expand All @@ -86,6 +89,7 @@ var Roles = RoleActions{
ModelUser: _ru_,
ModelUsers: none,
ModelScenario: crud,
ModelUserGroup: _r__,
ModelComponentConfiguration: crud,
ModelInfrastructureComponent: _r__,
ModelInfrastructureComponentAction: _ru_,
Expand Down Expand Up @@ -117,6 +121,7 @@ var Roles = RoleActions{
ModelInfrastructureComponentAction: none,
ModelUser: none,
ModelUsers: none,
ModelUserGroup: none,
ModelSignal: none,
ModelFile: _r__,
ModelResult: none,
Expand Down
Loading