title: Keycloak Authorization - best practice roles vs groups
body: <p>I have a web-application secured with Keycloak. To keep the description of the service short, we have Users and Documents as entities in the service. The users may have access to none or more documents and may edit or read the document.</p>
  <p>Currently we have roles such as Admin, EndUser, Developer etc. We then keep a database table outside of Keycloak that maps the documents to users and what user has what access level to what document. All our end-users have the EndUser role in Keycloak. Every single time an EndUser tries to read/edit a Document, we have to make a lookup in the database table for authorization.</p>
  <p>We would like to migrate that table to Keycloak. As I understand it I basically have two options:</p>
  <ul>
  <li><p>Create a lot of roles, two for each document with names such as <code>doc_read_[DOCUMENT-ID]</code> and <code>doc_edit_[DOCUMENT-ID]</code> and so on. Then assign the correct role to the correct user. The downside here is that the number of roles will grow A LOT. Also, the number of roles attached to a user will be very large.</p>
  </li>
  <li><p>Create a group for each document, with the name of the document id. Have different sub-groups for read/write and then add the users in the correct groups. The downside is that the number of groups will be very large. Also, I will rely Authorization on group names, so the list of group names has to be mapped to the token.</p>
  </li>
  </ul>
  <p>I do not want to add a user-attribute with the document-ids to each user. With this approach I can not get an overview of a document and see what users have access to a given Document.</p>
  <p>What is the best practice here? Are there any other solutions to solve this issue? This must be a very common setup.</p>