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
66
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
83
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
@Environment(\.keyPath)那么好我不允许你们不知道! / atEnvironment keyPath is so good and you should know it!
lovee
0
120
アプリの "かわいい" を支えるアニメーションツールRiveについて
uetyo
0
280
AIと私たちの学習の変化を考える - Claude Codeの学習モードを例に
azukiazusa1
11
4.4k
複雑なフォームに立ち向かう Next.js の技術選定
macchiitaka
2
220
GitHubとGitLabとAWS CodePipelineでCI/CDを組み比べてみた
satoshi256kbyte
4
250
250830 IaCの選定~AWS SAMのLambdaをECSに乗り換えたときの備忘録~
east_takumi
0
400
知っているようで知らない"rails new"の世界 / The World of "rails new" You Think You Know but Don't
luccafort
PRO
1
190
プロパティベーステストによるUIテスト: LLMによるプロパティ定義生成でエッジケースを捉える
tetta_pdnt
0
3.3k
個人開発で徳島大学生60%以上の心を掴んだアプリ、そして手放した話
akidon0000
1
140
奥深くて厄介な「改行」と仲良くなる20分
oguemon
1
560
Flutter with Dart MCP: All You Need - 박제창 2025 I/O Extended Busan
itsmedreamwalker
0
150
Oracle Database Technology Night 92 Database Connection control FAN-AC
oracle4engineer
PRO
1
470
Featured
See All Featured
[RailsConf 2023] Rails as a piece of cake
palkan
57
5.8k
Context Engineering - Making Every Token Count
addyosmani
3
58
The Cost Of JavaScript in 2023
addyosmani
53
8.9k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3k
Facilitating Awesome Meetings
lara
55
6.5k
Raft: Consensus for Rubyists
vanstee
140
7.1k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
The World Runs on Bad Software
bkeepers
PRO
70
11k
Code Review Best Practice
trishagee
71
19k
How to train your dragon (web standard)
notwaldorf
96
6.2k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.6k
Why Our Code Smells
bkeepers
PRO
339
57k
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!