diff --git a/gql.go b/gql.go index 95d3e40..50c338b 100644 --- a/gql.go +++ b/gql.go @@ -197,7 +197,7 @@ func AuthHandler(ctx *Context, server *GQLThread) func(http.ResponseWriter, *htt key_id := KeyID(remote_id) - _, exists := server.Users[key_id] + _, exists := server.UserMap[key_id] if exists { ctx.Log.Logf("gql", "REFRESHING AUTH FOR %s", key_id) } else { @@ -215,7 +215,7 @@ func AuthHandler(ctx *Context, server *GQLThread) func(http.ResponseWriter, *htt Resources: nil, }, }), func(context *StateContext) error { - server.Users[key_id] = &new_user + server.UserMap[key_id] = &new_user return nil }) if err != nil { @@ -378,7 +378,7 @@ func NewResolveContext(ctx *Context, server *GQLThread, r *http.Request) (*Resol return nil, fmt.Errorf("GQL_REQUEST_ERR: failed to parse ID from auth username: %s", username) } - user, exists := server.Users[auth_id] + user, exists := server.UserMap[auth_id] if exists == false { return nil, fmt.Errorf("GQL_REQUEST_ERR: no existing authorization for client %s", auth_id) } @@ -636,7 +636,7 @@ type GQLThread struct { tls_key []byte tls_cert []byte Listen string - Users map[NodeID]*User + UserMap map[NodeID]*User Key *ecdsa.PrivateKey ECDH ecdh.Curve SubscribeLock sync.Mutex @@ -681,6 +681,10 @@ func (thread * GQLThread) Serialize() ([]byte, error) { return json.MarshalIndent(&thread_json, "", " ") } +func (thread * GQLThread) Users() map[NodeID]*User { + return thread.UserMap +} + type GQLThreadJSON struct { ThreadJSON Listen string `json:"listen"` @@ -715,9 +719,9 @@ func NewGQLThreadJSON(thread *GQLThread) GQLThreadJSON { panic(err) } - users := make([]string, len(thread.Users)) + users := make([]string, len(thread.UserMap)) i := 0 - for id, _ := range(thread.Users) { + for id, _ := range(thread.UserMap) { users[i] = id.String() i += 1 } @@ -747,7 +751,7 @@ var LoadGQLThread = LoadJSONNode(func(id NodeID, j GQLThreadJSON) (Node, error) thread := NewGQLThread(id, j.Name, j.StateName, j.Listen, ecdh_curve, key, j.TLSCert, j.TLSKey) return &thread, nil }, func(ctx *Context, thread *GQLThread, j GQLThreadJSON, nodes NodeMap) error { - thread.Users = map[NodeID]*User{} + thread.UserMap = map[NodeID]*User{} for _, id_str := range(j.Users) { ctx.Log.Logf("db", "THREAD_LOAD_USER: %s", id_str) user_id, err := ParseID(id_str) @@ -758,7 +762,7 @@ var LoadGQLThread = LoadJSONNode(func(id NodeID, j GQLThreadJSON) (Node, error) if err != nil { return err } - thread.Users[user_id] = user.(*User) + thread.UserMap[user_id] = user.(*User) } return RestoreThread(ctx, thread, j.ThreadJSON, nodes) @@ -808,7 +812,7 @@ func NewGQLThread(id NodeID, name string, state_name string, listen string, ecdh Thread: NewThread(id, name, state_name, []InfoType{"parent"}, gql_actions, gql_handlers), Listen: listen, SubscribeListeners: []chan GraphSignal{}, - Users: map[NodeID]*User{}, + UserMap: map[NodeID]*User{}, http_done: &sync.WaitGroup{}, Key: key, ECDH: ecdh_curve, diff --git a/gql_resolvers.go b/gql_resolvers.go index 148e7a0..ea3fb8d 100644 --- a/gql_resolvers.go +++ b/gql_resolvers.go @@ -289,13 +289,13 @@ func GQLLockableOwner(p graphql.ResolveParams) (interface{}, error) { return owner, nil } -func GQLThreadUsers(p graphql.ResolveParams) (interface{}, error) { +func GQLUserNodeUsers(p graphql.ResolveParams) (interface{}, error) { ctx, err := PrepResolve(p) if err != nil { return nil, err } - node, ok := p.Source.(*GQLThread) + node, ok := p.Source.(NodeWithUsers) if ok == false || node == nil { return nil, fmt.Errorf("Failed to cast source to GQLThread") } @@ -303,9 +303,9 @@ func GQLThreadUsers(p graphql.ResolveParams) (interface{}, error) { var users []*User context := NewReadContext(ctx.Context) err = UseStates(context, ctx.User, NewLockInfo(node, []string{"users"}), func(context *StateContext) error { - users = make([]*User, len(node.Users)) + users = make([]*User, len(node.Users())) i := 0 - for _, user := range(node.Users) { + for _, user := range(node.Users()) { users[i] = user i += 1 } diff --git a/gql_test.go b/gql_test.go index 480333a..57e6698 100644 --- a/gql_test.go +++ b/gql_test.go @@ -72,7 +72,7 @@ func TestGQLDBLoad(t * testing.T) { err = UpdateStates(context, &gql, NewLockMap( NewLockInfo(&gql, []string{"users"}), ), func(context *StateContext) error { - gql.Users[u1.ID()] = &u1 + gql.UserMap[u1.ID()] = &u1 err := LinkThreads(context, &gql, &gql, ChildInfo{&t1, map[InfoType]interface{}{ "parent": &info, @@ -116,7 +116,7 @@ func TestGQLDBLoad(t * testing.T) { ctx.Log.Logf("test", "\n%s\n\n", ser) dependency := gql_loaded.(*GQLThread).Thread.Dependencies[l1.ID()].(*Listener) l1_loaded = dependency - u_loaded := gql_loaded.(*GQLThread).Users[u1.ID()] + u_loaded := gql_loaded.(*GQLThread).UserMap[u1.ID()] err = UseStates(context, gql_loaded, NewLockInfo(u_loaded, nil), func(context *StateContext) error { ser, err := u_loaded.Serialize() ctx.Log.Logf("test", "\n%s\n\n", ser) diff --git a/gql_types.go b/gql_types.go index 4a88fc0..f921278 100644 --- a/gql_types.go +++ b/gql_types.go @@ -92,7 +92,7 @@ var GQLTypeGQLThread = NewSingleton(func() *graphql.Object { gql_type_gql_thread.AddFieldConfig("Users", &graphql.Field{ Type: GQLTypeUser.List, - Resolve: GQLThreadUsers, + Resolve: GQLUserNodeUsers, }) gql_type_gql_thread.AddFieldConfig("Listen", &graphql.Field{ diff --git a/user.go b/user.go index 02ac745..7839281 100644 --- a/user.go +++ b/user.go @@ -8,6 +8,11 @@ import ( "crypto/x509" ) +type NodeWithUsers interface { + Node + Users() map[NodeID]*User +} + type User struct { Lockable