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
Jackson Pires
May 13, 2015
Programming
1
420
Ruby on Rails para Iniciantes - Aula 39
Usando autorização com Pundit e Devise (básico)
Jackson Pires
May 13, 2015
Tweet
Share
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
79
Conhecendo o módulo Forwardable do Ruby
jackson_pires
0
65
COMO INSPIRAR PESSOAS E MONETIZAR O SEU CONHECIMENTO?
jackson_pires
0
56
Conhecendo a gem guard, guard-rspec e guard-livereload
jackson_pires
0
80
GDG Meetup - Carreiras em T.I.
jackson_pires
0
82
20 minutos insanos de TDD e Ruby
jackson_pires
0
110
Ruby on Rails para Iniciantes - Aula 46
jackson_pires
0
160
Ruby on Rails para Iniciantes - Aula 47
jackson_pires
0
80
Ruby on Rails para Iniciantes - Aula 48
jackson_pires
0
370
Other Decks in Programming
See All in Programming
さようなら Date。 ようこそTemporal! 3年間先行利用して得られた知見の共有
8beeeaaat
1
860
Zendeskのチケットを Amazon Bedrockで 解析した
ryokosuge
3
250
Introducing ReActionView: A new ActionView-compatible ERB Engine @ Rails World 2025, Amsterdam
marcoroth
0
330
Trem on Rails - Prompt Engineering com Ruby
elainenaomi
1
100
複雑なドメインに挑む.pdf
yukisakai1225
5
950
2025 年のコーディングエージェントの現在地とエンジニアの仕事の変化について
azukiazusa1
16
7.4k
オープンセミナー2025@広島LT技術ブログを続けるには
satoshi256kbyte
0
150
CloudflareのChat Agent Starter Kitで簡単!AIチャットボット構築
syumai
2
380
UbieのAIパートナーを支えるコンテキストエンジニアリング実践
syucream
2
810
JSONataを使ってみよう Step Functionsが楽しくなる実践テクニック #devio2025
dafujii
0
260
Oracle Database Technology Night 92 Database Connection control FAN-AC
oracle4engineer
PRO
1
380
パッケージ設計の黒魔術/Kyoto.go#63
lufia
3
410
Featured
See All Featured
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
229
22k
4 Signs Your Business is Dying
shpigford
184
22k
Building an army of robots
kneath
306
46k
A Tale of Four Properties
chriscoyier
160
23k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
50k
Embracing the Ebb and Flow
colly
87
4.8k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
34
6k
VelocityConf: Rendering Performance Case Studies
addyosmani
332
24k
Being A Developer After 40
akosma
90
590k
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!