Skip to content
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

Migrate from role-based to permission-based auth #336

Open
wants to merge 29 commits into
base: main
Choose a base branch
from

Conversation

AmirAgassi
Copy link
Member

@AmirAgassi AmirAgassi commented Jan 9, 2025

Description

Refactored backend authorization system from role-based to permission-based access control. This change provides more specific control over user permissions and better scalability for future features.

Linked Issues

Testing

  • /internal/tests/auth_test.go
  • /internal/tests/companies_test.go
  • /internal/tests/projects_test.go
  • /internal/tests/teams_test.go
  • /internal/tests/transactions_test.go

Checklist

Before opening this PR, make sure the PR:

  • Has an assignee or group of assignees.
  • Has a reviewer or a group of reviewers.
  • Is labelled properly.
  • Has the SPUR project assigned to it.
  • Has an assigned milestone.

Additionally, make sure that:

  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation.
  • My changes generate no new warnings.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • Any dependent changes have been merged and published in downstream modules.

@AmirAgassi AmirAgassi added backend Related to the backend of the project refactor Any refactors being done in the project api Related to the backend routes and endpoints labels Jan 9, 2025
@AmirAgassi AmirAgassi added this to the Complete the project MVP milestone Jan 9, 2025
@AmirAgassi AmirAgassi self-assigned this Jan 9, 2025
@AmirAgassi AmirAgassi linked an issue Jan 9, 2025 that may be closed by this pull request
4 tasks
@AmirAgassi AmirAgassi marked this pull request as ready for review January 12, 2025 23:23
@Copilot Copilot bot review requested due to automatic review settings January 12, 2025 23:23
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 25 out of 40 changed files in this pull request and generated 1 comment.

Files not reviewed (15)
  • backend/.sqlc/migrations/20241215194302_initial_schema.sql: Language not supported
  • backend/.sqlc/queries/projects.sql: Language not supported
  • backend/.sqlc/queries/transactions.sql: Language not supported
  • backend/.sqlc/queries/users.sql: Language not supported
  • backend/internal/jwt/generate.go: Evaluated as low risk
  • backend/internal/middleware/types.go: Evaluated as low risk
  • backend/internal/tests/company_test.go: Evaluated as low risk
  • backend/internal/tests/auth_test.go: Evaluated as low risk
  • backend/db/users.sql.go: Evaluated as low risk
  • backend/db/models.go: Evaluated as low risk
  • backend/db/transactions.sql.go: Evaluated as low risk
  • backend/internal/tests/jwt_middleware_test.go: Evaluated as low risk
  • backend/internal/tests/helpers.go: Evaluated as low risk
  • backend/internal/jwt/types.go: Evaluated as low risk
  • backend/db/projects.sql.go: Evaluated as low risk
Comments suppressed due to low confidence (3)

backend/internal/middleware/jwt.go:38

  • The request method should be checked before allowing access based on the PermViewAllProjects permission to ensure that only GET requests are allowed without further validation.
if c.Request().Method == http.MethodGet {

backend/internal/tests/comments_test.go:21

  • The struct name 'CommentResponse' is appropriate and consistent with its functionality.
type CommentResponse struct {

backend/internal/tests/comments_test.go:32

  • The function name 'TestCommentEndpoints' is appropriate and consistent with its functionality.
func TestCommentEndpoints(t *testing.T) {

backend/internal/jwt/generate.go Outdated Show resolved Hide resolved
backend/internal/middleware/jwt.go Outdated Show resolved Hide resolved
@juancwu juancwu closed this Jan 17, 2025
@juancwu juancwu reopened this Jan 17, 2025
@AmirAgassi AmirAgassi requested review from juancwu and Copilot January 19, 2025 23:13
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 25 out of 40 changed files in this pull request and generated 1 comment.

Files not reviewed (15)
  • backend/.sqlc/migrations/20241215194302_initial_schema.sql: Language not supported
  • backend/.sqlc/queries/projects.sql: Language not supported
  • backend/.sqlc/queries/transactions.sql: Language not supported
  • backend/.sqlc/queries/users.sql: Language not supported
  • backend/internal/permissions/permissions.go: Evaluated as low risk
  • backend/db/models.go: Evaluated as low risk
  • backend/internal/jwt/generate.go: Evaluated as low risk
  • backend/internal/middleware/types.go: Evaluated as low risk
  • backend/internal/tests/company_test.go: Evaluated as low risk
  • backend/internal/tests/auth_test.go: Evaluated as low risk
  • backend/db/users.sql.go: Evaluated as low risk
  • backend/db/transactions.sql.go: Evaluated as low risk
  • backend/internal/tests/jwt_middleware_test.go: Evaluated as low risk
  • backend/internal/jwt/types.go: Evaluated as low risk
  • backend/internal/tests/helpers.go: Evaluated as low risk
Comments suppressed due to low confidence (3)

backend/internal/middleware/request_validator.go:209

  • [nitpick] The error message for 'valid_permissions' could be more descriptive. Consider providing more context or examples of valid permissions.
return fmt.Sprintf("%s contains invalid permissions", field)

backend/db/projects.sql.go:321

  • The comment is unclear. It should be more descriptive to explain the purpose of the permission check, such as 'Check if the user has the PermViewAllProjects permission'.
-- Check for PermViewAllProjects (1 << 0)

backend/internal/tests/helpers_test.go:173

  • [nitpick] The use of fmt.Sprint for permissions in the JSON string is unconventional. Consider using a more straightforward approach to set the permissions value.
"permissions": ` + fmt.Sprint(permissions.PermStartupOwner) + `,

backend/internal/tests/comments_test.go Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Related to the backend routes and endpoints backend Related to the backend of the project refactor Any refactors being done in the project
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

[BE Feat] Migrate the role-based system to a permission-based system
2 participants