Skip to content

Commit 775d896

Browse files
committed
TO REMOVE: add get user psw for debug purposes
1 parent 6f4b07b commit 775d896

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/dao/user.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,21 @@ func (d *UserDao) GetUserByOTP(c context.Context, otpCode string) (*api.UserResp
291291
}
292292
return &user, nil
293293
}
294+
295+
func (d *UserDao) GetPasswordByUsername(c context.Context, username string) (string, error) {
296+
query := "SELECT password FROM users WHERE username = $1"
297+
rows, err := d.db.Query(c, query, username)
298+
if err != nil {
299+
return "", fmt.Errorf("db error: %w", err)
300+
}
301+
defer rows.Close()
302+
303+
if rows.Next() {
304+
var password string
305+
if err := rows.Scan(&password); err != nil {
306+
return "", fmt.Errorf("failed to scan password: %w", err)
307+
}
308+
return password, nil
309+
}
310+
return "", ErrUserNotFound
311+
}

src/handlers/user.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,26 @@ func (uh *UserHandlers) DeleteUserByUsername(c *gin.Context, username string) {
202202

203203
c.JSON(http.StatusOK, gin.H{"message": "user deleted successfully"})
204204
}
205+
206+
func (uh *UserHandlers) GetUserPassword(c *gin.Context, username string) {
207+
_, role, err := auth.GetPermissions(c)
208+
if err != nil {
209+
return
210+
}
211+
if role != "superuser" && role != "admin" {
212+
c.JSON(http.StatusForbidden, gin.H{"error": "forbidden"})
213+
return
214+
}
215+
216+
password, err := uh.dao.GetPasswordByUsername(c.Request.Context(), username)
217+
if err != nil {
218+
if errors.Is(err, dao.ErrUserNotFound) {
219+
c.JSON(http.StatusNotFound, gin.H{"error": "user not found"})
220+
return
221+
}
222+
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to retrieve password"})
223+
return
224+
}
225+
226+
c.JSON(http.StatusOK, gin.H{"password": password})
227+
}

0 commit comments

Comments
 (0)