Support bot user
This commit is contained in:
@@ -551,39 +551,6 @@ func GetUserSalt() (string, error) {
|
||||
return hex.EncodeToString(rBytes), nil
|
||||
}
|
||||
|
||||
// NewGhostUser creates and returns a fake user for someone has deleted their account.
|
||||
func NewGhostUser() *User {
|
||||
return &User{
|
||||
ID: -1,
|
||||
Name: "Ghost",
|
||||
LowerName: "ghost",
|
||||
}
|
||||
}
|
||||
|
||||
// NewReplaceUser creates and returns a fake user for external user
|
||||
func NewReplaceUser(name string) *User {
|
||||
return &User{
|
||||
ID: -1,
|
||||
Name: name,
|
||||
LowerName: strings.ToLower(name),
|
||||
}
|
||||
}
|
||||
|
||||
// IsGhost check if user is fake user for a deleted account
|
||||
func (u *User) IsGhost() bool {
|
||||
if u == nil {
|
||||
return false
|
||||
}
|
||||
return u.ID == -1 && u.Name == "Ghost"
|
||||
}
|
||||
|
||||
func (u *User) IsBots() bool {
|
||||
if u == nil {
|
||||
return false
|
||||
}
|
||||
return u.ID == -2 && u.Name == "gitea-bots"
|
||||
}
|
||||
|
||||
var (
|
||||
reservedUsernames = []string{
|
||||
".",
|
||||
@@ -1017,6 +984,20 @@ func GetUserByIDCtx(ctx context.Context, id int64) (*User, error) {
|
||||
return u, nil
|
||||
}
|
||||
|
||||
// GetPossbileUserByID returns the user if id > 0 or return system usrs if id < 0
|
||||
func GetPossbileUserByID(ctx context.Context, id int64) (*User, error) {
|
||||
switch id {
|
||||
case -1:
|
||||
return NewGhostUser(), nil
|
||||
case BotUserID:
|
||||
return NewBotUser(), nil
|
||||
case 0:
|
||||
return nil, ErrUserNotExist{}
|
||||
default:
|
||||
return GetUserByIDCtx(ctx, id)
|
||||
}
|
||||
}
|
||||
|
||||
// GetUserByNameCtx returns user by given name.
|
||||
func GetUserByName(ctx context.Context, name string) (*User, error) {
|
||||
if len(name) == 0 {
|
||||
|
||||
63
models/user/user_system.go
Normal file
63
models/user/user_system.go
Normal file
@@ -0,0 +1,63 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/structs"
|
||||
)
|
||||
|
||||
// NewGhostUser creates and returns a fake user for someone has deleted their account.
|
||||
func NewGhostUser() *User {
|
||||
return &User{
|
||||
ID: -1,
|
||||
Name: "Ghost",
|
||||
LowerName: "ghost",
|
||||
}
|
||||
}
|
||||
|
||||
// IsGhost check if user is fake user for a deleted account
|
||||
func (u *User) IsGhost() bool {
|
||||
if u == nil {
|
||||
return false
|
||||
}
|
||||
return u.ID == -1 && u.Name == "Ghost"
|
||||
}
|
||||
|
||||
// NewReplaceUser creates and returns a fake user for external user
|
||||
func NewReplaceUser(name string) *User {
|
||||
return &User{
|
||||
ID: -1,
|
||||
Name: name,
|
||||
LowerName: strings.ToLower(name),
|
||||
}
|
||||
}
|
||||
|
||||
const BotUserID = -2
|
||||
|
||||
// NewBotUser creates and returns a fake user for running the build.
|
||||
func NewBotUser() *User {
|
||||
return &User{
|
||||
ID: BotUserID,
|
||||
Name: "gitea-bots",
|
||||
LowerName: "gitea-bots",
|
||||
IsActive: true,
|
||||
FullName: "Gitea Bots",
|
||||
Email: "teabot@gitea.io",
|
||||
KeepEmailPrivate: true,
|
||||
LoginName: "gitea-bots",
|
||||
Type: UserTypeIndividual,
|
||||
AllowCreateOrganization: true,
|
||||
Visibility: structs.VisibleTypePublic,
|
||||
}
|
||||
}
|
||||
|
||||
func (u *User) IsBots() bool {
|
||||
if u == nil {
|
||||
return false
|
||||
}
|
||||
return u.ID == BotUserID && u.Name == "gitea-bots"
|
||||
}
|
||||
Reference in New Issue
Block a user