3
0

upd(internal): rbac.Operations to rules.Operations

This commit is contained in:
Mitja Zivkovic
2019-01-25 13:45:13 +01:00
committed by Tit Petric
parent 8138766512
commit 4d57db5897
10 changed files with 125 additions and 98 deletions

View File

@@ -22,9 +22,9 @@ function permissions {
CGO_ENABLED=0 go build -o ./build/gen-permissions codegen/v2/permissions.go
fi
./build/gen-permissions -package types -function "func (c *Organisation) Permissions() []rbac.OperationGroup" -input sam/types/permissions/1-organisation.json -output sam/types/organisation.perms.go
./build/gen-permissions -package types -function "func (c *Team) Permissions() []rbac.OperationGroup" -input sam/types/permissions/2-team.json -output sam/types/team.perms.go
./build/gen-permissions -package types -function "func (c *Channel) Permissions() []rbac.OperationGroup" -input sam/types/permissions/3-channel.json -output sam/types/channel.perms.go
./build/gen-permissions -package types -function "func (c *Organisation) Permissions() []rules.OperationGroup" -input sam/types/permissions/1-organisation.json -output sam/types/organisation.perms.go
./build/gen-permissions -package types -function "func (c *Team) Permissions() []rules.OperationGroup" -input sam/types/permissions/2-team.json -output sam/types/team.perms.go
./build/gen-permissions -package types -function "func (c *Channel) Permissions() []rules.OperationGroup" -input sam/types/permissions/3-channel.json -output sam/types/channel.perms.go
green "OK"
}

View File

@@ -12,7 +12,7 @@ import (
"go/format"
"io/ioutil"
"github.com/crusttech/crust/internal/rbac"
"github.com/crusttech/crust/internal/rules"
)
func main() {
@@ -20,7 +20,7 @@ func main() {
pkg = flag.String("package", "main", "Package name")
input = flag.String("input", "", "Input .json filename")
output = flag.String("output", "", "Output .go filename")
fname = flag.String("function", "func Permissions() []rbac.OperationGroup", "Default function declaration")
fname = flag.String("function", "func Permissions() []rules.OperationGroup", "Default function declaration")
)
flag.Parse()
@@ -31,11 +31,15 @@ func main() {
s = strings.Replace(s, "}", ",\n}", -1)
s = strings.Replace(s, "\", ", "\",\n", -1)
s = strings.Replace(s, "Default:2,", "Default: rules.Allow,", -1)
s = strings.Replace(s, "Default:1,", "Default: rules.Deny,", -1)
s = strings.Replace(s, "Default:0,", "Default: rules.Inherit,", -1)
var w bytes.Buffer
fmt.Fprintln(&w, "package", *pkg)
fmt.Fprintln(&w)
fmt.Fprintln(&w, "import \"github.com/crusttech/crust/internal/rbac\"")
fmt.Fprintln(&w, "import \"github.com/crusttech/crust/internal/rules\"")
fmt.Fprintln(&w)
fmt.Fprintln(&w, "/* File is generated from", *input, "with permissions.go */")
fmt.Fprintln(&w)
@@ -52,7 +56,7 @@ func main() {
return fmtsrc
}
var result []rbac.OperationGroup
var result []rules.OperationGroup
f, err := os.Open(*input)
if err != nil {
log.Fatal(err)

View File

@@ -1,4 +1,8 @@
package rbac
package rules
import (
"github.com/crusttech/crust/system/types"
)
type (
OperationGroup struct {
@@ -19,11 +23,10 @@ type (
// nil = unset (inherit),
// true = checked (allow),
// false = unchecked (deny)
Default OperationState `json:"default"`
Default types.Access `json:"default"`
}
OperationState string
Access types.Access
Permission struct {
// Scope (organisation, team, channel)
@@ -33,12 +36,12 @@ type (
// Operation name (Operation.Key)
Operation string `json:"operation"`
// Operation state (inherit, allow, deny)
State OperationState `json:"state"`
State types.Access `json:"state"`
}
)
const (
OperationStateInherit OperationState = ""
OperationStateAllow = "allow"
OperationStateDeny = "deny"
Allow = types.Allow
Deny = types.Deny
Inherit = types.Inherit
)

View File

@@ -16,14 +16,6 @@ type resources struct {
db *factory.DB
}
type Access types.Access
var (
Allow = types.Allow
Deny = types.Deny
Inherit = types.Inherit
)
func NewResources(ctx context.Context, db *factory.DB) ResourcesInterface {
return (&resources{}).With(ctx, db)
}

View File

@@ -5,7 +5,7 @@ import (
"github.com/pkg/errors"
"github.com/crusttech/crust/internal/rbac"
"github.com/crusttech/crust/internal/rules"
"github.com/crusttech/crust/sam/repository"
)
@@ -20,7 +20,7 @@ type (
List() (interface{}, error)
Get(team string, scope string, resource string) (interface{}, error)
Set(team string, permissions []rbac.Permission) (interface{}, error)
Set(team string, permissions []rules.Permission) (interface{}, error)
}
)
@@ -44,6 +44,6 @@ func (p *permissions) Get(team string, scope string, resource string) (interface
return nil, errors.New("service.permissions.get: not implemented")
}
func (p *permissions) Set(team string, permissions []rbac.Permission) (interface{}, error) {
func (p *permissions) Set(team string, permissions []rules.Permission) (interface{}, error) {
return nil, errors.New("service.permissions.set: not implemented")
}

View File

@@ -1,55 +1,55 @@
package types
import "github.com/crusttech/crust/internal/rbac"
import "github.com/crusttech/crust/internal/rules"
/* File is generated from sam/types/permissions/3-channel.json with permissions.go */
func (c *Channel) Permissions() []rbac.OperationGroup {
return []rbac.OperationGroup{
rbac.OperationGroup{
func (c *Channel) Permissions() []rules.OperationGroup {
return []rules.OperationGroup{
rules.OperationGroup{
Title: "General permissions",
Operations: []rbac.Operation{
rbac.Operation{
Operations: []rules.Operation{
rules.Operation{
Key: "manage.webhooks",
Title: "Manage webhooks (@todo: implement webhooks)",
Subtitle: "Members with this permission can create, edit and delete webhooks",
Enabled: false,
Default: "",
Default: rules.Inherit,
},
},
}, rbac.OperationGroup{
}, rules.OperationGroup{
Title: "Text Permissions",
Operations: []rbac.Operation{
rbac.Operation{
Operations: []rules.Operation{
rules.Operation{
Key: "send",
Title: "Send Messages",
Subtitle: "",
Enabled: true,
Default: "",
}, rbac.Operation{
Default: rules.Inherit,
}, rules.Operation{
Key: "embed",
Title: "Embed Links",
Subtitle: "",
Enabled: true,
Default: "",
}, rbac.Operation{
Default: rules.Inherit,
}, rules.Operation{
Key: "attach",
Title: "Attach Files",
Subtitle: "",
Enabled: true,
Default: "",
}, rbac.Operation{
Default: rules.Inherit,
}, rules.Operation{
Key: "manage.messages",
Title: "Manage messages",
Subtitle: "Members with this permission can edit/delete messages inside this channel",
Enabled: true,
Default: "",
}, rbac.Operation{
Default: rules.Inherit,
}, rules.Operation{
Key: "react",
Title: "Manage reactions",
Subtitle: "Members with this permission can add new reactions to a message",
Enabled: true,
Default: "",
Default: rules.Inherit,
},
},
},

View File

@@ -1,85 +1,85 @@
package types
import "github.com/crusttech/crust/internal/rbac"
import "github.com/crusttech/crust/internal/rules"
/* File is generated from sam/types/permissions/1-organisation.json with permissions.go */
func (c *Organisation) Permissions() []rbac.OperationGroup {
return []rbac.OperationGroup{
rbac.OperationGroup{
func (c *Organisation) Permissions() []rules.OperationGroup {
return []rules.OperationGroup{
rules.OperationGroup{
Title: "General permissions",
Operations: []rbac.Operation{
rbac.Operation{
Operations: []rules.Operation{
rules.Operation{
Key: "admin",
Title: "Administrator",
Subtitle: "Members with this permission have every permission and also bypass channel specific permissions. Granting this permission is dangerous",
Enabled: true,
Default: "deny",
}, rbac.Operation{
Default: rules.Deny,
}, rules.Operation{
Key: "audit",
Title: "View Audit Log (@todo: add audit logs)",
Subtitle: "Members with this permission have access to view the servers audit logs",
Enabled: false,
Default: "deny",
}, rbac.Operation{
Default: rules.Deny,
}, rules.Operation{
Key: "manage.organisation",
Title: "Manage Organisation",
Subtitle: "Members with this permission can change the organisation name and other organisation details",
Enabled: true,
Default: "deny",
}, rbac.Operation{
Default: rules.Deny,
}, rules.Operation{
Key: "manage.roles",
Title: "Manage Roles",
Subtitle: "Members with this permission can create/edit/delete roles inside this organisation",
Enabled: true,
Default: "deny",
}, rbac.Operation{
Default: rules.Deny,
}, rules.Operation{
Key: "manage.channels",
Title: "Manage channels",
Subtitle: "Members with this permission can create/edit/delete channels inside this organisation",
Enabled: true,
Default: "deny",
}, rbac.Operation{
Default: rules.Deny,
}, rules.Operation{
Key: "manage.webhooks",
Title: "Manage webhooks (@todo: implement webhooks)",
Subtitle: "Members with this permission can create, edit and delete webhooks",
Enabled: false,
Default: "deny",
Default: rules.Deny,
},
},
}, rbac.OperationGroup{
}, rules.OperationGroup{
Title: "Text Permissions",
Operations: []rbac.Operation{
rbac.Operation{
Operations: []rules.Operation{
rules.Operation{
Key: "send",
Title: "Send Messages",
Subtitle: "",
Enabled: true,
Default: "allow",
}, rbac.Operation{
Default: rules.Allow,
}, rules.Operation{
Key: "embed",
Title: "Embed Links",
Subtitle: "",
Enabled: true,
Default: "allow",
}, rbac.Operation{
Default: rules.Allow,
}, rules.Operation{
Key: "attach",
Title: "Attach Files",
Subtitle: "",
Enabled: true,
Default: "allow",
}, rbac.Operation{
Default: rules.Allow,
}, rules.Operation{
Key: "manage.messages",
Title: "Manage messages",
Subtitle: "Members with this permission can edit/delete messages inside channels",
Enabled: true,
Default: "deny",
}, rbac.Operation{
Default: rules.Deny,
}, rules.Operation{
Key: "react",
Title: "Manage reactions",
Subtitle: "Members with this permission can add new reactions to a message",
Enabled: true,
Default: "allow",
Default: rules.Allow,
},
},
},

View File

@@ -1,7 +1,7 @@
package types
import (
"github.com/crusttech/crust/internal/rbac"
"github.com/crusttech/crust/internal/rules"
)
type (
@@ -9,7 +9,7 @@ type (
Scope() string
Resource() string
Operation(name string) string
Permissions() []rbac.OperationGroup
Permissions() []rules.OperationGroup
}
)

View File

@@ -1,55 +1,55 @@
package types
import "github.com/crusttech/crust/internal/rbac"
import "github.com/crusttech/crust/internal/rules"
/* File is generated from sam/types/permissions/2-team.json with permissions.go */
func (c *Team) Permissions() []rbac.OperationGroup {
return []rbac.OperationGroup{
rbac.OperationGroup{
func (c *Team) Permissions() []rules.OperationGroup {
return []rules.OperationGroup{
rules.OperationGroup{
Title: "General permissions",
Operations: []rbac.Operation{
rbac.Operation{
Operations: []rules.Operation{
rules.Operation{
Key: "manage.webhooks",
Title: "Manage webhooks (@todo: implement webhooks)",
Subtitle: "Members with this permission can create, edit and delete webhooks",
Enabled: false,
Default: "",
Default: rules.Inherit,
},
},
}, rbac.OperationGroup{
}, rules.OperationGroup{
Title: "Text Permissions",
Operations: []rbac.Operation{
rbac.Operation{
Operations: []rules.Operation{
rules.Operation{
Key: "send",
Title: "Send Messages",
Subtitle: "",
Enabled: true,
Default: "",
}, rbac.Operation{
Default: rules.Inherit,
}, rules.Operation{
Key: "embed",
Title: "Embed Links",
Subtitle: "",
Enabled: true,
Default: "",
}, rbac.Operation{
Default: rules.Inherit,
}, rules.Operation{
Key: "attach",
Title: "Attach Files",
Subtitle: "",
Enabled: true,
Default: "",
}, rbac.Operation{
Default: rules.Inherit,
}, rules.Operation{
Key: "manage.messages",
Title: "Manage messages",
Subtitle: "Members with this permission can edit/delete messages inside channels",
Enabled: true,
Default: "",
}, rbac.Operation{
Default: rules.Inherit,
}, rules.Operation{
Key: "react",
Title: "Manage reactions",
Subtitle: "Members with this permission can add new reactions to a message",
Enabled: true,
Default: "",
Default: rules.Inherit,
},
},
},

View File

@@ -1,11 +1,16 @@
package types
import (
"encoding/json"
"errors"
)
type Access int
const (
Allow Access = 1
Deny Access = 0
Inherit Access = -1
Allow Access = 2
Deny Access = 1
Inherit Access = 0
)
type Rules struct {
@@ -14,3 +19,26 @@ type Rules struct {
Operation string `db:"operation"`
Value Access `db:"value"`
}
func (a *Access) UnmarshalJSON(data []byte) error {
var i interface{}
err := json.Unmarshal(data, &i)
if err != nil {
return err
}
s, ok := i.(string)
if !ok {
return errors.New("Type assertion .(string) failed.")
}
switch s {
case "allow":
*a = Allow
case "deny":
*a = Deny
default:
*a = Inherit
}
return nil
}