i18n: Translate all Chinese to English across the codebase
Translate Chinese comments, log/error messages, validator labels and Swagger annotations to English throughout the source code, generated Swagger docs, config files and CI workflows. Make the English README primary: README.md now holds the English docs and README_EN.md holds the Chinese version, with cross-language links updated accordingly. Note: the generated docs/ swagger files were translated in place; run `go generate ./...` (swag init) to regenerate them from the now-English annotations when a Go toolchain is available. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+17
-17
@@ -53,29 +53,29 @@ func (s *AddressBookService) AddAddressBook(ab *model.AddressBook) error {
|
||||
|
||||
// UpdateAddressBook
|
||||
func (s *AddressBookService) UpdateAddressBook(abs []*model.AddressBook, userId uint) error {
|
||||
//比较peers和数据库中的数据,如果peers中的数据在数据库中不存在,则添加,如果存在则更新,如果数据库中的数据在peers中不存在,则删除
|
||||
// 开始事务
|
||||
// compare peers with the data in the database: if a peer does not exist in the database, add it; if it exists, update it; if data in the database does not exist in peers, delete it
|
||||
// begin transaction
|
||||
tx := DB.Begin()
|
||||
//1. 获取数据库中的数据
|
||||
// 1. get the data in the database
|
||||
var dbABs []*model.AddressBook
|
||||
tx.Where("user_id = ?", userId).Find(&dbABs)
|
||||
//2. 比较peers和数据库中的数据
|
||||
//2.1 获取peers中的id
|
||||
// 2. compare peers with the data in the database
|
||||
// 2.1 get the ids in peers
|
||||
aBIds := make(map[string]*model.AddressBook)
|
||||
for _, ab := range abs {
|
||||
aBIds[ab.Id] = ab
|
||||
}
|
||||
//2.2 获取数据库中的id
|
||||
// 2.2 get the ids from the database
|
||||
dbABIds := make(map[string]*model.AddressBook)
|
||||
for _, dbAb := range dbABs {
|
||||
dbABIds[dbAb.Id] = dbAb
|
||||
}
|
||||
//2.3 比较peers和数据库中的数据
|
||||
// 2.3 compare peers with the data in the database
|
||||
for id, ab := range aBIds {
|
||||
dbAB, ok := dbABIds[id]
|
||||
ab.UserId = userId
|
||||
if !ok {
|
||||
//添加
|
||||
//add
|
||||
if ab.Platform == "" || ab.Username == "" || ab.Hostname == "" {
|
||||
peer := AllService.PeerService.FindById(ab.Id)
|
||||
if peer.RowId != 0 {
|
||||
@@ -86,11 +86,11 @@ func (s *AddressBookService) UpdateAddressBook(abs []*model.AddressBook, userId
|
||||
}
|
||||
tx.Create(ab)
|
||||
} else {
|
||||
//更新
|
||||
//update
|
||||
tx.Model(&model.AddressBook{}).Where("row_id = ?", dbAB.RowId).Updates(ab)
|
||||
}
|
||||
}
|
||||
//2.4 删除
|
||||
// 2.4 delete
|
||||
for id, dbAB := range dbABIds {
|
||||
_, ok := aBIds[id]
|
||||
if !ok {
|
||||
@@ -126,7 +126,7 @@ func (s *AddressBookService) FromPeer(peer *model.Peer) (a *model.AddressBook) {
|
||||
return a
|
||||
}
|
||||
|
||||
// Create 创建
|
||||
// Create create
|
||||
func (s *AddressBookService) Create(u *model.AddressBook) error {
|
||||
res := DB.Create(u).Error
|
||||
return res
|
||||
@@ -135,22 +135,22 @@ func (s *AddressBookService) Delete(u *model.AddressBook) error {
|
||||
return DB.Delete(u).Error
|
||||
}
|
||||
|
||||
// Update 更新
|
||||
// Update update
|
||||
func (s *AddressBookService) Update(u *model.AddressBook) error {
|
||||
return DB.Model(u).Updates(u).Error
|
||||
}
|
||||
|
||||
// UpdateByMap 更新
|
||||
// UpdateByMap update
|
||||
func (s *AddressBookService) UpdateByMap(u *model.AddressBook, data map[string]interface{}) error {
|
||||
return DB.Model(u).Updates(data).Error
|
||||
}
|
||||
|
||||
// UpdateAll 更新
|
||||
// UpdateAll update
|
||||
func (s *AddressBookService) UpdateAll(u *model.AddressBook) error {
|
||||
return DB.Model(u).Select("*").Omit("created_at").Updates(u).Error
|
||||
}
|
||||
|
||||
// ShareByWebClient 分享
|
||||
// ShareByWebClient share
|
||||
func (s *AddressBookService) ShareByWebClient(m *model.ShareRecord) error {
|
||||
m.ShareToken = uuid.New().String()
|
||||
return DB.Create(m).Error
|
||||
@@ -279,7 +279,7 @@ func (s *AddressBookService) UpdateCollection(t *model.AddressBookCollection) er
|
||||
}
|
||||
|
||||
func (s *AddressBookService) DeleteCollection(t *model.AddressBookCollection) error {
|
||||
//删除集合下的所有规则、地址簿,再删除集合
|
||||
//delete all rules and address books under the collection, then delete the collection
|
||||
tx := DB.Begin()
|
||||
tx.Where("collection_id = ?", t.Id).Delete(&model.AddressBookCollectionRule{})
|
||||
tx.Where("collection_id = ?", t.Id).Delete(&model.AddressBook{})
|
||||
@@ -326,7 +326,7 @@ func (s *AddressBookService) DeleteRule(t *model.AddressBookCollectionRule) erro
|
||||
return DB.Delete(t).Error
|
||||
}
|
||||
|
||||
// CheckCollectionOwner 检查Collection的所有者
|
||||
// CheckCollectionOwner checkCollection's owner
|
||||
func (s *AddressBookService) CheckCollectionOwner(uid uint, cid uint) bool {
|
||||
p := s.CollectionInfoById(cid)
|
||||
return p.UserId == uid
|
||||
|
||||
+8
-8
@@ -9,25 +9,25 @@ import (
|
||||
func TestGetAppVersion(t *testing.T) {
|
||||
s := &AppService{}
|
||||
v := s.GetAppVersion()
|
||||
// 打印结果
|
||||
// print result
|
||||
t.Logf("App Version: %s", v)
|
||||
}
|
||||
|
||||
func TestMultipleGetAppVersion(t *testing.T) {
|
||||
s := &AppService{}
|
||||
//并发测试
|
||||
// 使用 WaitGroup 等待所有 goroutine 完成
|
||||
//concurrency test
|
||||
// use a WaitGroup to wait for all goroutines to finish
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(10) // 启动 10 个 goroutine
|
||||
// 启动 10 个 goroutine
|
||||
wg.Add(10) // start 10 goroutine
|
||||
// start 10 goroutine
|
||||
for i := 0; i < 10; i++ {
|
||||
go func() {
|
||||
defer wg.Done() // 完成后减少计数
|
||||
defer wg.Done() // decrement the count when done
|
||||
v := s.GetAppVersion()
|
||||
// 打印结果
|
||||
// print result
|
||||
t.Logf("App Version: %s", v)
|
||||
}()
|
||||
}
|
||||
// 等待所有 goroutine 完成
|
||||
// wait for all goroutine done
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
+3
-3
@@ -22,7 +22,7 @@ func (as *AuditService) AuditConnList(page, pageSize uint, where func(tx *gorm.D
|
||||
return
|
||||
}
|
||||
|
||||
// Create 创建
|
||||
// Create create
|
||||
func (as *AuditService) CreateAuditConn(u *model.AuditConn) error {
|
||||
res := DB.Create(u).Error
|
||||
return res
|
||||
@@ -31,7 +31,7 @@ func (as *AuditService) DeleteAuditConn(u *model.AuditConn) error {
|
||||
return DB.Delete(u).Error
|
||||
}
|
||||
|
||||
// Update 更新
|
||||
// Update update
|
||||
func (as *AuditService) UpdateAuditConn(u *model.AuditConn) error {
|
||||
return DB.Model(u).Updates(u).Error
|
||||
}
|
||||
@@ -80,7 +80,7 @@ func (as *AuditService) DeleteAuditFile(u *model.AuditFile) error {
|
||||
return DB.Delete(u).Error
|
||||
}
|
||||
|
||||
// Update 更新
|
||||
// Update update
|
||||
func (as *AuditService) UpdateAuditFile(u *model.AuditFile) error {
|
||||
return DB.Model(u).Updates(u).Error
|
||||
}
|
||||
|
||||
+4
-4
@@ -8,7 +8,7 @@ import (
|
||||
type GroupService struct {
|
||||
}
|
||||
|
||||
// InfoById 根据用户id取用户信息
|
||||
// InfoById gets the group info by id
|
||||
func (us *GroupService) InfoById(id uint) *model.Group {
|
||||
u := &model.Group{}
|
||||
DB.Where("id = ?", id).First(u)
|
||||
@@ -29,7 +29,7 @@ func (us *GroupService) List(page, pageSize uint, where func(tx *gorm.DB)) (res
|
||||
return
|
||||
}
|
||||
|
||||
// Create 创建
|
||||
// Create create
|
||||
func (us *GroupService) Create(u *model.Group) error {
|
||||
res := DB.Create(u).Error
|
||||
return res
|
||||
@@ -38,12 +38,12 @@ func (us *GroupService) Delete(u *model.Group) error {
|
||||
return DB.Delete(u).Error
|
||||
}
|
||||
|
||||
// Update 更新
|
||||
// Update update
|
||||
func (us *GroupService) Update(u *model.Group) error {
|
||||
return DB.Model(u).Updates(u).Error
|
||||
}
|
||||
|
||||
// DeviceGroupInfoById 根据用户id取用户信息
|
||||
// DeviceGroupInfoById gets the device group info by id
|
||||
func (us *GroupService) DeviceGroupInfoById(id uint) *model.DeviceGroup {
|
||||
u := &model.DeviceGroup{}
|
||||
DB.Where("id = ?", id).First(u)
|
||||
|
||||
+3
-3
@@ -8,7 +8,7 @@ import (
|
||||
type LoginLogService struct {
|
||||
}
|
||||
|
||||
// InfoById 根据用户id取用户信息
|
||||
// InfoById gets the login log info by id
|
||||
func (us *LoginLogService) InfoById(id uint) *model.LoginLog {
|
||||
u := &model.LoginLog{}
|
||||
DB.Where("id = ?", id).First(u)
|
||||
@@ -29,7 +29,7 @@ func (us *LoginLogService) List(page, pageSize uint, where func(tx *gorm.DB)) (r
|
||||
return
|
||||
}
|
||||
|
||||
// Create 创建
|
||||
// Create create
|
||||
func (us *LoginLogService) Create(u *model.LoginLog) error {
|
||||
res := DB.Create(u).Error
|
||||
return res
|
||||
@@ -38,7 +38,7 @@ func (us *LoginLogService) Delete(u *model.LoginLog) error {
|
||||
return DB.Delete(u).Error
|
||||
}
|
||||
|
||||
// Update 更新
|
||||
// Update update
|
||||
func (us *LoginLogService) Update(u *model.LoginLog) error {
|
||||
return DB.Model(u).Updates(u).Error
|
||||
}
|
||||
|
||||
+20
-20
@@ -36,7 +36,7 @@ type OidcEndpoint struct {
|
||||
|
||||
type OauthCacheItem struct {
|
||||
UserId uint `json:"user_id"`
|
||||
Id string `json:"id"` //rustdesk的设备ID
|
||||
Id string `json:"id"` //rustdesk's devicesID
|
||||
Op string `json:"op"`
|
||||
Action string `json:"action"`
|
||||
Uuid string `json:"uuid"`
|
||||
@@ -196,7 +196,7 @@ func (os *OauthService) GetOauthConfig(op string) (err error, oauthInfo *model.O
|
||||
provider = os.LinuxdoProvider()
|
||||
oauthConfig.Endpoint = provider.Endpoint()
|
||||
oauthConfig.Scopes = []string{"profile"}
|
||||
//case model.OauthTypeGoogle: //google单独出来,可以少一次FetchOidcEndpoint请求
|
||||
//case model.OauthTypeGoogle: //google separately to save one FetchOidcEndpointrequest
|
||||
// oauthConfig.Endpoint = google.Endpoint
|
||||
// oauthConfig.Scopes = os.constructScopes(oauthInfo.Scopes)
|
||||
case model.OauthTypeOidc, model.OauthTypeGoogle:
|
||||
@@ -234,7 +234,7 @@ func getHTTPClientWithProxy() *http.Client {
|
||||
}
|
||||
func (os *OauthService) callbackBase(oauthConfig *oauth2.Config, provider *oidc.Provider, code string, verifier string, nonce string, userData interface{}) (err error, client *http.Client) {
|
||||
|
||||
// 设置代理客户端
|
||||
// set proxy client
|
||||
httpClient := getHTTPClientWithProxy()
|
||||
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, httpClient)
|
||||
|
||||
@@ -250,10 +250,10 @@ func (os *OauthService) callbackBase(oauthConfig *oauth2.Config, provider *oidc.
|
||||
return errors.New("GetOauthTokenError"), nil
|
||||
}
|
||||
|
||||
// 获取 ID Token, github没有id_token
|
||||
// get the ID Token; github has no id_token
|
||||
rawIDToken, ok := token.Extra("id_token").(string)
|
||||
if ok && rawIDToken != "" {
|
||||
// 验证 ID Token
|
||||
// verify ID Token
|
||||
v := provider.Verifier(&oidc.Config{ClientID: oauthConfig.ClientID})
|
||||
idToken, err2 := v.Verify(ctx, rawIDToken)
|
||||
if err2 != nil {
|
||||
@@ -261,7 +261,7 @@ func (os *OauthService) callbackBase(oauthConfig *oauth2.Config, provider *oidc.
|
||||
return errors.New("IdTokenVerifyError"), nil
|
||||
}
|
||||
if nonce != "" {
|
||||
// 验证 nonce
|
||||
// verify nonce
|
||||
var claims struct {
|
||||
Nonce string `json:"nonce"`
|
||||
}
|
||||
@@ -277,7 +277,7 @@ func (os *OauthService) callbackBase(oauthConfig *oauth2.Config, provider *oidc.
|
||||
}
|
||||
}
|
||||
|
||||
// 获取用户信息
|
||||
// get user info
|
||||
client = oauthConfig.Client(ctx, token)
|
||||
resp, err := client.Get(provider.UserInfoEndpoint())
|
||||
if err != nil {
|
||||
@@ -290,7 +290,7 @@ func (os *OauthService) callbackBase(oauthConfig *oauth2.Config, provider *oidc.
|
||||
}
|
||||
}()
|
||||
|
||||
// 解析用户信息
|
||||
// parse user info
|
||||
if err = json.NewDecoder(resp.Body).Decode(userData); err != nil {
|
||||
Logger.Warn("failed decoding user info: ", err)
|
||||
return errors.New("DecodeOauthUserInfoError"), nil
|
||||
@@ -299,7 +299,7 @@ func (os *OauthService) callbackBase(oauthConfig *oauth2.Config, provider *oidc.
|
||||
return nil, client
|
||||
}
|
||||
|
||||
// githubCallback github回调
|
||||
// githubCallback handles the github callback
|
||||
func (os *OauthService) githubCallback(oauthConfig *oauth2.Config, provider *oidc.Provider, code, verifier, nonce string) (error, *model.OauthUser) {
|
||||
var user = &model.GithubUser{}
|
||||
err, client := os.callbackBase(oauthConfig, provider, code, verifier, nonce, user)
|
||||
@@ -313,7 +313,7 @@ func (os *OauthService) githubCallback(oauthConfig *oauth2.Config, provider *oid
|
||||
return nil, user.ToOauthUser()
|
||||
}
|
||||
|
||||
// linuxdoCallback linux.do回调
|
||||
// linuxdoCallback handles the linux.do callback
|
||||
func (os *OauthService) linuxdoCallback(oauthConfig *oauth2.Config, provider *oidc.Provider, code, verifier, nonce string) (error, *model.OauthUser) {
|
||||
var user = &model.LinuxdoUser{}
|
||||
err, _ := os.callbackBase(oauthConfig, provider, code, verifier, nonce, user)
|
||||
@@ -323,7 +323,7 @@ func (os *OauthService) linuxdoCallback(oauthConfig *oauth2.Config, provider *oi
|
||||
return nil, user.ToOauthUser()
|
||||
}
|
||||
|
||||
// oidcCallback oidc回调, 通过code获取用户信息
|
||||
// oidcCallback handles the oidc callback, getting user info via the code
|
||||
func (os *OauthService) oidcCallback(oauthConfig *oauth2.Config, provider *oidc.Provider, code, verifier, nonce string) (error, *model.OauthUser) {
|
||||
var user = &model.OidcUser{}
|
||||
if err, _ := os.callbackBase(oauthConfig, provider, code, verifier, nonce, user); err != nil {
|
||||
@@ -385,14 +385,14 @@ func (os *OauthService) DeleteUserByUserId(userId uint) error {
|
||||
return DB.Where("user_id = ?", userId).Delete(&model.UserThird{}).Error
|
||||
}
|
||||
|
||||
// InfoById 根据id获取Oauth信息
|
||||
// InfoById gets the oauth info by id
|
||||
func (os *OauthService) InfoById(id uint) *model.Oauth {
|
||||
oauthInfo := &model.Oauth{}
|
||||
DB.Where("id = ?", id).First(oauthInfo)
|
||||
return oauthInfo
|
||||
}
|
||||
|
||||
// InfoByOp 根据op获取Oauth信息
|
||||
// InfoByOp gets the oauth info by op
|
||||
func (os *OauthService) InfoByOp(op string) *model.Oauth {
|
||||
oauthInfo := &model.Oauth{}
|
||||
DB.Where("op = ?", op).First(oauthInfo)
|
||||
@@ -428,7 +428,7 @@ func (os *OauthService) List(page, pageSize uint, where func(tx *gorm.DB)) (res
|
||||
return
|
||||
}
|
||||
|
||||
// GetTypeByOp 根据op获取OauthType
|
||||
// GetTypeByOp gets the oauth type by op
|
||||
func (os *OauthService) GetTypeByOp(op string) (error, string) {
|
||||
oauthInfo := &model.Oauth{}
|
||||
if DB.Where("op = ?", op).First(oauthInfo).Error != nil {
|
||||
@@ -437,7 +437,7 @@ func (os *OauthService) GetTypeByOp(op string) (error, string) {
|
||||
return nil, oauthInfo.OauthType
|
||||
}
|
||||
|
||||
// ValidateOauthProvider 验证Oauth提供者是否正确
|
||||
// ValidateOauthProvider verifies whether the Oauth provider is correct
|
||||
func (os *OauthService) ValidateOauthProvider(op string) error {
|
||||
if !os.IsOauthProviderExist(op) {
|
||||
return fmt.Errorf("OAuth provider with op '%s' not found", op)
|
||||
@@ -445,17 +445,17 @@ func (os *OauthService) ValidateOauthProvider(op string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsOauthProviderExist 验证Oauth提供者是否存在
|
||||
// IsOauthProviderExist verifies whether the Oauth provider exists
|
||||
func (os *OauthService) IsOauthProviderExist(op string) bool {
|
||||
oauthInfo := &model.Oauth{}
|
||||
// 使用 Gorm 的 Take 方法查找符合条件的记录
|
||||
// use Gorm's Take method to find records matching the conditions
|
||||
if err := DB.Where("op = ?", op).Take(oauthInfo).Error; err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Create 创建
|
||||
// Create create
|
||||
func (os *OauthService) Create(oauthInfo *model.Oauth) error {
|
||||
err := oauthInfo.FormatOauthInfo()
|
||||
if err != nil {
|
||||
@@ -468,7 +468,7 @@ func (os *OauthService) Delete(oauthInfo *model.Oauth) error {
|
||||
return DB.Delete(oauthInfo).Error
|
||||
}
|
||||
|
||||
// Update 更新
|
||||
// Update update
|
||||
func (os *OauthService) Update(oauthInfo *model.Oauth) error {
|
||||
err := oauthInfo.FormatOauthInfo()
|
||||
if err != nil {
|
||||
@@ -477,7 +477,7 @@ func (os *OauthService) Update(oauthInfo *model.Oauth) error {
|
||||
return DB.Model(oauthInfo).Updates(oauthInfo).Error
|
||||
}
|
||||
|
||||
// GetOauthProviders 获取所有的provider
|
||||
// GetOauthProviders get all of the provider
|
||||
func (os *OauthService) GetOauthProviders() []string {
|
||||
var res []string
|
||||
DB.Model(&model.Oauth{}).Pluck("op", &res)
|
||||
|
||||
+18
-18
@@ -8,7 +8,7 @@ import (
|
||||
type PeerService struct {
|
||||
}
|
||||
|
||||
// FindById 根据id查找
|
||||
// FindById finds a peer by id
|
||||
func (ps *PeerService) FindById(id string) *model.Peer {
|
||||
p := &model.Peer{}
|
||||
DB.Where("id = ?", id).First(p)
|
||||
@@ -25,22 +25,22 @@ func (ps *PeerService) InfoByRowId(id uint) *model.Peer {
|
||||
return p
|
||||
}
|
||||
|
||||
// FindByUserIdAndUuid 根据用户id和uuid查找peer
|
||||
// FindByUserIdAndUuid finds a peer by user id and uuid
|
||||
func (ps *PeerService) FindByUserIdAndUuid(uuid string, userId uint) *model.Peer {
|
||||
p := &model.Peer{}
|
||||
DB.Where("uuid = ? and user_id = ?", uuid, userId).First(p)
|
||||
return p
|
||||
}
|
||||
|
||||
// UuidBindUserId 绑定用户id
|
||||
// UuidBindUserId binds a user id
|
||||
func (ps *PeerService) UuidBindUserId(deviceId string, uuid string, userId uint) {
|
||||
peer := ps.FindByUuid(uuid)
|
||||
// 如果存在则更新
|
||||
// if it exists, update it
|
||||
if peer.RowId > 0 {
|
||||
peer.UserId = userId
|
||||
ps.Update(peer)
|
||||
} else {
|
||||
// 不存在则创建
|
||||
// create if it does not exist
|
||||
/*if deviceId != "" {
|
||||
DB.Create(&model.Peer{
|
||||
Id: deviceId,
|
||||
@@ -51,7 +51,7 @@ func (ps *PeerService) UuidBindUserId(deviceId string, uuid string, userId uint)
|
||||
}
|
||||
}
|
||||
|
||||
// UuidUnbindUserId 解绑用户id, 用于用户注销
|
||||
// UuidUnbindUserId unbinds the user id, used for user logout
|
||||
func (ps *PeerService) UuidUnbindUserId(uuid string, userId uint) {
|
||||
peer := ps.FindByUserIdAndUuid(uuid, userId)
|
||||
if peer.RowId > 0 {
|
||||
@@ -59,12 +59,12 @@ func (ps *PeerService) UuidUnbindUserId(uuid string, userId uint) {
|
||||
}
|
||||
}
|
||||
|
||||
// EraseUserId 清除用户id, 用于用户删除
|
||||
// EraseUserId clears the user id, used for user deletion
|
||||
func (ps *PeerService) EraseUserId(userId uint) error {
|
||||
return DB.Model(&model.Peer{}).Where("user_id = ?", userId).Update("user_id", 0).Error
|
||||
}
|
||||
|
||||
// ListByUserIds 根据用户id取列表
|
||||
// ListByUserIds gets the list by user ids
|
||||
func (ps *PeerService) ListByUserIds(userIds []uint, page, pageSize uint) (res *model.PeerList) {
|
||||
res = &model.PeerList{}
|
||||
res.Page = int64(page)
|
||||
@@ -91,11 +91,11 @@ func (ps *PeerService) List(page, pageSize uint, where func(tx *gorm.DB)) (res *
|
||||
return
|
||||
}
|
||||
|
||||
// ListFilterByUserId 根据用户id过滤Peer列表
|
||||
// ListFilterByUserId filters the peer list by user id
|
||||
func (ps *PeerService) ListFilterByUserId(page, pageSize uint, where func(tx *gorm.DB), userId uint) (res *model.PeerList) {
|
||||
userWhere := func(tx *gorm.DB) {
|
||||
tx.Where("user_id = ?", userId)
|
||||
// 如果还有额外的筛选条件,执行它
|
||||
// if there are additional filter conditions, apply them
|
||||
if where != nil {
|
||||
where(tx)
|
||||
}
|
||||
@@ -103,30 +103,30 @@ func (ps *PeerService) ListFilterByUserId(page, pageSize uint, where func(tx *go
|
||||
return ps.List(page, pageSize, userWhere)
|
||||
}
|
||||
|
||||
// Create 创建
|
||||
// Create create
|
||||
func (ps *PeerService) Create(u *model.Peer) error {
|
||||
res := DB.Create(u).Error
|
||||
return res
|
||||
}
|
||||
|
||||
// Delete 删除, 同时也应该删除token
|
||||
// Delete deletes a peer; the associated token should also be deleted
|
||||
func (ps *PeerService) Delete(u *model.Peer) error {
|
||||
uuid := u.Uuid
|
||||
err := DB.Delete(u).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 删除token
|
||||
// delete the token
|
||||
return AllService.UserService.FlushTokenByUuid(uuid)
|
||||
}
|
||||
|
||||
// GetUuidListByIDs 根据ids获取uuid列表
|
||||
// GetUuidListByIDs gets the uuid list by ids
|
||||
func (ps *PeerService) GetUuidListByIDs(ids []uint) ([]string, error) {
|
||||
var uuids []string
|
||||
err := DB.Model(&model.Peer{}).
|
||||
Where("row_id in (?)", ids).
|
||||
Pluck("uuid", &uuids).Error
|
||||
//过滤uuids中的空字符串
|
||||
// filter out empty strings in uuids
|
||||
var newUuids []string
|
||||
for _, uuid := range uuids {
|
||||
if uuid != "" {
|
||||
@@ -136,18 +136,18 @@ func (ps *PeerService) GetUuidListByIDs(ids []uint) ([]string, error) {
|
||||
return newUuids, err
|
||||
}
|
||||
|
||||
// BatchDelete 批量删除, 同时也应该删除token
|
||||
// BatchDelete batch deletes peers; the associated tokens should also be deleted
|
||||
func (ps *PeerService) BatchDelete(ids []uint) error {
|
||||
uuids, err := ps.GetUuidListByIDs(ids)
|
||||
err = DB.Where("row_id in (?)", ids).Delete(&model.Peer{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 删除token
|
||||
// delete the token
|
||||
return AllService.UserService.FlushTokenByUuids(uuids)
|
||||
}
|
||||
|
||||
// Update 更新
|
||||
// Update update
|
||||
func (ps *PeerService) Update(u *model.Peer) error {
|
||||
return DB.Model(u).Updates(u).Error
|
||||
}
|
||||
|
||||
@@ -39,15 +39,15 @@ func (is *ServerCmdService) Create(u *model.ServerCmd) error {
|
||||
return res
|
||||
}
|
||||
|
||||
// SendCmd 发送命令
|
||||
// SendCmd send command
|
||||
func (is *ServerCmdService) SendCmd(port int, cmd string, arg string) (string, error) {
|
||||
//组装命令
|
||||
//assemble command
|
||||
cmd = cmd + " " + arg
|
||||
res, err := is.SendSocketCmd("v6", port, cmd)
|
||||
if err == nil {
|
||||
return res, nil
|
||||
}
|
||||
//v6连接失败,尝试v4
|
||||
//v6connection failed, trying v4
|
||||
res, err = is.SendSocketCmd("v4", port, cmd)
|
||||
if err == nil {
|
||||
return res, nil
|
||||
@@ -69,14 +69,14 @@ func (is *ServerCmdService) SendSocketCmd(ty string, port int, cmd string) (stri
|
||||
return "", err
|
||||
}
|
||||
defer conn.Close()
|
||||
//发送命令
|
||||
//send command
|
||||
_, err = conn.Write([]byte(cmd))
|
||||
if err != nil {
|
||||
Logger.Debugf("%s send cmd failed: %v", ty, err)
|
||||
return "", err
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
//读取返回
|
||||
//read response
|
||||
buf := make([]byte, 1024)
|
||||
n, err := conn.Read(buf)
|
||||
if err != nil && err.Error() != "EOF" {
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
type ShareRecordService struct {
|
||||
}
|
||||
|
||||
// InfoById 根据用户id取用户信息
|
||||
// InfoById gets the share record info by id
|
||||
func (srs *ShareRecordService) InfoById(id uint) *model.ShareRecord {
|
||||
u := &model.ShareRecord{}
|
||||
DB.Where("id = ?", id).First(u)
|
||||
@@ -29,7 +29,7 @@ func (srs *ShareRecordService) List(page, pageSize uint, where func(tx *gorm.DB)
|
||||
return
|
||||
}
|
||||
|
||||
// Create 创建
|
||||
// Create create
|
||||
func (srs *ShareRecordService) Create(u *model.ShareRecord) error {
|
||||
res := DB.Create(u).Error
|
||||
return res
|
||||
@@ -38,7 +38,7 @@ func (srs *ShareRecordService) Delete(u *model.ShareRecord) error {
|
||||
return DB.Delete(u).Error
|
||||
}
|
||||
|
||||
// Update 更新
|
||||
// Update update
|
||||
func (srs *ShareRecordService) Update(u *model.ShareRecord) error {
|
||||
return DB.Model(u).Updates(u).Error
|
||||
}
|
||||
|
||||
+8
-8
@@ -34,24 +34,24 @@ func (s *TagService) ListByUserIdAndCollectionId(userId, cid uint) (res *model.T
|
||||
}
|
||||
func (s *TagService) UpdateTags(userId uint, tags map[string]uint) {
|
||||
tx := DB.Begin()
|
||||
//先查询所有tag
|
||||
//first query alltag
|
||||
var allTags []*model.Tag
|
||||
tx.Where("user_id = ?", userId).Find(&allTags)
|
||||
for _, t := range allTags {
|
||||
if _, ok := tags[t.Name]; !ok {
|
||||
//删除
|
||||
//delete
|
||||
tx.Delete(t)
|
||||
} else {
|
||||
if tags[t.Name] != t.Color {
|
||||
//更新
|
||||
//update
|
||||
t.Color = tags[t.Name]
|
||||
tx.Save(t)
|
||||
}
|
||||
//移除
|
||||
//remove
|
||||
delete(tags, t.Name)
|
||||
}
|
||||
}
|
||||
//新增
|
||||
//add
|
||||
for tag, color := range tags {
|
||||
t := &model.Tag{}
|
||||
t.Name = tag
|
||||
@@ -62,7 +62,7 @@ func (s *TagService) UpdateTags(userId uint, tags map[string]uint) {
|
||||
tx.Commit()
|
||||
}
|
||||
|
||||
// InfoById 根据用户id取用户信息
|
||||
// InfoById gets the tag info by id
|
||||
func (s *TagService) InfoById(id uint) *model.Tag {
|
||||
u := &model.Tag{}
|
||||
DB.Where("id = ?", id).First(u)
|
||||
@@ -83,7 +83,7 @@ func (s *TagService) List(page, pageSize uint, where func(tx *gorm.DB)) (res *mo
|
||||
return
|
||||
}
|
||||
|
||||
// Create 创建
|
||||
// Create create
|
||||
func (s *TagService) Create(u *model.Tag) error {
|
||||
res := DB.Create(u).Error
|
||||
return res
|
||||
@@ -92,7 +92,7 @@ func (s *TagService) Delete(u *model.Tag) error {
|
||||
return DB.Delete(u).Error
|
||||
}
|
||||
|
||||
// Update 更新
|
||||
// Update update
|
||||
func (s *TagService) Update(u *model.Tag) error {
|
||||
return DB.Model(u).Select("*").Omit("created_at").Updates(u).Error
|
||||
}
|
||||
|
||||
+42
-42
@@ -16,35 +16,35 @@ import (
|
||||
type UserService struct {
|
||||
}
|
||||
|
||||
// InfoById 根据用户id取用户信息
|
||||
// InfoById gets user info by user id
|
||||
func (us *UserService) InfoById(id uint) *model.User {
|
||||
u := &model.User{}
|
||||
DB.Where("id = ?", id).First(u)
|
||||
return u
|
||||
}
|
||||
|
||||
// InfoByUsername 根据用户名取用户信息
|
||||
// InfoByUsername get user info by username
|
||||
func (us *UserService) InfoByUsername(un string) *model.User {
|
||||
u := &model.User{}
|
||||
DB.Where("username = ?", un).First(u)
|
||||
return u
|
||||
}
|
||||
|
||||
// InfoByEmail 根据邮箱取用户信息
|
||||
// InfoByEmail get user info by email
|
||||
func (us *UserService) InfoByEmail(email string) *model.User {
|
||||
u := &model.User{}
|
||||
DB.Where("email = ?", email).First(u)
|
||||
return u
|
||||
}
|
||||
|
||||
// InfoByOpenid 根据openid取用户信息
|
||||
// InfoByOpenid gets user info by openid
|
||||
func (us *UserService) InfoByOpenid(openid string) *model.User {
|
||||
u := &model.User{}
|
||||
DB.Where("openid = ?", openid).First(u)
|
||||
return u
|
||||
}
|
||||
|
||||
// InfoByUsernamePassword 根据用户名密码取用户信息
|
||||
// InfoByUsernamePassword get user info by username and password
|
||||
func (us *UserService) InfoByUsernamePassword(username, password string) *model.User {
|
||||
if Config.Ldap.Enable {
|
||||
u, err := AllService.LdapService.Authenticate(username, password)
|
||||
@@ -70,7 +70,7 @@ func (us *UserService) InfoByUsernamePassword(username, password string) *model.
|
||||
return u
|
||||
}
|
||||
|
||||
// InfoByAccesstoken 根据accesstoken取用户信息
|
||||
// InfoByAccesstoken gets user info by access token
|
||||
func (us *UserService) InfoByAccessToken(token string) (*model.User, *model.UserToken) {
|
||||
u := &model.User{}
|
||||
ut := &model.UserToken{}
|
||||
@@ -85,7 +85,7 @@ func (us *UserService) InfoByAccessToken(token string) (*model.User, *model.User
|
||||
return u, ut
|
||||
}
|
||||
|
||||
// GenerateToken 生成token
|
||||
// GenerateToken generates a token
|
||||
func (us *UserService) GenerateToken(u *model.User) string {
|
||||
if len(Jwt.Key) > 0 {
|
||||
return Jwt.GenerateToken(u.Id)
|
||||
@@ -93,7 +93,7 @@ func (us *UserService) GenerateToken(u *model.User) string {
|
||||
return utils.Md5(u.Username + time.Now().String())
|
||||
}
|
||||
|
||||
// Login 登录
|
||||
// Login login
|
||||
func (us *UserService) Login(u *model.User, llog *model.LoginLog) *model.UserToken {
|
||||
token := us.GenerateToken(u)
|
||||
ut := &model.UserToken{
|
||||
@@ -112,7 +112,7 @@ func (us *UserService) Login(u *model.User, llog *model.LoginLog) *model.UserTok
|
||||
return ut
|
||||
}
|
||||
|
||||
// CurUser 获取当前用户
|
||||
// CurUser get current user
|
||||
func (us *UserService) CurUser(c *gin.Context) *model.User {
|
||||
user, _ := c.Get("curUser")
|
||||
u, ok := user.(*model.User)
|
||||
@@ -141,7 +141,7 @@ func (us *UserService) ListByIds(ids []uint) (res []*model.User) {
|
||||
return res
|
||||
}
|
||||
|
||||
// ListByGroupId 根据组id取用户列表
|
||||
// ListByGroupId gets the user list by group id
|
||||
func (us *UserService) ListByGroupId(groupId, page, pageSize uint) (res *model.UserList) {
|
||||
res = us.List(page, pageSize, func(tx *gorm.DB) {
|
||||
tx.Where("group_id = ?", groupId)
|
||||
@@ -149,25 +149,25 @@ func (us *UserService) ListByGroupId(groupId, page, pageSize uint) (res *model.U
|
||||
return
|
||||
}
|
||||
|
||||
// ListIdsByGroupId 根据组id取用户id列表
|
||||
// ListIdsByGroupId gets the user id list by group id
|
||||
func (us *UserService) ListIdsByGroupId(groupId uint) (ids []uint) {
|
||||
DB.Model(&model.User{}).Where("group_id = ?", groupId).Pluck("id", &ids)
|
||||
return ids
|
||||
|
||||
}
|
||||
|
||||
// ListIdAndNameByGroupId 根据组id取用户id和用户名列表
|
||||
// ListIdAndNameByGroupId gets the user id and username list by group id
|
||||
func (us *UserService) ListIdAndNameByGroupId(groupId uint) (res []*model.User) {
|
||||
DB.Model(&model.User{}).Where("group_id = ?", groupId).Select("id, username").Find(&res)
|
||||
return res
|
||||
}
|
||||
|
||||
// CheckUserEnable 判断用户是否禁用
|
||||
// CheckUserEnable check whether the user is disabled
|
||||
func (us *UserService) CheckUserEnable(u *model.User) bool {
|
||||
return u.Status == model.COMMON_STATUS_ENABLE
|
||||
}
|
||||
|
||||
// Create 创建
|
||||
// Create create
|
||||
func (us *UserService) Create(u *model.User) error {
|
||||
// The initial username should be formatted, and the username should be unique
|
||||
if us.IsUsernameExists(u.Username) {
|
||||
@@ -183,7 +183,7 @@ func (us *UserService) Create(u *model.User) error {
|
||||
return res
|
||||
}
|
||||
|
||||
// GetUuidByToken 根据token和user取uuid
|
||||
// GetUuidByToken gets the uuid by token and user
|
||||
func (us *UserService) GetUuidByToken(u *model.User, token string) string {
|
||||
ut := &model.UserToken{}
|
||||
err := DB.Where("user_id = ? and token = ?", u.Id, token).First(ut).Error
|
||||
@@ -193,7 +193,7 @@ func (us *UserService) GetUuidByToken(u *model.User, token string) string {
|
||||
return ut.DeviceUuid
|
||||
}
|
||||
|
||||
// Logout 退出登录 -> 删除token, 解绑uuid
|
||||
// Logout logs out -> delete token, unbind uuid
|
||||
func (us *UserService) Logout(u *model.User, token string) error {
|
||||
uuid := us.GetUuidByToken(u, token)
|
||||
err := DB.Where("user_id = ? and token = ?", u.Id, token).Delete(&model.UserToken{}).Error
|
||||
@@ -206,40 +206,40 @@ func (us *UserService) Logout(u *model.User, token string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete 删除用户和oauth信息
|
||||
// Delete deletes the user and oauth info
|
||||
func (us *UserService) Delete(u *model.User) error {
|
||||
userCount := us.getAdminUserCount()
|
||||
if userCount <= 1 && us.IsAdmin(u) {
|
||||
return errors.New("The last admin user cannot be deleted")
|
||||
}
|
||||
tx := DB.Begin()
|
||||
// 删除用户
|
||||
// delete user
|
||||
if err := tx.Delete(u).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
// 删除关联的 OAuth 信息
|
||||
// delete the associated OAuth info
|
||||
if err := tx.Where("user_id = ?", u.Id).Delete(&model.UserThird{}).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
// 删除关联的ab
|
||||
// delete the associated ab
|
||||
if err := tx.Where("user_id = ?", u.Id).Delete(&model.AddressBook{}).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
// 删除关联的abc
|
||||
// delete the associated abc
|
||||
if err := tx.Where("user_id = ?", u.Id).Delete(&model.AddressBookCollection{}).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
// 删除关联的abcr
|
||||
// delete the associated abcr
|
||||
if err := tx.Where("user_id = ?", u.Id).Delete(&model.AddressBookCollectionRule{}).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
tx.Commit()
|
||||
// 删除关联的peer
|
||||
// delete the associated peer
|
||||
if err := AllService.PeerService.EraseUserId(u.Id); err != nil {
|
||||
Logger.Warn("User deleted successfully, but failed to unlink peer.")
|
||||
return nil
|
||||
@@ -247,13 +247,13 @@ func (us *UserService) Delete(u *model.User) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Update 更新
|
||||
// Update update
|
||||
func (us *UserService) Update(u *model.User) error {
|
||||
currentUser := us.InfoById(u.Id)
|
||||
// 如果当前用户是管理员并且 IsAdmin 不为空,进行检查
|
||||
// if the current user is an administrator and IsAdmin is not empty, perform the check
|
||||
if us.IsAdmin(currentUser) {
|
||||
adminCount := us.getAdminUserCount()
|
||||
// 如果这是唯一的管理员,确保不能禁用或取消管理员权限
|
||||
// if this is the only administrator, make sure admin privileges cannot be disabled or revoked
|
||||
if adminCount <= 1 && (!us.IsAdmin(u) || u.Status == model.COMMON_STATUS_DISABLED) {
|
||||
return errors.New("The last admin user cannot be disabled or demoted")
|
||||
}
|
||||
@@ -261,22 +261,22 @@ func (us *UserService) Update(u *model.User) error {
|
||||
return DB.Model(u).Updates(u).Error
|
||||
}
|
||||
|
||||
// FlushToken 清空token
|
||||
// FlushToken clears the token
|
||||
func (us *UserService) FlushToken(u *model.User) error {
|
||||
return DB.Where("user_id = ?", u.Id).Delete(&model.UserToken{}).Error
|
||||
}
|
||||
|
||||
// FlushTokenByUuid 清空token
|
||||
// FlushTokenByUuid clears the token
|
||||
func (us *UserService) FlushTokenByUuid(uuid string) error {
|
||||
return DB.Where("device_uuid = ?", uuid).Delete(&model.UserToken{}).Error
|
||||
}
|
||||
|
||||
// FlushTokenByUuids 清空token
|
||||
// FlushTokenByUuids clears the token
|
||||
func (us *UserService) FlushTokenByUuids(uuids []string) error {
|
||||
return DB.Where("device_uuid in (?)", uuids).Delete(&model.UserToken{}).Error
|
||||
}
|
||||
|
||||
// UpdatePassword 更新密码
|
||||
// UpdatePassword update password
|
||||
func (us *UserService) UpdatePassword(u *model.User, password string) error {
|
||||
var err error
|
||||
u.Password, err = utils.EncryptPassword(password)
|
||||
@@ -291,7 +291,7 @@ func (us *UserService) UpdatePassword(u *model.User, password string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// IsAdmin 是否管理员
|
||||
// IsAdmin whether administrator
|
||||
func (us *UserService) IsAdmin(u *model.User) bool {
|
||||
return u != nil && *u.IsAdmin
|
||||
}
|
||||
@@ -304,7 +304,7 @@ func (us *UserService) RouteNames(u *model.User) []string {
|
||||
return model.UserRouteNames
|
||||
}
|
||||
|
||||
// InfoByOauthId 根据oauth的name和openId取用户信息
|
||||
// InfoByOauthId gets user info by the oauth name and openId
|
||||
func (us *UserService) InfoByOauthId(op string, openId string) *model.User {
|
||||
ut := AllService.OauthService.UserThirdInfo(op, openId)
|
||||
if ut.Id == 0 {
|
||||
@@ -317,7 +317,7 @@ func (us *UserService) InfoByOauthId(op string, openId string) *model.User {
|
||||
return u
|
||||
}
|
||||
|
||||
// RegisterByOauth 注册
|
||||
// RegisterByOauth register
|
||||
func (us *UserService) RegisterByOauth(oauthUser *model.OauthUser, op string) (error, *model.User) {
|
||||
Lock.Lock("registerByOauth")
|
||||
defer Lock.UnLock("registerByOauth")
|
||||
@@ -375,7 +375,7 @@ func (us *UserService) RegisterByOauth(oauthUser *model.OauthUser, op string) (e
|
||||
return nil, user
|
||||
}
|
||||
|
||||
// GenerateUsernameByOauth 生成用户名
|
||||
// GenerateUsernameByOauth generates a username
|
||||
func (us *UserService) GenerateUsernameByOauth(name string) string {
|
||||
for us.IsUsernameExists(name) {
|
||||
name += strconv.Itoa(rand.Intn(10)) // Append a random digit (0-9)
|
||||
@@ -395,14 +395,14 @@ func (us *UserService) UserThirdInfo(userId uint, op string) *model.UserThird {
|
||||
return ut
|
||||
}
|
||||
|
||||
// FindLatestUserIdFromLoginLogByUuid 根据uuid和设备id查找最后登录的用户id
|
||||
// FindLatestUserIdFromLoginLogByUuid finds the last logged-in user id by uuid and device id
|
||||
func (us *UserService) FindLatestUserIdFromLoginLogByUuid(uuid string, deviceId string) uint {
|
||||
llog := &model.LoginLog{}
|
||||
DB.Where("uuid = ? and device_id = ?", uuid, deviceId).Order("id desc").First(llog)
|
||||
return llog.UserId
|
||||
}
|
||||
|
||||
// IsPasswordEmptyById 根据用户id判断密码是否为空,主要用于第三方登录的自动注册
|
||||
// IsPasswordEmptyById checks whether the password is empty by user id, mainly used for auto-registration via third-party login
|
||||
func (us *UserService) IsPasswordEmptyById(id uint) bool {
|
||||
u := &model.User{}
|
||||
if DB.Where("id = ?", id).First(u).Error != nil {
|
||||
@@ -411,7 +411,7 @@ func (us *UserService) IsPasswordEmptyById(id uint) bool {
|
||||
return u.Password == ""
|
||||
}
|
||||
|
||||
// IsPasswordEmptyByUsername 根据用户id判断密码是否为空,主要用于第三方登录的自动注册
|
||||
// IsPasswordEmptyByUsername checks whether the password is empty by username, mainly used for auto-registration via third-party login
|
||||
func (us *UserService) IsPasswordEmptyByUsername(username string) bool {
|
||||
u := &model.User{}
|
||||
if DB.Where("username = ?", username).First(u).Error != nil {
|
||||
@@ -420,12 +420,12 @@ func (us *UserService) IsPasswordEmptyByUsername(username string) bool {
|
||||
return u.Password == ""
|
||||
}
|
||||
|
||||
// IsPasswordEmptyByUser 判断密码是否为空,主要用于第三方登录的自动注册
|
||||
// IsPasswordEmptyByUser check whether the password is empty, mainly used for auto-registration via third-party login
|
||||
func (us *UserService) IsPasswordEmptyByUser(u *model.User) bool {
|
||||
return us.IsPasswordEmptyById(u.Id)
|
||||
}
|
||||
|
||||
// Register 注册, 如果用户名已存在则返回nil
|
||||
// Register registers a user; if the username already exists, return nil
|
||||
func (us *UserService) Register(username string, email string, password string, status model.StatusCode) *model.User {
|
||||
u := &model.User{
|
||||
Username: username,
|
||||
@@ -486,11 +486,11 @@ func (us *UserService) getAdminUserCount() int64 {
|
||||
return count
|
||||
}
|
||||
|
||||
// UserTokenExpireTimestamp 生成用户token过期时间
|
||||
// UserTokenExpireTimestamp generates the user token expiration time
|
||||
func (us *UserService) UserTokenExpireTimestamp() int64 {
|
||||
exp := Config.App.TokenExpire
|
||||
if exp == 0 {
|
||||
//默认七天
|
||||
//default seven days
|
||||
exp = 604800
|
||||
}
|
||||
return time.Now().Add(exp).Unix()
|
||||
@@ -515,7 +515,7 @@ func (us *UserService) VerifyJWT(token string) (uint, error) {
|
||||
return Jwt.ParseToken(token)
|
||||
}
|
||||
|
||||
// IsUsernameExists 判断用户名是否存在, it will check the internal database and LDAP(if enabled)
|
||||
// IsUsernameExists check whether the username exists, it will check the internal database and LDAP(if enabled)
|
||||
func (us *UserService) IsUsernameExists(username string) bool {
|
||||
return us.IsUsernameExistsLocal(username) || AllService.LdapService.IsUsernameExists(username)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user