|
|
|
@ -9,6 +9,7 @@ from zope.interface import implementer |
|
|
|
|
from .validators import is_valid_shell, is_valid_term |
|
|
|
|
from ceo_common.interfaces import ILDAPService, IKerberosService, IFileService, \ |
|
|
|
|
IUser, IConfig, IMailmanService |
|
|
|
|
from ceo_common.model import Term |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@implementer(IUser) |
|
|
|
@ -26,6 +27,7 @@ class User: |
|
|
|
|
home_directory: Union[str, None] = None, |
|
|
|
|
positions: Union[List[str], None] = None, |
|
|
|
|
mail_local_addresses: Union[List[str], None] = None, |
|
|
|
|
is_club_rep: Union[bool, None] = None, |
|
|
|
|
is_club: bool = False, |
|
|
|
|
ldap3_entry: Union[ldap3.Entry, None] = None, |
|
|
|
|
): |
|
|
|
@ -52,6 +54,10 @@ class User: |
|
|
|
|
self.positions = positions or [] |
|
|
|
|
self.mail_local_addresses = mail_local_addresses or [] |
|
|
|
|
self._is_club = is_club |
|
|
|
|
if is_club_rep is None: |
|
|
|
|
self.is_club_rep = self.should_be_club_rep() |
|
|
|
|
else: |
|
|
|
|
self.is_club_rep = is_club_rep |
|
|
|
|
self.ldap3_entry = ldap3_entry |
|
|
|
|
|
|
|
|
|
self.ldap_srv = component.getUtility(ILDAPService) |
|
|
|
@ -66,6 +72,7 @@ class User: |
|
|
|
|
'login_shell': self.login_shell, |
|
|
|
|
'home_directory': self.home_directory, |
|
|
|
|
'is_club': self.is_club(), |
|
|
|
|
'is_club_rep': self.is_club_rep, |
|
|
|
|
'program': self.program or 'Unknown', |
|
|
|
|
} |
|
|
|
|
if self.terms: |
|
|
|
@ -86,6 +93,19 @@ class User: |
|
|
|
|
def is_club(self) -> bool: |
|
|
|
|
return self._is_club |
|
|
|
|
|
|
|
|
|
def should_be_club_rep(self) -> bool: |
|
|
|
|
if self._is_club: |
|
|
|
|
# not a real user |
|
|
|
|
return False |
|
|
|
|
if not self.terms: |
|
|
|
|
# no member terms => was only ever a club rep |
|
|
|
|
return True |
|
|
|
|
if not self.non_member_terms: |
|
|
|
|
# no non-member terms => was only ever a member |
|
|
|
|
return False |
|
|
|
|
# decide using the most recent term (member or non-member) |
|
|
|
|
return max(map(Term, self.non_member_terms)) > max(map(Term, self.terms)) |
|
|
|
|
|
|
|
|
|
def add_to_ldap(self): |
|
|
|
|
self.ldap_srv.add_user(self) |
|
|
|
|
|
|
|
|
@ -131,6 +151,7 @@ class User: |
|
|
|
|
home_directory=attrs['homeDirectory'][0], |
|
|
|
|
positions=attrs.get('position'), |
|
|
|
|
mail_local_addresses=attrs.get('mailLocalAddress'), |
|
|
|
|
is_club_rep=attrs.get('isClubRep', [False])[0], |
|
|
|
|
is_club=('club' in attrs['objectClass']), |
|
|
|
|
ldap3_entry=entry, |
|
|
|
|
) |
|
|
|
@ -148,7 +169,10 @@ class User: |
|
|
|
|
raise Exception('%s is not a valid term' % term) |
|
|
|
|
with self.ldap_srv.entry_ctx_for_user(self) as entry: |
|
|
|
|
entry.term.add(terms) |
|
|
|
|
if entry.isClubRep.value: |
|
|
|
|
entry.isClubRep.remove() |
|
|
|
|
self.terms.extend(terms) |
|
|
|
|
self.is_club_rep = False |
|
|
|
|
|
|
|
|
|
def add_non_member_terms(self, terms: List[str]): |
|
|
|
|
for term in terms: |
|
|
|
@ -156,7 +180,9 @@ class User: |
|
|
|
|
raise Exception('%s is not a valid term' % term) |
|
|
|
|
with self.ldap_srv.entry_ctx_for_user(self) as entry: |
|
|
|
|
entry.nonMemberTerm.add(terms) |
|
|
|
|
entry.isClubRep = True |
|
|
|
|
self.non_member_terms.extend(terms) |
|
|
|
|
self.is_club_rep = True |
|
|
|
|
|
|
|
|
|
def set_positions(self, positions: List[str]): |
|
|
|
|
with self.ldap_srv.entry_ctx_for_user(self) as entry: |
|
|
|
|