forked from open-networks/go-msgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroup.go
More file actions
135 lines (121 loc) · 5.1 KB
/
Copy pathGroup.go
File metadata and controls
135 lines (121 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package msgraph
import (
"encoding/json"
"fmt"
"time"
)
// Group represents one group of ms graph
//
// See: https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/group_get
type Group struct {
ID string
Description string
DisplayName string
CreatedDateTime time.Time
GroupTypes []string
Mail string
MailEnabled bool
MailNickname string
OnPremisesLastSyncDateTime time.Time // defaults to 0001-01-01 00:00:00 +0000 UTC if there's none
OnPremisesSecurityIdentifier string
OnPremisesSyncEnabled bool
ProxyAddresses []string
SecurityEnabled bool
Visibility string
graphClient *GraphClient // the graphClient that called the group
}
func (g Group) String() string {
return fmt.Sprintf("Group(ID: \"%v\", Description: \"%v\" DisplayName: \"%v\", CreatedDateTime: \"%v\", GroupTypes: \"%v\", Mail: \"%v\", MailEnabled: \"%v\", MailNickname: \"%v\", OnPremisesLastSyncDateTime: \"%v\", OnPremisesSecurityIdentifier: \"%v\", OnPremisesSyncEnabled: \"%v\", ProxyAddresses: \"%v\", SecurityEnabled \"%v\", Visibility: \"%v\", DirectAPIConnection: %v)",
g.ID, g.Description, g.DisplayName, g.CreatedDateTime, g.GroupTypes, g.Mail, g.MailEnabled, g.MailNickname, g.OnPremisesLastSyncDateTime, g.OnPremisesSecurityIdentifier, g.OnPremisesSyncEnabled, g.ProxyAddresses, g.SecurityEnabled, g.Visibility, g.graphClient != nil)
}
// setGraphClient sets the graphClient instance in this instance and all child-instances (if any)
func (g *Group) setGraphClient(gC *GraphClient) {
g.graphClient = gC
}
// ListMembers - Get a list of the group's direct members. A group can have users,
// contacts, and other groups as members. This operation is not transitive. This
// method will currently ONLY return User-instances of members
//
// See https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/group_list_members
func (g Group) ListMembers() (Users, error) {
if g.graphClient == nil {
return nil, ErrNotGraphClientSourced
}
resource := fmt.Sprintf("/groups/%v/members", g.ID)
var userlist Users
getUsers := func(pageOfUsers *Users) error {
for _, u := range *pageOfUsers {
userlist = append(userlist, u)
}
return nil
}
err := g.graphClient.makeGETUsersCall(resource, getUsers)
userlist.setGraphClient(g.graphClient)
return userlist, err
}
// UnmarshalJSON implements the json unmarshal to be used by the json-library
func (g *Group) UnmarshalJSON(data []byte) error {
tmp := struct {
ID string `json:"id"`
Description string `json:"description"`
DisplayName string `json:"displayName"`
CreatedDateTime string `json:"createdDateTime"`
GroupTypes []string `json:"groupTypes"`
Mail string `json:"mail"`
MailEnabled bool `json:"mailEnabled"`
MailNickname string `json:"mailNickname"`
OnPremisesLastSyncDateTime string `json:"onPremisesLastSyncDateTime"`
OnPremisesSecurityIdentifier string `json:"onPremisesSecurityIdentifier"`
OnPremisesSyncEnabled bool `json:"onPremisesSyncEnabled"`
ProxyAddresses []string `json:"proxyAddresses"`
SecurityEnabled bool `json:"securityEnabled"`
Visibility string `json:"visibility"`
}{}
err := json.Unmarshal(data, &tmp)
if err != nil {
return err
}
g.ID = tmp.ID
g.Description = tmp.Description
g.DisplayName = tmp.DisplayName
g.CreatedDateTime, err = time.Parse(time.RFC3339, tmp.CreatedDateTime)
if err != nil && tmp.CreatedDateTime != "" {
return fmt.Errorf("Can not parse CreatedDateTime %v with RFC3339: %v", tmp.CreatedDateTime, err)
}
g.GroupTypes = tmp.GroupTypes
g.Mail = tmp.Mail
g.MailEnabled = tmp.MailEnabled
g.MailNickname = tmp.MailNickname
g.OnPremisesLastSyncDateTime, err = time.Parse(time.RFC3339, tmp.OnPremisesLastSyncDateTime)
if err != nil && tmp.OnPremisesLastSyncDateTime != "" {
return fmt.Errorf("Can not parse OnPremisesLastSyncDateTime %v with RFC3339: %v", tmp.OnPremisesLastSyncDateTime, err)
}
g.OnPremisesSecurityIdentifier = tmp.OnPremisesSecurityIdentifier
g.OnPremisesSyncEnabled = tmp.OnPremisesSyncEnabled
g.ProxyAddresses = tmp.ProxyAddresses
g.SecurityEnabled = tmp.SecurityEnabled
g.Visibility = tmp.Visibility
return nil
}
func (g Group) AddMember(id string) error {
if g.graphClient == nil {
return ErrNotGraphClientSourced
}
resource := fmt.Sprintf("/groups/%v/members/$ref", g.ID)
type idStruct struct {
ID string `json:"@odata.id"`
}
ids := idStruct{ID: fmt.Sprintf("https://graph.microsoft.com/v1.0/users/%s", id)}
jsonID, err := json.Marshal(ids)
if err != nil {
return err
}
return g.graphClient.makePOSTAPICall(resource, string(jsonID))
}
func (g Group) RemoveMember(id string) error {
if g.graphClient == nil {
return ErrNotGraphClientSourced
}
resource := fmt.Sprintf("/groups/%v/members/%s/$ref", g.ID, id)
return g.graphClient.makeDELETEAPICall(resource)
}