go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gae/filter/count/user.go (about)

     1  // Copyright 2015 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 count
    16  
    17  import (
    18  	"context"
    19  
    20  	"go.chromium.org/luci/gae/service/user"
    21  )
    22  
    23  // UserCounter is the counter object for the User service.
    24  type UserCounter struct {
    25  	Current           Entry
    26  	CurrentOAuth      Entry
    27  	IsAdmin           Entry
    28  	LoginURL          Entry
    29  	LoginURLFederated Entry
    30  	LogoutURL         Entry
    31  	OAuthConsumerKey  Entry
    32  }
    33  
    34  type userCounter struct {
    35  	c *UserCounter
    36  
    37  	u user.RawInterface
    38  }
    39  
    40  var _ user.RawInterface = (*userCounter)(nil)
    41  
    42  func (u *userCounter) Current() *user.User {
    43  	u.c.Current.up()
    44  	return u.u.Current()
    45  }
    46  
    47  func (u *userCounter) CurrentOAuth(scopes ...string) (*user.User, error) {
    48  	ret, err := u.u.CurrentOAuth(scopes...)
    49  	return ret, u.c.CurrentOAuth.up(err)
    50  }
    51  
    52  func (u *userCounter) IsAdmin() bool {
    53  	u.c.IsAdmin.up()
    54  	return u.u.IsAdmin()
    55  }
    56  
    57  func (u *userCounter) LoginURL(dest string) (string, error) {
    58  	ret, err := u.u.LoginURL(dest)
    59  	return ret, u.c.LoginURL.up(err)
    60  }
    61  
    62  func (u *userCounter) LoginURLFederated(dest, identity string) (string, error) {
    63  	ret, err := u.u.LoginURLFederated(dest, identity)
    64  	return ret, u.c.LoginURLFederated.up(err)
    65  }
    66  
    67  func (u *userCounter) LogoutURL(dest string) (string, error) {
    68  	ret, err := u.u.LogoutURL(dest)
    69  	return ret, u.c.LogoutURL.up(err)
    70  }
    71  
    72  func (u *userCounter) OAuthConsumerKey() (string, error) {
    73  	ret, err := u.u.OAuthConsumerKey()
    74  	return ret, u.c.OAuthConsumerKey.up(err)
    75  }
    76  
    77  func (u *userCounter) GetTestable() user.Testable {
    78  	return u.u.GetTestable()
    79  }
    80  
    81  // FilterUser installs a counter User filter in the context.
    82  func FilterUser(c context.Context) (context.Context, *UserCounter) {
    83  	state := &UserCounter{}
    84  	return user.AddFilters(c, func(ic context.Context, u user.RawInterface) user.RawInterface {
    85  		return &userCounter{state, u}
    86  	}), state
    87  }