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
72
Conhecendo o módulo Forwardable do Ruby
jackson_pires
0
60
COMO INSPIRAR PESSOAS E MONETIZAR O SEU CONHECIMENTO?
jackson_pires
0
54
Conhecendo a gem guard, guard-rspec e guard-livereload
jackson_pires
0
78
GDG Meetup - Carreiras em T.I.
jackson_pires
0
70
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
78
Ruby on Rails para Iniciantes - Aula 48
jackson_pires
0
360
Other Decks in Programming
See All in Programming
毎日13時間もかかるバッチ処理をたった3日で60%短縮するためにやったこと
sho_ssk_
1
490
テストケースの名前はどうつけるべきか?
orgachem
PRO
1
180
rails newと同時に型を書く
aki19035vc
4
580
生成AIでGitHubソースコード取得して仕様書を作成
shukob
0
600
Monixと常駐プログラムの勘どころ / Scalaわいわい勉強会 #4
stoneream
0
320
オニオンアーキテクチャを使って、 Unityと.NETでコードを共有する
soi013
0
340
バグを見つけた?それAppleに直してもらおう!
uetyo
0
210
Findy Team+ Awardを受賞したかった!ベストプラクティス応募内容をふりかえり、開発生産性向上もふりかえる / Findy Team Plus Award BestPractice and DPE Retrospective 2024
honyanya
0
130
create_tableをしただけなのに〜囚われのuuid編〜
daisukeshinoku
0
330
数十万行のプロジェクトを Scala 2から3に完全移行した
xuwei_k
0
490
見えないメモリを観測する: PHP 8.4 `pg_result_memory_size()` とSQL結果のメモリ管理
kentaroutakeda
0
880
コンテナをたくさん詰め込んだシステムとランタイムの変化
makihiro
1
170
Featured
See All Featured
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
7
550
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
19
2.3k
The Pragmatic Product Professional
lauravandoore
32
6.3k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.2k
GraphQLとの向き合い方2022年版
quramy
44
13k
Statistics for Hackers
jakevdp
797
220k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Measuring & Analyzing Core Web Vitals
bluesmoon
5
190
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
The World Runs on Bad Software
bkeepers
PRO
66
11k
How to Ace a Technical Interview
jacobian
276
23k
Visualization
eitanlees
146
15k
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!