go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/acls/project.go (about)

     1  // Copyright 2021 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package acls
    16  
    17  import (
    18  	"context"
    19  
    20  	"go.chromium.org/luci/cv/internal/configs/prjcfg"
    21  )
    22  
    23  // CheckProjectAccess checks if the calling user has access to the LUCI project.
    24  //
    25  // Returns true if project exists and is active and user has access to this
    26  // LUCI project, false otherwise.
    27  func CheckProjectAccess(ctx context.Context, project string) (bool, error) {
    28  	// TODO(yiwzhang): Consider returning a enum (UNKNOWN, ALLOWED, DENIED,
    29  	// PROJECT_NOT_EXISTS) so that callsite can explicitly specify logic to
    30  	// handle various scenarios.
    31  	//
    32  	// TODO(https://crbug.com/1233963): design & implement & test.
    33  
    34  	// Check whether project is active.
    35  	switch m, err := prjcfg.GetLatestMeta(ctx, project); {
    36  	case err != nil:
    37  		return false, err
    38  	case m.Status != prjcfg.StatusEnabled:
    39  		return false, nil
    40  	}
    41  
    42  	switch yes, err := checkLegacyCQStatusAccess(ctx, project); {
    43  	case err != nil:
    44  		return false, err
    45  	case yes:
    46  		return true, nil
    47  	}
    48  
    49  	// Default to no access.
    50  	return false, nil
    51  }