2021-08-18 19:48:17 -04:00
|
|
|
from typing import List, Dict, Union
|
2021-08-03 10:09:07 -04:00
|
|
|
|
2021-07-19 01:47:39 -04:00
|
|
|
from zope.interface import Interface
|
|
|
|
|
|
|
|
from .IUser import IUser
|
|
|
|
from .IGroup import IGroup
|
|
|
|
|
|
|
|
|
|
|
|
class ILDAPService(Interface):
|
|
|
|
"""An interface to the LDAP database."""
|
|
|
|
|
2022-07-22 23:51:59 -04:00
|
|
|
def uid_to_dn(uid: str) -> str:
|
2021-08-15 01:04:49 -04:00
|
|
|
"""Get the LDAP DN for the user with this UID."""
|
|
|
|
|
2022-07-22 23:51:59 -04:00
|
|
|
def group_cn_to_dn(cn: str) -> str:
|
2021-08-15 01:04:49 -04:00
|
|
|
"""Get the LDAP DN for the group with this CN."""
|
|
|
|
|
2021-07-19 01:47:39 -04:00
|
|
|
def get_user(username: str) -> IUser:
|
|
|
|
"""Retrieve the user with the given username."""
|
|
|
|
|
2021-08-18 19:48:17 -04:00
|
|
|
def get_display_info_for_users(usernames: List[str]) -> List[Dict[str, str]]:
|
|
|
|
"""
|
|
|
|
Retrieve a subset of the LDAP attributes for the given users.
|
|
|
|
Useful for displaying a list of users in a compact way.
|
|
|
|
"""
|
|
|
|
|
2022-07-22 23:51:59 -04:00
|
|
|
def get_users_with_positions() -> List[IUser]:
|
2021-08-22 17:57:36 -04:00
|
|
|
"""Retrieve users who have a non-empty position attribute."""
|
|
|
|
|
2021-08-15 01:04:49 -04:00
|
|
|
def add_user(user: IUser):
|
2021-07-19 01:47:39 -04:00
|
|
|
"""
|
2021-07-23 20:08:22 -04:00
|
|
|
Add the user to the database.
|
2021-07-19 01:47:39 -04:00
|
|
|
A new UID and GID will be generated and returned in the new user.
|
|
|
|
"""
|
|
|
|
|
2022-09-09 17:26:54 -04:00
|
|
|
def get_groups_for_user(username: str) -> List[str]:
|
|
|
|
"""
|
|
|
|
Get a list of the groups to which the user belongs.
|
|
|
|
"""
|
|
|
|
|
2021-07-24 17:09:10 -04:00
|
|
|
def remove_user(user: IUser):
|
|
|
|
"""Remove this user from the database."""
|
|
|
|
|
2022-07-22 23:51:59 -04:00
|
|
|
def get_group(cn: str) -> IGroup:
|
2021-07-19 01:47:39 -04:00
|
|
|
"""Retrieve the group with the given cn (Unix group name)."""
|
|
|
|
|
2021-08-15 01:04:49 -04:00
|
|
|
def add_group(group: IGroup):
|
2021-07-19 01:47:39 -04:00
|
|
|
"""
|
2021-07-23 20:08:22 -04:00
|
|
|
Add the group to the database.
|
2021-07-19 01:47:39 -04:00
|
|
|
The GID will not be changed and must be valid.
|
|
|
|
"""
|
|
|
|
|
2021-07-24 17:09:10 -04:00
|
|
|
def remove_group(group: IGroup):
|
|
|
|
"""Remove this group from the database."""
|
|
|
|
|
2021-08-15 01:04:49 -04:00
|
|
|
def entry_ctx_for_user(user: IUser):
|
|
|
|
"""
|
|
|
|
Get a context manager which yields an ldap3.WritableEntry
|
|
|
|
for this user.
|
|
|
|
"""
|
2021-07-19 01:47:39 -04:00
|
|
|
|
2021-08-15 01:04:49 -04:00
|
|
|
def entry_ctx_for_group(group: IGroup):
|
|
|
|
"""
|
|
|
|
Get a context manager which yields an ldap3.WritableEntry
|
|
|
|
for this group.
|
|
|
|
"""
|
2021-07-23 20:08:22 -04:00
|
|
|
|
|
|
|
def add_sudo_role(uid: str):
|
|
|
|
"""Create a sudo role for the club with this UID."""
|
2021-07-24 17:09:10 -04:00
|
|
|
|
|
|
|
def remove_sudo_role(uid: str):
|
|
|
|
"""Remove the sudo role for this club from the database."""
|
2021-08-03 10:09:07 -04:00
|
|
|
|
|
|
|
def update_programs(
|
|
|
|
dry_run: bool = False,
|
|
|
|
members: Union[List[str], None] = None,
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Sync the 'program' attribute in CSC LDAP with UW LDAP.
|
|
|
|
If `dry_run` is set to True, then a list of members whose programs
|
|
|
|
*would* be changed is returned along with their old and new programs:
|
|
|
|
```
|
|
|
|
[
|
|
|
|
('user1', 'old_program1', 'new_program1'),
|
|
|
|
('user2', 'old_program2', 'new_program2'),
|
|
|
|
...
|
|
|
|
]
|
|
|
|
```
|
|
|
|
If `members` is set to a list of usernames, then only
|
|
|
|
those members will (possibly) have their programs updated.
|
|
|
|
On success, a list of members whose programs *were* changed will
|
|
|
|
be returned along with their new programs, in the same format
|
|
|
|
described above.
|
|
|
|
"""
|
2021-12-11 16:30:18 -05:00
|
|
|
|
2022-07-22 23:51:59 -04:00
|
|
|
def get_nonflagged_expired_users() -> List[IUser]:
|
2021-12-11 16:30:18 -05:00
|
|
|
"""
|
|
|
|
Retrieves members whose term or nonMemberTerm does not contain the
|
|
|
|
current or the last term.
|
|
|
|
"""
|
2022-06-30 20:02:06 -04:00
|
|
|
|
2022-07-22 23:51:59 -04:00
|
|
|
def get_expiring_users() -> List[IUser]:
|
2022-06-30 20:02:06 -04:00
|
|
|
"""
|
|
|
|
Retrieves members whose membership will expire in less than a month.
|
|
|
|
This is used to send membership renewal reminders at the beginning
|
|
|
|
of a term, during the one-month grace period.
|
|
|
|
"""
|
2022-07-22 23:51:59 -04:00
|
|
|
|
|
|
|
def get_clubs() -> List[IGroup]:
|
|
|
|
"""
|
|
|
|
Retrieves all clubs.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# couldn't import the Term class from ceo_common.model due to some
|
|
|
|
# circular import issue...
|
|
|
|
def get_club_reps_non_member_terms(club_reps: List[str]) -> Dict[str, List['Term']]: # noqa: F821
|
|
|
|
"""
|
|
|
|
Retrieves the non-member terms for the given club reps.
|
|
|
|
e.g.
|
|
|
|
{
|
|
|
|
"user1": [w2022, s2022],
|
|
|
|
"user2": [s2022],
|
|
|
|
...
|
|
|
|
}
|
|
|
|
"""
|