github.com/cs3org/reva/v2@v2.27.7/pkg/siteacc/manager/gocdb/account.go (about) 1 // Copyright 2018-2020 CERN 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package gocdb 20 21 import ( 22 "bytes" 23 "encoding/json" 24 "io" 25 "net/http" 26 27 "github.com/cs3org/reva/v2/pkg/mentix/utils/network" 28 "github.com/cs3org/reva/v2/pkg/siteacc/data" 29 "github.com/pkg/errors" 30 ) 31 32 const ( 33 opCreateOrUpdate = "CreateOrUpdate" 34 opDelete = "Delete" 35 ) 36 37 type writeAccountUserData struct { 38 Email string `json:"Email"` 39 FirstName string `json:"FirstName"` 40 LastName string `json:"LastName"` 41 PhoneNumber string `json:"PhoneNumber"` 42 } 43 44 type writeAccountData struct { 45 APIKey string `json:"APIKey"` 46 Operation string `json:"Operation"` 47 48 Data writeAccountUserData `json:"Data"` 49 } 50 51 func writeAccount(account *data.Account, operation string, address string, apiKey string) error { 52 // Fill in the data to send 53 userData := getWriteAccountData(account) 54 userData.APIKey = apiKey 55 userData.Operation = operation 56 57 // Send the data to the GOCDB endpoint 58 endpointURL, err := network.GenerateURL(address, "/ext/v1/user", network.URLParams{}) 59 if err != nil { 60 return errors.Wrap(err, "unable to generate the GOCDB URL") 61 } 62 63 jsonData, err := json.Marshal(userData) 64 if err != nil { 65 return errors.Wrap(err, "unable to marshal the user data") 66 } 67 68 req, err := http.NewRequest(http.MethodPost, endpointURL.String(), bytes.NewReader(jsonData)) 69 if err != nil { 70 return errors.Wrap(err, "unable to create HTTP request") 71 } 72 req.Header.Set("Content-Type", "application/json; charset=UTF-8") 73 74 resp, err := http.DefaultClient.Do(req) 75 if err != nil { 76 return errors.Wrap(err, "unable to send data to endpoint") 77 } 78 defer resp.Body.Close() 79 80 if resp.StatusCode >= 400 { 81 msg, _ := io.ReadAll(resp.Body) 82 return errors.Errorf("unable to perform request: %v", string(msg)) 83 } 84 85 return nil 86 } 87 88 func getWriteAccountData(account *data.Account) *writeAccountData { 89 return &writeAccountData{ 90 Data: writeAccountUserData{ 91 Email: account.Email, 92 FirstName: account.FirstName, 93 LastName: account.LastName, 94 PhoneNumber: account.PhoneNumber, 95 }, 96 } 97 }