Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

form: add options for multiple users and groups #1726

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/utils/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,24 @@ class OptionType(str, Enum):
# entity
domain = "domain"
app = "app"
apps = "apps"
user = "user"
users = "users"
group = "group"
groups = "groups"
users_and_groups = "users_and_groups"


FORBIDDEN_READONLY_TYPES = {
OptionType.password,
OptionType.app,
OptionType.apps,
OptionType.domain,
OptionType.user,
OptionType.users,
OptionType.users_and_groups,
OptionType.group,
OptionType.groups,
}


Expand Down Expand Up @@ -932,6 +940,10 @@ def _app_display(app):
self.choices.update({app["id"]: _app_display(app) for app in apps})


class AppsOption(AppOption, TagsOption):
type: Literal[OptionType.apps] = OptionType.apps


class UserOption(BaseChoicesOption):
type: Literal[OptionType.user] = OptionType.user

Expand Down Expand Up @@ -963,6 +975,10 @@ def __init__(self, question):
break


class UsersOption(UserOption, TagsOption):
type: Literal[OptionType.users] = OptionType.users


class GroupOption(BaseChoicesOption):
type: Literal[OptionType.group] = OptionType.group

Expand All @@ -987,6 +1003,37 @@ def _human_readable_group(g):
self.default = "all_users"


class GroupsOption(GroupOption, TagsOption):
type: Literal[OptionType.groups] = OptionType.groups


class UsersAndGroupsOption(TagsOption):
type: Literal[OptionType.users_and_groups] = OptionType.users_and_groups

def __init__(self, question):
from yunohost.user import user_group_list

super().__init__(question)

users = {
username: f"{infos['fullname']} ({infos['mail']})"
for username, infos in user_list()["users"].items()
}

def _human_readable_group(g):
# i18n: visitors
# i18n: all_users
# i18n: admins
return m18n.n(g) if g in ["visitors", "all_users", "admins"] else g

groups = {
group: _human_readable_group(group)
for group in user_group_list(short=True, include_primary_groups=False)["groups"]
}

self.choices = {**users, **groups}

Check warning

Code scanning / CodeQL

Overwriting attribute in super-class or sub-class

Assignment overwrites attribute choices, which was previously defined in superclass [BaseChoicesOption](1).


OPTIONS = {
OptionType.display_text: DisplayTextOption,
OptionType.markdown: MarkdownOption,
Expand All @@ -1011,6 +1058,10 @@ def _human_readable_group(g):
OptionType.app: AppOption,
OptionType.user: UserOption,
OptionType.group: GroupOption,
OptionType.apps: AppsOption,
OptionType.users: UsersOption,
OptionType.groups: GroupsOption,
OptionType.users_and_groups: UsersAndGroupsOption,
}


Expand Down