48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package model
|
|
|
|
import (
|
|
"code.gitea.io/sdk/gitea"
|
|
"golang.org/x/text/transform"
|
|
"golang.org/x/text/unicode/norm"
|
|
"math/rand"
|
|
"unicode"
|
|
)
|
|
|
|
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456!@#$%")
|
|
|
|
func randSeq(n int) string {
|
|
b := make([]rune, n)
|
|
for i := range b {
|
|
b[i] = letters[rand.Intn(len(letters))]
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
type PostUserProxy struct {
|
|
Email string `json:"email"`
|
|
FirstName string `json:"first_name"`
|
|
LastName string `json:"last_name"`
|
|
}
|
|
|
|
func isMn(r rune) bool {
|
|
return unicode.Is(unicode.Mn, r)
|
|
}
|
|
|
|
func (c PostUserProxy) TransformToGiteaUser() (gitea.CreateUserOption) {
|
|
|
|
t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC)
|
|
username, _, _ := transform.String(t, c.FirstName+c.LastName)
|
|
|
|
giteaObject := gitea.CreateUserOption{
|
|
SourceID: 0,
|
|
LoginName: c.Email,
|
|
Username: username,
|
|
FullName: c.FirstName + " " + c.LastName,
|
|
Email: c.Email,
|
|
Password: randSeq(12),
|
|
MustChangePassword: nil,
|
|
SendNotify: false,
|
|
}
|
|
return giteaObject
|
|
}
|