2020-10-14 09:36:00 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gitea
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DeleteOrgMembership remove a member from an organization
|
|
|
|
func (c *Client) DeleteOrgMembership(org, user string) (*Response, error) {
|
2021-03-22 22:33:18 +05:30
|
|
|
if err := escapeValidatePathSegments(&org, &user); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/orgs/%s/members/%s", org, user), nil, nil)
|
2020-10-14 09:36:00 +05:30
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListOrgMembershipOption list OrgMembership options
|
|
|
|
type ListOrgMembershipOption struct {
|
|
|
|
ListOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListOrgMembership list an organization's members
|
|
|
|
func (c *Client) ListOrgMembership(org string, opt ListOrgMembershipOption) ([]*User, *Response, error) {
|
2021-03-22 22:33:18 +05:30
|
|
|
if err := escapeValidatePathSegments(&org); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-10-14 09:36:00 +05:30
|
|
|
opt.setDefaults()
|
|
|
|
users := make([]*User, 0, opt.PageSize)
|
|
|
|
|
2021-03-22 22:33:18 +05:30
|
|
|
link, _ := url.Parse(fmt.Sprintf("/orgs/%s/members", org))
|
2020-10-14 09:36:00 +05:30
|
|
|
link.RawQuery = opt.getURLQuery().Encode()
|
|
|
|
resp, err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &users)
|
|
|
|
return users, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListPublicOrgMembership list an organization's members
|
|
|
|
func (c *Client) ListPublicOrgMembership(org string, opt ListOrgMembershipOption) ([]*User, *Response, error) {
|
2021-03-22 22:33:18 +05:30
|
|
|
if err := escapeValidatePathSegments(&org); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-10-14 09:36:00 +05:30
|
|
|
opt.setDefaults()
|
|
|
|
users := make([]*User, 0, opt.PageSize)
|
|
|
|
|
2021-03-22 22:33:18 +05:30
|
|
|
link, _ := url.Parse(fmt.Sprintf("/orgs/%s/public_members", org))
|
2020-10-14 09:36:00 +05:30
|
|
|
link.RawQuery = opt.getURLQuery().Encode()
|
|
|
|
resp, err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &users)
|
|
|
|
return users, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckOrgMembership Check if a user is a member of an organization
|
|
|
|
func (c *Client) CheckOrgMembership(org, user string) (bool, *Response, error) {
|
2021-03-22 22:33:18 +05:30
|
|
|
if err := escapeValidatePathSegments(&org, &user); err != nil {
|
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
status, resp, err := c.getStatusCode("GET", fmt.Sprintf("/orgs/%s/members/%s", org, user), nil, nil)
|
2020-10-14 09:36:00 +05:30
|
|
|
if err != nil {
|
|
|
|
return false, resp, err
|
|
|
|
}
|
|
|
|
switch status {
|
|
|
|
case http.StatusNoContent:
|
|
|
|
return true, resp, nil
|
|
|
|
case http.StatusNotFound:
|
|
|
|
return false, resp, nil
|
|
|
|
default:
|
|
|
|
return false, resp, fmt.Errorf("unexpected Status: %d", status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckPublicOrgMembership Check if a user is a member of an organization
|
|
|
|
func (c *Client) CheckPublicOrgMembership(org, user string) (bool, *Response, error) {
|
2021-03-22 22:33:18 +05:30
|
|
|
if err := escapeValidatePathSegments(&org, &user); err != nil {
|
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
status, resp, err := c.getStatusCode("GET", fmt.Sprintf("/orgs/%s/public_members/%s", org, user), nil, nil)
|
2020-10-14 09:36:00 +05:30
|
|
|
if err != nil {
|
|
|
|
return false, resp, err
|
|
|
|
}
|
|
|
|
switch status {
|
|
|
|
case http.StatusNoContent:
|
|
|
|
return true, resp, nil
|
|
|
|
case http.StatusNotFound:
|
|
|
|
return false, resp, nil
|
|
|
|
default:
|
|
|
|
return false, resp, fmt.Errorf("unexpected Status: %d", status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPublicOrgMembership publicize/conceal a user's membership
|
|
|
|
func (c *Client) SetPublicOrgMembership(org, user string, visible bool) (*Response, error) {
|
2021-03-22 22:33:18 +05:30
|
|
|
if err := escapeValidatePathSegments(&org, &user); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-14 09:36:00 +05:30
|
|
|
var (
|
|
|
|
status int
|
|
|
|
err error
|
|
|
|
resp *Response
|
|
|
|
)
|
|
|
|
if visible {
|
2021-03-22 22:33:18 +05:30
|
|
|
status, resp, err = c.getStatusCode("PUT", fmt.Sprintf("/orgs/%s/public_members/%s", org, user), nil, nil)
|
2020-10-14 09:36:00 +05:30
|
|
|
} else {
|
2021-03-22 22:33:18 +05:30
|
|
|
status, resp, err = c.getStatusCode("DELETE", fmt.Sprintf("/orgs/%s/public_members/%s", org, user), nil, nil)
|
2020-10-14 09:36:00 +05:30
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
switch status {
|
|
|
|
case http.StatusNoContent:
|
|
|
|
return resp, nil
|
|
|
|
case http.StatusNotFound:
|
|
|
|
return resp, fmt.Errorf("forbidden")
|
|
|
|
default:
|
|
|
|
return resp, fmt.Errorf("unexpected Status: %d", status)
|
|
|
|
}
|
|
|
|
}
|