FreedomBox/plinth/modules/users/components.py
Sunil Mohan Adapa 2dd00a8f08
*: Fix all typing hint related errors
- Try to mark class variables in component classes.

- Leave typing hints generic, such as 'list' and 'dict' where content is usually
not filled, too complex, or context is unimportant.

- backups: Handle failure for tarfile extraction so that methods are not called
on potentially None valued variables.

- backups: Prevent potentially passing a keyword argument twice.

- dynamicdns: Deal properly with outcome of urlparsing.

- ejabberd: Deal with failed regex match

- email: Fix a mypy compliant when iterating a filtered list.

- tor: Don't reuse variables for different typed values.

- tor: Don't reuse variables for different typed values.

- operation: Return None explicitly.

- operation: Ensure that keyword argument is not repeated.

Tests:

- Where only typing hints were modified and no syntax error came up, additional
testing was not done.

- `mypy --ignore-missing-imports .` run successfully.

- Generate developer documentation.

- Service runs without errors upon start up.

- backups: Listing and restoring specific apps from a backup works.

- backups: Mounting a remote backup repository works.

- NOT TESTED: dynamicdns: Migrating from old style configuration works.

- ejabberd: Verify that setting coturn configuration works.

- email: Test that showing configuration from postfix works.

- tor: Orport value is properly shown.

- transmission: Configuration values are properly set.

- users: Running unit tests as root works.

- operation: Operation status messages are show properly during app install.

- ./setup.py install runs

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2023-09-25 20:03:24 -04:00

58 lines
2.0 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
"""
App component to manage users and groups.
"""
import itertools
from typing import ClassVar
from plinth import app
class UsersAndGroups(app.FollowerComponent):
"""Component to manage users and groups of an app."""
# Class variable to hold a list of user groups for apps
_all_components: ClassVar[set['UsersAndGroups']] = set()
def __init__(self, component_id, reserved_usernames=[], groups={}):
"""Store reserved_usernames and groups of the app.
'reserved_usernames' is a list of operating system user names that the
app uses. It is not permitted to create a FreedomBox user with one of
these names.
'groups' is a dictionary of the following format: {"group_name": "A
localized string describing what permissions are offered to the users
of this group"}.
"""
super().__init__(component_id)
self.reserved_usernames = reserved_usernames
self.groups = groups
self._all_components.add(self)
@classmethod
def get_groups(cls):
"""Return a set of all groups."""
all_groups = itertools.chain(*(component.groups.keys()
for component in cls._all_components))
return set(all_groups)
@classmethod
def get_group_choices(cls):
"""Return list of groups that can be used as form choices."""
all_groups = itertools.chain(*(component.groups.items()
for component in cls._all_components))
choices = [(group, f'{description} ({group})')
for group, description in set(all_groups)]
return sorted(choices, key=lambda g: g[0])
@classmethod
def is_username_reserved(cls, username):
"""Returns whether the given username is reserved or not."""
return any((username in component.reserved_usernames
for component in cls._all_components))