Local customizations to the user permissions

This page explains how to locally override a user types module.

This is a tested document. The following instructions are used for initialization:

>>> from lino import startup
>>> startup('lino_book.projects.myroles.settings')
>>> from lino.api.doctest import *

The example

For example on a standard Lino Polly site (lino_book.projects.polly), only a site administrator can see the global list of all polls. This list is visible through Explorer ‣ Polls ‣ Polls. A normal user does not see that menu command.

We are going to apply a local customization. In our variant of a Lino Polly application, every authenticated user (not only site admins) can see that table.

Here is the settings.py file used by this tutorial:

from lino_book.projects.polly.settings import *

class Site(Site):
    user_types_module = 'lino_book.projects.myroles.myroles'

SITE = Site(globals())

In our settings.py file, we override the user_types_module of the Polly Site class and set it to the Python path of our myroles.py file:

user_types_module = 'mysite.myroles'

Now we create this module, i.e. a file named myroles.py with the following content:

from lino_xl.lib.xl.user_types import *

from lino.api import dd, rt
from lino_xl.lib.polls.roles import PollsUser

AllPolls = rt.models.polls.AllPolls
AllPolls.required_roles = dd.login_required(PollsUser)

The first line imports everything from the standard module:

from lino_xl.lib.xl.user_types import *

How did we know that the name of the standard user types module of a Lino Polly is lino_xl.lib.xl.user_types? For example like this:

>>> from lino_book.projects.polly.settings import Site
>>> print(Site.user_types_module)
lino_xl.lib.xl.user_types

The following lines basically just set the required_roles of the polls.AllPolls table require the PollsUser role:

AllPolls.required_roles = dd.login_required(PollsUser)

Testing it

Yes our local myroles.py module is being imported at startup:

>>> print(settings.SITE.user_types_module)
lino_book.projects.myroles.myroles

The following code snippets are to test whether a normal user now really can see all polls (i.e. has the Explorer ‣ Polls ‣ Polls menu command):

>>> u = users.User(username="user", user_type="100")
>>> u.full_clean()
>>> u.save()
>>> show_menu('user')
... 
- Polls : My Polls, My Responses
- Explorer :
  - Polls : Polls
- Site : About