Example #1
0
def create_g_suite_account(ow4_user):

    if not ow4_user.online_mail:
        create_online_mail_alias(ow4_user)

    directory = setup_g_suite_client()

    password = create_temporary_password()

    domain = settings.OW4_GSUITE_SETTINGS.get("DOMAIN")

    query = directory.users().insert(
        body={
            "primaryEmail": "{}@{}".format(ow4_user.online_mail, domain),
            "password": password,
            "name": {
                "givenName": ow4_user.first_name,
                "familyName": ow4_user.last_name,
                "fullName": ow4_user.get_full_name(),
            },
            "changePasswordAtNextLogin": True,
        })

    try:
        resp = query.execute()
    except HttpError as err:
        logger.error(
            "HttpError while requesting G Suite Account creation: {}".format(
                err.content),
            extra={"error": err},
        )
        if err.resp.status == 409:
            logger.error(
                'G Suite account creation: User "{}@online.ntnu.no" already exists.'
                .format(ow4_user.online_mail))
        raise err

    logger.info(
        'Created G Suite account for "{user}" with username "{gsuite_username}"'
        .format(user=ow4_user, gsuite_username=resp.get("primaryEmail")))
    logger.debug("Created G Suite account, response: {}".format(resp))

    notify_g_suite_user_account(ow4_user, password)

    update_g_suite_user(domain, ow4_user)

    return resp
Example #2
0
def run_group_syncer(user: User) -> None:
    """
    Tasks to run after User is changed.
    :param user: The user instance to sync groups for.
    """
    SynchronizeGroups.run()
    if settings.OW4_GSUITE_SYNC.get("ENABLED", False):
        ow4_gsuite_domain = settings.OW4_GSUITE_SYNC.get("DOMAIN")
        if isinstance(user, User):
            logger.debug("Running G Suite syncer for user {}".format(user))
            update_g_suite_user(ow4_gsuite_domain, user, suppress_http_errors=True)
        elif isinstance(user, Group):
            group = user
            logger.debug("Running G Suite syncer for group {}".format(group))
            update_g_suite_group(
                ow4_gsuite_domain, group.name, suppress_http_errors=True
            )