Skip to content

Commit

Permalink
Move admin actions near User related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
gusah009 committed Jul 23, 2024
1 parent fe4397a commit 94a25ea
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
12 changes: 6 additions & 6 deletions server/backend/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ type Database interface {
hashedPassword string,
) (*UserInfo, error)

// DeleteUserInfoByName deletes a user by name.
DeleteUserInfoByName(ctx context.Context, username string) error

// ChangeUserPassword changes to new password for user.
ChangeUserPassword(ctx context.Context, username, hashedNewPassword string) error

// FindUserInfoByID returns a user by the given ID.
FindUserInfoByID(ctx context.Context, id types.ID) (*UserInfo, error)

Expand All @@ -143,12 +149,6 @@ type Database interface {
// after handling PushPull.
UpdateClientInfoAfterPushPull(ctx context.Context, clientInfo *ClientInfo, docInfo *DocInfo) error

// DeleteUserInfoByName deletes a user by name.
DeleteUserInfoByName(ctx context.Context, username string) error

// ChangeUserPassword changes to new password for user.
ChangeUserPassword(ctx context.Context, username, hashedNewPassword string) error

// FindNextNCyclingProjectInfos finds the next N cycling projects from the given projectID.
FindNextNCyclingProjectInfos(
ctx context.Context,
Expand Down
58 changes: 29 additions & 29 deletions server/backend/database/mongo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,35 @@ func (c *Client) CreateUserInfo(
return info, nil
}

// DeleteUserInfoByName deletes a user by name.
func (c *Client) DeleteUserInfoByName(ctx context.Context, username string) error {
deleteResult, err := c.collection(ColUsers).DeleteOne(ctx, bson.M{
"username": username,
})
if err != nil {
return err
}
if deleteResult.DeletedCount == 0 {
return fmt.Errorf("no user found with username %s", username)
}
return nil
}

// ChangeUserPassword changes to new password for user.
func (c *Client) ChangeUserPassword(ctx context.Context, username, hashedNewPassword string) error {
updateResult, err := c.collection(ColUsers).UpdateOne(ctx,
bson.M{"username": username},
bson.M{"$set": bson.M{"hashed_password": hashedNewPassword}},
)
if err != nil {
return err
}
if updateResult.ModifiedCount == 0 {
return fmt.Errorf("no user found with username %s", username)
}
return nil
}

// FindUserInfoByID returns a user by ID.
func (c *Client) FindUserInfoByID(ctx context.Context, clientID types.ID) (*database.UserInfo, error) {
result := c.collection(ColUsers).FindOne(ctx, bson.M{
Expand Down Expand Up @@ -643,35 +672,6 @@ func (c *Client) UpdateClientInfoAfterPushPull(
return nil
}

// DeleteUserInfoByName deletes a user by name.
func (c *Client) DeleteUserInfoByName(ctx context.Context, username string) error {
deleteResult, err := c.collection(ColUsers).DeleteOne(ctx, bson.M{
"username": username,
})
if err != nil {
return err
}
if deleteResult.DeletedCount == 0 {
return fmt.Errorf("no user found with username %s", username)
}
return nil
}

// ChangeUserPassword changes to new password for user.
func (c *Client) ChangeUserPassword(ctx context.Context, username, hashedNewPassword string) error {
updateResult, err := c.collection(ColUsers).UpdateOne(ctx,
bson.M{"username": username},
bson.M{"$set": bson.M{"hashed_password": hashedNewPassword}},
)
if err != nil {
return err
}
if updateResult.ModifiedCount == 0 {
return fmt.Errorf("no user found with username %s", username)
}
return nil
}

// FindDeactivateCandidatesPerProject finds the clients that need housekeeping per project.
func (c *Client) FindDeactivateCandidatesPerProject(
ctx context.Context,
Expand Down

0 comments on commit 94a25ea

Please sign in to comment.