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

Security layer #198

Merged
merged 13 commits into from
Dec 14, 2023
Merged

Security layer #198

merged 13 commits into from
Dec 14, 2023

Conversation

edobrb
Copy link
Member

@edobrb edobrb commented Dec 7, 2023

Closes #76

TODOS

  • Documentation
  • Tests
  • Implementation

Usage:

export const instance = module.build({
  name: 'test',
  // ...
  policies(context) {
    if (context.userId != null) {
      return userPolicies(context.userId)
    } else {
      return guestPolicies()
    }
  },
})



function userPolicies(userId: number) {
  return (
    security

      //User
      .of(User)
      .read({
        selection: true,
        restriction: { id: { equals: userId } },
      })
      .read({
        selection: { id: true, firstName: true, lastName: true },
      })

      //Post
      .of(Post)
      .read({
        selection: true,
        filter: { visibility: { equals: 'PRIVATE' }, author: { id: { equals: userId } } },
      })
      .read({
        selection: true,
        filter: {
          visibility: { equals: 'FOLLOWERS' },
          author: { followers: { some: { follower: { id: { equals: userId } } } } },
        },
      })
      .read({
        selection: true,
        filter: { visibility: { equals: 'PUBLIC' } },
      })

      // Others
      .of(Like)
      .read({ selection: true })
      .of(Follower)
      .read({ selection: true })
      .build()
  )
}

function guestPolicies() {
  return security
    .of(User)
    .read({
      selection: { firstName: true },
    })
    .build()
}

Copy link

codecov bot commented Dec 7, 2023

Codecov Report

Attention: 150 lines in your changes are missing coverage. Please review.

Comparison is base (3b572fa) 82.47% compared to head (f111954) 82.11%.
Report is 3 commits behind head on develop.

Files Patch % Lines
packages/module/src/security.ts 73.76% 7 Missing and 115 partials ⚠️
packages/module/src/errors.ts 65.62% 6 Missing and 5 partials ⚠️
packages/module/src/retrieve.ts 75.86% 0 Missing and 7 partials ⚠️
packages/module/src/middleware.ts 80.00% 2 Missing and 4 partials ⚠️
packages/rest/src/utils.ts 60.00% 0 Missing and 2 partials ⚠️
packages/graphql/src/graphql.ts 75.00% 1 Missing ⚠️
packages/module/src/module.ts 83.33% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #198      +/-   ##
===========================================
- Coverage    82.47%   82.11%   -0.37%     
===========================================
  Files           84       87       +3     
  Lines        11839    12376     +537     
  Branches      1964     2090     +126     
===========================================
+ Hits          9764    10162     +398     
- Misses         151      166      +15     
- Partials      1924     2048     +124     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@edobrb edobrb marked this pull request as ready for review December 14, 2023 11:40
@edobrb edobrb merged commit a06670d into develop Dec 14, 2023
2 checks passed
@edobrb edobrb deleted the security-layer branch December 14, 2023 11:49
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.

RBAC
1 participant