bouncer.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * Contract source: https://git.io/Jte3T
  3. *
  4. * Feel free to let us know via PR, if you find something broken in this config
  5. * file.
  6. */
  7. import Bouncer from '@ioc:Adonis/Addons/Bouncer'
  8. import { UserRoles } from 'App/Models/User'
  9. /*
  10. |--------------------------------------------------------------------------
  11. | Bouncer Actions
  12. |--------------------------------------------------------------------------
  13. |
  14. | Actions allows you to separate your application business logic from the
  15. | authorization logic. Feel free to make use of policies when you find
  16. | yourself creating too many actions
  17. |
  18. | You can define an action using the `.define` method on the Bouncer object
  19. | as shown in the following example
  20. |
  21. | ```
  22. | Bouncer.define('deletePost', (user: User, post: Post) => {
  23. | return post.user_id === user.id
  24. | })
  25. | ```
  26. |
  27. |****************************************************************
  28. | NOTE: Always export the "actions" const from this file
  29. |****************************************************************
  30. */
  31. export const { actions } = Bouncer.define('admin', (user) => {
  32. return user.role === UserRoles.Admin
  33. }).define('owner', (user, model) => {
  34. return model.userId === user.id
  35. })
  36. /*
  37. |--------------------------------------------------------------------------
  38. | Bouncer Policies
  39. |--------------------------------------------------------------------------
  40. |
  41. | Policies are self contained actions for a given resource. For example: You
  42. | can create a policy for a "User" resource, one policy for a "Post" resource
  43. | and so on.
  44. |
  45. | The "registerPolicies" accepts a unique policy name and a function to lazy
  46. | import the policy
  47. |
  48. | ```
  49. | Bouncer.registerPolicies({
  50. | UserPolicy: () => import('App/Policies/User'),
  51. | PostPolicy: () => import('App/Policies/Post')
  52. | })
  53. | ```
  54. |
  55. |****************************************************************
  56. | NOTE: Always export the "policies" const from this file
  57. |****************************************************************
  58. */
  59. export const { policies } = Bouncer.registerPolicies({})