Skip to content

Commit

Permalink
feat: add SetPassword API and expose DoPost API (#38)
Browse files Browse the repository at this point in the history
* feat: add `UpdateUserPassword`

* chore: rename

* feat: DoPost

* refactor: extract to common func
  • Loading branch information
qianxi0410 authored Aug 22, 2022
1 parent 55c9f7e commit 20e330f
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 17 deletions.
4 changes: 2 additions & 2 deletions casdoorsdk/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func AddApplication(application *Application) (bool, error) {
return false, err
}

resp, err := doPost("add-application", nil, postBytes, false)
resp, err := DoPost("add-application", nil, postBytes, false, false)
if err != nil {
return false, err
}
Expand All @@ -75,7 +75,7 @@ func DeleteApplication(name string) (bool, error) {
return false, err
}

resp, err := doPost("delete-application", nil, postBytes, false)
resp, err := DoPost("delete-application", nil, postBytes, false, false)
if err != nil {
return false, err
}
Expand Down
25 changes: 19 additions & 6 deletions casdoorsdk/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,31 @@ func DoGetBytesRaw(url string) ([]byte, error) {
return respBytes, nil
}

func doPost(action string, queryMap map[string]string, postBytes []byte, isFile bool) (*Response, error) {
func DoPost(action string, queryMap map[string]string, postBytes []byte, isForm, isFile bool) (*Response, error) {
client := &http.Client{}
url := GetUrl(action, queryMap)

var resp *http.Response
var err error
var contentType string
var body io.Reader
if isFile {
contentType, body, err = createForm(map[string][]byte{"file": postBytes})
if err != nil {
return nil, err
if isForm {
if isFile {
contentType, body, err = createFormFile(map[string][]byte{"file": postBytes})
if err != nil {
return nil, err
}
} else {
var params map[string]string
err = json.Unmarshal(postBytes, &params)
if err != nil {
return nil, err
}

contentType, body, err = createForm(params)
if err != nil {
return nil, err
}
}
} else {
contentType = "text/plain;charset=UTF-8"
Expand Down Expand Up @@ -174,7 +187,7 @@ func modifyUser(action string, user *User, columns []string) (*Response, bool, e
return nil, false, err
}

resp, err := doPost(action, queryMap, postBytes, false)
resp, err := DoPost(action, queryMap, postBytes, false, false)
if err != nil {
return nil, false, err
}
Expand Down
2 changes: 1 addition & 1 deletion casdoorsdk/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func SendEmail(title string, content string, sender string, receivers ...string)
return err
}

resp, err := doPost("send-email", nil, postBytes, false)
resp, err := DoPost("send-email", nil, postBytes, false, false)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions casdoorsdk/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func AddOrganization(organization *Organization) (bool, error) {
return false, err
}

resp, err := doPost("add-organization", nil, postBytes, false)
resp, err := DoPost("add-organization", nil, postBytes, false, false)
if err != nil {
return false, err
}
Expand All @@ -60,7 +60,7 @@ func DeleteOrganization(name string) (bool, error) {
return false, err
}

resp, err := doPost("delete-organization", nil, postBytes, false)
resp, err := DoPost("delete-organization", nil, postBytes, false, false)
if err != nil {
return false, err
}
Expand Down
2 changes: 1 addition & 1 deletion casdoorsdk/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func AddPermission(q Permission) (*Response, error) {
return nil, err
}

resp, err := doPost("add-permission", nil, postBytes, false)
resp, err := DoPost("add-permission", nil, postBytes, false, false)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions casdoorsdk/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func UploadResource(user string, tag string, parent string, fullFilePath string,
"fullFilePath": fullFilePath,
}

resp, err := doPost("upload-resource", queryMap, fileBytes, true)
resp, err := DoPost("upload-resource", queryMap, fileBytes, true, true)
if err != nil {
return "", "", err
}
Expand All @@ -61,7 +61,7 @@ func UploadResourceEx(user string, tag string, parent string, fullFilePath strin
"description": description,
}

resp, err := doPost("upload-resource", queryMap, fileBytes, true)
resp, err := DoPost("upload-resource", queryMap, fileBytes, true, true)
if err != nil {
return "", "", err
}
Expand All @@ -85,7 +85,7 @@ func DeleteResource(name string) (bool, error) {
return false, err
}

resp, err := doPost("delete-resource", nil, postBytes, false)
resp, err := DoPost("delete-resource", nil, postBytes, false, false)
if err != nil {
return false, err
}
Expand Down
2 changes: 1 addition & 1 deletion casdoorsdk/sms.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func SendSms(content string, receivers ...string) error {
return err
}

resp, err := doPost("send-sms", nil, postBytes, false)
resp, err := DoPost("send-sms", nil, postBytes, false, false)
if err != nil {
return err
}
Expand Down
22 changes: 22 additions & 0 deletions casdoorsdk/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,28 @@ func GetUserByUserId(userId string) (*User, error) {
return user, nil
}

// note: oldPassword is not required, if you don't need, just pass a empty string
func SetPassword(owner, name, oldPassword, newPassword string) (bool, error) {
param := map[string]string{
"userOwner": owner,
"userName": name,
"oldPassword": oldPassword,
"newPassword": newPassword,
}

bytes, err := json.Marshal(param)
if err != nil {
return false, err
}

resp, err := DoPost("set-password", nil, bytes, true, false)
if err != nil {
return false, err
}

return resp.Status == "ok", nil
}

func UpdateUser(user *User) (bool, error) {
_, affected, err := modifyUser("update-user", user, nil)
return affected, err
Expand Down
13 changes: 12 additions & 1 deletion casdoorsdk/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func GetUrl(action string, queryMap map[string]string) string {
return url
}

func createForm(formData map[string][]byte) (string, io.Reader, error) {
func createFormFile(formData map[string][]byte) (string, io.Reader, error) {
// https://tonybai.com/2021/01/16/upload-and-download-file-using-multipart-form-over-http/

body := new(bytes.Buffer)
Expand All @@ -54,3 +54,14 @@ func createForm(formData map[string][]byte) (string, io.Reader, error) {

return w.FormDataContentType(), body, nil
}

func createForm(formData map[string]string) (string, io.Reader, error) {
body := new(bytes.Buffer)
w := multipart.NewWriter(body)
for k, v := range formData {
w.WriteField(k, v)
}
w.Close()

return w.FormDataContentType(), body, nil
}

0 comments on commit 20e330f

Please sign in to comment.