Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to add a user? #193

Closed
SerkanSipahi opened this issue Aug 30, 2017 · 10 comments
Closed

how to add a user? #193

SerkanSipahi opened this issue Aug 30, 2017 · 10 comments

Comments

@SerkanSipahi
Copy link

SerkanSipahi commented Aug 30, 2017

1.) This is not a bug or issue, its just a question for how to add a user to "org.couchdb.user:myuser" ! i found nothing about it in yours test.
2.) What happens e.g. when kivik not supporting to addUser? what to do then? maybe something like this: https://github.com/flimzy/kivik/blob/8bd551c52ecc2918bb2e9eb48969a578e791d1c7/serve/couchserver/session_test.go#L23 but in this case httptest.NewRequest("PUT", "/_users", nil).... ?!?
There is an example for getUser https://github.com/flimzy/kivik/blob/987ab2b4511ca6e42961c48a29e899978d21bce9/authdb/usersdb/usersdb.go#L36 but not for addUser
3.) Why do I ask so many questions? Because im very new with golang (since 9 month) and just 1 month with couchdb.

I hope you'll understand :( please show me a way :)

@flimzy
Copy link
Member

flimzy commented Aug 30, 2017

Kivik does not currently support user manipulation (adding, updating, or deleting users). I have always handled this through the management interface (Fauxton, etc). But it's probably a good idea to add support to Kivik, too.

@SerkanSipahi
Copy link
Author

@flimzy How do you handle features they are not supported in kivik? maybe like this? in very plain style?

type User struct {
	Name     string        `json:"name"`
	Password string        `json:"password"`
	Roles    []interface{} `json:"roles"`
	Type     string        `json:"type"`
}

data := User{
	// fill struct
}
userBytes, err := json.Marshal(data)
if err != nil {
	// handle err
}
body := bytes.NewReader(userBytes)

req, err := http.NewRequest("PUT", "http://localhost:5984/_users/org.couchdb.user"+data.Name, body)
if err != nil {
	// handle err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
if err != nil {
	// handle err
}
defer resp.Body.Close()

@flimzy
Copy link
Member

flimzy commented Aug 30, 2017

Yes, that's a reasonable approach as a stop-gap, until I have time to add user management to Kivik. :) I'll try to get around to adding that soon, but it may be a week or two before I have time.

@SerkanSipahi
Copy link
Author

@flimzy ok thank you. Let me know when it implemented :)

@flimzy
Copy link
Member

flimzy commented Aug 31, 2017

Please forgive my hasty response earlier; adding a standard user is quite possible with Kivik, as users are stored as standard documents. It is creation of admin users which is not currently possible.

For your use case, just do something like this:

	client, _ := kivik.New(context.TODO(), "couch", "http://localhost:5984/")
	usersDB, _ := client.DB(context.TODO(), "_users")
	user := map[string]interface{}{
		"_id":      kivik.UserPrefix + "username",
		"type":     "user",
		"password": "abc123",
	}
	_, _ = usersDB.Put(context.TODO(), kivik.UserPrefix+"username", user)

@SerkanSipahi
Copy link
Author

my quote:

please show me a way :)

this is a way "for me" how to handle features thats not implemented in kivik ! thank you very much :)

@flimzy
Copy link
Member

flimzy commented Aug 31, 2017

Sorry it took me a while to catch up... I guess I was sleeping when I responded yesterday. :)

I'll add this example to the wiki before I close this issue.

@SerkanSipahi
Copy link
Author

Sorry it took me a while to catch up... I guess I was sleeping when I responded yesterday. :)

lol

I'll add this example to the wiki before I close this issue.

I think this will help many developers. Your example is very useful for many cases. Its reduce my example to few lines of code :)

@flimzy
Copy link
Member

flimzy commented Aug 31, 2017

The wiki is now updated.

@SerkanSipahi
Copy link
Author

thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants