Skip to content

Commit c95d973

Browse files
committed
[WIP] add usergroups endpoints
Signed-off-by: iripiri <[email protected]>
1 parent a1afc21 commit c95d973

File tree

9 files changed

+728
-388
lines changed

9 files changed

+728
-388
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ go run start.go --help
4242
```
4343
to get a list of available parameters and default values
4444

45+
### Generating Docs
46+
```bash
47+
$(go env GOPATH)/bin/swag init --generalInfo start.go --output ./doc/api --parseDependency
48+
```
49+
4550
## Environment variables
4651

4752
| Variable | Description |

database/models.go

+20
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,32 @@ type User struct {
4444
Mail string `json:"mail" gorm:"default:''"`
4545
// Role of user
4646
Role string `json:"role" gorm:"default:'user'"`
47+
// Group of user by which the user is added to scenarios
48+
Group string `json:"group" gorm:"default:''"`
4749
// Indicating status of user (false means user is inactive and should not be able to login)
4850
Active bool `json:"active" gorm:"default:true"`
4951
// Scenarios to which user has access
5052
Scenarios []*Scenario `json:"-" gorm:"many2many:user_scenarios;"`
5153
}
5254

55+
// ScenarioMapping data model
56+
type ScenarioMapping struct {
57+
Model
58+
// ID of Scenario
59+
ScenarioID uint `json:"scenarioID"`
60+
// Duplicate Scenario or add to existing Scenario
61+
Duplicate bool `json:"duplicate"`
62+
}
63+
64+
// UserGroup data model
65+
type UserGroup struct {
66+
Model
67+
// Name of user group
68+
Name string `json:"name" gorm:"unique;not null"`
69+
// Scenarios that belong to the user group
70+
ScenarioMappings []*ScenarioMapping `json:"scenarioMappings" gorm:"foreignkey:UserGroupID"`
71+
}
72+
5373
// Scenario data model
5474
type Scenario struct {
5575
Model

database/permissions.go

+24
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,30 @@ func CheckSignalPermissions(c *gin.Context, operation CRUD) (bool, Signal) {
142142

143143
}
144144

145+
func CheckUserGroupPermissions(c *gin.Context, operation CRUD, userGroupSourceID string, dabIDBody int) (bool, UserGroup) {
146+
147+
var usrgrp UserGroup
148+
149+
err := ValidateRole(c, ModelUserGroup, operation)
150+
if err != nil {
151+
helper.UnprocessableEntityError(c, fmt.Sprintf("Access denied (role validation failed): %v", err.Error()))
152+
return false, usrgrp
153+
}
154+
155+
groupID, err := helper.GetIDOfElement(c, "usergroupID", userGroupSourceID, dabIDBody)
156+
if err != nil {
157+
return false, usrgrp
158+
}
159+
160+
db := GetDB()
161+
err = db.Find(&usrgrp, uint(groupID)).Error
162+
if helper.DBNotFoundError(c, err, strconv.Itoa(groupID), "UserGroup") {
163+
return false, usrgrp
164+
}
165+
166+
return true, usrgrp
167+
}
168+
145169
func CheckDashboardPermissions(c *gin.Context, operation CRUD, dabIDSource string, dabIDBody int) (bool, Dashboard) {
146170

147171
var dab Dashboard

database/roles.go

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package database
1919

2020
import (
2121
"fmt"
22+
2223
"github.com/gin-gonic/gin"
2324
)
2425

@@ -33,6 +34,7 @@ type ModelName string
3334
const ModelUser = ModelName("user")
3435
const ModelUsers = ModelName("users")
3536
const ModelScenario = ModelName("scenario")
37+
const ModelUserGroup = ModelName("usergroup")
3638
const ModelInfrastructureComponent = ModelName("ic")
3739
const ModelInfrastructureComponentAction = ModelName("icaction")
3840
const ModelDashboard = ModelName("dashboard")
@@ -73,6 +75,7 @@ var Roles = RoleActions{
7375
ModelUser: crud,
7476
ModelUsers: crud,
7577
ModelScenario: crud,
78+
ModelUserGroup: crud,
7679
ModelComponentConfiguration: crud,
7780
ModelInfrastructureComponent: crud,
7881
ModelInfrastructureComponentAction: crud,
@@ -86,6 +89,7 @@ var Roles = RoleActions{
8689
ModelUser: _ru_,
8790
ModelUsers: none,
8891
ModelScenario: crud,
92+
ModelUserGroup: _r__,
8993
ModelComponentConfiguration: crud,
9094
ModelInfrastructureComponent: _r__,
9195
ModelInfrastructureComponentAction: _ru_,
@@ -117,6 +121,7 @@ var Roles = RoleActions{
117121
ModelInfrastructureComponentAction: none,
118122
ModelUser: none,
119123
ModelUsers: none,
124+
ModelUserGroup: none,
120125
ModelSignal: none,
121126
ModelFile: _r__,
122127
ModelResult: none,

0 commit comments

Comments
 (0)