From 88cd7288f52c18f827664b733774c06d9edb88f8 Mon Sep 17 00:00:00 2001 From: Yang Luo Date: Sun, 15 Aug 2021 21:51:57 +0800 Subject: [PATCH] Add CheckUserPassword() API. --- auth/user.go | 20 ++++++++++++++------ auth/util.go | 12 ++++++------ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/auth/user.go b/auth/user.go index b373893..ee5f1af 100644 --- a/auth/user.go +++ b/auth/user.go @@ -38,7 +38,7 @@ type User struct { Name string `xorm:"varchar(100) notnull pk" json:"name"` CreatedTime string `xorm:"varchar(100)" json:"createdTime"` UpdatedTime string `xorm:"varchar(100)" json:"updatedTime"` - + Id string `xorm:"varchar(100)" json:"id"` Type string `xorm:"varchar(100)" json:"type"` Password string `xorm:"varchar(100)" json:"password"` @@ -57,7 +57,7 @@ type User struct { SignupApplication string `xorm:"varchar(100)" json:"signupApplication"` Hash string `xorm:"varchar(100)" json:"hash"` PreHash string `xorm:"varchar(100)" json:"preHash"` - + Github string `xorm:"varchar(100)" json:"github"` Google string `xorm:"varchar(100)" json:"google"` QQ string `xorm:"qq varchar(100)" json:"qq"` @@ -67,7 +67,7 @@ type User struct { Weibo string `xorm:"weibo varchar(100)" json:"weibo"` Gitee string `xorm:"gitee varchar(100)" json:"gitee"` LinkedIn string `xorm:"linkedin varchar(100)" json:"linkedin"` - + Properties map[string]string `json:"properties"` } @@ -112,13 +112,21 @@ func GetUser(name string) (*User, error) { } func UpdateUser(user User) (bool, error) { - return modifyUser("update-user", user) + _, affected, err := modifyUser("update-user", user) + return affected, err } func AddUser(user User) (bool, error) { - return modifyUser("add-user", user) + _, affected, err := modifyUser("add-user", user) + return affected, err } func DeleteUser(user User) (bool, error) { - return modifyUser("delete-user", user) + _, affected, err := modifyUser("delete-user", user) + return affected, err +} + +func CheckUserPassword(user User) (bool, error) { + response, _, err := modifyUser("delete-user", user) + return response.Status == "ok", err } diff --git a/auth/util.go b/auth/util.go index 624bb69..66d936b 100644 --- a/auth/util.go +++ b/auth/util.go @@ -54,7 +54,7 @@ func getBytes(url string) ([]byte, error) { // modifyUser is an encapsulation of user CUD(Create, Update, Delete) operations. // allowable values of parameter method are `add-user`, `update-user`, `delete-user`, // get one user information directly through the GetUser function. -func modifyUser(method string, user User) (bool, error) { +func modifyUser(method string, user User) (*Response, bool, error) { user.Owner = authConfig.OrganizationName url := fmt.Sprintf("%s/api/%s?id=%s/%s&clientId=%s&clientSecret=%s", authConfig.Endpoint, method, user.Owner, user.Name, authConfig.ClientId, authConfig.ClientSecret) @@ -65,7 +65,7 @@ func modifyUser(method string, user User) (bool, error) { resp, err := http.Post(url, "text/plain;charset=UTF-8", bytes.NewReader(userByte)) if err != nil { - return false, err + return nil, false, err } defer func(Body io.ReadCloser) { err := Body.Close() @@ -76,17 +76,17 @@ func modifyUser(method string, user User) (bool, error) { respByte, err := ioutil.ReadAll(resp.Body) if err != nil { - return false, err + return nil, false, err } var response Response err = json.Unmarshal(respByte, &response) if err != nil { - return false, err + return nil, false, err } if response.Data == "Affected" { - return true, nil + return &response, true, nil } - return false, nil + return &response, false, nil }