Skip to content

Instantly share code, notes, and snippets.

@harrisonmalone
Last active July 10, 2019 02:22
Show Gist options
  • Select an option

  • Save harrisonmalone/13e32ba489afe185f78de4204d88e880 to your computer and use it in GitHub Desktop.

Select an option

Save harrisonmalone/13e32ba489afe185f78de4204d88e880 to your computer and use it in GitHub Desktop.
<div>
<% if !current_user %>
<%= link_to "Sign in", new_user_session_path %>
<%= link_to "Sign up", new_user_registration_path %>
<% else %>
<p>Hello <%= current_user.email %>. Your role is <%= current_user.role.priviledge %></p>
<%= link_to "Sign out", destroy_user_session_path, {method: :delete} %>
<% end %>
</div>

pundit zoos challenge 🐊

  1. add another role to your roles table with the privilege "zoo keeper", the description of this role is "medium access"

  2. you should now have 3 levels of roles

  • admin: can do everything (add zoos and animals etc)
  • zoo keeper: can do some stuff (like add an animal, edit an animal, delete an animal, but they can't create, edit or delete zoos)
  • normal level: can't do anything, can't add animals
  • note that a normal level user can view pages like show animal and show zoos 🦁
  1. add a route called '/zoo-keepers', it should have an associated controller and action

  2. on this page add a button that can be clicked, the button should be a "become a zoo keeper button"

  • this button when clicked will change the users role from normal level to zoo keeper level
  • this will also require adding a new update action to your controller
  1. add the associated pundit code to ensure controller actions are protected at different role levels

  2. test to see that all your different roles and pundit authorisation is working

  • make sure a normal level user can only view the index and show pages
  • make sure a zoo keeper user can add, update and delete animals but they can't add, update or delete zoos
  • make sure an admin user can do everything
  • make sure that when you type the url in the pages are not accessible, for instance if a normal level user tries to access the 'localhost:3000/zoos/new' page it should throw an error
  1. add a custom error page for these pundit related errors, you can find out how to do this is matt's documentation on pundit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment