github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/teams/get.go (about)

     1  package teams
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"golang.org/x/net/context"
     7  
     8  	"github.com/keybase/client/go/libkb"
     9  	"github.com/keybase/client/go/protocol/keybase1"
    10  )
    11  
    12  // needAdmin must be set when interacting with links that have a possibility of being stubbed.
    13  func GetForTeamManagementByStringName(ctx context.Context, g *libkb.GlobalContext, name string, needAdmin bool) (*Team, error) {
    14  	// assume private team
    15  	public := false
    16  
    17  	team, err := Load(ctx, g, keybase1.LoadTeamArg{
    18  		Name:        name,
    19  		Public:      public,
    20  		ForceRepoll: true,
    21  		NeedAdmin:   needAdmin,
    22  	})
    23  	if err != nil {
    24  		return nil, fixupTeamGetError(ctx, g, err, name, public)
    25  	}
    26  	if team.IsImplicit() {
    27  		return nil, fmt.Errorf("cannot manage implicit team by name")
    28  	}
    29  
    30  	return team, nil
    31  }
    32  
    33  func GetForTeamManagementByTeamID(ctx context.Context, g *libkb.GlobalContext, id keybase1.TeamID, needAdmin bool) (*Team, error) {
    34  	team, err := Load(ctx, g, keybase1.LoadTeamArg{
    35  		ID:          id,
    36  		Public:      id.IsPublic(), // infer publicness from team id
    37  		ForceRepoll: true,
    38  		NeedAdmin:   needAdmin,
    39  	})
    40  	return team, fixupTeamGetError(ctx, g, err, id.String(), id.IsPublic())
    41  }
    42  
    43  func GetTeamByNameForTest(ctx context.Context, g *libkb.GlobalContext, name string, public bool, needAdmin bool) (*Team, error) {
    44  	team, err := Load(ctx, g, keybase1.LoadTeamArg{
    45  		Name:        name,
    46  		Public:      public,
    47  		ForceRepoll: true,
    48  		NeedAdmin:   needAdmin,
    49  	})
    50  	return team, fixupTeamGetError(ctx, g, err, name, public)
    51  }
    52  
    53  // Get a team with no stubbed links if we are an admin. Use this instead of NeedAdmin when you don't
    54  // know whether you are an admin. This always causes roundtrips. Doesn't work for implicit admins.
    55  func GetMaybeAdminByStringName(ctx context.Context, g *libkb.GlobalContext, name string, public bool) (*Team, error) {
    56  	// Find out our up-to-date role.
    57  	team, err := Load(ctx, g, keybase1.LoadTeamArg{
    58  		Name:                      name,
    59  		Public:                    public,
    60  		ForceRepoll:               true,
    61  		RefreshUIDMapper:          true,
    62  		AllowNameLookupBurstCache: true,
    63  	})
    64  	if err != nil {
    65  		return nil, fixupTeamGetError(ctx, g, err, name, public)
    66  	}
    67  	return getMaybeAdmin(ctx, g, team)
    68  }
    69  
    70  func GetMaybeAdminByID(ctx context.Context, g *libkb.GlobalContext, id keybase1.TeamID, public bool) (*Team, error) {
    71  	// Find out our up-to-date role.
    72  	team, err := Load(ctx, g, keybase1.LoadTeamArg{
    73  		ID:                        id,
    74  		Public:                    public,
    75  		ForceRepoll:               true,
    76  		RefreshUIDMapper:          true,
    77  		AllowNameLookupBurstCache: true,
    78  	})
    79  	if err != nil {
    80  		return nil, fixupTeamGetError(ctx, g, err, id.String(), public)
    81  	}
    82  	return getMaybeAdmin(ctx, g, team)
    83  }
    84  
    85  func getMaybeAdmin(ctx context.Context, g *libkb.GlobalContext, team *Team) (*Team, error) {
    86  	me, err := loadUserVersionByUID(ctx, g, g.Env.GetUID())
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  	role, err := team.MemberRole(ctx, me)
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  	if role.IsAdminOrAbove() {
    95  		// Will hit the cache _unless_ we had a cached non-admin team
    96  		// and are now an admin.
    97  		team, err = Load(ctx, g, keybase1.LoadTeamArg{
    98  			ID:                        team.ID,
    99  			Public:                    team.IsPublic(),
   100  			NeedAdmin:                 true,
   101  			AllowNameLookupBurstCache: true,
   102  		})
   103  		if err != nil {
   104  			return nil, err
   105  		}
   106  	}
   107  	return team, nil
   108  }