Skip to content

Fix JukeboxRole logic in GetUser to respect AdminOnly setting #4170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2025

Conversation

deluan
Copy link
Member

@deluan deluan commented Jun 3, 2025

Description

Fixes the bug where GetUser was incorrectly setting jukeboxRole to true for all users when jukebox was enabled, regardless of the Jukebox.AdminOnly configuration setting.

This PR also refactors the code to eliminate duplication between GetUser and GetUsers methods.

Changes Made

🐛 Primary Fix: JukeboxRole Logic

  • Fixed GetUser method to properly implement jukebox role logic: !conf.Server.Jukebox.AdminOnly || user.IsAdmin
  • Previously it was simply setting JukeboxRole = conf.Server.Jukebox.Enabled ignoring the AdminOnly setting

🔄 Code Refactoring

  • Extracted buildUserResponse() helper function to eliminate ~30 lines of duplicated code between GetUser and GetUsers
  • Fixed username field inconsistency: GetUsers was incorrectly using loggedUser.Name instead of loggedUser.UserName
  • Improved maintainability: User response logic is now centralized in one location

Added Comprehensive Tests

  • Happy path tests ensuring both methods work correctly and return consistent data
  • Jukebox role permission tests using DescribeTable to cover all scenarios:
    • Jukebox disabled → jukeboxRole: false
    • Jukebox enabled, not admin-only → jukeboxRole: true (for all users)
    • Jukebox enabled, admin-only → jukeboxRole: true (admin only), jukeboxRole: false (regular users)

Testing

All existing tests pass, and new comprehensive tests cover the fixed functionality:

$ make test PKG=./server/subsonic/...
# All tests pass ✅

The new tests specifically validate that:

  • Non-admin users get jukeboxRole: false when Jukebox.AdminOnly = true
  • Admin users get jukeboxRole: true when jukebox is enabled
  • Both GetUser and GetUsers return identical user data (consistency)

Fixes

Closes #4160

Breaking Changes

None. This is a bug fix that corrects the behavior to match the expected specification.

Before this fix:

  • GetUser ignored Jukebox.AdminOnly setting
  • Non-admin users incorrectly received jukeboxRole: true when Jukebox.AdminOnly = true

After this fix:

  • GetUser properly respects Jukebox.AdminOnly setting
  • Non-admin users correctly receive jukeboxRole: false when Jukebox.AdminOnly = true

…plication

- Fix GetUser JukeboxRole to properly respect AdminOnly setting

- Extract buildUserResponse helper to eliminate duplication between GetUser and GetUsers

- Fix username field inconsistency (GetUsers was using loggedUser.Name instead of UserName)

- Add comprehensive tests covering jukebox role permissions and consistency between methods

Fixes #4160
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @deluan, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

Hello team, gemini-code-assist here to provide a summary of this pull request. This PR primarily addresses a bug in the GetUser Subsonic endpoint where the jukeboxRole was incorrectly set to true for all users when the jukebox feature was enabled, regardless of the Jukebox.AdminOnly configuration setting. It also includes a refactoring to consolidate user response logic and adds comprehensive tests to cover the fix and ensure consistency between GetUser and GetUsers endpoints.

Highlights

  • Bug Fix: JukeboxRole Logic: Corrected the logic in the GetUser endpoint to properly respect the conf.Server.Jukebox.AdminOnly setting when determining the jukeboxRole for a user. The logic is now !conf.Server.Jukebox.AdminOnly || user.IsAdmin.
  • Code Refactoring: Extracted the common logic for building a user response object into a new helper function buildUserResponse. Both GetUser and GetUsers now utilize this function, reducing code duplication. This refactoring also fixes an inconsistency where GetUsers was using loggedUser.Name instead of loggedUser.UserName.
  • Comprehensive Tests Added: Introduced a new test file (server/subsonic/users_test.go) with tests covering the happy path (consistency between GetUser and GetUsers) and specific DescribeTable tests to validate the JukeboxRole logic across all relevant configuration and user type scenarios.

Changelog

  • server/subsonic/users.go
    • Added new helper function buildUserResponse (lines 12-29) to centralize user response creation logic.
    • Updated GetUser (lines 32-42) to use the new buildUserResponse helper.
    • Updated GetUsers (lines 46-53) to use the new buildUserResponse helper, fixing the Username field inconsistency and applying the correct JukeboxRole logic via the helper.
  • server/subsonic/users_test.go
    • Added a new test file (users_test.go) for the Subsonic users API.
    • Included a 'Happy path' test (lines 33-78) to verify consistency between GetUser and GetUsers responses.
    • Added a DescribeTable test (lines 80-95) specifically for validating the JukeboxRole logic under different configurations and user types.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


A bug did hide,
Jukebox role defied.
Refactor came,
Tests played the game,
Now logic's applied.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This is an excellent pull request that effectively addresses the JukeboxRole bug and significantly improves code maintainability through refactoring. The introduction of the buildUserResponse helper function is a great way to centralize user response logic and eliminate duplication. The fix for the username field inconsistency is also well-handled by this refactoring.

The added tests are comprehensive and very well-structured, particularly the use of DescribeTable for testing various jukebox permission scenarios. This ensures the fix is robust and covers all relevant cases. The "Happy path" test also provides good confidence in the consistency between GetUser and GetUsers.

The code adheres to common Go best practices and is clear and easy to understand. The PR description itself is also very thorough and clearly outlines the changes and their rationale.

Overall, this is a high-quality contribution.

Summary of Findings

  • Global Configuration Access in Helper Function: The buildUserResponse function directly accesses the global conf.Server variable (e.g., conf.Server.Jukebox, conf.Server.EnableDownloads). While this is consistent with some existing patterns in the codebase and works correctly, an alternative approach for future consideration could be to pass the necessary configuration values as parameters to buildUserResponse. This would make the function more self-contained, improve its testability in complete isolation (without relying on global state setup like configtest.SetupConfig()), and make its dependencies more explicit. This is a minor point and considered low severity, thus no specific review comment was added per the review settings.

Merge Readiness

The pull request is in excellent shape. The bug fix is correct, the refactoring improves the codebase, and the tests are thorough. From my perspective, after considering the findings, the code is ready to be merged. As I am an AI assistant, I am not authorized to approve pull requests; please ensure it undergoes the standard review and approval process by project maintainers.

@deluan deluan merged commit e3527f9 into master Jun 3, 2025
20 checks passed
@deluan deluan deleted the fix/subsonic-users-duplication-4160 branch June 3, 2025 01:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: user jukeboxRole is always true
1 participant