-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
get.go
108 lines (97 loc) · 3.27 KB
/
get.go
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
package teams
import (
"fmt"
"golang.org/x/net/context"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/keybase1"
)
// needAdmin must be set when interacting with links that have a possibility of being stubbed.
func GetForTeamManagementByStringName(ctx context.Context, g *libkb.GlobalContext, name string, needAdmin bool) (*Team, error) {
// assume private team
public := false
team, err := Load(ctx, g, keybase1.LoadTeamArg{
Name: name,
Public: public,
ForceRepoll: true,
NeedAdmin: needAdmin,
})
if err != nil {
return nil, fixupTeamGetError(ctx, g, err, name, public)
}
if team.IsImplicit() {
return nil, fmt.Errorf("cannot manage implicit team by name")
}
return team, nil
}
func GetForTeamManagementByTeamID(ctx context.Context, g *libkb.GlobalContext, id keybase1.TeamID, needAdmin bool) (*Team, error) {
team, err := Load(ctx, g, keybase1.LoadTeamArg{
ID: id,
Public: id.IsPublic(), // infer publicness from team id
ForceRepoll: true,
NeedAdmin: needAdmin,
})
return team, fixupTeamGetError(ctx, g, err, id.String(), id.IsPublic())
}
func GetTeamByNameForTest(ctx context.Context, g *libkb.GlobalContext, name string, public bool, needAdmin bool) (*Team, error) {
team, err := Load(ctx, g, keybase1.LoadTeamArg{
Name: name,
Public: public,
ForceRepoll: true,
NeedAdmin: needAdmin,
})
return team, fixupTeamGetError(ctx, g, err, name, public)
}
// Get a team with no stubbed links if we are an admin. Use this instead of NeedAdmin when you don't
// know whether you are an admin. This always causes roundtrips. Doesn't work for implicit admins.
func GetMaybeAdminByStringName(ctx context.Context, g *libkb.GlobalContext, name string, public bool) (*Team, error) {
// Find out our up-to-date role.
team, err := Load(ctx, g, keybase1.LoadTeamArg{
Name: name,
Public: public,
ForceRepoll: true,
RefreshUIDMapper: true,
AllowNameLookupBurstCache: true,
})
if err != nil {
return nil, fixupTeamGetError(ctx, g, err, name, public)
}
return getMaybeAdmin(ctx, g, team)
}
func GetMaybeAdminByID(ctx context.Context, g *libkb.GlobalContext, id keybase1.TeamID, public bool) (*Team, error) {
// Find out our up-to-date role.
team, err := Load(ctx, g, keybase1.LoadTeamArg{
ID: id,
Public: public,
ForceRepoll: true,
RefreshUIDMapper: true,
AllowNameLookupBurstCache: true,
})
if err != nil {
return nil, fixupTeamGetError(ctx, g, err, id.String(), public)
}
return getMaybeAdmin(ctx, g, team)
}
func getMaybeAdmin(ctx context.Context, g *libkb.GlobalContext, team *Team) (*Team, error) {
me, err := loadUserVersionByUID(ctx, g, g.Env.GetUID())
if err != nil {
return nil, err
}
role, err := team.MemberRole(ctx, me)
if err != nil {
return nil, err
}
if role.IsAdminOrAbove() {
// Will hit the cache _unless_ we had a cached non-admin team
// and are now an admin.
team, err = Load(ctx, g, keybase1.LoadTeamArg{
ID: team.ID,
Public: team.IsPublic(),
NeedAdmin: true,
AllowNameLookupBurstCache: true,
})
if err != nil {
return nil, err
}
}
return team, nil
}