|
1 | 1 | package adapters
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
| 5 | + |
4 | 6 | "github.com/leesper/couchdb-golang"
|
5 | 7 | )
|
6 | 8 |
|
| 9 | +type securityConfig struct{ |
| 10 | + names []string |
| 11 | + roles []string |
| 12 | +} |
| 13 | + |
7 | 14 | type couchdbAdapter struct {
|
8 | 15 | db *couchdb.Server
|
9 | 16 | }
|
10 | 17 |
|
11 | 18 | func (adapter couchdbAdapter) HasDatabase(database string) (bool, error) {
|
12 |
| - return false, nil |
| 19 | + return adapter.db.Contains(database), nil |
13 | 20 | }
|
14 | 21 |
|
15 | 22 | func (adapter couchdbAdapter) CreateDatabase(database string) error {
|
16 |
| - // TODO create if not exists |
17 | 23 | _, err := adapter.db.Create(database)
|
18 | 24 | return err
|
19 | 25 | }
|
20 | 26 |
|
21 | 27 | func (adapter couchdbAdapter) DeleteDatabase(database string) error {
|
22 |
| - // TODO delete if exists |
23 | 28 | return adapter.db.Delete(database)
|
24 | 29 | }
|
25 | 30 |
|
| 31 | +func (adapter couchdbAdapter) getDatabaseAdmins(database string) ([]string, error) { |
| 32 | + db, err := adapter.db.Get(database) |
| 33 | + if err != nil { |
| 34 | + return nil, err |
| 35 | + } |
| 36 | + |
| 37 | + sc, err := db.GetSecurity() |
| 38 | + if err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + |
| 42 | + admins, ok := sc["admins"].(securityConfig) |
| 43 | + if !ok { |
| 44 | + return nil, fmt.Errorf("can't find admin users in security context") |
| 45 | + } |
| 46 | + |
| 47 | + return admins.names, nil |
| 48 | +} |
| 49 | + |
26 | 50 | func (adapter couchdbAdapter) HasDatabaseUserWithAccess(username string, database string) (bool, error) {
|
27 |
| - // TODO implement |
| 51 | + admins, err := adapter.getDatabaseAdmins(database) |
| 52 | + if err != nil { |
| 53 | + return false, err |
| 54 | + } |
| 55 | + |
| 56 | + for _, name := range admins { |
| 57 | + if name == username { |
| 58 | + return true, nil |
| 59 | + } |
| 60 | + } |
| 61 | + |
28 | 62 | return false, nil
|
29 | 63 | }
|
30 | 64 |
|
31 | 65 | func (adapter couchdbAdapter) CreateDatabaseUser(database string, username string, password string) error {
|
32 |
| - // TODO implement |
33 |
| - return nil |
| 66 | + adapter.db.AddUser(username, password, nil) |
| 67 | + db, err := adapter.db.Get(database) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + sc, err := db.GetSecurity() |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + admins, err := adapter.getDatabaseAdmins(database) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + sc["admins"] = &securityConfig{ |
| 83 | + names: append(admins, username), |
| 84 | + roles: nil, |
| 85 | + } |
| 86 | + |
| 87 | + return db.SetSecurity(sc) |
34 | 88 | }
|
35 | 89 |
|
36 | 90 | func (adapter couchdbAdapter) DeleteDatabaseUser(database string, username string) error {
|
37 |
| - // TODO implement |
38 |
| - return nil |
| 91 | + return adapter.db.RemoveUser(username) |
39 | 92 | }
|
40 | 93 |
|
41 | 94 | func (adapter couchdbAdapter) Close() error {
|
42 |
| - // TODO implement |
| 95 | + // couchdb is using single http calls and has no active connection we could close |
43 | 96 | return nil
|
44 | 97 | }
|
45 | 98 |
|
|
0 commit comments