mirror of
https://gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud
synced 2024-11-22 07:06:25 +00:00
#6 Add method creating user only with username and email
This commit is contained in:
parent
74ee331f4f
commit
68f06ac164
1
auth.go
1
auth.go
@ -27,6 +27,7 @@ func (c *Client) Login(username string, password string) error {
|
|||||||
res.JSON(&r)
|
res.JSON(&r)
|
||||||
// No need to check for Ocs.Meta.StatusCode as capabilities are always returned
|
// No need to check for Ocs.Meta.StatusCode as capabilities are always returned
|
||||||
c.capabilities = &r.Ocs.Data.Capabilities
|
c.capabilities = &r.Ocs.Data.Capabilities
|
||||||
|
c.version = &r.Ocs.Data.Version
|
||||||
// Check if authentication failed
|
// Check if authentication failed
|
||||||
if !c.loggedIn() {
|
if !c.loggedIn() {
|
||||||
e := types.APIError{Message: "authentication failed"}
|
e := types.APIError{Message: "authentication failed"}
|
||||||
|
@ -14,6 +14,7 @@ type Client struct {
|
|||||||
session *req.Session
|
session *req.Session
|
||||||
headers map[string]string
|
headers map[string]string
|
||||||
capabilities *types.Capabilities
|
capabilities *types.Capabilities
|
||||||
|
version *types.Version
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient create a new Client from the Nextcloud Instance URL
|
// NewClient create a new Client from the Nextcloud Instance URL
|
||||||
|
@ -5,3 +5,4 @@ app-name: testapp
|
|||||||
share-folder: /Documents
|
share-folder: /Documents
|
||||||
not-existing-user: this-user-should-not-exist
|
not-existing-user: this-user-should-not-exist
|
||||||
not-existing-group: this-group-should-not-exist
|
not-existing-group: this-group-should-not-exist
|
||||||
|
email: my@mail.com
|
@ -8,6 +8,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -19,6 +20,7 @@ type Config struct {
|
|||||||
ShareFolder string `yaml:"share-folder"`
|
ShareFolder string `yaml:"share-folder"`
|
||||||
NotExistingUser string `yaml:"not-existing-user"`
|
NotExistingUser string `yaml:"not-existing-user"`
|
||||||
NotExistingGroup string `yaml:"not-existing-group"`
|
NotExistingGroup string `yaml:"not-existing-group"`
|
||||||
|
Email string `yaml:"email"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const password = "somecomplicatedpassword"
|
const password = "somecomplicatedpassword"
|
||||||
@ -544,6 +546,37 @@ func TestGroupFolders(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUserCreateWithoutPassword(t *testing.T) {
|
||||||
|
c = nil
|
||||||
|
if err := initClient(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
err := c.UserCreateWithoutPassword(config.NotExistingUser, config.Email, strings.Title(config.NotExistingUser))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
err = c.UserDelete(config.NotExistingUser)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUserListDetails(t *testing.T) {
|
||||||
|
c = nil
|
||||||
|
if err := initClient(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
us, err := c.UserListDetails()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Contains(t, us, config.Login)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGroupListDetails(t *testing.T) {
|
||||||
|
c = nil
|
||||||
|
if err := initClient(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
gs, err := c.GroupListDetails()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotEmpty(t, gs)
|
||||||
|
}
|
||||||
|
|
||||||
// LoadConfig loads the test configuration
|
// LoadConfig loads the test configuration
|
||||||
func LoadConfig() error {
|
func LoadConfig() error {
|
||||||
f, err := os.Open("./config.yml")
|
f, err := os.Open("./config.yml")
|
||||||
|
11
groups.go
11
groups.go
@ -17,6 +17,17 @@ func (c *Client) GroupList() ([]string, error) {
|
|||||||
return r.Ocs.Data.Groups, nil
|
return r.Ocs.Data.Groups, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//GroupListDetails lists the Nextcloud groups
|
||||||
|
func (c *Client) GroupListDetails() ([]types.Group, error) {
|
||||||
|
res, err := c.baseRequest(http.MethodGet, routes.groups, nil, "details")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var r types.GroupListDetailsResponse
|
||||||
|
res.JSON(&r)
|
||||||
|
return r.Ocs.Data.Groups, nil
|
||||||
|
}
|
||||||
|
|
||||||
//GroupUsers list the group's users
|
//GroupUsers list the group's users
|
||||||
func (c *Client) GroupUsers(name string) ([]string, error) {
|
func (c *Client) GroupUsers(name string) ([]string, error) {
|
||||||
res, err := c.baseRequest(http.MethodGet, routes.groups, nil, name)
|
res, err := c.baseRequest(http.MethodGet, routes.groups, nil, name)
|
||||||
|
11
types/group.go
Normal file
11
types/group.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package types
|
||||||
|
|
||||||
|
//Group
|
||||||
|
type Group struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Displayname string `json:"displayname"`
|
||||||
|
UserCount int `json:"usercount"`
|
||||||
|
Disabled int `json:"disabled"`
|
||||||
|
CanAdd bool `json:"canAdd"`
|
||||||
|
CanRemove bool `json:"canRemove"`
|
||||||
|
}
|
@ -27,6 +27,15 @@ type UserListResponse struct {
|
|||||||
} `json:"ocs"`
|
} `json:"ocs"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UserListDetailsResponse struct {
|
||||||
|
Ocs struct {
|
||||||
|
Meta Meta `json:"meta"`
|
||||||
|
Data struct {
|
||||||
|
Users map[string]User `json:"users"`
|
||||||
|
} `json:"data"`
|
||||||
|
} `json:"ocs"`
|
||||||
|
}
|
||||||
|
|
||||||
//UserResponse
|
//UserResponse
|
||||||
type UserResponse struct {
|
type UserResponse struct {
|
||||||
Ocs struct {
|
Ocs struct {
|
||||||
@ -53,6 +62,16 @@ type GroupListResponse struct {
|
|||||||
} `json:"ocs"`
|
} `json:"ocs"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//GroupListDetailsResponse
|
||||||
|
type GroupListDetailsResponse struct {
|
||||||
|
Ocs struct {
|
||||||
|
Meta Meta `json:"meta"`
|
||||||
|
Data struct {
|
||||||
|
Groups []Group `json:"groups"`
|
||||||
|
} `json:"data"`
|
||||||
|
} `json:"ocs"`
|
||||||
|
}
|
||||||
|
|
||||||
//AppListResponse
|
//AppListResponse
|
||||||
type AppListResponse struct {
|
type AppListResponse struct {
|
||||||
Ocs struct {
|
Ocs struct {
|
||||||
@ -76,16 +95,18 @@ type CapabilitiesResponse struct {
|
|||||||
Ocs struct {
|
Ocs struct {
|
||||||
Meta Meta `json:"meta"`
|
Meta Meta `json:"meta"`
|
||||||
Data struct {
|
Data struct {
|
||||||
Version struct {
|
Version Version `json:"version"`
|
||||||
|
Capabilities Capabilities `json:"capabilities"`
|
||||||
|
} `json:"data"`
|
||||||
|
} `json:"ocs"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Version struct {
|
||||||
Major int `json:"major"`
|
Major int `json:"major"`
|
||||||
Minor int `json:"minor"`
|
Minor int `json:"minor"`
|
||||||
Micro int `json:"micro"`
|
Micro int `json:"micro"`
|
||||||
String string `json:"string"`
|
String string `json:"string"`
|
||||||
Edition string `json:"edition"`
|
Edition string `json:"edition"`
|
||||||
} `json:"version"`
|
|
||||||
Capabilities Capabilities `json:"capabilities"`
|
|
||||||
} `json:"data"`
|
|
||||||
} `json:"ocs"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type MonitoringResponse struct {
|
type MonitoringResponse struct {
|
||||||
|
@ -18,5 +18,11 @@ type User struct {
|
|||||||
Website string `json:"website"`
|
Website string `json:"website"`
|
||||||
Twitter string `json:"twitter"`
|
Twitter string `json:"twitter"`
|
||||||
Groups []string `json:"groups"`
|
Groups []string `json:"groups"`
|
||||||
Language string `json:"language"`
|
Language string `json:"language,omitempty"`
|
||||||
|
|
||||||
|
StorageLocation string `json:"storageLocation,omitempty"`
|
||||||
|
LastLogin int64 `json:"lastLogin,omitempty"`
|
||||||
|
Backend string `json:"backend,omitempty"`
|
||||||
|
Subadmin []interface{} `json:"subadmin,omitempty"`
|
||||||
|
Locale string `json:"locale,omitempty"`
|
||||||
}
|
}
|
||||||
|
36
users.go
36
users.go
@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/fatih/structs"
|
"github.com/fatih/structs"
|
||||||
req "github.com/levigross/grequests"
|
req "github.com/levigross/grequests"
|
||||||
|
"github.com/pkg/errors"
|
||||||
"gitlab.adphi.fr/partitio/Nextcloud-Partitio/gonextcloud/types"
|
"gitlab.adphi.fr/partitio/Nextcloud-Partitio/gonextcloud/types"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
@ -24,6 +25,18 @@ func (c *Client) UserList() ([]string, error) {
|
|||||||
return r.Ocs.Data.Users, nil
|
return r.Ocs.Data.Users, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//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
|
||||||
|
}
|
||||||
|
|
||||||
// User return the details about the specified user
|
// User return the details about the specified user
|
||||||
func (c *Client) User(name string) (*types.User, error) {
|
func (c *Client) User(name string) (*types.User, error) {
|
||||||
if name == "" {
|
if name == "" {
|
||||||
@ -77,6 +90,29 @@ func (c *Client) UserCreate(username string, password string, user *types.User)
|
|||||||
return c.UserUpdate(user)
|
return c.UserUpdate(user)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
//UserDelete delete the user
|
//UserDelete delete the user
|
||||||
func (c *Client) UserDelete(name string) error {
|
func (c *Client) UserDelete(name string) error {
|
||||||
return c.userBaseRequest(http.MethodDelete, nil, name)
|
return c.userBaseRequest(http.MethodDelete, nil, name)
|
||||||
|
Loading…
Reference in New Issue
Block a user