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