Skip to content

Commit 8971ecd

Browse files
committed
feat(go): add access token count to ReturnUser model;
1 parent 7b970cb commit 8971ecd

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

controllers/user.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package controller
22

33
import (
4+
"context"
45
"encoding/json"
56
"errors"
67
"fmt"
8+
"github.com/gravitl/netmaker/db"
79
"net/http"
810
"reflect"
911
"time"
@@ -584,6 +586,18 @@ func getUsers(w http.ResponseWriter, r *http.Request) {
584586
return
585587
}
586588

589+
for i, user := range users {
590+
// only setting num_access_tokens here, because only UI needs it.
591+
user.NumAccessTokens, err = (&schema.UserAccessToken{
592+
UserName: user.UserName,
593+
}).CountByUser(db.WithContext(context.TODO()))
594+
if err != nil {
595+
continue
596+
}
597+
598+
users[i] = user
599+
}
600+
587601
logic.SortUsers(users[:])
588602
logger.Log(2, r.Header.Get("user"), "fetched users")
589603
json.NewEncoder(w).Encode(users)

logic/auth.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func GetUsers() ([]models.ReturnUser, error) {
106106
if err != nil {
107107
continue // get users
108108
}
109+
109110
users = append(users, user)
110111
}
111112

models/user_mgmt.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ type ReturnUser struct {
190190
PlatformRoleID UserRoleID `json:"platform_role_id"`
191191
NetworkRoles map[NetworkID]map[UserRoleID]struct{} `json:"network_roles"`
192192
LastLoginTime time.Time `json:"last_login_time"`
193+
NumAccessTokens int `json:"num_access_tokens"`
193194
}
194195

195196
// UserAuthParams - user auth params struct

schema/user_access_token.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ func (a *UserAccessToken) ListByUser(ctx context.Context) (ats []UserAccessToken
4343
return
4444
}
4545

46+
func (a *UserAccessToken) CountByUser(ctx context.Context) (int, error) {
47+
var count int64
48+
err := db.FromContext(ctx).Model(&UserAccessToken{}).
49+
Where("user_name = ?", a.UserName).
50+
Count(&count).
51+
Error
52+
return int(count), err
53+
}
54+
4655
func (a *UserAccessToken) Delete(ctx context.Context) error {
4756
return db.FromContext(ctx).Model(&UserAccessToken{}).Where("id = ?", a.ID).Delete(&a).Error
4857
}

0 commit comments

Comments
 (0)