Skip to content

Commit

Permalink
Add CheckUserPassword() API.
Browse files Browse the repository at this point in the history
  • Loading branch information
hsluoyz committed Aug 15, 2021
1 parent 1eaa4a0 commit 88cd728
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
20 changes: 14 additions & 6 deletions auth/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"`
Expand All @@ -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"`
}

Expand Down Expand Up @@ -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
}
12 changes: 6 additions & 6 deletions auth/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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
}

0 comments on commit 88cd728

Please sign in to comment.