StoragePolicies.constants.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * ----------------------------------------------------------------
  3. * PostgreSQL policy templates for the storage dashboard
  4. * ----------------------------------------------------------------
  5. * id: Unique identifier for the monaco editor to dynamically refresh
  6. * templateName: As a display for a more descriptive title for the policy
  7. * description: Additional details about the template and how to make it yours
  8. * statement: SQL statement template for the policy
  9. *
  10. * name: Actual policy name that will be used in the editor
  11. * definition: Actual policy definition that will be used in the editor
  12. * allowedOperations: Operations to create policies for
  13. */
  14. export const STORAGE_POLICY_TEMPLATES = [
  15. {
  16. id: 'policy-1',
  17. templateName: 'Allow access to JPG images in a public folder to anonymous users',
  18. description:
  19. 'This policy uses native postgres functions, functions from auth and storage schema',
  20. name: 'Give anon users access to JPG images in folder',
  21. statement: `
  22. CREATE POLICY "policy_name"
  23. ON storage.objects FOR {operation} {USING | WITH CHECK} (
  24. -- restrict bucket
  25. bucket_id = {bucket_name}
  26. -- allow access to only jpg file
  27. AND storage."extension"(name) = 'jpg'
  28. -- in the public folder
  29. AND LOWER((storage.foldername(name))[1]) = 'public'
  30. -- to anonymous users
  31. AND auth.role() = 'anon'
  32. );
  33. `.trim(),
  34. definition: `bucket_id = {bucket_id} AND storage."extension"(name) = 'jpg' AND LOWER((storage.foldername(name))[1]) = 'public' AND auth.role() = 'anon'`,
  35. allowedOperations: [],
  36. },
  37. {
  38. id: 'policy-2',
  39. templateName: 'Give users access to only their own top level folder named as uid',
  40. description:
  41. 'For example a user with id d7bed83c-44a0-4a4f-925f-efc384ea1e50 will be able to access anything under the folder d7bed83c-44a0-4a4f-925f-efc384ea1e50/',
  42. name: 'Give users access to own folder',
  43. statement: `
  44. CREATE POLICY "policy_name"
  45. ON storage.objects FOR {operation} {USING | WITH CHECK} (
  46. -- restrict bucket
  47. bucket_id = {bucket_name}
  48. and (select auth.uid()::text) = (storage.foldername(name))[1]
  49. );
  50. `.trim(),
  51. definition: `bucket_id = {bucket_id} AND (select auth.uid()::text) = (storage.foldername(name))[1]`,
  52. allowedOperations: [],
  53. },
  54. {
  55. id: 'policy-3',
  56. templateName: 'Give users access to a folder only to authenticated users',
  57. description:
  58. 'This policy gives users access to a folder (e.g private) only if they are authenticated',
  59. name: 'Give users authenticated access to folder',
  60. statement: `
  61. CREATE POLICY "policy_name"
  62. ON storage.objects FOR {operation} {USING | WITH CHECK} (
  63. -- restrict bucket
  64. bucket_id = {bucket_name}
  65. AND (storage.foldername(name))[1] = 'private'
  66. AND (select auth.role()) = 'authenticated'
  67. );
  68. `.trim(),
  69. definition: `bucket_id = {bucket_id} AND (storage.foldername(name))[1] = 'private' AND auth.role() = 'authenticated'`,
  70. allowedOperations: [],
  71. },
  72. {
  73. id: 'policy-4',
  74. templateName: 'Give access to a nested folder called admin/assets only to a specific user',
  75. description:
  76. 'This policy gives read access to all authenticated users for your project to the folder "public"',
  77. name: 'Give access to a folder',
  78. statement: `
  79. CREATE POLICY "policy_name"
  80. ON storage.objects FOR {operation} {USING | WITH CHECK} (
  81. -- restrict bucket
  82. bucket_id = {bucket_name}
  83. AND (storage.foldername(name))[1] = 'admin' AND (storage.foldername(name))[2] = 'assets'
  84. AND (select auth.uid()::text) = 'd7bed83c-44a0-4a4f-925f-efc384ea1e50'
  85. );
  86. `.trim(),
  87. definition: `bucket_id = {bucket_id} AND (storage.foldername(name))[1] = 'admin' AND (storage.foldername(name))[2] = 'assets' AND (select auth.uid()::text) = 'd7bed83c-44a0-4a4f-925f-efc384ea1e50'`,
  88. allowedOperations: [],
  89. },
  90. {
  91. id: 'policy-5',
  92. templateName: 'Give access to a file to a user',
  93. description: 'This policy gives access to a specific file to a specific user',
  94. name: 'Give access to a file to user',
  95. statement: `
  96. CREATE POLICY "policy_name"
  97. ON storage.objects FOR {operation} {USING | WITH CHECK} (
  98. -- restrict bucket
  99. bucket_id = {bucket_name}
  100. AND name = 'admin/assets/Costa Rican Frog.jpg'
  101. AND (select auth.uid()::text) = 'd7bed83c-44a0-4a4f-925f-efc384ea1e50'
  102. );
  103. `.trim(),
  104. definition: `bucket_id = {bucket_id} AND name = 'admin/assets/Costa Rican Frog.jpg' AND (select auth.uid()::text) = 'd7bed83c-44a0-4a4f-925f-efc384ea1e50'`,
  105. allowedOperations: [],
  106. },
  107. ]