2018-07-25 13:06:31 +00:00
|
|
|
package gonextcloud
|
2018-07-09 10:53:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2019-07-22 14:17:11 +00:00
|
|
|
|
|
|
|
req "github.com/levigross/grequests"
|
2018-07-09 10:53:53 +00:00
|
|
|
)
|
|
|
|
|
2019-07-22 14:17:11 +00:00
|
|
|
//groups contains all groups available actions
|
|
|
|
type groups struct {
|
|
|
|
c *client
|
2018-10-21 12:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//List lists the Nextcloud groups
|
2019-07-22 14:17:11 +00:00
|
|
|
func (g *groups) List() ([]string, error) {
|
2018-10-21 12:53:12 +00:00
|
|
|
res, err := g.c.baseRequest(http.MethodGet, routes.groups, nil)
|
2018-07-09 10:53:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-22 14:40:42 +00:00
|
|
|
var r groupListResponse
|
2018-07-24 18:26:12 +00:00
|
|
|
res.JSON(&r)
|
|
|
|
return r.Ocs.Data.Groups, nil
|
2018-07-09 10:53:53 +00:00
|
|
|
}
|
|
|
|
|
2018-10-21 12:53:12 +00:00
|
|
|
//ListDetails lists the Nextcloud groups
|
2019-07-22 14:17:11 +00:00
|
|
|
func (g *groups) ListDetails(search string) ([]Group, error) {
|
2019-01-31 12:23:06 +00:00
|
|
|
ro := &req.RequestOptions{
|
|
|
|
Params: map[string]string{
|
|
|
|
"search": search,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
res, err := g.c.baseRequest(http.MethodGet, routes.groups, ro, "details")
|
2018-10-16 09:02:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-22 14:40:42 +00:00
|
|
|
var r groupListDetailsResponse
|
2018-10-16 09:02:04 +00:00
|
|
|
res.JSON(&r)
|
|
|
|
return r.Ocs.Data.Groups, nil
|
|
|
|
}
|
|
|
|
|
2019-07-22 14:17:11 +00:00
|
|
|
//users list the group's users
|
|
|
|
func (g *groups) Users(name string) ([]string, error) {
|
2018-10-21 12:53:12 +00:00
|
|
|
res, err := g.c.baseRequest(http.MethodGet, routes.groups, nil, name)
|
2018-07-09 10:53:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-22 14:40:42 +00:00
|
|
|
var r userListResponse
|
2018-07-09 10:53:53 +00:00
|
|
|
res.JSON(&r)
|
|
|
|
return r.Ocs.Data.Users, nil
|
|
|
|
}
|
|
|
|
|
2018-10-21 12:53:12 +00:00
|
|
|
//Search return the list of groups matching the search string
|
2019-07-22 14:17:11 +00:00
|
|
|
func (g *groups) Search(search string) ([]string, error) {
|
2018-07-09 11:36:07 +00:00
|
|
|
ro := &req.RequestOptions{
|
|
|
|
Params: map[string]string{"search": search},
|
|
|
|
}
|
2018-10-21 12:53:12 +00:00
|
|
|
res, err := g.c.baseRequest(http.MethodGet, routes.groups, ro)
|
2018-07-09 11:36:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-22 14:40:42 +00:00
|
|
|
var r groupListResponse
|
2018-07-09 11:36:07 +00:00
|
|
|
res.JSON(&r)
|
|
|
|
return r.Ocs.Data.Groups, nil
|
|
|
|
}
|
|
|
|
|
2018-10-21 12:53:12 +00:00
|
|
|
//Create creates a group
|
2019-07-22 14:17:11 +00:00
|
|
|
func (g *groups) Create(name string) error {
|
2018-07-09 10:53:53 +00:00
|
|
|
ro := &req.RequestOptions{
|
|
|
|
Data: map[string]string{
|
|
|
|
"groupid": name,
|
|
|
|
},
|
|
|
|
}
|
2018-10-21 12:53:12 +00:00
|
|
|
return g.baseRequest(http.MethodPost, ro)
|
2018-07-09 10:53:53 +00:00
|
|
|
}
|
|
|
|
|
2018-10-21 12:53:12 +00:00
|
|
|
//Delete deletes the group
|
2019-07-22 14:17:11 +00:00
|
|
|
func (g *groups) Delete(name string) error {
|
2018-10-21 12:53:12 +00:00
|
|
|
return g.baseRequest(http.MethodDelete, nil, name)
|
2018-07-09 10:53:53 +00:00
|
|
|
}
|
|
|
|
|
2018-10-21 12:53:12 +00:00
|
|
|
//SubAdminList lists the group's subadmins
|
2019-07-22 14:17:11 +00:00
|
|
|
func (g *groups) SubAdminList(name string) ([]string, error) {
|
2018-10-21 12:53:12 +00:00
|
|
|
res, err := g.c.baseRequest(http.MethodGet, routes.groups, nil, name, "subadmins")
|
2018-07-09 10:53:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-22 14:40:42 +00:00
|
|
|
var r userListResponse
|
2018-07-09 10:53:53 +00:00
|
|
|
res.JSON(&r)
|
|
|
|
return r.Ocs.Data.Users, nil
|
|
|
|
}
|
|
|
|
|
2019-07-22 14:17:11 +00:00
|
|
|
func (g *groups) baseRequest(method string, ro *req.RequestOptions, subRoute ...string) error {
|
2018-10-21 12:53:12 +00:00
|
|
|
_, err := g.c.baseRequest(method, routes.groups, ro, subRoute...)
|
2018-07-25 13:06:31 +00:00
|
|
|
return err
|
2018-07-09 10:53:53 +00:00
|
|
|
}
|