fix: couple session deletion with user deletion
This commit is contained in:
parent
1fe0d65874
commit
bfbeb6add2
|
@ -2,7 +2,6 @@ package arangodb
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/authorizerdev/authorizer/server/db/models"
|
||||
|
@ -24,17 +23,3 @@ func (p *provider) AddSession(ctx context.Context, session models.Session) error
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteSession to delete session information from database
|
||||
func (p *provider) DeleteSession(ctx context.Context, userId string) error {
|
||||
query := fmt.Sprintf(`FOR d IN %s FILTER d.user_id == @userId REMOVE { _key: d._key } IN %s`, models.Collections.Session, models.Collections.Session)
|
||||
bindVars := map[string]interface{}{
|
||||
"userId": userId,
|
||||
}
|
||||
cursor, err := p.db.Query(ctx, query, bindVars)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer cursor.Close()
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -63,6 +63,16 @@ func (p *provider) DeleteUser(ctx context.Context, user models.User) error {
|
|||
return err
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`FOR d IN %s FILTER d.user_id == @userId REMOVE { _key: d._key } IN %s`, models.Collections.Session, models.Collections.Session)
|
||||
bindVars := map[string]interface{}{
|
||||
"userId": user.ID,
|
||||
}
|
||||
cursor, err := p.db.Query(ctx, query, bindVars)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer cursor.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -25,13 +25,3 @@ func (p *provider) AddSession(ctx context.Context, session models.Session) error
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteSession to delete session information from database
|
||||
func (p *provider) DeleteSession(ctx context.Context, userId string) error {
|
||||
deleteSessionQuery := fmt.Sprintf("DELETE FROM %s WHERE user_id = '%s'", KeySpace+"."+models.Collections.Session, userId)
|
||||
err := p.db.Query(deleteSessionQuery).Exec()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -131,7 +131,17 @@ func (p *provider) UpdateUser(ctx context.Context, user models.User) (models.Use
|
|||
func (p *provider) DeleteUser(ctx context.Context, user models.User) error {
|
||||
query := fmt.Sprintf("DELETE FROM %s WHERE id = '%s'", KeySpace+"."+models.Collections.User, user.ID)
|
||||
err := p.db.Query(query).Exec()
|
||||
return err
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
deleteSessionQuery := fmt.Sprintf("DELETE FROM %s WHERE user_id = '%s'", KeySpace+"."+models.Collections.Session, user.ID)
|
||||
err = p.db.Query(deleteSessionQuery).Exec()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListUsers to get list of users from database
|
||||
|
|
|
@ -6,7 +6,6 @@ import (
|
|||
|
||||
"github.com/authorizerdev/authorizer/server/db/models"
|
||||
"github.com/google/uuid"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
|
@ -26,13 +25,3 @@ func (p *provider) AddSession(ctx context.Context, session models.Session) error
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteSession to delete session information from database
|
||||
func (p *provider) DeleteSession(ctx context.Context, userId string) error {
|
||||
sessionCollection := p.db.Collection(models.Collections.Session, options.Collection())
|
||||
_, err := sessionCollection.DeleteMany(ctx, bson.M{"user_id": userId}, options.Delete())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -57,6 +57,12 @@ func (p *provider) DeleteUser(ctx context.Context, user models.User) error {
|
|||
return err
|
||||
}
|
||||
|
||||
sessionCollection := p.db.Collection(models.Collections.Session, options.Collection())
|
||||
_, err = sessionCollection.DeleteMany(ctx, bson.M{"user_id": user.ID}, options.Delete())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -34,8 +34,6 @@ type Provider interface {
|
|||
|
||||
// AddSession to save session information in database
|
||||
AddSession(ctx context.Context, session models.Session) error
|
||||
// DeleteSession to delete session information from database
|
||||
DeleteSession(ctx context.Context, userId string) error
|
||||
|
||||
// AddEnv to save environment information in database
|
||||
AddEnv(ctx context.Context, env models.Env) (models.Env, error)
|
||||
|
|
|
@ -27,13 +27,3 @@ func (p *provider) AddSession(ctx context.Context, session models.Session) error
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteSession to delete session information from database
|
||||
func (p *provider) DeleteSession(ctx context.Context, userId string) error {
|
||||
result := p.db.Where("user_id = ?", userId).Delete(&models.Session{})
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -63,6 +63,11 @@ func (p *provider) DeleteUser(ctx context.Context, user models.User) error {
|
|||
return result.Error
|
||||
}
|
||||
|
||||
result = p.db.Where("user_id = ?", user.ID).Delete(&models.Session{})
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -45,11 +45,6 @@ func DeleteUserResolver(ctx context.Context, params model.DeleteUserInput) (*mod
|
|||
return res, err
|
||||
}
|
||||
|
||||
err = db.Provider.DeleteSession(ctx, user.ID)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
res = &model.Response{
|
||||
Message: `user deleted successfully`,
|
||||
}
|
||||
|
|
|
@ -59,7 +59,6 @@ func cleanData(email string) {
|
|||
dbUser, err := db.Provider.GetUserByEmail(ctx, email)
|
||||
if err == nil {
|
||||
db.Provider.DeleteUser(ctx, dbUser)
|
||||
db.Provider.DeleteSession(ctx, dbUser.ID)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user