Skip to content

Commit

Permalink
fixed group permission bug
Browse files Browse the repository at this point in the history
  • Loading branch information
rebely3ll committed Aug 15, 2024
1 parent 3b0b914 commit 7def827
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 12 deletions.
33 changes: 33 additions & 0 deletions cmd/cli/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var (
Subcommands: []*cli.Command{
GetCustodiansCmd,
GetFoldersCmd,
GetGroupsCmd,
GetMattersCmd,
GetLegalholdsCmd,
},
Expand Down Expand Up @@ -106,6 +107,13 @@ var (
Flags: DefaultListOptions,
}

GetGroupsCmd = &cli.Command{
Name: "groups",
Category: "get",
Action: execute,
Flags: DefaultListOptions,
}

GetMattersCmd = &cli.Command{
Name: "matters",
Category: "get",
Expand Down Expand Up @@ -185,6 +193,8 @@ func execute(ctx *cli.Context) error {
return getMatters(ctx)
case "legalholds":
return getLegalholds(ctx)
case "groups":
return getGroups(ctx)
}
case "verify":
switch ctx.Command.Name {
Expand Down Expand Up @@ -312,6 +322,29 @@ func getFolders(ctx *cli.Context) error {
return nil
}

func getGroups(ctx *cli.Context) error {
var err error
var v any

client := NewClient(ctx)

if ctx.Int("id") > 0 {
v, err = client.GetGroup(ctx.Int("id"))
} else {
v, err = client.GetGroups(listOptions(ctx))
}

if err != nil {
return err
}

fmt.Printf("%+v\n", v)

printer := otlh.NewPrinter().JSON().Build()
printer.Print(v)
return nil
}

func getMatters(ctx *cli.Context) error {
var err error
var v any
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func main() {

app := &cli.App{
Name: "otlh",
Version: "0.3.6-beta",
Version: "0.3.7-beta",
Usage: "Command Line Interface to access Opentext LegalHold service",
Flags: []cli.Flag{
&cli.StringFlag{
Expand Down
Binary file added cmd/cli/testdata/test02.xlsx
Binary file not shown.
76 changes: 67 additions & 9 deletions pkg/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,7 @@ func (b *ClientBuilder) Build() *Client {
"Content-Type": "application/json",
})

/*
if log.Logger.GetLevel() == zerolog.TraceLevel {
r.SetDebug(true)
}
*/
// r.SetDebug(true)

if b.skipVerify {
r.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
Expand Down Expand Up @@ -252,6 +248,41 @@ func (c *Client) GetFolders(opts Options) (Folders, error) {
return resp.Embedded.Folders, nil
}

func (c *Client) GetGroup(id int) (Group, error) {
var v []byte
var err error

group := Group{}

req, _ := NewRequest().WithTenant(c.tenant).Group().WithID(id).Build()
if v, err = c.Send(req); err != nil {
return group, err
}
if err = json.Unmarshal(v, &group); err != nil {
return group, err
}

return group, nil
}

func (c *Client) GetGroups(opts Options) (Groups, error) {
var err error

var body []byte
var resp GroupsResponse

req, _ := NewRequest().WithTenant(c.tenant).Get().Group().Build()
if body, err = c.Send(req, opts); err != nil {
return nil, err
}

if err = json.Unmarshal(body, &resp); err != nil {
return nil, err
}

return resp.Embedded.Groups, nil
}

func (c *Client) GetMatter(id int) (Matter, error) {
var v []byte
var err error
Expand Down Expand Up @@ -504,6 +535,7 @@ func (c *Client) FindFolderByName(name string) (Folder, error) {
*
* Parameters:
* - name: the name of the folder to create
* - groupIDs: list of group ids to associate with the folder
*
* Returns:
* - Folder: the newly created folder
Expand All @@ -513,13 +545,13 @@ func (c *Client) FindFolderByName(name string) (Folder, error) {
* creation is successful, the new folder is returned. If an error occurs, the error
* is returned.
*/
func (c *Client) CreateFolder(name string) (Folder, error) {
func (c *Client) CreateFolder(name string, groupIDs []int) (Folder, error) {
var err error
var respBody []byte

var folder Folder = Folder{}

var createFolder *CreateFolderBody = NewCreateFolderBody().WithName(name)
var createFolder *CreateFolderBody = NewCreateFolderBody().WithName(name).WithGroupIDs(groupIDs)

req, _ := NewRequest().WithTenant(c.tenant).Post().Folder().Build()

Expand All @@ -537,7 +569,6 @@ func (c *Client) CreateFolder(name string) (Folder, error) {
log.Debug().Msgf("created folder %s with id %d", name, folder.ID)

return folder, nil

}

/**
Expand All @@ -554,7 +585,13 @@ func (c *Client) FindOrCreateFolder(name string) (Folder, error) {

log.Debug().Msgf("folder [%s] not found, creating", name)

return c.CreateFolder(name)
group, err := c.FindGroupByName("All Admins")
if err != nil {
log.Debug().Msg("failed to find defalt admin group [All Admins]")
return Folder{}, err
}

return c.CreateFolder(name, []int{group.ID})
}

/**
Expand Down Expand Up @@ -669,3 +706,24 @@ func (c *Client) FindLegalhold(name string, matterID int) (Legalhold, error) {

return Legalhold{}, fmt.Errorf("legalhold [%s] not found", name)
}

func (c *Client) FindGroupByName(name string) (Group, error) {
var err error
var groups Groups = Groups{}

log.Debug().Msgf("searching groups by name [%s]", name)

opts := NewListOptions().WithFilterName(name)

if groups, err = c.GetGroups(opts); err != nil {
return Group{}, err
}

for _, group := range groups {
if group.Name == name {
return group, nil
}
}

return Group{}, fmt.Errorf("group [%s] not found", name)
}
10 changes: 9 additions & 1 deletion pkg/folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,22 @@ type CreateFolderBody struct {
ContactEmail string `json:"contact_email,omitempty"`
ContactPhone string `json:"contact_phone,omitempty"`
Notes string `json:"notes,omitempty"`
GroupIDs []int `json:"group_ids,omitempty"`
}

// TODO: enable group name support
func NewCreateFolderBody() *CreateFolderBody {
return &CreateFolderBody{InheritEmailConfig: true}
return &CreateFolderBody{
InheritEmailConfig: true,
}
}

func (b *CreateFolderBody) WithName(name string) *CreateFolderBody {
b.Name = name
return b
}

func (b *CreateFolderBody) WithGroupIDs(groupIDs []int) *CreateFolderBody {
b.GroupIDs = groupIDs
return b
}
68 changes: 68 additions & 0 deletions pkg/groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package otlh

import "fmt"

type Group struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Type string `json:"type,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
Links struct {
Self struct {
Href string `json:"href,omitempty"`
} `json:"self,omitempty"`
Users struct {
Href string `json:"href,omitempty"`
} `json:"users,omitempty"`
Folders struct {
Href string `json:"href,omitempty"`
} `json:"folders,omitempty"`
Site struct {
Href string `json:"href,omitempty"`
} `json:"site,omitempty"`
} `json:"_links,omitempty"`
}

type Groups []Group

type GroupsResponse struct {
Links struct {
Self struct {
Href string `json:"href"`
} `json:"self"`
} `json:"_links"`
Page struct {
HasMore bool `json:"has-more"`
TotalCount int `json:"total-count"`
} `json:"page"`
Embedded struct {
Groups []Group `json:"groups"`
} `json:"_embedded"`
}

type GroupRequest struct {
id int
Request
}

type GroupRequestBuilder struct {
*GroupRequest
}

func (b *GroupRequestBuilder) WithID(id int) *GroupRequestBuilder {
b.id = id
return b
}

func (b *GroupRequestBuilder) Build() (*GroupRequest, error) {
return b.GroupRequest, nil
}

func (req *GroupRequest) Endpoint() string {
if req.id == 0 {
return fmt.Sprintf("/t/%s/api/%s/groups", req.tenant, APIVERSION)
}
return fmt.Sprintf("/t/%s/api/%s/group/%d", req.tenant, APIVERSION, req.id)
}
6 changes: 5 additions & 1 deletion pkg/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func (req *Request) Folder() *FolderRequestBuilder {
return &FolderRequestBuilder{FolderRequest: &FolderRequest{Request: *req}}
}

func (req *Request) Group() *GroupRequestBuilder {
return &GroupRequestBuilder{GroupRequest: &GroupRequest{Request: *req}}
}

func (req *Request) Matter() *MatterRequestBuilder {
return &MatterRequestBuilder{MatterRequest: &MatterRequest{Request: *req}}
}
}

0 comments on commit 7def827

Please sign in to comment.