|
|
|
@ -8,7 +8,7 @@ import (
|
|
|
|
|
"crypto/x509"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type NodeWithUsers interface {
|
|
|
|
|
type GroupNode interface {
|
|
|
|
|
Node
|
|
|
|
|
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) {
|
|
|
|
|
ctx.Log.Logf("test", "LOADING_USER: %s", id)
|
|
|
|
|
var j UserJSON
|
|
|
|
|
err := json.Unmarshal(data, &j)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var LoadUser = LoadJSONNode(func(id NodeID, j UserJSON) (Node, error) {
|
|
|
|
|
pub, err := x509.ParsePKIXPublicKey(j.Pubkey)
|
|
|
|
|
if err != nil {
|
|
|
|
|
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)
|
|
|
|
|
nodes[id] = &user
|
|
|
|
|
|
|
|
|
|
err = RestoreLockable(ctx, &user.Lockable, j.LockableJSON, nodes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
id := KeyID(pubkey)
|
|
|
|
@ -89,3 +77,65 @@ func NewUser(name string, granted time.Time, pubkey *ecdsa.PublicKey, shared []b
|
|
|
|
|
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)
|
|
|
|
|
})
|
|
|
|
|