github.com/go-kivik/kivik/v4@v4.3.2/x/memorydb/security.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 package memorydb 14 15 import ( 16 "context" 17 "errors" 18 "net/http" 19 20 "github.com/go-kivik/kivik/v4/driver" 21 ) 22 23 func cloneSecurity(in *driver.Security) *driver.Security { 24 return &driver.Security{ 25 Admins: driver.Members{ 26 Names: in.Admins.Names, 27 Roles: in.Admins.Roles, 28 }, 29 Members: driver.Members{ 30 Names: in.Members.Names, 31 Roles: in.Members.Roles, 32 }, 33 } 34 } 35 36 func (d *db) Security(ctx context.Context) (*driver.Security, error) { 37 if exists, _ := d.DBExists(ctx, d.dbName, nil); !exists { 38 return nil, statusError{status: http.StatusNotFound, error: errors.New("database does not exist")} 39 } 40 d.db.mu.RLock() 41 defer d.db.mu.RUnlock() 42 if d.db.deleted { 43 return nil, statusError{status: http.StatusNotFound, error: errors.New("missing")} 44 } 45 return cloneSecurity(d.db.security), nil 46 } 47 48 func (d *db) SetSecurity(_ context.Context, sec *driver.Security) error { 49 d.db.mu.Lock() 50 defer d.db.mu.Unlock() 51 if d.db.deleted { 52 return statusError{status: http.StatusNotFound, error: errors.New("missing")} 53 } 54 d.db.security = cloneSecurity(sec) 55 return nil 56 }