mirror of
				https://github.com/kovetskiy/mark.git
				synced 2025-11-04 14:27:36 +08:00 
			
		
		
		
	Merge pull request #6 from seletskiy/master
api: use different restrict api for cloud
This commit is contained in:
		
						commit
						4fc1aa6553
					
				
							
								
								
									
										17
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								main.go
									
									
									
									
									
								
							@ -38,10 +38,10 @@ Confluence instance and update it accordingly.
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
File in extended format should follow specification:
 | 
					File in extended format should follow specification:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  []:# (Space: <space key>)
 | 
					  <!-- Space: <space key> -->
 | 
				
			||||||
  []:# (Parent: <parent 1>)
 | 
					  <!-- Parent: <parent 1> -->
 | 
				
			||||||
  []:# (Parent: <parent 2>)
 | 
					  <!-- Parent: <parent 2> -->
 | 
				
			||||||
  []:# (Title: <title>)
 | 
					  <!-- Title: <title> -->
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  <page contents>
 | 
					  <page contents>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -50,7 +50,7 @@ parent by title, it will be created.
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
Also, optional following headers are supported:
 | 
					Also, optional following headers are supported:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  * []:# (Layout: <article|plain>)
 | 
					  * <!-- Layout: <article|plain> -->
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    - (default) article: content will be put in narrow column for ease of
 | 
					    - (default) article: content will be put in narrow column for ease of
 | 
				
			||||||
      reading;
 | 
					      reading;
 | 
				
			||||||
@ -217,12 +217,9 @@ func main() {
 | 
				
			|||||||
			creds.Username,
 | 
								creds.Username,
 | 
				
			||||||
		)
 | 
							)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		err := api.SetPagePermissions(
 | 
							err := api.RestrictPageUpdates(
 | 
				
			||||||
			target,
 | 
								target,
 | 
				
			||||||
			confluence.RestrictionEdit,
 | 
								creds.Username,
 | 
				
			||||||
			[]confluence.Restriction{
 | 
					 | 
				
			||||||
				{User: creds.Username},
 | 
					 | 
				
			||||||
			},
 | 
					 | 
				
			||||||
		)
 | 
							)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			log.Fatal(err)
 | 
								log.Fatal(err)
 | 
				
			||||||
 | 
				
			|||||||
@ -9,6 +9,7 @@ import (
 | 
				
			|||||||
	"mime/multipart"
 | 
						"mime/multipart"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/bndr/gopencils"
 | 
						"github.com/bndr/gopencils"
 | 
				
			||||||
	"github.com/kovetskiy/lorg"
 | 
						"github.com/kovetskiy/lorg"
 | 
				
			||||||
@ -16,16 +17,8 @@ import (
 | 
				
			|||||||
	"github.com/reconquest/karma-go"
 | 
						"github.com/reconquest/karma-go"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type RestrictionOperation string
 | 
					type User struct {
 | 
				
			||||||
 | 
						AccountID string `json:"accountId"`
 | 
				
			||||||
const (
 | 
					 | 
				
			||||||
	RestrictionEdit RestrictionOperation = `Edit`
 | 
					 | 
				
			||||||
	RestrictionView                      = `View`
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type Restriction struct {
 | 
					 | 
				
			||||||
	User  string `json:"userName"`
 | 
					 | 
				
			||||||
	Group string `json:"groupName,omitempty"`
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type API struct {
 | 
					type API struct {
 | 
				
			||||||
@ -478,19 +471,76 @@ func (api *API) UpdatePage(
 | 
				
			|||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (api *API) SetPagePermissions(
 | 
					func (api *API) GetCurrentUser() (*User, error) {
 | 
				
			||||||
 | 
						var user User
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, err := api.rest.
 | 
				
			||||||
 | 
							Res("user").
 | 
				
			||||||
 | 
							Res("current", &user).
 | 
				
			||||||
 | 
							Get()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return &user, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (api *API) RestrictPageUpdatesCloud(
 | 
				
			||||||
	page *PageInfo,
 | 
						page *PageInfo,
 | 
				
			||||||
	operation RestrictionOperation,
 | 
						allowedUser string,
 | 
				
			||||||
	restrictions []Restriction,
 | 
					 | 
				
			||||||
) error {
 | 
					) error {
 | 
				
			||||||
 | 
						user, err := api.GetCurrentUser()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var result interface{}
 | 
						var result interface{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						request, err := api.rest.
 | 
				
			||||||
 | 
							Res("content").
 | 
				
			||||||
 | 
							Id(page.ID).
 | 
				
			||||||
 | 
							Res("restriction", &result).
 | 
				
			||||||
 | 
							Post([]map[string]interface{}{
 | 
				
			||||||
 | 
								{
 | 
				
			||||||
 | 
									"operation": "update",
 | 
				
			||||||
 | 
									"restrictions": map[string]interface{}{
 | 
				
			||||||
 | 
										"user": []map[string]interface{}{
 | 
				
			||||||
 | 
											{
 | 
				
			||||||
 | 
												"type":      "known",
 | 
				
			||||||
 | 
												"accountId": user.AccountID,
 | 
				
			||||||
 | 
											},
 | 
				
			||||||
 | 
										},
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if request.Raw.StatusCode != 200 {
 | 
				
			||||||
 | 
							return newErrorStatusNotOK(request)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (api *API) RestrictPageUpdatesServer(
 | 
				
			||||||
 | 
						page *PageInfo,
 | 
				
			||||||
 | 
						allowedUser string,
 | 
				
			||||||
 | 
					) error {
 | 
				
			||||||
 | 
						var (
 | 
				
			||||||
 | 
							err    error
 | 
				
			||||||
 | 
							result interface{}
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	request, err := api.json.Res(
 | 
						request, err := api.json.Res(
 | 
				
			||||||
		"setContentPermissions", &result,
 | 
							"setContentPermissions", &result,
 | 
				
			||||||
	).Post([]interface{}{
 | 
						).Post([]interface{}{
 | 
				
			||||||
		page.ID,
 | 
							page.ID,
 | 
				
			||||||
		operation,
 | 
							"Edit",
 | 
				
			||||||
		restrictions,
 | 
							map[string]interface{}{
 | 
				
			||||||
 | 
								"userName": allowedUser,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
@ -510,6 +560,21 @@ func (api *API) SetPagePermissions(
 | 
				
			|||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (api *API) RestrictPageUpdates(
 | 
				
			||||||
 | 
						page *PageInfo,
 | 
				
			||||||
 | 
						allowedUser string,
 | 
				
			||||||
 | 
					) error {
 | 
				
			||||||
 | 
						var err error
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if strings.HasSuffix(api.rest.Api.BaseUrl.Host, "atlassian.net") {
 | 
				
			||||||
 | 
							err = api.RestrictPageUpdatesCloud(page, allowedUser)
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							err = api.RestrictPageUpdatesServer(page, allowedUser)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return err
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func newErrorStatusNotOK(request *gopencils.Resource) error {
 | 
					func newErrorStatusNotOK(request *gopencils.Resource) error {
 | 
				
			||||||
	if request.Raw.StatusCode == 401 {
 | 
						if request.Raw.StatusCode == 401 {
 | 
				
			||||||
		return errors.New(
 | 
							return errors.New(
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user