From 67f3f66be481afbd6b85baef1cfb9249d3b0be60 Mon Sep 17 00:00:00 2001 From: Clemens Date: Wed, 7 Feb 2024 15:37:48 +0100 Subject: [PATCH] Created FederatedUser --- models/user/federated_user.go | 42 +++++++++++++++++++++++++++++++ modules/validation/validatable.go | 4 +++ 2 files changed, 46 insertions(+) create mode 100644 models/user/federated_user.go diff --git a/models/user/federated_user.go b/models/user/federated_user.go new file mode 100644 index 0000000000..134efc38f2 --- /dev/null +++ b/models/user/federated_user.go @@ -0,0 +1,42 @@ +// Copyright 2024 The Forgejo Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package user + +import ( + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/modules/validation" +) + +func init() { + db.RegisterModel(new(FederatedUser)) +} + +type FederatedUser struct { + ID int64 `xorm:"pk NOT NULL"` + UserID int64 `xorm:"NOT NULL"` + ExternalID string `xorm:"TEXT UNIQUE(federation_mapping) NOT NULL"` + FederationHostID int64 `xorm:"UNIQUE(federation_mapping) NOT NULL"` + RawData map[string]any `xorm:"TEXT JSON"` +} + +func NewFederatedUser(userID int64, externalID string, federationHostID int64, rawData map[string]any) (FederatedUser, error) { + result := FederatedUser{ + UserID: userID, + ExternalID: externalID, + FederationHostID: federationHostID, + RawData: rawData, + } + if valid, err := validation.IsValid(result); !valid { + return FederatedUser{}, err + } + return result, nil +} + +func (user FederatedUser) Validate() []string { + var result []string + result = append(result, validation.ValidateNotEmpty(user.UserID, "UserID")...) + result = append(result, validation.ValidateNotEmpty(user.ExternalID, "ExternalID")...) + result = append(result, validation.ValidateNotEmpty(user.FederationHostID, "FederationHostID")...) + return result +} diff --git a/modules/validation/validatable.go b/modules/validation/validatable.go index 27317bdd91..24b1ce6682 100644 --- a/modules/validation/validatable.go +++ b/modules/validation/validatable.go @@ -35,6 +35,10 @@ func ValidateNotEmpty(value any, fieldName string) []string { if v.IsZero() { isValid = false } + case int64: + if v == 0 { + isValid = false + } default: isValid = false }