pyceo/ceo/cli/members.py

167 lines
6.0 KiB
Python
Raw Normal View History

2021-08-23 09:59:01 -04:00
import sys
from typing import Dict
import click
from zope import component
2021-09-06 12:40:05 -04:00
from ..term_utils import get_terms_for_new_user, get_terms_for_renewal
2021-08-28 23:09:02 -04:00
from ..utils import http_post, http_get, http_patch, http_delete, \
2021-09-06 12:40:05 -04:00
get_failed_operations, user_dict_lines, get_adduser_operations
2021-08-28 23:09:02 -04:00
from .utils import handle_stream_response, handle_sync_response, print_lines, \
2021-08-24 01:48:55 -04:00
check_if_in_development
2021-08-23 09:59:01 -04:00
from ceo_common.interfaces import IConfig
2021-08-28 23:09:02 -04:00
from ceod.transactions.members import DeleteMemberTransaction
2021-08-23 09:59:01 -04:00
2021-08-24 15:37:05 -04:00
@click.group(short_help='Perform operations on CSC members and club reps')
2021-08-23 09:59:01 -04:00
def members():
pass
@members.command(short_help='Add a new member or club rep')
@click.argument('username')
@click.option('--cn', help='Full name', prompt='Full name')
@click.option('--program', required=False, help='Academic program')
@click.option('--terms', 'num_terms', type=click.IntRange(1, 100),
2021-09-06 12:40:05 -04:00
help='Number of terms to add', default=1)
2021-08-23 09:59:01 -04:00
@click.option('--clubrep', is_flag=True, default=False,
help='Add non-member terms instead of member terms')
@click.option('--forwarding-address', required=False,
help=('Forwarding address to set in ~/.forward. '
'Default is UW address. '
'Set to the empty string to disable forwarding.'))
def add(username, cn, program, num_terms, clubrep, forwarding_address):
cfg = component.getUtility(IConfig)
uw_domain = cfg.get('uw_domain')
2021-08-28 23:09:02 -04:00
terms = get_terms_for_new_user(num_terms)
2021-08-23 19:01:24 -04:00
2021-08-28 23:09:02 -04:00
# TODO: get email address from UWLDAP
2021-08-23 09:59:01 -04:00
if forwarding_address is None:
forwarding_address = username + '@' + uw_domain
body = {
'uid': username,
'cn': cn,
}
if program is not None:
body['program'] = program
if clubrep:
body['non_member_terms'] = terms
2021-08-23 19:01:24 -04:00
else:
body['terms'] = terms
2021-08-23 09:59:01 -04:00
if forwarding_address != '':
body['forwarding_addresses'] = [forwarding_address]
2021-08-28 23:09:02 -04:00
else:
body['forwarding_addresses'] = []
click.echo("The following user will be created:")
print_user_lines(body)
click.confirm('Do you want to continue?', abort=True)
operations = get_adduser_operations(body)
2021-08-23 19:01:24 -04:00
2021-08-23 09:59:01 -04:00
resp = http_post('/api/members', json=body)
2021-08-23 19:01:24 -04:00
data = handle_stream_response(resp, operations)
2021-08-23 09:59:01 -04:00
result = data[-1]['result']
print_user_lines(result)
failed_operations = get_failed_operations(data)
if 'send_welcome_message' in failed_operations:
click.echo(click.style(
'Warning: welcome message was not sent. You now need to manually '
'send the user their password.', fg='yellow'))
2021-08-28 23:09:02 -04:00
def print_user_lines(d: Dict):
"""Pretty-print a serialized User."""
print_lines(user_dict_lines(d))
2021-08-23 09:59:01 -04:00
@members.command(short_help='Get info about a user')
@click.argument('username')
def get(username):
resp = http_get('/api/members/' + username)
result = handle_sync_response(resp)
print_user_lines(result)
@members.command(short_help="Replace a user's login shell or forwarding addresses")
@click.argument('username')
@click.option('--login-shell', required=False, help='Login shell')
@click.option('--forwarding-addresses', required=False,
2021-08-23 19:01:24 -04:00
help=(
'Comma-separated list of forwarding addresses. '
'Set to the empty string to disable forwarding.'
))
2021-08-23 09:59:01 -04:00
def modify(username, login_shell, forwarding_addresses):
if login_shell is None and forwarding_addresses is None:
click.echo('Nothing to do.')
sys.exit()
operations = []
body = {}
if login_shell is not None:
body['login_shell'] = login_shell
operations.append('replace_login_shell')
click.echo('Login shell will be set to: ' + login_shell)
if forwarding_addresses is not None:
2021-08-23 19:01:24 -04:00
if forwarding_addresses == '':
forwarding_addresses = []
else:
forwarding_addresses = forwarding_addresses.split(',')
2021-08-23 09:59:01 -04:00
body['forwarding_addresses'] = forwarding_addresses
operations.append('replace_forwarding_addresses')
prefix = '~/.forward will be set to: '
2021-08-23 19:01:24 -04:00
if len(forwarding_addresses) > 0:
click.echo(prefix + forwarding_addresses[0])
for address in forwarding_addresses[1:]:
click.echo((' ' * len(prefix)) + address)
else:
click.echo(prefix)
2021-08-23 09:59:01 -04:00
click.confirm('Do you want to continue?', abort=True)
resp = http_patch('/api/members/' + username, json=body)
handle_stream_response(resp, operations)
@members.command(short_help="Renew a member or club rep's membership")
@click.argument('username')
@click.option('--terms', 'num_terms', type=click.IntRange(1, 100),
help='Number of terms to add', prompt='Number of terms')
@click.option('--clubrep', is_flag=True, default=False,
help='Add non-member terms instead of member terms')
def renew(username, num_terms, clubrep):
2021-09-06 12:40:05 -04:00
terms = get_terms_for_renewal(username, num_terms, clubrep)
2021-08-23 09:59:01 -04:00
if clubrep:
body = {'non_member_terms': terms}
click.echo('The following non-member terms will be added: ' + ','.join(terms))
else:
body = {'terms': terms}
click.echo('The following member terms will be added: ' + ','.join(terms))
click.confirm('Do you want to continue?', abort=True)
resp = http_post(f'/api/members/{username}/renew', json=body)
handle_sync_response(resp)
click.echo('Done.')
@members.command(short_help="Reset a user's password")
@click.argument('username')
def pwreset(username):
click.confirm(f"Are you sure you want to reset {username}'s password?", abort=True)
resp = http_post(f'/api/members/{username}/pwreset')
result = handle_sync_response(resp)
click.echo('New password: ' + result['password'])
@members.command(short_help="Delete a user")
@click.argument('username')
def delete(username):
2021-08-24 01:48:55 -04:00
check_if_in_development()
2021-08-23 09:59:01 -04:00
click.confirm(f"Are you sure you want to delete {username}?", abort=True)
resp = http_delete(f'/api/members/{username}')
handle_stream_response(resp, DeleteMemberTransaction.operations)