mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-19 08:46:38 +00:00
switch PATs to map and add deletion
This commit is contained in:
@@ -46,7 +46,7 @@ type User struct {
|
||||
Role UserRole
|
||||
// AutoGroups is a list of Group IDs to auto-assign to peers registered by this user
|
||||
AutoGroups []string
|
||||
PATs []PersonalAccessToken
|
||||
PATs map[string]*PersonalAccessToken
|
||||
}
|
||||
|
||||
// IsAdmin returns true if user is an admin, false otherwise
|
||||
@@ -94,8 +94,12 @@ func (u *User) toUserInfo(userData *idp.UserData) (*UserInfo, error) {
|
||||
func (u *User) Copy() *User {
|
||||
autoGroups := make([]string, len(u.AutoGroups))
|
||||
copy(autoGroups, u.AutoGroups)
|
||||
pats := make([]PersonalAccessToken, len(u.PATs))
|
||||
copy(pats, u.PATs)
|
||||
pats := make(map[string]*PersonalAccessToken, len(u.PATs))
|
||||
for k, v := range u.PATs {
|
||||
patCopy := new(PersonalAccessToken)
|
||||
*patCopy = *v
|
||||
pats[k] = patCopy
|
||||
}
|
||||
return &User{
|
||||
Id: u.Id,
|
||||
Role: u.Role,
|
||||
@@ -190,7 +194,7 @@ func (am *DefaultAccountManager) CreateUser(accountID, userID string, invite *Us
|
||||
}
|
||||
|
||||
// AddPATToUser takes the userID and the accountID the user belongs to and assigns a provided PersonalAccessToken to that user
|
||||
func (am *DefaultAccountManager) AddPATToUser(accountID string, userID string, pat PersonalAccessToken) error {
|
||||
func (am *DefaultAccountManager) AddPATToUser(accountID string, userID string, pat *PersonalAccessToken) error {
|
||||
unlock := am.Store.AcquireAccountLock(accountID)
|
||||
defer unlock()
|
||||
|
||||
@@ -204,7 +208,40 @@ func (am *DefaultAccountManager) AddPATToUser(accountID string, userID string, p
|
||||
return status.Errorf(status.NotFound, "user not found")
|
||||
}
|
||||
|
||||
user.PATs = append(user.PATs, pat)
|
||||
user.PATs[pat.ID] = pat
|
||||
|
||||
err = am.Store.SaveAccount(account)
|
||||
return err
|
||||
}
|
||||
|
||||
func (am *DefaultAccountManager) DeletePAT(accountID string, userID string, tokenID string) error {
|
||||
unlock := am.Store.AcquireAccountLock(accountID)
|
||||
defer unlock()
|
||||
|
||||
account, err := am.Store.GetAccount(accountID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
user := account.Users[userID]
|
||||
if user == nil {
|
||||
return status.Errorf(status.NotFound, "user not found")
|
||||
}
|
||||
|
||||
pat := user.PATs["tokenID"]
|
||||
if pat == nil {
|
||||
return status.Errorf(status.NotFound, "PAT not found")
|
||||
}
|
||||
|
||||
err = am.Store.DeleteTokenID2UserIDIndex(pat.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = am.Store.DeleteHashedPAT2TokenIDIndex(pat.HashedToken)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
delete(user.PATs, tokenID)
|
||||
|
||||
err = am.Store.SaveAccount(account)
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user