1.2.5.2 Using Groups

It is sometimes useful to consider groups of users, perhaps if people from different companies use your application but you want to keep their users separate. This can be achieved with groups.

Before you can use a group, it must be added to the database:

admin.addGroup('butcher')
admin.addGroup('fishmonger')

The when adding users you can specify the group:

admin.addUser(username='james', password='password', group='butcher')
admin.addUser(username='sally', password='password', group='butcher')
admin.addUser(username='anne',  password='password', group='fishmonger')

Alternatively you can specify or change the group of an already created user:

user = admin.user('james')
user.group = 'fishmonger'

You can obtain a list of groups using the groups() method:

>>> admin.groups()
('butcher', 'fishmonger')

You can then authorise a user based on their group.

>>> admin.user('anne').authorise(group='fishmonger')
1
>>> user = admin.user('james')
>>> user.authorise(group='butcher')
0
>>> user.authorise(group='fishmonger')
1

There are also methods groupExists() and removeGroups().