Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def check(bot, msg):
"""Print information about an OCF user."""
user = msg.match.group(1).strip()
attrs = search.user_attrs(user)
if attrs is not None:
groups = [grp.getgrgid(attrs['gidNumber']).gr_name]
groups.extend(
sorted(
group.gr_name for group in grp.getgrall() if user in group.gr_mem
),
)
groups = [
'{}{}\x0f'.format(GROUP_COLOR_MAPPING.get(group, ''), group)
for group in groups
]
if 'creationTime' in attrs:
created = attrs['creationTime'].strftime('%Y-%m-%d')
else:
creds.kerberos_principal,
):
report_status('kerberos principal already exists; skipping creation')
else:
with report_status('Creating', 'Created', 'Kerberos keytab'):
create_kerberos_principal_with_keytab(
request.user_name,
creds.kerberos_keytab,
creds.kerberos_principal,
password=decrypt_password(
request.encrypted_password,
RSA.importKey(open(creds.encryption_key).read()),
),
)
if search.user_attrs(request.user_name):
report_status('LDAP entry already exists; skipping creation')
else:
with report_status('Finding', 'Found', 'first available UID'):
new_uid = _get_first_available_uid(known_uid)
dn = utils.dn_for_username(request.user_name)
attrs = {
'objectClass': ['ocfAccount', 'account', 'posixAccount'],
'cn': [request.real_name],
'uidNumber': new_uid,
'gidNumber': getgrnam('ocf').gr_gid,
'homeDirectory': utils.home_dir(request.user_name),
'loginShell': '/bin/bash',
'ocfEmail': request.user_name + '@ocf.berkeley.edu',
'mail': [request.email],
'userPassword': '{SASL}' + request.user_name + '@OCF.BERKELEY.EDU',
def _notify_password_change(username, comment=None):
"""Send email about a password change.
:param username:
:param comment: a string to include indicating how/why the password was
reset
>>> _notify_password_change('ckuehl', comment='Your password was reset in the lab.')
"""
name = search.user_attrs(username)['cn'][0]
body = """Howdy there {name},
Just a quick heads up that your Open Computing Facility account password was
just reset, hopefully by you.
{comment_line}
As a reminder, your OCF username is: {username}
If you're not sure why this happened, please reply to this email ASAP.
{signature}""".format(
name=name,
username=username,
signature=mail.MAIL_SIGNATURE,
comment_line=('\n' + comment + '\n') if comment else '',
)
def request_vhost(request: HttpRequest) -> HttpResponse:
user = logged_in_user(request)
attrs = user_attrs(user)
is_group = 'callinkOid' in attrs
error = None
if has_vhost(user):
return render(
request,
'account/vhost/already_have_vhost.html',
{
'title': 'You already have virtual hosting',
'user': user,
},
)
elif not eligible_for_vhost(user):
return render(
request,
'account/vhost/not_eligible.html',
# Because of the schema, we are guaranteed that these days are always here.
for day in ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']:
if not staff_hours['staff-hours'][day]:
continue
for staff_hour in staff_hours['staff-hours'][day]:
staff_hour_list.append(
Hour(
day=day,
time=_parse_hour(staff_hour['time']),
staff=[
Staffer(
user_name=attrs['uid'][0],
real_name=_remove_middle_names(attrs['cn'][0]),
position=position(attrs['uid'][0]),
) for attrs in map(user_attrs, staff_hour['staff'])
],
cancelled=staff_hour.get('cancelled', False)
)
)
return staff_hour_list
def eligible_for_vhost(user):
"""Returns whether a user account is eligible for virtual hosting.
Currently, group accounts, faculty, and staff are eligible for virtual
hosting.
"""
attrs = user_attrs(user)
if "callinkOid" in attrs:
return True
elif "calnetUid" in attrs:
attrs_ucb = user_attrs_ucb(attrs["calnetUid"])
# TODO: Uncomment when we get a privileged LDAP bind.
if (
attrs_ucb
): # and 'EMPLOYEE-TYPE-ACADEMIC' in attrs_ucb['berkeleyEduAffiliations']:
return True
return False
end: Optional[date]
if isinstance(uid_or_info, tuple):
if len(uid_or_info) == 3:
uid, start, end = cast(Tuple[str, date, date], uid_or_info)
acting = False
else:
uid, start, end, acting = cast(Tuple[str, date, date, bool], uid_or_info)
else:
uid = uid_or_info
start = end = None
acting = False
name = MISSING_NAMES.get(uid)
if not name:
name, = user_attrs(uid)['cn']
return cls(uid=uid, name=name, start=start, end=end, acting=acting)