SlideShare une entreprise Scribd logo
1  sur  24
Télécharger pour lire hors ligne
Composici´on de Autorizaciones en Ruby
usando Walruz
Rom´an Gonz´alez
LATAM on Rails
November 7, 2010
Definiciones
→ Autenticaci´on != Autorizaci´on
→ Autenticaci´on: Es el proceso de verificar algo o alguien
como autentico
→ Autorizaci´on: Es el proceso de verificar los privilegios de
una entidad
Plugins existentes en Rails
→ Autenticaci´on
devise
warden
acts as authenticated
→ Autorizaci´on
declarative authorization
walruz
Definamos un escenario
c l a s s User < ActiveRecord : : Base
# a t t r i b u t e : admin , : boolean
has many : friendships
has many : albums
end
c l a s s Friendship < ActiveRecord : : Base
belongs to : friend , : class name => ’ User ’
belongs to : befriend , : class name => ’ User ’
end
c l a s s Album < ActiveRecord : : Base
belongs to : owner , : class name => ’ User ’
end
AlbumsController
c l a s s AlbumsController < ApplicationController
b e f o r e f i l t e r : find user
def show
@album = user . albums . find ( params [ : id ] )
render : action => ’show ’
end
protected : find user
def find user
@user = User . find ( params [ : user id ] )
end
end
S´olo el creador del Album
1 def show
2 i f current user == user
3 @album = user . albums . find ( params [ : id ] )
4 render : action => ’show ’
5 else
6 unauthorized
7 end
8 end
+ Admin
1 def show
2 i f current user . admin? | | current user == user
3 @album = user . albums . find ( params [ : id ] )
4 render : action => ’show ’
5 else
6 unauthorized
7 end
8 end
+ Amigos
1 def show
2 allowed = current user . admin? | |
3 current user == user | |
4 user . friend ?( current user )
5 i f allowed
6 @album = user . albums . find ( params [ : id ] )
7 render : action => ’show ’
8 else
9 unauthorized
10 end
11 end
- Amigos que esten autorizados
1 def show
2 @album = user . albums . find ( params [ : id ] )
3 allowed = current user . admin? | |
4 current user == user | |
5 ( ( f = user . friendship with ( current user ) ) &&
6 f . allow to see album ?(@album ) )
7 i f allowed
8 render : action => ’show ’
9 else
10 unauthorized
11 end
12 end
+ Verifica que el Album sea publico
1 allowed = album . i s p u b l i c ? | |
2 current user . admin? | |
3 current user == user | |
4 ( ( f = user . friendship with ( current user ) ) &&
5 f . allow to see album ?(@album ) )
Observaciones
→ Estamos implementando una l´ogica que puede no
pertenecer a un solo controlador (Copy-Paste)
→ Los controladores dejan de ser skinny por tener c´odigo que
se encarga de las autorizaciones
Posibles Soluciones
→ ¿Mover la l´ogica de validaci´on de autorizaciones al
Modelo?
→ NO. El c´odigo estar´ıa esparcido en distintos modelos,
haciendo casi imposible mantener rastro de las distintas
autorizaciones que existen en proyectos de mediano a gran
tama˜no.
Usando Walruz
1 def show
2 @album = user . albums . find ( params [ : id ] )
3 # a c t o r . can ?( action , s u b j e c t )
4 i f current user . can ? ( : read , @album)
5 render : action => ’show ’
6 else
7 unauthorized
8 end
9 end
Removemos toda la l´ogica de autorizaci´on
→ Los controladores pasan a ser Open For Extension/Closed
For Modification
→ El codigo sigue siendo legible y claro en sus intenciones
¿C´omo funciona?
c l a s s User < ActiveRecord : : Base
include Walruz : : Actor
end
c l a s s Album < ActiveRecord : : Base
include Walruz : : Subject
check authorizations
# a c t i o n => p o l i c y
: read => P o l i c i e s : : AlbumReadAuthorization
end
¿Que es Policies::AlbumReadAuthorization?
Policies File
# l i b / walruz / p o l i c i e s . rb
module P o l i c i e s
require ” p o l i c i e s /actor is admin ”
require ” p o l i c i e s /actor is owner ”
require ” p o l i c i e s / a c t o r i s f r i e n d ”
require ” p o l i c i e s /friendship allow read albums ”
AlbumReadAuthorization =
orP ( ActorIsAdmin ,
ActorIsOwner ,
andP( ActorIsFriend . f o r s u b j e c t ( : owner ) ,
FriendshipAllowReadAlbums )
)
end
ActorIsAdmin
c l a s s ActorIsAdmin < Walruz : : Policy
# c u r r e n t u s e r . can ? ( : read , @album )
# a c t o r −> c u r r e n t u s e r
# s u b j e c t −> @album
def authorized ?( actor , subject )
actor . admin?
end
end
ActorIsOwner
c l a s s ActorIsOwner < Walruz : : Policy
# c u r r e n t u s e r . can ? ( : read , @album )
# a c t o r −> c u r r e n t u s e r
# s u b j e c t −> @album
def authorized ?( actor , subject )
actor == subject . owner
end
end
ActorIsFriend
# A c t o r I s F r i e n d . f o r s u b j e c t ( : owner )
c l a s s ActorIsFriend < Walruz : : Policy
# c u r r e n t u s e r . can ? ( : read , @album )
# a c t o r −> c u r r e n t u s e r
# s u b j e c t −> @album . owner
def authorized ?( user , other user )
friendship = other user . friendship with ( user )
i f friendship
[ true , : friendship => friendship ]
else
f a l s e
end
end
end
FriendshipAllowReadAlbum
# andP ( A c t o r I s F r i e n d . f o r s u b j e c t ( : owner ) ,
# FriendshipAllowReadAlbum )
c l a s s FriendshipAllowReadAlbum < Walruz : : Policy
def authorized ?( actor , album )
params [ : friendship ] . allow to see album ?( album )
end
end
Observaciones
→ Las diferentes pol´ıticas que se implementan se mantienen
bien aisladas y simples
→ Mediante la composici´on de estas pol´ıticas podemos
definir facilmente pol´ıticas con mayor complejidad
→ Todo el sistema de seguridad se mantiene en un s´olo sitio
en el proyecto
→ El problema deja de ser, invocar las autorizaciones en el
proyecto, y pasa a ser manejar el archivo de pol´ıticas
autorizaciones
Desventajas
→ Esto es Ruby, no SQL
→ A medida que las pol´ıticas son mas complejas, mas tiempo
se va a tomar en ejecutar la verificaciones
→ Las colecciones de objetos no son el mejor caso de uso, pero
en el caso de que sea requerido, memcache es tu amigo
→ Por ahora, walruz-rails s´olo soporta rails 2
¿Preguntas?
¡Gracias!
+ Informaci´on:
Comunidad:
http://ruby.org.ve
Twitter:
@romanandreg
Proyecto:
http://github.com/noomii/walruz
http://github.com/noomii/walruz-rails

Contenu connexe

Dernier

Resistencia extrema al cobre por un consorcio bacteriano conformado por Sulfo...
Resistencia extrema al cobre por un consorcio bacteriano conformado por Sulfo...Resistencia extrema al cobre por un consorcio bacteriano conformado por Sulfo...
Resistencia extrema al cobre por un consorcio bacteriano conformado por Sulfo...JohnRamos830530
 
How to use Redis with MuleSoft. A quick start presentation.
How to use Redis with MuleSoft. A quick start presentation.How to use Redis with MuleSoft. A quick start presentation.
How to use Redis with MuleSoft. A quick start presentation.FlorenciaCattelani
 
investigación de los Avances tecnológicos del siglo XXI
investigación de los Avances tecnológicos del siglo XXIinvestigación de los Avances tecnológicos del siglo XXI
investigación de los Avances tecnológicos del siglo XXIhmpuellon
 
Innovaciones tecnologicas en el siglo 21
Innovaciones tecnologicas en el siglo 21Innovaciones tecnologicas en el siglo 21
Innovaciones tecnologicas en el siglo 21mariacbr99
 
Buenos_Aires_Meetup_Redis_20240430_.pptx
Buenos_Aires_Meetup_Redis_20240430_.pptxBuenos_Aires_Meetup_Redis_20240430_.pptx
Buenos_Aires_Meetup_Redis_20240430_.pptxFederico Castellari
 
Avances tecnológicos del siglo XXI y ejemplos de estos
Avances tecnológicos del siglo XXI y ejemplos de estosAvances tecnológicos del siglo XXI y ejemplos de estos
Avances tecnológicos del siglo XXI y ejemplos de estossgonzalezp1
 
Guia Basica para bachillerato de Circuitos Basicos
Guia Basica para bachillerato de Circuitos BasicosGuia Basica para bachillerato de Circuitos Basicos
Guia Basica para bachillerato de Circuitos BasicosJhonJairoRodriguezCe
 
redes informaticas en una oficina administrativa
redes informaticas en una oficina administrativaredes informaticas en una oficina administrativa
redes informaticas en una oficina administrativanicho110
 
Avances tecnológicos del siglo XXI 10-07 eyvana
Avances tecnológicos del siglo XXI 10-07 eyvanaAvances tecnológicos del siglo XXI 10-07 eyvana
Avances tecnológicos del siglo XXI 10-07 eyvanamcerpam
 
EVOLUCION DE LA TECNOLOGIA Y SUS ASPECTOSpptx
EVOLUCION DE LA TECNOLOGIA Y SUS ASPECTOSpptxEVOLUCION DE LA TECNOLOGIA Y SUS ASPECTOSpptx
EVOLUCION DE LA TECNOLOGIA Y SUS ASPECTOSpptxJorgeParada26
 

Dernier (10)

Resistencia extrema al cobre por un consorcio bacteriano conformado por Sulfo...
Resistencia extrema al cobre por un consorcio bacteriano conformado por Sulfo...Resistencia extrema al cobre por un consorcio bacteriano conformado por Sulfo...
Resistencia extrema al cobre por un consorcio bacteriano conformado por Sulfo...
 
How to use Redis with MuleSoft. A quick start presentation.
How to use Redis with MuleSoft. A quick start presentation.How to use Redis with MuleSoft. A quick start presentation.
How to use Redis with MuleSoft. A quick start presentation.
 
investigación de los Avances tecnológicos del siglo XXI
investigación de los Avances tecnológicos del siglo XXIinvestigación de los Avances tecnológicos del siglo XXI
investigación de los Avances tecnológicos del siglo XXI
 
Innovaciones tecnologicas en el siglo 21
Innovaciones tecnologicas en el siglo 21Innovaciones tecnologicas en el siglo 21
Innovaciones tecnologicas en el siglo 21
 
Buenos_Aires_Meetup_Redis_20240430_.pptx
Buenos_Aires_Meetup_Redis_20240430_.pptxBuenos_Aires_Meetup_Redis_20240430_.pptx
Buenos_Aires_Meetup_Redis_20240430_.pptx
 
Avances tecnológicos del siglo XXI y ejemplos de estos
Avances tecnológicos del siglo XXI y ejemplos de estosAvances tecnológicos del siglo XXI y ejemplos de estos
Avances tecnológicos del siglo XXI y ejemplos de estos
 
Guia Basica para bachillerato de Circuitos Basicos
Guia Basica para bachillerato de Circuitos BasicosGuia Basica para bachillerato de Circuitos Basicos
Guia Basica para bachillerato de Circuitos Basicos
 
redes informaticas en una oficina administrativa
redes informaticas en una oficina administrativaredes informaticas en una oficina administrativa
redes informaticas en una oficina administrativa
 
Avances tecnológicos del siglo XXI 10-07 eyvana
Avances tecnológicos del siglo XXI 10-07 eyvanaAvances tecnológicos del siglo XXI 10-07 eyvana
Avances tecnológicos del siglo XXI 10-07 eyvana
 
EVOLUCION DE LA TECNOLOGIA Y SUS ASPECTOSpptx
EVOLUCION DE LA TECNOLOGIA Y SUS ASPECTOSpptxEVOLUCION DE LA TECNOLOGIA Y SUS ASPECTOSpptx
EVOLUCION DE LA TECNOLOGIA Y SUS ASPECTOSpptx
 

En vedette

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

En vedette (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Composición de Autorizaciones en Ruby usando Walruz

  • 1. Composici´on de Autorizaciones en Ruby usando Walruz Rom´an Gonz´alez LATAM on Rails November 7, 2010
  • 2. Definiciones → Autenticaci´on != Autorizaci´on → Autenticaci´on: Es el proceso de verificar algo o alguien como autentico → Autorizaci´on: Es el proceso de verificar los privilegios de una entidad
  • 3. Plugins existentes en Rails → Autenticaci´on devise warden acts as authenticated → Autorizaci´on declarative authorization walruz
  • 4. Definamos un escenario c l a s s User < ActiveRecord : : Base # a t t r i b u t e : admin , : boolean has many : friendships has many : albums end c l a s s Friendship < ActiveRecord : : Base belongs to : friend , : class name => ’ User ’ belongs to : befriend , : class name => ’ User ’ end c l a s s Album < ActiveRecord : : Base belongs to : owner , : class name => ’ User ’ end
  • 5. AlbumsController c l a s s AlbumsController < ApplicationController b e f o r e f i l t e r : find user def show @album = user . albums . find ( params [ : id ] ) render : action => ’show ’ end protected : find user def find user @user = User . find ( params [ : user id ] ) end end
  • 6. S´olo el creador del Album 1 def show 2 i f current user == user 3 @album = user . albums . find ( params [ : id ] ) 4 render : action => ’show ’ 5 else 6 unauthorized 7 end 8 end
  • 7. + Admin 1 def show 2 i f current user . admin? | | current user == user 3 @album = user . albums . find ( params [ : id ] ) 4 render : action => ’show ’ 5 else 6 unauthorized 7 end 8 end
  • 8. + Amigos 1 def show 2 allowed = current user . admin? | | 3 current user == user | | 4 user . friend ?( current user ) 5 i f allowed 6 @album = user . albums . find ( params [ : id ] ) 7 render : action => ’show ’ 8 else 9 unauthorized 10 end 11 end
  • 9. - Amigos que esten autorizados 1 def show 2 @album = user . albums . find ( params [ : id ] ) 3 allowed = current user . admin? | | 4 current user == user | | 5 ( ( f = user . friendship with ( current user ) ) && 6 f . allow to see album ?(@album ) ) 7 i f allowed 8 render : action => ’show ’ 9 else 10 unauthorized 11 end 12 end
  • 10. + Verifica que el Album sea publico 1 allowed = album . i s p u b l i c ? | | 2 current user . admin? | | 3 current user == user | | 4 ( ( f = user . friendship with ( current user ) ) && 5 f . allow to see album ?(@album ) )
  • 11. Observaciones → Estamos implementando una l´ogica que puede no pertenecer a un solo controlador (Copy-Paste) → Los controladores dejan de ser skinny por tener c´odigo que se encarga de las autorizaciones
  • 12. Posibles Soluciones → ¿Mover la l´ogica de validaci´on de autorizaciones al Modelo? → NO. El c´odigo estar´ıa esparcido en distintos modelos, haciendo casi imposible mantener rastro de las distintas autorizaciones que existen en proyectos de mediano a gran tama˜no.
  • 13. Usando Walruz 1 def show 2 @album = user . albums . find ( params [ : id ] ) 3 # a c t o r . can ?( action , s u b j e c t ) 4 i f current user . can ? ( : read , @album) 5 render : action => ’show ’ 6 else 7 unauthorized 8 end 9 end
  • 14. Removemos toda la l´ogica de autorizaci´on → Los controladores pasan a ser Open For Extension/Closed For Modification → El codigo sigue siendo legible y claro en sus intenciones
  • 15. ¿C´omo funciona? c l a s s User < ActiveRecord : : Base include Walruz : : Actor end c l a s s Album < ActiveRecord : : Base include Walruz : : Subject check authorizations # a c t i o n => p o l i c y : read => P o l i c i e s : : AlbumReadAuthorization end ¿Que es Policies::AlbumReadAuthorization?
  • 16. Policies File # l i b / walruz / p o l i c i e s . rb module P o l i c i e s require ” p o l i c i e s /actor is admin ” require ” p o l i c i e s /actor is owner ” require ” p o l i c i e s / a c t o r i s f r i e n d ” require ” p o l i c i e s /friendship allow read albums ” AlbumReadAuthorization = orP ( ActorIsAdmin , ActorIsOwner , andP( ActorIsFriend . f o r s u b j e c t ( : owner ) , FriendshipAllowReadAlbums ) ) end
  • 17. ActorIsAdmin c l a s s ActorIsAdmin < Walruz : : Policy # c u r r e n t u s e r . can ? ( : read , @album ) # a c t o r −> c u r r e n t u s e r # s u b j e c t −> @album def authorized ?( actor , subject ) actor . admin? end end
  • 18. ActorIsOwner c l a s s ActorIsOwner < Walruz : : Policy # c u r r e n t u s e r . can ? ( : read , @album ) # a c t o r −> c u r r e n t u s e r # s u b j e c t −> @album def authorized ?( actor , subject ) actor == subject . owner end end
  • 19. ActorIsFriend # A c t o r I s F r i e n d . f o r s u b j e c t ( : owner ) c l a s s ActorIsFriend < Walruz : : Policy # c u r r e n t u s e r . can ? ( : read , @album ) # a c t o r −> c u r r e n t u s e r # s u b j e c t −> @album . owner def authorized ?( user , other user ) friendship = other user . friendship with ( user ) i f friendship [ true , : friendship => friendship ] else f a l s e end end end
  • 20. FriendshipAllowReadAlbum # andP ( A c t o r I s F r i e n d . f o r s u b j e c t ( : owner ) , # FriendshipAllowReadAlbum ) c l a s s FriendshipAllowReadAlbum < Walruz : : Policy def authorized ?( actor , album ) params [ : friendship ] . allow to see album ?( album ) end end
  • 21. Observaciones → Las diferentes pol´ıticas que se implementan se mantienen bien aisladas y simples → Mediante la composici´on de estas pol´ıticas podemos definir facilmente pol´ıticas con mayor complejidad → Todo el sistema de seguridad se mantiene en un s´olo sitio en el proyecto → El problema deja de ser, invocar las autorizaciones en el proyecto, y pasa a ser manejar el archivo de pol´ıticas autorizaciones
  • 22. Desventajas → Esto es Ruby, no SQL → A medida que las pol´ıticas son mas complejas, mas tiempo se va a tomar en ejecutar la verificaciones → Las colecciones de objetos no son el mejor caso de uso, pero en el caso de que sea requerido, memcache es tu amigo → Por ahora, walruz-rails s´olo soporta rails 2