2018-07-25 13:06:31 +00:00
|
|
|
package gonextcloud
|
2018-07-05 10:50:56 +00:00
|
|
|
|
|
|
|
import (
|
2018-07-24 18:26:12 +00:00
|
|
|
"encoding/json"
|
2018-07-25 13:06:31 +00:00
|
|
|
"github.com/fatih/structs"
|
2018-07-05 10:50:56 +00:00
|
|
|
req "github.com/levigross/grequests"
|
2018-10-16 09:02:04 +00:00
|
|
|
"github.com/pkg/errors"
|
2018-10-08 16:50:11 +00:00
|
|
|
"gitlab.adphi.fr/partitio/Nextcloud-Partitio/gonextcloud/types"
|
2018-07-05 13:22:56 +00:00
|
|
|
"net/http"
|
2018-07-05 10:50:56 +00:00
|
|
|
"path"
|
2018-07-24 18:26:12 +00:00
|
|
|
"strconv"
|
2018-07-25 13:06:31 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2018-07-05 10:50:56 +00:00
|
|
|
)
|
|
|
|
|
2018-07-30 08:03:38 +00:00
|
|
|
// UserList return the Nextcloud'user list
|
2018-07-05 10:50:56 +00:00
|
|
|
func (c *Client) UserList() ([]string, error) {
|
2018-09-03 10:17:02 +00:00
|
|
|
res, err := c.baseRequest(http.MethodGet, routes.users, nil)
|
2018-07-25 13:06:31 +00:00
|
|
|
//res, err := c.session.Get(u.String(), nil)
|
2018-07-05 10:50:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-07-24 18:26:12 +00:00
|
|
|
var r types.UserListResponse
|
|
|
|
res.JSON(&r)
|
|
|
|
return r.Ocs.Data.Users, nil
|
2018-07-05 10:50:56 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 09:02:04 +00:00
|
|
|
//UserListDetails return a map of user with details
|
|
|
|
func (c *Client) UserListDetails() (map[string]types.User, error) {
|
|
|
|
res, err := c.baseRequest(http.MethodGet, routes.users, nil, "details")
|
|
|
|
//res, err := c.session.Get(u.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var r types.UserListDetailsResponse
|
|
|
|
res.JSON(&r)
|
|
|
|
return r.Ocs.Data.Users, nil
|
|
|
|
}
|
|
|
|
|
2018-07-30 08:03:38 +00:00
|
|
|
// User return the details about the specified user
|
2018-07-05 10:50:56 +00:00
|
|
|
func (c *Client) User(name string) (*types.User, error) {
|
2018-07-24 18:26:12 +00:00
|
|
|
if name == "" {
|
|
|
|
return nil, &types.APIError{Message: "name cannot be empty"}
|
|
|
|
}
|
2018-09-03 10:17:02 +00:00
|
|
|
res, err := c.baseRequest(http.MethodGet, routes.users, nil, name)
|
2018-07-05 10:50:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-07-24 18:26:12 +00:00
|
|
|
var r types.UserResponse
|
|
|
|
js := res.String()
|
|
|
|
// Nextcloud does not encode JSON properly
|
|
|
|
js = reformatJSON(js)
|
|
|
|
if err := json.Unmarshal([]byte(js), &r); err != nil {
|
|
|
|
return nil, err
|
2018-07-05 10:50:56 +00:00
|
|
|
}
|
2018-07-24 18:26:12 +00:00
|
|
|
return &r.Ocs.Data, nil
|
2018-07-05 10:50:56 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 08:03:38 +00:00
|
|
|
// UserSearch returns the users whose name match the search string
|
2018-07-05 10:50:56 +00:00
|
|
|
func (c *Client) UserSearch(search string) ([]string, error) {
|
|
|
|
ro := &req.RequestOptions{
|
|
|
|
Params: map[string]string{"search": search},
|
|
|
|
}
|
2018-09-03 10:17:02 +00:00
|
|
|
res, err := c.baseRequest(http.MethodGet, routes.users, ro)
|
2018-07-05 10:50:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var r types.UserListResponse
|
|
|
|
res.JSON(&r)
|
|
|
|
return r.Ocs.Data.Users, nil
|
|
|
|
}
|
|
|
|
|
2018-07-30 08:03:38 +00:00
|
|
|
// UserCreate create a new user
|
2018-07-25 13:06:31 +00:00
|
|
|
func (c *Client) UserCreate(username string, password string, user *types.User) error {
|
2018-10-16 07:34:56 +00:00
|
|
|
// Create base User
|
2018-07-05 10:50:56 +00:00
|
|
|
ro := &req.RequestOptions{
|
|
|
|
Data: map[string]string{
|
|
|
|
"userid": username,
|
|
|
|
"password": password,
|
|
|
|
},
|
|
|
|
}
|
2018-09-03 10:17:02 +00:00
|
|
|
if err := c.userBaseRequest(http.MethodPost, ro); err != nil {
|
2018-07-25 13:06:31 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-10-16 07:34:56 +00:00
|
|
|
// Check if we need to add user details information
|
2018-07-25 13:06:31 +00:00
|
|
|
if user == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-10-16 07:34:56 +00:00
|
|
|
// Add user details information
|
2018-07-25 13:06:31 +00:00
|
|
|
return c.UserUpdate(user)
|
2018-07-05 10:50:56 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 09:02:04 +00:00
|
|
|
// UserCreateWithoutPassword create a user without provisioning a password, the email address must be provided to send
|
|
|
|
// an init password email
|
|
|
|
func (c *Client) UserCreateWithoutPassword(username, email, displayName string) error {
|
|
|
|
if c.version.Major < 14 {
|
|
|
|
return errors.New("unsupported method: requires Nextcloud 14+")
|
|
|
|
}
|
|
|
|
if username == "" || email == "" {
|
|
|
|
return errors.New("username and email cannot be empty")
|
|
|
|
}
|
|
|
|
ro := &req.RequestOptions{
|
|
|
|
Data: map[string]string{
|
|
|
|
"userid": username,
|
|
|
|
"email": email,
|
|
|
|
"displayName": displayName,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.userBaseRequest(http.MethodPost, ro); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-30 08:03:38 +00:00
|
|
|
//UserDelete delete the user
|
2018-07-05 10:50:56 +00:00
|
|
|
func (c *Client) UserDelete(name string) error {
|
2018-09-03 10:17:02 +00:00
|
|
|
return c.userBaseRequest(http.MethodDelete, nil, name)
|
2018-07-05 13:01:25 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 08:03:38 +00:00
|
|
|
//UserEnable enables the user
|
2018-07-05 13:01:25 +00:00
|
|
|
func (c *Client) UserEnable(name string) error {
|
|
|
|
ro := &req.RequestOptions{
|
|
|
|
Data: map[string]string{},
|
|
|
|
}
|
2018-09-03 10:17:02 +00:00
|
|
|
return c.userBaseRequest(http.MethodPut, ro, name, "enable")
|
2018-07-05 13:01:25 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 08:03:38 +00:00
|
|
|
//UserDisable disables the user
|
2018-07-05 13:01:25 +00:00
|
|
|
func (c *Client) UserDisable(name string) error {
|
|
|
|
ro := &req.RequestOptions{
|
|
|
|
Data: map[string]string{},
|
|
|
|
}
|
2018-09-03 10:17:02 +00:00
|
|
|
return c.userBaseRequest(http.MethodPut, ro, name, "disable")
|
2018-07-05 13:01:25 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 08:03:38 +00:00
|
|
|
//UserSendWelcomeEmail (re)send the welcome mail to the user (return an error if the user has not configured his email)
|
2018-07-05 13:01:25 +00:00
|
|
|
func (c *Client) UserSendWelcomeEmail(name string) error {
|
2018-09-03 10:17:02 +00:00
|
|
|
return c.userBaseRequest(http.MethodPost, nil, name, "welcome")
|
2018-07-05 13:01:25 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 08:03:38 +00:00
|
|
|
//UserUpdate takes a *types.User struct to update the user's information
|
2018-07-25 13:06:31 +00:00
|
|
|
func (c *Client) UserUpdate(user *types.User) error {
|
|
|
|
m := structs.Map(user)
|
|
|
|
errs := make(chan types.UpdateError)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for k := range m {
|
|
|
|
if !ignoredUserField(k) && m[k].(string) != "" {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(key string, value string) {
|
|
|
|
defer wg.Done()
|
|
|
|
if err := c.userUpdateAttribute(user.ID, strings.ToLower(key), value); err != nil {
|
|
|
|
errs <- types.UpdateError{
|
|
|
|
Field: key,
|
|
|
|
Error: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(k, m[k].(string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(errs)
|
|
|
|
}()
|
|
|
|
return types.NewUpdateError(errs)
|
|
|
|
}
|
|
|
|
|
2018-07-30 08:03:38 +00:00
|
|
|
//UserUpdateEmail update the user's email
|
2018-07-05 13:01:25 +00:00
|
|
|
func (c *Client) UserUpdateEmail(name string, email string) error {
|
|
|
|
return c.userUpdateAttribute(name, "email", email)
|
|
|
|
}
|
|
|
|
|
2018-07-30 08:03:38 +00:00
|
|
|
//UserUpdateDisplayName update the user's display name
|
2018-07-05 13:01:25 +00:00
|
|
|
func (c *Client) UserUpdateDisplayName(name string, displayName string) error {
|
|
|
|
return c.userUpdateAttribute(name, "displayname", displayName)
|
|
|
|
}
|
|
|
|
|
2018-07-30 08:03:38 +00:00
|
|
|
//UserUpdatePhone update the user's phone
|
2018-07-05 13:01:25 +00:00
|
|
|
func (c *Client) UserUpdatePhone(name string, phone string) error {
|
|
|
|
return c.userUpdateAttribute(name, "phone", phone)
|
|
|
|
}
|
|
|
|
|
2018-07-30 08:03:38 +00:00
|
|
|
//UserUpdateAddress update the user's address
|
2018-07-05 13:01:25 +00:00
|
|
|
func (c *Client) UserUpdateAddress(name string, address string) error {
|
|
|
|
return c.userUpdateAttribute(name, "address", address)
|
|
|
|
}
|
|
|
|
|
2018-07-30 08:03:38 +00:00
|
|
|
//UserUpdateWebSite update the user's website
|
2018-07-05 13:01:25 +00:00
|
|
|
func (c *Client) UserUpdateWebSite(name string, website string) error {
|
|
|
|
return c.userUpdateAttribute(name, "website", website)
|
|
|
|
}
|
|
|
|
|
2018-07-30 08:03:38 +00:00
|
|
|
//UserUpdateTwitter update the user's twitter
|
2018-07-05 13:01:25 +00:00
|
|
|
func (c *Client) UserUpdateTwitter(name string, twitter string) error {
|
|
|
|
return c.userUpdateAttribute(name, "twitter", twitter)
|
|
|
|
}
|
|
|
|
|
2018-07-30 08:03:38 +00:00
|
|
|
//UserUpdatePassword update the user's password
|
2018-07-05 13:01:25 +00:00
|
|
|
func (c *Client) UserUpdatePassword(name string, password string) error {
|
|
|
|
return c.userUpdateAttribute(name, "password", password)
|
|
|
|
}
|
|
|
|
|
2018-07-30 08:03:38 +00:00
|
|
|
//UserUpdateQuota update the user's quota (bytes)
|
2018-07-24 18:26:12 +00:00
|
|
|
func (c *Client) UserUpdateQuota(name string, quota int) error {
|
|
|
|
return c.userUpdateAttribute(name, "quota", strconv.Itoa(quota))
|
2018-07-05 13:01:25 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 08:03:38 +00:00
|
|
|
//UserGroupList lists the user's groups
|
2018-07-05 14:15:51 +00:00
|
|
|
func (c *Client) UserGroupList(name string) ([]string, error) {
|
2018-09-03 10:17:02 +00:00
|
|
|
res, err := c.baseRequest(http.MethodGet, routes.users, nil, name, "groups")
|
2018-07-05 14:15:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var r types.GroupListResponse
|
|
|
|
res.JSON(&r)
|
|
|
|
return r.Ocs.Data.Groups, nil
|
|
|
|
}
|
|
|
|
|
2018-07-30 08:03:38 +00:00
|
|
|
//UserGroupAdd adds a the user to the group
|
2018-07-05 14:15:51 +00:00
|
|
|
func (c *Client) UserGroupAdd(name string, group string) error {
|
|
|
|
ro := &req.RequestOptions{
|
|
|
|
Data: map[string]string{
|
|
|
|
"groupid": group,
|
|
|
|
},
|
|
|
|
}
|
2018-09-03 10:17:02 +00:00
|
|
|
return c.userBaseRequest(http.MethodPost, ro, name, "groups")
|
2018-07-05 14:15:51 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 08:03:38 +00:00
|
|
|
//UserGroupRemove removes the user from the group
|
2018-07-05 14:15:51 +00:00
|
|
|
func (c *Client) UserGroupRemove(name string, group string) error {
|
|
|
|
ro := &req.RequestOptions{
|
|
|
|
Data: map[string]string{
|
|
|
|
"groupid": group,
|
|
|
|
},
|
|
|
|
}
|
2018-09-03 10:17:02 +00:00
|
|
|
return c.userBaseRequest(http.MethodDelete, ro, name, "groups")
|
2018-07-05 14:15:51 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 08:03:38 +00:00
|
|
|
//UserGroupPromote promotes the user as group admin
|
2018-07-05 14:15:51 +00:00
|
|
|
func (c *Client) UserGroupPromote(name string, group string) error {
|
|
|
|
ro := &req.RequestOptions{
|
|
|
|
Data: map[string]string{
|
|
|
|
"groupid": group,
|
|
|
|
},
|
|
|
|
}
|
2018-09-03 10:17:02 +00:00
|
|
|
return c.userBaseRequest(http.MethodPost, ro, name, "subadmins")
|
2018-07-05 14:15:51 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 08:03:38 +00:00
|
|
|
//UserGroupDemote demotes the user
|
2018-07-05 14:15:51 +00:00
|
|
|
func (c *Client) UserGroupDemote(name string, group string) error {
|
|
|
|
ro := &req.RequestOptions{
|
|
|
|
Data: map[string]string{
|
|
|
|
"groupid": group,
|
|
|
|
},
|
|
|
|
}
|
2018-09-03 10:17:02 +00:00
|
|
|
return c.userBaseRequest(http.MethodDelete, ro, name, "subadmins")
|
2018-07-05 14:15:51 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 08:03:38 +00:00
|
|
|
//UserGroupSubAdminList lists the groups where he is subadmin
|
2018-07-05 14:15:51 +00:00
|
|
|
func (c *Client) UserGroupSubAdminList(name string) ([]string, error) {
|
|
|
|
if !c.loggedIn() {
|
2018-07-30 08:08:35 +00:00
|
|
|
return nil, errUnauthorized
|
2018-07-05 14:15:51 +00:00
|
|
|
}
|
|
|
|
u := c.baseURL.ResolveReference(routes.users)
|
|
|
|
u.Path = path.Join(u.Path, name, "subadmins")
|
|
|
|
res, err := c.session.Get(u.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-07-09 10:53:53 +00:00
|
|
|
var r types.BaseResponse
|
2018-07-05 14:15:51 +00:00
|
|
|
res.JSON(&r)
|
|
|
|
return r.Ocs.Data, nil
|
|
|
|
}
|
|
|
|
|
2018-07-05 13:01:25 +00:00
|
|
|
func (c *Client) userUpdateAttribute(name string, key string, value string) error {
|
|
|
|
ro := &req.RequestOptions{
|
|
|
|
Data: map[string]string{
|
|
|
|
"key": key,
|
|
|
|
"value": value,
|
|
|
|
},
|
|
|
|
}
|
2018-09-03 10:17:02 +00:00
|
|
|
return c.userBaseRequest(http.MethodPut, ro, name)
|
2018-07-05 13:22:56 +00:00
|
|
|
}
|
|
|
|
|
2018-09-03 10:17:02 +00:00
|
|
|
func (c *Client) userBaseRequest(method string, ro *req.RequestOptions, subRoutes ...string) error {
|
|
|
|
_, err := c.baseRequest(method, routes.users, ro, subRoutes...)
|
2018-07-25 13:06:31 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func ignoredUserField(key string) bool {
|
|
|
|
keys := []string{"ID", "Quota", "Enabled", "Groups", "Language"}
|
|
|
|
for _, k := range keys {
|
|
|
|
if key == k {
|
|
|
|
return true
|
|
|
|
}
|
2018-07-05 10:50:56 +00:00
|
|
|
}
|
2018-07-25 13:06:31 +00:00
|
|
|
return false
|
2018-07-05 10:50:56 +00:00
|
|
|
}
|