Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Ruby on Rails para Iniciantes - Aula 39
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Jackson Pires
May 13, 2015
Programming
430
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Ruby on Rails para Iniciantes - Aula 39
Usando autorização com Pundit e Devise (básico)
Jackson Pires
May 13, 2015
More Decks by Jackson Pires
See All by Jackson Pires
Como usar uma box Vagrant com a Cloud9 IDE para desenvolver com Elixir ou Phoenix?
jackson_pires
0
93
Conhecendo o módulo Forwardable do Ruby
jackson_pires
0
78
COMO INSPIRAR PESSOAS E MONETIZAR O SEU CONHECIMENTO?
jackson_pires
0
67
Conhecendo a gem guard, guard-rspec e guard-livereload
jackson_pires
0
87
GDG Meetup - Carreiras em T.I.
jackson_pires
0
100
20 minutos insanos de TDD e Ruby
jackson_pires
0
120
Ruby on Rails para Iniciantes - Aula 46
jackson_pires
0
180
Ruby on Rails para Iniciantes - Aula 47
jackson_pires
0
87
Ruby on Rails para Iniciantes - Aula 48
jackson_pires
0
380
Other Decks in Programming
See All in Programming
20260722_microCMSで考える、AI時代のコンテンツ運用設計
yosh1
0
420
PostgreSQL 18で考えるUUID主キー
kazuhiro1982
0
470
Go 1.27 における memory allocation の高速化
andpad
0
230
PHP初心者セッション2026 〜生成AIでは見えない裏側を知る:今だからLAMPを通して仕組みを学ぶ〜
kashioka
0
950
yield再入門 #phpcon
o0h
PRO
0
1.1k
為什麼你並不需要ViewModel / No, you don't need a ViewModel
lovee
1
500
仕様駆動開発へのトライを機に チームに適合する手法を模索し続けている話
freee
PRO
0
560
Flow は今どうなっているか
mizdra
PRO
0
600
PHP Application における Kubernetes 内 gRPC 通信
ganchiku
0
590
言葉の格闘技のススメ~紙とペンと言葉から始める、キャリアの描き方~
progresscicada
2
150
2年かけて Deno に DOMMatrix を実装した話 / How I implemented DOMMatrix in Deno over two years
petamoriken
0
210
「人を評価する AI」の設計と実装
ryoyanara
0
210
Featured
See All Featured
4 Signs Your Business is Dying
shpigford
187
23k
First, design no harm
axbom
PRO
2
1.2k
Google's AI Overviews - The New Search
badams
0
1.1k
A designer walks into a library…
pauljervisheath
211
24k
Designing for humans not robots
tammielis
254
26k
Thoughts on Productivity
jonyablonski
76
5.3k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
1.2k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
280
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.5k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
470
KATA
mclloyd
PRO
35
15k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.5k
Transcript
Ruby on Rails Para iniciantes - 3a Temporada \o/
http://videosdeti.com.br
[email protected]
Mentoring
Patrocinadores Roger Bernardi
Aula 39 Usando autorização com Pundit e Devise (básico)
Ruby on Rails Index Customer Products Usuários Autenticados
Ruby on Rails Index Customer Products Usuários Autenticados (normal_user, admin)
Addresses ControlUsers Usuários Autenticados (admin)
Ruby on Rails Observe a migration/tabela do User Como diferenciar
usuários normais e admin? Users email encrypted_password ... ...
Ruby on Rails Observe a migration/tabela do User Users email
encrypted_password role ...
Ruby on Rails Observe a migration/tabela do User Users email
<
[email protected]
> encrypted_password <7as89df9asdf9> role < 0 / normal_user> ... Users email <
[email protected]
> encrypted_password <l35l435n66m3fds> role < 1 / admin> ...
Ruby on Rails email encrypted_password role
[email protected]
a9asfdasdf9 0
[email protected]
43324l5klkjn 1
Ruby on Rails ActiveRecord::Enum
Ruby on Rails Vamos adicionar uma migration para a role
rails g migration AddRoleToUsers
Ruby on Rails Altere a migration add_column :users, :role, :integer,
: default => 0
Ruby on Rails Adicione ao Model User enum role: [:normal_user,
:admin]
Ruby on Rails Rode a migration e teste um a
criação de um novo user
Ruby on Rails Ajuste a visualização dos usuários
Ruby on Rails Faça testes no rails console
Ruby on Rails Pundit
Ruby on Rails Adicione ao Gemfile... gem ‘pundit’
Ruby on Rails Rode o bundle install
Ruby on Rails Inclua o Pundit no ApplicationController class ApplicationController
< ActionController::Base include Pundit protect_from_forgery end
Ruby on Rails Instale o pundit na aplicação rails g
pundit:install
Ruby on Rails Após a instalação o pundit irá criar
a pasta app/policies
Ruby on Rails Crie uma nova política para o model
User rails g pundit:policy user
Ruby on Rails Index Customer Products Usuários Autenticados (normal_user, admin)
Addresses ControlUsers Usuários Autenticados (admin)
Ruby on Rails Teste o controller control_user def index …
authorize @user end
Ruby on Rails Crie o método para testar se o
usuário é admin def index? user.admin? end
Ruby on Rails Ajustando mensagens de erro rescue_from Pundit::NotAuthorizedError, with:
:user_not_authorized private def user_not_authorized flash[:alert] = "Você não tem permissão para fazer esta ação." redirect_to(request.referrer || root_path) end
Ruby on Rails Agora façamos o mesmo para o model
Address rails g pundit:policy address
Ruby on Rails Testando na View <% if policy(@address).index? %>
... <% end %>
Ruby on Rails Crie o método para testar se o
usuário é admin def index? user.admin? end
Ruby on Rails Obrigado!