From 98922c8d9aeec24b9083654c27a618a3f0127833 Mon Sep 17 00:00:00 2001 From: Philippe-Adrien Nousse Date: Thu, 5 Jul 2018 15:01:25 +0200 Subject: [PATCH] Add user update/enable/disable/welcome-mail --- README.md | 34 ++++++++++++-- client/users.go | 118 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 146 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8de4b5b..7670fa7 100644 --- a/README.md +++ b/README.md @@ -11,17 +11,17 @@ A simple Client for Nextcloud's API in Go. - ~~list~~ - ~~get infos~~ - ~~create~~ - - update + - ~~update~~ - ~~delete~~ - - enable - - disable + - ~~enable~~ + - ~~disable~~ - get groups - add to group - remove from group - get subadmin group - promote subadmin - demote subadmin - - send welcome mail + - ~~send welcome mail~~ - [Groups](#groups) - create - delete @@ -78,6 +78,32 @@ Delete ```go func (c *Client) UserDelete(name string) error ``` +Enable +```go +func (c *Client) UserEnable(name string) error +``` +Disable +```go +func (c *Client) UserDisable(name string) error +``` +Update +```go +func (c *Client) UserUpdateEmail(name string, email string) error + +func (c *Client) UserUpdateDisplayName(name string, displayName string) error + +func (c *Client) UserUpdatePhone(name string, phone string) error + +func (c *Client) UserUpdateAddress(name string, address string) error + +func (c *Client) UserUpdateWebSite(name string, website string) error + +func (c *Client) UserUpdateTwitter(name string, twitter string) error + +func (c *Client) UserUpdatePassword(name string, password string) error + +func (c *Client) UserUpdateQuota(name string, quota string) error +``` ## Groups TODO diff --git a/client/users.go b/client/users.go index f5b91d8..b4a5542 100644 --- a/client/users.go +++ b/client/users.go @@ -74,7 +74,6 @@ func (c *Client) UserCreate(username string, password string) error { if err != nil { return err } - fmt.Println(res.String()) var r types.UserResponse res.JSON(&r) if r.Ocs.Meta.Statuscode != 100 { @@ -94,7 +93,122 @@ func (c *Client) UserDelete(name string) error { return err } var ur types.UserResponse - fmt.Println(res.String()) + res.JSON(&ur) + if ur.Ocs.Meta.Statuscode != 100 { + return fmt.Errorf(ur.Ocs.Meta.Message) + } + return nil +} + +func (c *Client) UserEnable(name string) error { + if !c.loggedIn() { + return unauthorized + } + u := c.baseURL.ResolveReference(routes.users) + u.Path = path.Join(u.Path, name, "enable") + ro := &req.RequestOptions{ + Data: map[string]string{}, + } + res, err := c.session.Put(u.String(), ro) + if err != nil { + return err + } + var ur types.UserResponse + res.JSON(&ur) + if ur.Ocs.Meta.Statuscode != 100 { + return fmt.Errorf(ur.Ocs.Meta.Message) + } + return nil +} + +func (c *Client) UserDisable(name string) error { + if !c.loggedIn() { + return unauthorized + } + u := c.baseURL.ResolveReference(routes.users) + u.Path = path.Join(u.Path, name, "disable") + ro := &req.RequestOptions{ + Data: map[string]string{}, + } + res, err := c.session.Put(u.String(), ro) + if err != nil { + return err + } + var ur types.UserResponse + res.JSON(&ur) + if ur.Ocs.Meta.Statuscode != 100 { + return fmt.Errorf(ur.Ocs.Meta.Message) + } + return nil +} + +func (c *Client) UserSendWelcomeEmail(name string) error { + if !c.loggedIn() { + return unauthorized + } + u := c.baseURL.ResolveReference(routes.users) + u.Path = path.Join(u.Path, name, "welcome") + res, err := c.session.Post(u.String(), nil) + if err != nil { + return err + } + var ur types.UserResponse + res.JSON(&ur) + if ur.Ocs.Meta.Statuscode != 100 { + return fmt.Errorf(ur.Ocs.Meta.Message) + } + return nil +} + +func (c *Client) UserUpdateEmail(name string, email string) error { + return c.userUpdateAttribute(name, "email", email) +} + +func (c *Client) UserUpdateDisplayName(name string, displayName string) error { + return c.userUpdateAttribute(name, "displayname", displayName) +} + +func (c *Client) UserUpdatePhone(name string, phone string) error { + return c.userUpdateAttribute(name, "phone", phone) +} + +func (c *Client) UserUpdateAddress(name string, address string) error { + return c.userUpdateAttribute(name, "address", address) +} + +func (c *Client) UserUpdateWebSite(name string, website string) error { + return c.userUpdateAttribute(name, "website", website) +} + +func (c *Client) UserUpdateTwitter(name string, twitter string) error { + return c.userUpdateAttribute(name, "twitter", twitter) +} + +func (c *Client) UserUpdatePassword(name string, password string) error { + return c.userUpdateAttribute(name, "password", password) +} + +func (c *Client) UserUpdateQuota(name string, quota string) error { + return c.userUpdateAttribute(name, "quota", quota) +} + +func (c *Client) userUpdateAttribute(name string, key string, value string) error { + if !c.loggedIn() { + return unauthorized + } + u := c.baseURL.ResolveReference(routes.users) + u.Path = path.Join(u.Path, name) + ro := &req.RequestOptions{ + Data: map[string]string{ + "key": key, + "value": value, + }, + } + res, err := c.session.Put(u.String(), ro) + if err != nil { + return err + } + var ur types.UserResponse res.JSON(&ur) if ur.Ocs.Meta.Statuscode != 100 { return fmt.Errorf(ur.Ocs.Meta.Message)