parent
da14764687
commit
c32e565f68
@ -0,0 +1,42 @@ |
||||
from typing import Union, List |
||||
|
||||
from zope import component |
||||
|
||||
from ..AbstractTransaction import AbstractTransaction |
||||
from ceo_common.errors import BadRequest |
||||
from ceo_common.interfaces import ILDAPService |
||||
|
||||
|
||||
class RenewMemberTransaction(AbstractTransaction): |
||||
"""Transaction to renew a user's terms or non-member terms.""" |
||||
|
||||
operations = [ |
||||
'add_terms', |
||||
'add_non_member_terms', |
||||
] |
||||
|
||||
def __init__( |
||||
self, |
||||
username: str, |
||||
terms: Union[List[str], None], |
||||
non_member_terms: Union[List[str], None], |
||||
): |
||||
super().__init__() |
||||
self.username = username |
||||
if (terms and non_member_terms) or not (terms or non_member_terms): |
||||
raise BadRequest('Must specify either terms or non-member terms') |
||||
self.terms = terms |
||||
self.non_member_terms = non_member_terms |
||||
self.ldap_srv = component.getUtility(ILDAPService) |
||||
|
||||
def child_execute_iter(self): |
||||
user = self.ldap_srv.get_user(self.username) |
||||
|
||||
if self.terms: |
||||
user.add_terms(self.terms) |
||||
yield 'add_terms' |
||||
elif self.non_member_terms: |
||||
user.add_non_member_terms(self.non_member_terms) |
||||
yield 'add_non_member_terms' |
||||
|
||||
self.finish('OK') |
@ -0,0 +1,27 @@ |
||||
from zope import component |
||||
|
||||
from ..AbstractTransaction import AbstractTransaction |
||||
from .utils import gen_password |
||||
from ceo_common.interfaces import ILDAPService |
||||
|
||||
|
||||
class ResetPasswordTransaction(AbstractTransaction): |
||||
"""Transaction to reset a user's password.""" |
||||
|
||||
operations = [ |
||||
'change_password', |
||||
] |
||||
|
||||
def __init__(self, username: str): |
||||
super().__init__() |
||||
self.username = username |
||||
self.ldap_srv = component.getUtility(ILDAPService) |
||||
|
||||
def child_execute_iter(self): |
||||
user = self.ldap_srv.get_user(self.username) |
||||
|
||||
password = gen_password() |
||||
user.change_password(password) |
||||
yield 'change_password' |
||||
|
||||
self.finish({'password': password}) |
@ -1,2 +1,4 @@ |
||||
from .AddMemberTransaction import AddMemberTransaction |
||||
from .ModifyMemberTransaction import ModifyMemberTransaction |
||||
from .RenewMemberTransaction import RenewMemberTransaction |
||||
from .ResetPasswordTransaction import ResetPasswordTransaction |
||||
|
@ -0,0 +1,7 @@ |
||||
import base64 |
||||
import os |
||||
|
||||
|
||||
def gen_password() -> str: |
||||
"""Generate a temporary password.""" |
||||
return base64.b64encode(os.urandom(18)).decode() |
Loading…
Reference in new issue