🐛 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" />
</component>
<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$/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>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -159,7 +159,6 @@
</component>
<component name="VcsManagerConfiguration">
<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=":bug: Fix auth cache" />
<MESSAGE value=":sparkles: Allow get realm by numeric auto increment id" />
@ -184,7 +183,8 @@
<MESSAGE value=":truck: Move make friendship api" />
<MESSAGE value=":boom: Move remove member api arguments from body to querystring just as messaging" />
<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" />
</component>
<component name="VgoProject">

View File

@ -1,10 +1,12 @@
package services
import (
"errors"
"fmt"
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
"github.com/samber/lo"
"gorm.io/gorm"
"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 {
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 member, err := GetRealmMember(user.ID, target.ID); err != nil {
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,
AccountID: affected.ID,
}