Skip to content

Commit 00e22a4

Browse files
committed
add rls policies
1 parent 6937d8f commit 00e22a4

File tree

3 files changed

+139
-32
lines changed

3 files changed

+139
-32
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
-- Drop policies and disable RLS for 'categories'
2+
DROP POLICY IF EXISTS "Allow modification for authenticated users" ON categories;
3+
DROP POLICY IF EXISTS "Allow public select access" ON categories;
4+
ALTER TABLE categories DISABLE ROW LEVEL SECURITY;
5+
6+
-- Drop policies and disable RLS for 'products'
7+
DROP POLICY IF EXISTS "Allow modification for authenticated users" ON products;
8+
DROP POLICY IF EXISTS "Allow public select access" ON products;
9+
ALTER TABLE products DISABLE ROW LEVEL SECURITY;
10+
11+
-- Drop policies and disable RLS for 'order_items'
12+
DROP POLICY IF EXISTS "Allow select based on order owner" ON order_items;
13+
ALTER TABLE order_items DISABLE ROW LEVEL SECURITY;
14+
15+
-- Drop policies and disable RLS for 'orders'
16+
DROP POLICY IF EXISTS "Allow insert for authenticated users" ON orders;
17+
DROP POLICY IF EXISTS "Allow select access to owner" ON orders;
18+
ALTER TABLE orders DISABLE ROW LEVEL SECURITY;
19+
20+
-- Drop policies and disable RLS for 'cart_items'
21+
DROP POLICY IF EXISTS "Allow access based on cart owner" ON cart_items;
22+
ALTER TABLE cart_items DISABLE ROW LEVEL SECURITY;
23+
24+
-- Drop policies and disable RLS for 'carts'
25+
DROP POLICY IF EXISTS "Allow full access to owner" ON carts;
26+
ALTER TABLE carts DISABLE ROW LEVEL SECURITY;
27+
28+
-- Drop policies and disable RLS for 'addresses'
29+
DROP POLICY IF EXISTS "Allow full access to owner" ON addresses;
30+
ALTER TABLE addresses DISABLE ROW LEVEL SECURITY;
31+
32+
-- Drop policies and disable RLS for 'users'
33+
DROP POLICY IF EXISTS "Allow individual update access" ON users;
34+
DROP POLICY IF EXISTS "Allow individual select access" ON users;
35+
ALTER TABLE users DISABLE ROW LEVEL SECURITY;
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
-- Enable Row Level Security for all relevant tables
2+
ALTER TABLE users ENABLE ROW LEVEL SECURITY;
3+
ALTER TABLE addresses ENABLE ROW LEVEL SECURITY;
4+
ALTER TABLE carts ENABLE ROW LEVEL SECURITY;
5+
ALTER TABLE cart_items ENABLE ROW LEVEL SECURITY;
6+
ALTER TABLE orders ENABLE ROW LEVEL SECURITY;
7+
ALTER TABLE order_items ENABLE ROW LEVEL SECURITY;
8+
ALTER TABLE products ENABLE ROW LEVEL SECURITY;
9+
ALTER TABLE categories ENABLE ROW LEVEL SECURITY;
10+
11+
-- Force RLS for table owners (recommended by Supabase)
12+
ALTER TABLE users FORCE ROW LEVEL SECURITY;
13+
ALTER TABLE addresses FORCE ROW LEVEL SECURITY;
14+
ALTER TABLE carts FORCE ROW LEVEL SECURITY;
15+
ALTER TABLE cart_items FORCE ROW LEVEL SECURITY;
16+
ALTER TABLE orders FORCE ROW LEVEL SECURITY;
17+
ALTER TABLE order_items FORCE ROW LEVEL SECURITY;
18+
ALTER TABLE products FORCE ROW LEVEL SECURITY;
19+
ALTER TABLE categories FORCE ROW LEVEL SECURITY;
20+
21+
22+
-- Policies for 'users' table
23+
-- Users can select their own data
24+
CREATE POLICY "Allow individual select access" ON users FOR SELECT
25+
USING (auth.uid() = id);
26+
-- Users can update their own data
27+
CREATE POLICY "Allow individual update access" ON users FOR UPDATE
28+
USING (auth.uid() = id)
29+
WITH CHECK (auth.uid() = id);
30+
31+
-- Policies for 'addresses' table
32+
-- Users can manage their own addresses fully
33+
CREATE POLICY "Allow full access to owner" ON addresses FOR ALL
34+
USING (auth.uid() = user_id)
35+
WITH CHECK (auth.uid() = user_id);
36+
37+
-- Policies for 'carts' table
38+
-- Users can manage their own cart fully
39+
CREATE POLICY "Allow full access to owner" ON carts FOR ALL
40+
USING (auth.uid() = user_id)
41+
WITH CHECK (auth.uid() = user_id);
42+
43+
-- Policies for 'cart_items' table
44+
-- Users can manage items only if they own the corresponding cart
45+
CREATE POLICY "Allow access based on cart owner" ON cart_items FOR ALL
46+
USING ( EXISTS (SELECT 1 FROM carts WHERE carts.id = cart_items.cart_id AND carts.user_id = auth.uid()) )
47+
WITH CHECK ( EXISTS (SELECT 1 FROM carts WHERE carts.id = cart_items.cart_id AND carts.user_id = auth.uid()) );
48+
49+
-- Policies for 'orders' table
50+
-- Users can select their own orders
51+
CREATE POLICY "Allow select access to owner" ON orders FOR SELECT
52+
USING (auth.uid() = user_id);
53+
-- Users can insert orders (user_id check ensures they insert for themselves)
54+
CREATE POLICY "Allow insert for authenticated users" ON orders FOR INSERT
55+
WITH CHECK (auth.uid() = user_id);
56+
-- (No UPDATE/DELETE policies initially - managed by API logic)
57+
58+
-- Policies for 'order_items' table
59+
-- Users can select items belonging to their own orders
60+
CREATE POLICY "Allow select based on order owner" ON order_items FOR SELECT
61+
USING ( EXISTS (SELECT 1 FROM orders WHERE orders.id = order_items.order_id AND orders.user_id = auth.uid()) );
62+
-- (No INSERT/UPDATE/DELETE policies initially)
63+
64+
-- Policies for 'products' table
65+
-- Allow public read access to products
66+
CREATE POLICY "Allow public select access" ON products FOR SELECT
67+
USING (true);
68+
-- Allow authenticated users to manage products (can be restricted to admin later)
69+
CREATE POLICY "Allow modification for authenticated users" ON products FOR ALL
70+
USING (auth.role() = 'authenticated') -- Allow reading existing rows if authenticated
71+
WITH CHECK (auth.role() = 'authenticated'); -- Check applies to INSERT/UPDATE
72+
73+
-- Policies for 'categories' table
74+
-- Allow public read access to categories
75+
CREATE POLICY "Allow public select access" ON categories FOR SELECT
76+
USING (true);
77+
-- Allow authenticated users to manage categories (can be restricted to admin later)
78+
CREATE POLICY "Allow modification for authenticated users" ON categories FOR ALL
79+
USING (auth.role() = 'authenticated') -- Allow reading existing rows if authenticated
80+
WITH CHECK (auth.role() = 'authenticated'); -- Check applies to INSERT/UPDATE

readme.md

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,24 @@
2424
</p>
2525

2626
# ✨ Recursos Atuais e Planejados
27-
<div>
28-
Autenticação e Gerenciamento de Usuários (Registro, Login, Dados do Usuário, Endereços)
29-
</div>
30-
<div>
31-
Gerenciamento de Produtos e Categorias
32-
</div>
33-
<div>
34-
Carrinho de Compras
35-
</div>
36-
<div>
37-
Gerenciamento de Pedidos (Criação e Listagem)
38-
</div>
39-
<div>
40-
Armazenamento de dados com PostgreSQL (via Supabase)
41-
</div>
42-
<div>
43-
Autenticação segura com JWT e Hashing de Senha (bcrypt)
44-
</div>
45-
<div>
46-
Endpoints RESTful com prefixo `/api`
47-
</div>
48-
<div>
27+
28+
- Autenticação e Gerenciamento de Usuários (Registro, Login, Dados do Usuário, Endereços)
29+
- Gerenciamento de Produtos e Categorias
30+
- Carrinho de Compras
31+
- Gerenciamento de Pedidos (Criação e Listagem)
32+
- Armazenamento de dados com PostgreSQL (via Supabase)
33+
- Autenticação segura com JWT e Hashing de Senha (bcrypt)
34+
- Endpoints RESTful com prefixo `/api`
4935
Health check
50-
</div>
51-
<div>
52-
Testes Unitários para Handlers (Auth, User/Address, Product, Category, Cart)
53-
</div>
54-
<div>
55-
*Planejado:* Testes para OrderHandler, Testes de Integração, Lógica de Frete, Paginação, Filtros, Validação Avançada, Permissões (Admin), Documentação Swagger completa.
56-
</div>
36+
- Testes Unitários para Handlers (Auth, User/Address, Product, Category, Cart)
5737

58-
## 🚀 Exemplo de uso
38+
## Planejado:
39+
>>> Testes para OrderHandler, Testes de Integração, Lógica de Frete, Paginação, Filtros, Validação Avançada, Permissões (Admin), Documentação Swagger completa.
40+
41+
42+
## Exemplo de uso
43+
44+
<details>
5945

6046
(Veja a seção Endpoints Atuais para mais detalhes)
6147

@@ -126,6 +112,7 @@ curl -X POST http://localhost:4444/api/orders \\
126112
-H "Authorization: Bearer $TOKEN"
127113
```
128114

115+
</details>
129116

130117
## Documentação da API (Planejada)
131118

@@ -220,6 +207,8 @@ stretchr/testify (Testes Unitários)
220207

221208
## 🔍 Endpoints Atuais
222209

210+
<details>
211+
223212
**Saúde**
224213
* `GET /api/health`: Verifica status da aplicação.
225214

@@ -323,6 +312,8 @@ stretchr/testify (Testes Unitários)
323312
* **Sucesso (200):** Objeto `{"order": {...}, "items": [{...}]}`.
324313
* **Erros:** `401`, `403` (não é dono), `404` (pedido não encontrado/ID inválido), `500`.
325314

315+
</details>
316+
326317
*(Funcionalidades de Pedidos como cancelamento e atualização de status foram implementadas no repositório mas não expostas em rotas ainda).*
327318

328319

@@ -333,6 +324,7 @@ Para rodar os testes unitários dos handlers:
333324
go test -v ./internal/handlers/...
334325
```
335326

336-
📄 Licença
327+
### 📄 Licença
328+
329+
GNU-General-Public-License-v3.0
337330

338-
BulletDEv all rights reserveds

0 commit comments

Comments
 (0)