snapshot of https://github.com/go-gitea/gitea.git (main, 2026-09-15; blobs > 131072B omitted)
Some checks failed
cache-seeder / prune (push) Has been skipped
cache-seeder / frontend (push) Failing after 3s
cache-seeder / python (push) Failing after 3s
cache-seeder / gobuild (push) Failing after 4s
cache-seeder / lint (bindata, lint-backend) (push) Failing after 3s
giteabot backport / giteabot (push) Has been skipped
giteabot / giteabot (push) Has been skipped
cache-prune / prune (push) Has been skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Has been cancelled
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Has been cancelled
release-nightly / nightly-binary (push) Has been cancelled
release-nightly / nightly-container (push) Has been cancelled
cron-renovate / cron-renovate (push) Has been skipped
Some checks failed
cache-seeder / prune (push) Has been skipped
cache-seeder / frontend (push) Failing after 3s
cache-seeder / python (push) Failing after 3s
cache-seeder / gobuild (push) Failing after 4s
cache-seeder / lint (bindata, lint-backend) (push) Failing after 3s
giteabot backport / giteabot (push) Has been skipped
giteabot / giteabot (push) Has been skipped
cache-prune / prune (push) Has been skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Has been cancelled
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Has been cancelled
release-nightly / nightly-binary (push) Has been cancelled
release-nightly / nightly-container (push) Has been cancelled
cron-renovate / cron-renovate (push) Has been skipped
This commit is contained in:
130
models/user/user_system.go
Normal file
130
models/user/user_system.go
Normal file
@@ -0,0 +1,130 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"gitea.dev/modules/structs"
|
||||
)
|
||||
|
||||
const (
|
||||
GhostUserID int64 = -1
|
||||
GhostUserName = "Ghost"
|
||||
)
|
||||
|
||||
// NewGhostUser creates and returns a fake user for someone has deleted their account.
|
||||
func NewGhostUser() *User {
|
||||
return &User{
|
||||
ID: GhostUserID,
|
||||
Name: GhostUserName,
|
||||
LowerName: strings.ToLower(GhostUserName),
|
||||
}
|
||||
}
|
||||
|
||||
// IsGhost check if user is fake user for a deleted account
|
||||
func (u *User) IsGhost() bool {
|
||||
if u == nil {
|
||||
return false
|
||||
}
|
||||
return u.ID == GhostUserID && u.Name == GhostUserName
|
||||
}
|
||||
|
||||
// newSystemUser creates and returns a fake user for system use.
|
||||
// The builtin username can be wrapped in parentheses to avoid conflicts with real usernames.
|
||||
func newSystemUser(id int64, name, fullName string) *User {
|
||||
return &User{
|
||||
ID: id,
|
||||
Name: name,
|
||||
LowerName: strings.ToLower(name),
|
||||
IsActive: true,
|
||||
FullName: fullName,
|
||||
Type: UserTypeBot,
|
||||
Visibility: structs.VisibleTypePublic,
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
ActionsUserID int64 = -2
|
||||
DeployKeyUserID int64 = -3
|
||||
CliUserID int64 = -4
|
||||
AuthSourceUserID int64 = -5
|
||||
)
|
||||
|
||||
// NewActionsUser creates and returns a fake user for running the actions.
|
||||
func NewActionsUser() *User {
|
||||
return newSystemUser(ActionsUserID, "gitea-actions", "Gitea Actions")
|
||||
}
|
||||
|
||||
func GetActionsUserTaskID(u *User) (int64, bool) {
|
||||
if u == nil || u.ExtDoerData == nil || u.ID != ActionsUserID {
|
||||
return 0, false
|
||||
}
|
||||
extData := u.ExtDoerData.(*extDoerGiteaActions) //nolint:forcetypeassert // must be valid
|
||||
return extData.TaskID, true
|
||||
}
|
||||
|
||||
func NewActionsUserWithTaskID(id int64) *User {
|
||||
u := NewActionsUser()
|
||||
u.ExtDoerData = &extDoerGiteaActions{TaskID: id}
|
||||
return u
|
||||
}
|
||||
|
||||
func NewDeployKeyUser() *User {
|
||||
return newSystemUser(DeployKeyUserID, "(deploy-key)", "Deploy Key")
|
||||
}
|
||||
|
||||
func GetDeployKeyUserDeployKeyID(u *User) (int64, bool) {
|
||||
// ok, the function name seems wordy, it is intentionally to distinguish from other "keys" like "public key id"
|
||||
// it was a mess in the "pre-receive" hook code
|
||||
if u == nil || u.ExtDoerData == nil || u.ID != DeployKeyUserID {
|
||||
return 0, false
|
||||
}
|
||||
extData := u.ExtDoerData.(*extDoerDeployKey) //nolint:forcetypeassert // must be valid
|
||||
return extData.DeployKeyID, true
|
||||
}
|
||||
|
||||
func NewDeployKeyUserWithKeyID(id int64) *User {
|
||||
u := NewDeployKeyUser()
|
||||
u.ExtDoerData = &extDoerDeployKey{DeployKeyID: id}
|
||||
return u
|
||||
}
|
||||
|
||||
func NewCliUser() *User {
|
||||
// for audit log only
|
||||
return newSystemUser(CliUserID, "(gitea-cli)", "Gitea CLI")
|
||||
}
|
||||
|
||||
func NewAuthSourceUser() *User {
|
||||
// for audit log only
|
||||
return newSystemUser(AuthSourceUserID, "(gitea-auth-source)", "Gitea Auth Source")
|
||||
}
|
||||
|
||||
func GetSystemUserByName(name string) *User {
|
||||
lowerName := strings.ToLower(name)
|
||||
uid := globalVars().systemUserNameIdMap[lowerName]
|
||||
if fn := globalVars().systemUserNewFuncs[uid]; fn != nil {
|
||||
return fn()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetDoerPermissionUser(ctx context.Context, id int64, extDoerData string) (u *User, _ error) {
|
||||
if id > 0 {
|
||||
return GetUserByID(ctx, id)
|
||||
}
|
||||
switch id {
|
||||
case ActionsUserID:
|
||||
u = NewActionsUser()
|
||||
u.ExtDoerData = &extDoerGiteaActions{}
|
||||
case DeployKeyUserID:
|
||||
u = NewDeployKeyUser()
|
||||
u.ExtDoerData = &extDoerDeployKey{}
|
||||
default:
|
||||
// other system users are not real doers for the permission system
|
||||
return nil, ErrUserNotExist{UID: id}
|
||||
}
|
||||
return u, u.ExtDoerData.DecodeFromString(extDoerData)
|
||||
}
|
||||
Reference in New Issue
Block a user