github.com/cyverse/go-irodsclient@v0.13.2/irods/message/modify_access_request.go (about)

     1  package message
     2  
     3  import (
     4  	"encoding/xml"
     5  	"fmt"
     6  
     7  	"github.com/cyverse/go-irodsclient/irods/common"
     8  	"golang.org/x/xerrors"
     9  )
    10  
    11  // IRODSMessageModifyAccessRequest stores alter access control request
    12  type IRODSMessageModifyAccessRequest struct {
    13  	XMLName       xml.Name `xml:"modAccessControlInp_PI"`
    14  	RecursiveFlag int      `xml:"recursiveFlag"`
    15  	AccessLevel   string   `xml:"accessLevel"`
    16  	UserName      string   `xml:"userName"`
    17  	Zone          string   `xml:"zone"`
    18  	Path          string   `xml:"path"`
    19  }
    20  
    21  // NewIRODSMessageModifyAccessRequest creates a IRODSMessageModAccessRequest message for altering the access control list of a object or collection.
    22  func NewIRODSMessageModifyAccessRequest(accessLevel, user, zone, path string, recursive, asAdmin bool) *IRODSMessageModifyAccessRequest {
    23  	if asAdmin {
    24  		accessLevel = fmt.Sprintf("admin:%s", accessLevel)
    25  	}
    26  
    27  	request := &IRODSMessageModifyAccessRequest{
    28  		RecursiveFlag: 0,
    29  		AccessLevel:   accessLevel,
    30  		UserName:      user,
    31  		Zone:          zone,
    32  		Path:          path,
    33  	}
    34  
    35  	return request
    36  }
    37  
    38  // GetBytes returns byte array
    39  func (msg *IRODSMessageModifyAccessRequest) GetBytes() ([]byte, error) {
    40  	xmlBytes, err := xml.Marshal(msg)
    41  	if err != nil {
    42  		return nil, xerrors.Errorf("failed to marshal irods message to xml: %w", err)
    43  	}
    44  	return xmlBytes, nil
    45  }
    46  
    47  // FromBytes returns struct from bytes
    48  func (msg *IRODSMessageModifyAccessRequest) FromBytes(bytes []byte) error {
    49  	err := xml.Unmarshal(bytes, msg)
    50  	if err != nil {
    51  		return xerrors.Errorf("failed to unmarshal xml to irods message: %w", err)
    52  	}
    53  	return nil
    54  }
    55  
    56  // GetMessage builds a message
    57  func (msg *IRODSMessageModifyAccessRequest) GetMessage() (*IRODSMessage, error) {
    58  	bytes, err := msg.GetBytes()
    59  	if err != nil {
    60  		return nil, xerrors.Errorf("failed to get bytes from irods message: %w", err)
    61  	}
    62  
    63  	msgBody := IRODSMessageBody{
    64  		Type:    RODS_MESSAGE_API_REQ_TYPE,
    65  		Message: bytes,
    66  		Error:   nil,
    67  		Bs:      nil,
    68  		IntInfo: int32(common.MOD_ACCESS_CONTROL_AN),
    69  	}
    70  
    71  	msgHeader, err := msgBody.BuildHeader()
    72  	if err != nil {
    73  		return nil, xerrors.Errorf("failed to build header from irods message: %w", err)
    74  	}
    75  
    76  	return &IRODSMessage{
    77  		Header: msgHeader,
    78  		Body:   &msgBody,
    79  	}, nil
    80  }