File: components.py

package info (click to toggle)
freedombox 26.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,092 kB
  • sloc: python: 48,542; javascript: 1,730; xml: 481; makefile: 290; sh: 137; php: 32
file content (57 lines) | stat: -rw-r--r-- 1,997 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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))