Skip to content

Commit

Permalink
Add more details to distinct query and creation, also permission system
Browse files Browse the repository at this point in the history
  • Loading branch information
torabian committed Dec 24, 2024
1 parent 75da46c commit 1ac9fd5
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 77 deletions.
11 changes: 11 additions & 0 deletions modules/workspaces/AuthContextDto.dyno.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func CastAuthContextFromCli(c *cli.Context) *AuthContextDto {
value := c.String("token")
template.Token = &value
}
if c.IsSet("security-id") {
value := c.String("security-id")
template.SecurityId = &value
}
return template
}

Expand Down Expand Up @@ -56,12 +60,19 @@ var AuthContextDtoCommonCliFlagsOptional = []cli.Flag{
Required: false,
Usage: `token`,
},
&cli.StringFlag{
Name: "security-id",
Required: false,
Usage: `security`,
},
}

type AuthContextDto struct {
SkipWorkspaceId *bool `json:"skipWorkspaceId" yaml:"skipWorkspaceId" `
WorkspaceId *string `json:"workspaceId" yaml:"workspaceId" `
Token *string `json:"token" yaml:"token" `
Security *SecurityModel `json:"security" yaml:"security" gorm:"foreignKey:SecurityId;references:UniqueId" `
SecurityId *string `json:"securityId" yaml:"securityId"`
Capabilities []PermissionInfo `json:"capabilities" yaml:"capabilities" `
}
type AuthContextDtoList struct {
Expand Down
10 changes: 6 additions & 4 deletions modules/workspaces/NotificationConfigEntity.dyno.go
Original file line number Diff line number Diff line change
Expand Up @@ -1890,19 +1890,21 @@ func NotificationConfigDistinctActionUpdate(
) (*NotificationConfigEntity, *IError) {
query.UniqueId = query.UserId
entity, err := NotificationConfigActionGetOne(query)
// Because we are updating by workspace, the unique id and workspace id
// are important to be the same.
fields.UniqueId = query.WorkspaceId
fields.WorkspaceId = &query.WorkspaceId
if err != nil || entity.UniqueId == "" {
fields.UniqueId = query.UserId
return NotificationConfigActionCreateFn(fields, query)
} else {
fields.UniqueId = query.UniqueId
return NotificationConfigActionUpdateFn(query, fields)
}
}
func NotificationConfigDistinctActionGetOne(
query QueryDSL,
) (*NotificationConfigEntity, *IError) {
query.UniqueId = query.UserId
entity, err := NotificationConfigActionGetOne(query)
// Get's by workspace
entity, err := NotificationConfigActionGetByWorkspace(query)
if err != nil && err.HttpCode == 404 {
return &NotificationConfigEntity{}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion modules/workspaces/WorkspaceCli.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ var ViewAuthorize cli.Command = cli.Command{
fmt.Println("Workspace:", config.CliWorkspace)
fmt.Println("Token:", config.CliToken)

result, err := CliAuth()
result, err := CliAuth(nil)
if err != nil {
log.Fatalln(err)
} else {
Expand Down
10 changes: 6 additions & 4 deletions modules/workspaces/WorkspaceConfigEntity.dyno.go
Original file line number Diff line number Diff line change
Expand Up @@ -1353,19 +1353,21 @@ func WorkspaceConfigDistinctActionUpdate(
) (*WorkspaceConfigEntity, *IError) {
query.UniqueId = query.UserId
entity, err := WorkspaceConfigActionGetOne(query)
// Because we are updating by workspace, the unique id and workspace id
// are important to be the same.
fields.UniqueId = query.WorkspaceId
fields.WorkspaceId = &query.WorkspaceId
if err != nil || entity.UniqueId == "" {
fields.UniqueId = query.UserId
return WorkspaceConfigActionCreateFn(fields, query)
} else {
fields.UniqueId = query.UniqueId
return WorkspaceConfigActionUpdateFn(query, fields)
}
}
func WorkspaceConfigDistinctActionGetOne(
query QueryDSL,
) (*WorkspaceConfigEntity, *IError) {
query.UniqueId = query.UserId
entity, err := WorkspaceConfigActionGetOne(query)
// Get's by workspace
entity, err := WorkspaceConfigActionGetByWorkspace(query)
if err != nil && err.HttpCode == 404 {
return &WorkspaceConfigEntity{}, nil
}
Expand Down
5 changes: 5 additions & 0 deletions modules/workspaces/WorkspaceModule3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,9 @@ dtos:
type: string
- name: token
type: string
- name: security
type: one
target: SecurityModel
- name: capabilities
primitive: PermissionInfo
type: arrayP
Expand Down Expand Up @@ -1234,4 +1237,6 @@ messages:
en: File not found
validationFailedOnSomeFields:
en: Validation has failed on some fields
actionOnlyInRoot:
en: This action is only allowed when user belongs to root workspace, and root is selected
description: This is the fireback core module, which includes everything. In fact you could say workspaces is fireback itself. Maybe in the future that would be changed
6 changes: 6 additions & 0 deletions modules/workspaces/WorkspacesModule.dyno.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var ALL_PERM_WORKSPACES_MODULE = []PermissionInfo{
type workspacesCode string

const (
ActionOnlyInRoot workspacesCode = "ActionOnlyInRoot"
AlreadyConfirmed workspacesCode = "AlreadyConfirmed"
BodyIsMissing workspacesCode = "BodyIsMissing"
DataTypeDoesNotExistsInFireback workspacesCode = "DataTypeDoesNotExistsInFireback"
Expand Down Expand Up @@ -66,6 +67,10 @@ var WorkspacesMessages = newWorkspacesMessageCode()

func newWorkspacesMessageCode() *workspacesMsgs {
return &workspacesMsgs{
ActionOnlyInRoot: ErrorItem{
"$": "ActionOnlyInRoot",
"en": "This action is only allowed when user belongs to root workspace, and root is selected",
},
AlreadyConfirmed: ErrorItem{
"$": "AlreadyConfirmed",
"en": "Already confirmed",
Expand Down Expand Up @@ -208,6 +213,7 @@ func newWorkspacesMessageCode() *workspacesMsgs {
}

type workspacesMsgs struct {
ActionOnlyInRoot ErrorItem
AlreadyConfirmed ErrorItem
BodyIsMissing ErrorItem
DataTypeDoesNotExistsInFireback ErrorItem
Expand Down
36 changes: 27 additions & 9 deletions modules/workspaces/codegen/firebackgo/SharedSnippets.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -1370,21 +1370,38 @@ func {{ .e.Upper }}ActionWipeClean(query {{ .wsprefix }}QueryDSL) (int64, error)
query.UniqueId = query.UserId
entity, err := {{ .e.Upper }}ActionGetOne(query)

if err != nil || entity.UniqueId == "" {

{{ if or (eq .e.DistinctBy "workspace")}}
// Because we are updating by workspace, the unique id and workspace id
// are important to be the same.
fields.UniqueId = query.WorkspaceId
fields.WorkspaceId = &query.WorkspaceId
{{ else if (eq .e.DistinctBy "user" )}}
// It's distinct by user, then unique id and user needs to be equal
fields.UniqueId = query.UserId
fields.UserId = &query.UserId
{{ end }}


if err != nil || entity.UniqueId == "" {
return {{ .e.Upper }}ActionCreateFn(fields, query)
} else {
fields.UniqueId = query.UniqueId
return {{ .e.Upper }}ActionUpdateFn(query, fields)
}
}

func {{ .e.Upper }}DistinctActionGetOne(
query {{ .wsprefix }}QueryDSL,
) (*{{ .e.EntityName }}, *{{ .wsprefix }}IError) {
query.UniqueId = query.UserId
entity, err := {{ .e.Upper }}ActionGetOne(query)
{{ if or (eq .e.DistinctBy "workspace")}}
// Get's by workspace
entity, err := {{ .e.Upper }}ActionGetByWorkspace(query)
{{ else }}
// This needs to be fixed for distinct by user or workspace/user
query.UniqueId = query.UserId
entity, err := {{ .e.Upper }}ActionGetOne(query)
{{ end }}

if err != nil && err.HttpCode == 404 {
return &{{ .e.EntityName }}{}, nil
Expand Down Expand Up @@ -1778,6 +1795,7 @@ type x{{$prefix}}{{ .PublicName}} struct {
{{ define "entityCliCastRecursive" }}
{{ $fields := index . 0 }}
{{ $prefix := index . 1 }}
{{ $wsprefix := index . 2 }}

{{ range $fields }}

Expand Down Expand Up @@ -1826,7 +1844,7 @@ type x{{$prefix}}{{ .PublicName}} struct {
value := c.String("{{ $prefix }}{{ .ComputedCliName }}")
template.{{ .PublicName }}ListId = strings.Split(value, ",")
} {{ if endsWithDto .Target }} {{ else }} else {
template.{{ .PublicName }}ListId = CliInteractiveSearchAndSelect(
template.{{ .PublicName }}ListId = {{ $wsprefix }}CliInteractiveSearchAndSelect(
"Select {{ .PublicName }}",
{{ .PublicName }}ActionQueryString,
)
Expand Down Expand Up @@ -1855,7 +1873,7 @@ func Cast{{ .e.Upper }}FromCli (c *cli.Context) *{{ .e.ObjectName }} {
}
{{ end }}

{{ template "entityCliCastRecursive" (arr .e.CompleteFields "")}}
{{ template "entityCliCastRecursive" (arr .e.CompleteFields "" .wsprefix)}}

return template
}
Expand All @@ -1867,7 +1885,7 @@ func Cast{{ .e.Upper }}FromCli (c *cli.Context) *{{ .e.ObjectName }} {
func Cast{{ .e.Upper }}FromCli (c *cli.Context) *{{ .e.ObjectName }} {
template := &{{ .e.ObjectName }}{}

{{ template "entityCliCastRecursive" (arr .e.CompleteFields "")}}
{{ template "entityCliCastRecursive" (arr .e.CompleteFields "" .wsprefix)}}

return template
}
Expand Down Expand Up @@ -2902,7 +2920,7 @@ type {{ $name }}Msgs struct {
func Cast{{ .Upper }}FromCli (c *cli.Context) *{{ .Upper }}ActionReqDto {
template := &{{ .Upper }}ActionReqDto{}

{{ template "entityCliCastRecursive" (arr .In.Fields "")}}
{{ template "entityCliCastRecursive" (arr .In.Fields "" $wsprefix)}}

return template
}
Expand Down
Loading

0 comments on commit 1ac9fd5

Please sign in to comment.