Skip to content

Commit

Permalink
Add admin flag, refactor user-related code
Browse files Browse the repository at this point in the history
- Introduce is_admin flag in member table and user-related functions
  - Update createOrReturnID to return is_admin status
  - Automatically set first user as admin
- Refactor User struct to include IsAdmin field
  - Update GetUser function to handle new IsAdmin field
  - Modify UserMiddleware to set is_admin in context
- Remove unused model structs (Favorite, MemberPref, Message, etc.)
- Update CreateOrReturnID query to return both ID and is_admin status
- Adjust tests to accommodate new is_admin functionality
  - Update mock functions and test cases for user-related operations
- Replace os.Stderr with io.Discard in test logger for clean test output

Change-Id: I6238f1e736e6924d32f95c0f41b48578f86104f6
Signed-off-by: Ian Meyer <[email protected]>
  • Loading branch information
imeyer committed Oct 13, 2024
1 parent 3672b88 commit db4d594
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 118 deletions.
60 changes: 0 additions & 60 deletions models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 10 additions & 5 deletions queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 16 additions & 8 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ type ExtendedQuerier interface {
}

type User struct {
ID int64
Email string
ID int64
Email string
IsAdmin bool
}

type QueriesWrapper struct {
Expand Down Expand Up @@ -396,7 +397,7 @@ func (s *DiscussService) CreateThreadPost(w http.ResponseWriter, r *http.Request

user, err := GetUser(r)
if err != nil {
s.logger.DebugContext(r.Context(), "CreateThreadPost", slog.String("user", user.Email), slog.String("user_id", strconv.FormatInt(user.ID, 10)))
s.logger.DebugContext(r.Context(), "CreateThreadPost", slog.String("user", user.Email), slog.Int64("user_id", user.ID))
s.renderError(w, http.StatusInternalServerError)
return
}
Expand Down Expand Up @@ -1024,16 +1025,17 @@ func UserMiddleware(s *DiscussService, next http.Handler) http.HandlerFunc {

ctx := context.WithValue(r.Context(), "email", email)

id, err := s.queries.CreateOrReturnID(ctx, email)
user, err := s.queries.CreateOrReturnID(ctx, email)
if err != nil {
s.logger.ErrorContext(ctx, err.Error())
s.renderError(w, http.StatusInternalServerError)
return
}

ctx = context.WithValue(ctx, "user_id", id)
ctx = context.WithValue(ctx, "user_id", user.ID)
ctx = context.WithValue(ctx, "is_admin", user.IsAdmin)

s.logger.DebugContext(ctx, "UserMiddleware", slog.String("email", email), slog.Int64("user_id", id))
s.logger.DebugContext(ctx, "UserMiddleware", slog.String("email", email), slog.Int64("user_id", user.ID), slog.Bool("is_admin", user.IsAdmin))

r = r.WithContext(ctx)

Expand All @@ -1049,13 +1051,19 @@ func GetUser(r *http.Request) (User, error) {
if uid, ok := ctx.Value("user_id").(int64); ok {
u.ID = uid
} else {
return User{}, errors.New("user ID not found in context or invalid type")
return User{}, errors.New("user_id not found in context or invalid type")
}

if email, ok := ctx.Value("email").(string); ok {
u.Email = email
} else {
return User{}, errors.New("user email not found in context or invalid type")
return User{}, errors.New("email not found in context or invalid type")
}

if isAdmin, ok := ctx.Value("is_admin").(bool); ok {
u.IsAdmin = isAdmin
} else {
return User{}, errors.New("is_admin not found in context or invalid type")
}

return u, nil
Expand Down
Loading

0 comments on commit db4d594

Please sign in to comment.