Added Group node

graph-rework-2
noah metz 2023-07-25 09:51:55 -06:00
parent 4e69ba19f6
commit b3f6ea67c9
5 changed files with 77 additions and 23 deletions

@ -195,6 +195,10 @@ func NewContext(db * badger.DB, log Logger) * Context {
if err != nil { if err != nil {
panic(err) panic(err)
} }
err = ctx.RegisterNodeType(NewNodeDef((*Group)(nil), LoadGroup, GQLTypeSimpleLockable.Type))
if err != nil {
panic(err)
}
err = ctx.RegisterNodeType(NewNodeDef((*PerNodePolicy)(nil), LoadPerNodePolicy, GQLTypeSimpleNode.Type)) err = ctx.RegisterNodeType(NewNodeDef((*PerNodePolicy)(nil), LoadPerNodePolicy, GQLTypeSimpleNode.Type))
if err != nil { if err != nil {
panic(err) panic(err)

@ -289,13 +289,13 @@ func GQLLockableOwner(p graphql.ResolveParams) (interface{}, error) {
return owner, nil return owner, nil
} }
func GQLUserNodeUsers(p graphql.ResolveParams) (interface{}, error) { func GQLGroupNodeUsers(p graphql.ResolveParams) (interface{}, error) {
ctx, err := PrepResolve(p) ctx, err := PrepResolve(p)
if err != nil { if err != nil {
return nil, err return nil, err
} }
node, ok := p.Source.(NodeWithUsers) node, ok := p.Source.(GroupNode)
if ok == false || node == nil { if ok == false || node == nil {
return nil, fmt.Errorf("Failed to cast source to GQLThread") return nil, fmt.Errorf("Failed to cast source to GQLThread")
} }

@ -92,7 +92,7 @@ var GQLTypeGQLThread = NewSingleton(func() *graphql.Object {
gql_type_gql_thread.AddFieldConfig("Users", &graphql.Field{ gql_type_gql_thread.AddFieldConfig("Users", &graphql.Field{
Type: GQLTypeUser.List, Type: GQLTypeUser.List,
Resolve: GQLUserNodeUsers, Resolve: GQLGroupNodeUsers,
}) })
gql_type_gql_thread.AddFieldConfig("Listen", &graphql.Field{ gql_type_gql_thread.AddFieldConfig("Listen", &graphql.Field{

@ -284,7 +284,7 @@ func (policy *ChildrenPolicy) Allows(context *StateContext, node Node, resource
type UserOfPolicy struct { type UserOfPolicy struct {
SimplePolicy SimplePolicy
Target NodeWithUsers Target GroupNode
} }
type UserOfPolicyJSON struct { type UserOfPolicyJSON struct {
@ -329,9 +329,9 @@ var LoadUserOfPolicy = LoadJSONNode(func(id NodeID, j UserOfPolicyJSON) (Node, e
return err return err
} }
target, ok := target_node.(NodeWithUsers) target, ok := target_node.(GroupNode)
if ok == false { if ok == false {
return fmt.Errorf("%s is not a NodeWithUsers", target_node.ID()) return fmt.Errorf("%s is not a GroupNode", target_node.ID())
} }
policy.Target = target policy.Target = target
return nil return nil

@ -8,7 +8,7 @@ import (
"crypto/x509" "crypto/x509"
) )
type NodeWithUsers interface { type GroupNode interface {
Node Node
Users() map[NodeID]*User Users() map[NodeID]*User
} }
@ -48,14 +48,7 @@ func (user *User) Serialize() ([]byte, error) {
}, "", " ") }, "", " ")
} }
func LoadUser(ctx *Context, id NodeID, data []byte, nodes NodeMap) (Node, error) { var LoadUser = LoadJSONNode(func(id NodeID, j UserJSON) (Node, error) {
ctx.Log.Logf("test", "LOADING_USER: %s", id)
var j UserJSON
err := json.Unmarshal(data, &j)
if err != nil {
return nil, err
}
pub, err := x509.ParsePKIXPublicKey(j.Pubkey) pub, err := x509.ParsePKIXPublicKey(j.Pubkey)
if err != nil { if err != nil {
return nil, err return nil, err
@ -70,15 +63,10 @@ func LoadUser(ctx *Context, id NodeID, data []byte, nodes NodeMap) (Node, error)
} }
user := NewUser(j.Name, j.Granted, pubkey, j.Shared) user := NewUser(j.Name, j.Granted, pubkey, j.Shared)
nodes[id] = &user
err = RestoreLockable(ctx, &user.Lockable, j.LockableJSON, nodes)
if err != nil {
return nil, err
}
return &user, nil return &user, nil
} }, func(ctx *Context, user *User, j UserJSON, nodes NodeMap) error {
return RestoreLockable(ctx, user, j.LockableJSON, nodes)
})
func NewUser(name string, granted time.Time, pubkey *ecdsa.PublicKey, shared []byte) User { func NewUser(name string, granted time.Time, pubkey *ecdsa.PublicKey, shared []byte) User {
id := KeyID(pubkey) id := KeyID(pubkey)
@ -89,3 +77,65 @@ func NewUser(name string, granted time.Time, pubkey *ecdsa.PublicKey, shared []b
Shared: shared, Shared: shared,
} }
} }
type Group struct {
Lockable
UserMap map[NodeID]*User
}
func NewGroup(id NodeID, name string) Group {
return Group{
Lockable: NewLockable(id, name),
UserMap: map[NodeID]*User{},
}
}
type GroupJSON struct {
LockableJSON
Users []string `json:"users"`
}
func (group *Group) Type() NodeType {
return NodeType("group")
}
func (group *Group) Serialize() ([]byte, error) {
users := make([]string, len(group.UserMap))
i := 0
for id, _ := range(group.UserMap) {
users[i] = id.String()
i += 1
}
return json.MarshalIndent(&GroupJSON{
LockableJSON: NewLockableJSON(&group.Lockable),
Users: users,
}, "", " ")
}
var LoadGroup = LoadJSONNode(func(id NodeID, j GroupJSON) (Node, error) {
group := NewGroup(id, j.Name)
return &group, nil
}, func(ctx *Context, group *Group, j GroupJSON, nodes NodeMap) error {
for _, id_str := range(j.Users) {
id, err := ParseID(id_str)
if err != nil {
return err
}
user_node, err := LoadNodeRecurse(ctx, id, nodes)
if err != nil {
return err
}
user, ok := user_node.(*User)
if ok == false {
return fmt.Errorf("%s is not a *User", id_str)
}
group.UserMap[id] = user
}
return RestoreLockable(ctx, group, j.LockableJSON, nodes)
})