1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
// Copyright Earl Warren <contact@earl-warren.org>
// Copyright Loïc Dachary <loic@dachary.org>
// SPDX-License-Identifier: MIT
package forgejo
import (
"context"
"fmt"
"strings"
"code.forgejo.org/f3/gof3/v3/f3"
"code.forgejo.org/f3/gof3/v3/id"
f3_tree "code.forgejo.org/f3/gof3/v3/tree/f3"
"code.forgejo.org/f3/gof3/v3/tree/generic"
"code.forgejo.org/f3/gof3/v3/util"
forgejo_sdk "code.forgejo.org/f3/gof3/v3/forges/forgejo/sdk"
"github.com/hashicorp/go-version"
)
type user struct {
common
forgejoUser *forgejo_sdk.User
Password string
}
var _ f3_tree.ForgeDriverInterface = &user{}
const fakeEmailSuffix = ".fakeemail"
func fromFakeEmail(mail string) string {
return strings.TrimSuffix(mail, fakeEmailSuffix)
}
func toFakeEmail(mail string) string {
if !strings.HasSuffix(mail, fakeEmailSuffix) {
return mail + fakeEmailSuffix
}
return mail
}
func newUser() generic.NodeDriverInterface {
return &user{}
}
const (
GhostUserID = int64(-1)
GhostUserName = "Ghost"
ActionsUserID = int64(-2)
ActionsUserName = "forgejo-actions"
)
var SystemUserMinVersion = ForgejoVersion600
func getGhostUser() *forgejo_sdk.User {
return &forgejo_sdk.User{
ID: GhostUserID,
UserName: GhostUserName,
FullName: "Ghost",
Email: "ghost@forgejo.org",
}
}
func getActionsUser() *forgejo_sdk.User {
return &forgejo_sdk.User{
ID: ActionsUserID,
UserName: ActionsUserName,
FullName: "Forgejo Actions",
Email: "noreply@forgejo.org",
}
}
func getSystemUserByID(id int64) *forgejo_sdk.User {
switch id {
case GhostUserID:
return getGhostUser()
case ActionsUserID:
return getActionsUser()
default:
return nil
}
}
func getSystemUserByName(name string) *forgejo_sdk.User {
switch name {
case GhostUserName:
return getGhostUser()
case ActionsUserName:
return getActionsUser()
default:
return nil
}
}
// Hardcode system users for Forgejo versions that do not support it
func (o *user) getSystemUserByID(ctx context.Context, minVersion *version.Version, id int64) *forgejo_sdk.User {
if id < 0 {
if o.getVersion().LessThan(minVersion) {
return getSystemUserByID(id)
}
}
return nil
}
func (o *user) SetNative(user any) {
o.forgejoUser = user.(*forgejo_sdk.User)
}
func (o *user) GetNativeID() string {
return fmt.Sprintf("%d", o.forgejoUser.ID)
}
func (o *user) NewFormat() f3.Interface {
node := o.GetNode()
return node.GetTree().(f3_tree.TreeInterface).NewFormat(node.GetKind())
}
func (o *user) ToFormat() f3.Interface {
if o.forgejoUser == nil {
return o.NewFormat()
}
return &f3.User{
Common: f3.NewCommon(fmt.Sprintf("%d", o.forgejoUser.ID)),
UserName: o.forgejoUser.UserName,
Name: o.forgejoUser.FullName,
Email: o.forgejoUser.Email,
IsAdmin: o.forgejoUser.IsAdmin,
Password: o.Password,
}
}
func (o *user) FromFormat(content f3.Interface) {
user := content.(*f3.User)
o.forgejoUser = &forgejo_sdk.User{
ID: util.ParseInt(user.GetID()),
UserName: user.UserName,
FullName: user.Name,
Email: user.Email,
IsAdmin: user.IsAdmin,
}
o.Password = user.Password
}
func (o *user) Get(ctx context.Context) bool {
node := o.GetNode()
o.Trace("%s", node.GetID())
if user := o.getSystemUserByID(ctx, SystemUserMinVersion, node.GetID().Int64()); user != nil {
o.forgejoUser = user
return true
}
var user *forgejo_sdk.User
var err error
if node.GetID() != id.NilID {
user, _, err = o.getClient().GetUserByID(node.GetID().Int64())
} else {
panic("GetID() == 0")
}
if err != nil {
if strings.Contains(err.Error(), "user not found") {
return false
}
panic(fmt.Errorf("user %v %w", o, err))
}
user.Email = fromFakeEmail(user.Email)
o.forgejoUser = user
return true
}
func (o *user) Patch(context.Context) {
node := o.GetNode()
o.Trace("%s", node.GetID())
}
func (o *user) Put(context.Context) id.NodeID {
if user := getSystemUserByName(o.forgejoUser.UserName); user != nil {
o.forgejoUser.UserName += "PLACEHOLDER"
o.forgejoUser.Email = o.forgejoUser.UserName + "@example.com"
}
mustChangePassword := false
if o.Password == "" {
o.Password = util.RandSeq(30)
}
u, _, err := o.getClient().AdminCreateUser(forgejo_sdk.CreateUserOption{
Username: o.forgejoUser.UserName,
FullName: o.forgejoUser.FullName,
Email: toFakeEmail(o.forgejoUser.Email),
Password: o.Password,
MustChangePassword: &mustChangePassword,
})
if err != nil {
panic(fmt.Errorf("%v: %w", o.forgejoUser, err))
}
o.forgejoUser = u
o.forgejoUser.Email = fromFakeEmail(o.forgejoUser.Email)
o.Trace("%s %d", o.forgejoUser.UserName, o.forgejoUser.ID)
return id.NewNodeID(u.ID)
}
func (o *user) Delete(ctx context.Context) {
if user := getSystemUserByID(o.forgejoUser.ID); user != nil {
return
}
_, err := o.getClient().AdminDeleteUser(o.forgejoUser.UserName)
if err != nil {
panic(fmt.Errorf("%v: %v", o.forgejoUser.UserName, err))
}
}
|