🐛 Prevent user adding a user twice into a realm

This commit is contained in:
LittleSheep 2024-12-01 02:04:57 +08:00
parent d3a1382711
commit e0c9646c98
2 changed files with 15 additions and 5 deletions

8
.idea/workspace.xml generated
View File

@ -4,9 +4,9 @@
<option name="autoReloadType" value="ALL" /> <option name="autoReloadType" value="ALL" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Hotfix previous commit compile issue"> <list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":sparkles: Add realm member support both account name and id">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/http/api/realm_members_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/http/api/realm_members_api.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/pkg/internal/services/realms.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/realms.go" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -159,7 +159,6 @@
</component> </component>
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<option name="CHECK_CODE_SMELLS_BEFORE_PROJECT_COMMIT" value="false" /> <option name="CHECK_CODE_SMELLS_BEFORE_PROJECT_COMMIT" value="false" />
<MESSAGE value=":sparkles: Authkit support check user related permission" />
<MESSAGE value=":sparkles: Authkit can get user" /> <MESSAGE value=":sparkles: Authkit can get user" />
<MESSAGE value=":bug: Fix auth cache" /> <MESSAGE value=":bug: Fix auth cache" />
<MESSAGE value=":sparkles: Allow get realm by numeric auto increment id" /> <MESSAGE value=":sparkles: Allow get realm by numeric auto increment id" />
@ -184,7 +183,8 @@
<MESSAGE value=":truck: Move make friendship api" /> <MESSAGE value=":truck: Move make friendship api" />
<MESSAGE value=":boom: Move remove member api arguments from body to querystring just as messaging" /> <MESSAGE value=":boom: Move remove member api arguments from body to querystring just as messaging" />
<MESSAGE value=":bug: Hotfix previous commit compile issue" /> <MESSAGE value=":bug: Hotfix previous commit compile issue" />
<option name="LAST_COMMIT_MESSAGE" value=":bug: Hotfix previous commit compile issue" /> <MESSAGE value=":sparkles: Add realm member support both account name and id" />
<option name="LAST_COMMIT_MESSAGE" value=":sparkles: Add realm member support both account name and id" />
<option name="GROUP_MULTIFILE_MERGE_BY_DIRECTORY" value="true" /> <option name="GROUP_MULTIFILE_MERGE_BY_DIRECTORY" value="true" />
</component> </component>
<component name="VgoProject"> <component name="VgoProject">

View File

@ -1,10 +1,12 @@
package services package services
import ( import (
"errors"
"fmt" "fmt"
"git.solsynth.dev/hypernet/passport/pkg/authkit/models" "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/passport/pkg/internal/database" "git.solsynth.dev/hypernet/passport/pkg/internal/database"
"github.com/samber/lo" "github.com/samber/lo"
"gorm.io/gorm"
"strconv" "strconv"
) )
@ -97,6 +99,14 @@ func GetRealmMember(userId uint, realmId uint) (models.RealmMember, error) {
} }
func AddRealmMember(user models.Account, affected models.Account, target models.Realm) error { func AddRealmMember(user models.Account, affected models.Account, target models.Realm) error {
var member models.RealmMember
if err := database.C.Where(&models.RealmMember{
AccountID: affected.ID,
RealmID: target.ID,
}).First(&member).Error; err == nil || errors.Is(err, gorm.ErrRecordNotFound) {
return fmt.Errorf("the user is already in the realm")
}
if !target.IsPublic && !target.IsCommunity { if !target.IsPublic && !target.IsCommunity {
if member, err := GetRealmMember(user.ID, target.ID); err != nil { if member, err := GetRealmMember(user.ID, target.ID); err != nil {
return fmt.Errorf("only realm member can add people: %v", err) return fmt.Errorf("only realm member can add people: %v", err)
@ -114,7 +124,7 @@ func AddRealmMember(user models.Account, affected models.Account, target models.
} }
} }
member := models.RealmMember{ member = models.RealmMember{
RealmID: target.ID, RealmID: target.ID,
AccountID: affected.ID, AccountID: affected.ID,
} }