Manages a list of users.




Module Contents

user.collections.user.MIN_PASSWORD_LENGTH = 8[source]
user.collections.user.as_set(value:[_T]) set[_T][source]
user.collections.user.as_set(value: _T) set[_T]
user.collections.user.as_dictionary_of_sets(d: Mapping[str, _T | Iterable[_T] | None]) dict[str, set[_T]][source]
user.collections.user.as_dictionary_of_sets(d: Mapping[str, Any]) dict[str, set[Any]]
class user.collections.user.UserCollection(session: sqlalchemy.orm.Session, **filters: Any)[source]

Manages a list of users.

Use it like this:

from onegov.user import UserCollection
users = UserCollection(session)
__getattr__(name: str) set[Any] | None[source]
for_filter(**filters: Any) typing_extensions.Self[source]
query() Query[User][source]

Returns a query using onegov.user.models.User. With the current filters applied.

apply_filter(query: Query[User], key: str, values: Collection[Any]) Query[User][source]
apply_tag_filter(query: Query[User], key: str, values:[str]) Query[User][source]
add(username: str, password: str, role: str, data: dict[str, Any] | None = None, second_factor: dict[str, Any] | None = None, active: bool = True, realname: str | None = None, phone_number: str | None = None, signup_token: str | None = None, group: UserGroup | None = None) onegov.user.models.User[source]

Add a user to the collection.

The arguments given to this function are the attributes of the User class with the same name.

property tags: tuple[str, Ellipsis][source]

All available tags.

property sources: tuple[str, Ellipsis][source]

All available sources.

property usernames: tuple[tuple[str, str], Ellipsis][source]

All available usernames.

usernames_by_tags(tags: list[str]) tuple[str, Ellipsis][source]

All usernames where the user’s tags match at least one tag from the given list.

exists(username: str) bool[source]

Returns True if the given username exists.

This function does not actually load a user, so it is the quickest way to find out if a user exists. It should be used if you don’t care about finding out anything about the user.

by_id(id: uuid.UUID) onegov.user.models.User | None[source]

Returns the user by the internal id.

Use this if you need to refer to a user by path. Usernames are not the correct way, since they allow for user enumeration.

by_username(username: str) onegov.user.models.User | None[source]

Returns the user by username.

by_source_id(source: str, source_id: str) onegov.user.models.User | None[source]

Returns the user by source and source_id.

by_username_and_password(username: str, password: str) onegov.user.models.User | None[source]

Returns the user by username and password.

Note that although the password can be empty on the user, this function will not query for empty passwords as an added security measure.

Apart from that everything is fair game though, as it is not the job of onegov.user to enforce a password policy.

by_roles(role: str, *roles: str) Query[User][source]

Queries the users by roles.

by_signup_token(signup_token: str) Query[User][source]
register(username: str, password: str, request: onegov.core.request.CoreRequest, role: str = 'member', signup_token: str | None = None) onegov.user.models.User[source]

Registers a new user.

The so created user needs to activated with a token before it becomes active. Use the activation_token in the data dictionary together with the activate_with_token() function.

You probably want to use the provided RegistrationForm in conjunction with Auth as it includes a lot of extras like signup links and robots protection.

activate_with_token(username: str, token: object) None[source]

Activates the user if the given token matches the verification token stored in the data dictionary.

by_yubikey(token: str, active_only: bool = True) onegov.user.models.User | None[source]

Returns the user with the given yubikey token.

Only considers active users by default.

delete(username: str) None[source]

Deletes the user if it exists.

If the user does not exist, an onegov.user.errors.UnknownUserError is raised.