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
83
Conhecendo o módulo Forwardable do Ruby
jackson_pires
0
69
COMO INSPIRAR PESSOAS E MONETIZAR O SEU CONHECIMENTO?
jackson_pires
0
58
Conhecendo a gem guard, guard-rspec e guard-livereload
jackson_pires
0
84
GDG Meetup - Carreiras em T.I.
jackson_pires
0
90
20 minutos insanos de TDD e Ruby
jackson_pires
0
110
Ruby on Rails para Iniciantes - Aula 46
jackson_pires
0
170
Ruby on Rails para Iniciantes - Aula 47
jackson_pires
0
82
Ruby on Rails para Iniciantes - Aula 48
jackson_pires
0
370
Other Decks in Programming
See All in Programming
20260228_JAWS_Beginner_Kansai
takuyay0ne
5
620
守る「だけ」の優しいEMを抜けて、 事業とチームを両方見る視点を身につけた話
maroon8021
3
1.4k
Linux Kernelの1文字のミスで 権限昇格ができた話
rqda
0
2.1k
PHPで TLSのプロトコルを実装してみる
higaki_program
0
450
GoのDB アクセスにおける 「型安全」と「柔軟性」の両立 - Bob という選択肢
tak848
0
270
Nostalgia Meets Technology: Super Mario with TypeScript
manfredsteyer
PRO
0
110
ふつうの Rubyist、ちいさなデバイス、大きな一年
bash0c7
0
1.1k
Codex CLIのSubagentsによる並列API実装 / Parallel API Implementation with Codex CLI Subagents
takatty
2
460
AIコードレビューの導入・運用と AI駆動開発における「AI4QA」の取り組みについて
hagevvashi
0
560
LM Linkで(非力な!)ノートPCでローカルLLM
seosoft
0
220
Rで始めるML・LLM活用入門
wakamatsu_takumu
0
200
Reactive ❤️ Loom: A Forbidden Love Story
franz1981
2
160
Featured
See All Featured
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
200
Amusing Abliteration
ianozsvald
0
140
Faster Mobile Websites
deanohume
310
31k
Color Theory Basics | Prateek | Gurzu
gurzu
0
260
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
140
Agile that works and the tools we love
rasmusluckow
331
21k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
300
How STYLIGHT went responsive
nonsquared
100
6k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
220
Navigating Team Friction
lara
192
16k
Chasing Engaging Ingredients in Design
codingconduct
0
150
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.2k
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!