예제 #1
0
def find_or_create_user(
    *, sdk: methods.Looker40SDK, first_name: str, last_name: str, email: str
) -> models.User:
    try:
        users = sdk.search_users(email=email)
        if users:
            user = users[0]
            if (
                user.first_name != first_name
                or user.last_name != last_name
                or user.is_disabled
            ):
                assert user.id
                user = sdk.update_user(
                    user_id=user.id,
                    body=models.WriteUser(
                        first_name=first_name, last_name=last_name, is_disabled=False
                    ),
                )
        else:
            user = sdk.create_user(
                models.WriteUser(
                    first_name=first_name, last_name=last_name, is_disabled=False
                )
            )
    except error.SDKError as create_ex:
        raise RegisterError(f"Failed to find or create User ({create_ex})")
    return user
예제 #2
0
def get_hackathon_attr_id(*, sdk: methods.Looker40SDK) -> int:
    global HACKATHON_ATTR_ID
    if HACKATHON_ATTR_ID is not None:
        return HACKATHON_ATTR_ID

    main_hackathon = "hackathon"
    user_attrs = sdk.all_user_attributes(fields="name,id")
    for user_attr in user_attrs:
        if user_attr.name == main_hackathon:
            assert user_attr.id
            HACKATHON_ATTR_ID = user_attr.id
            break
    else:
        attrib = sdk.create_user_attribute(
            body=models.WriteUserAttribute(
                name=main_hackathon, label="Hackathon", type="string"
            )
        )
        if not attrib:
            raise RegisterError(f"Could not find '{main_hackathon}' user attribute")
        else:
            assert attrib.id
            HACKATHON_ATTR_ID = attrib.id

    return HACKATHON_ATTR_ID
예제 #3
0
def set_user_attributes(*, sdk: methods.Looker40SDK, user_id, hackathon):
    hackathon_attr_id = get_hackathon_attr_id(sdk=sdk)
    assert hackathon_attr_id
    sdk.set_user_attribute_user_value(
        user_id=user_id,
        user_attribute_id=hackathon_attr_id,
        body=models.WriteUserAttributeWithValue(value=hackathon),
    )
예제 #4
0
def set_user_group(*, sdk: methods.Looker40SDK, user_id: int, hackathon: str):
    global LOOKER_GROUP_PREFIX
    # TODO - switch to sdk.search_groups once that method is live on
    # sandboxcl and hack instances
    groups = sdk.all_groups(fields="id,name")
    name = f"{LOOKER_GROUP_PREFIX}{hackathon}"
    for group in groups:
        if group.name == name:
            assert group.id
            break
    else:
        role = get_hackathon_role(sdk=sdk)
        assert role.id
        role_groups = []
        for g in sdk.role_groups(role_id=role.id, fields="id"):
            assert g.id
            role_groups.append(g.id)
        group = sdk.create_group(body=models.WriteGroup(name=name))
        assert group.id
        role_groups.append(group.id)
        sdk.set_role_groups(role_id=role.id, body=role_groups)

        user_attrs = sdk.all_user_attributes(fields="name,id")
        for user_attr in user_attrs:
            if user_attr.name == "landing_page":
                break
        assert user_attr.id
        sdk.set_user_attribute_group_values(
            user_attribute_id=user_attr.id,
            body=[
                models.UserAttributeGroupValue(
                    group_id=group.id, value="/extensions/hack::hack"
                )
            ],
        )
    sdk.add_group_user(
        group_id=group.id, body=models.GroupIdForGroupUserInclusion(user_id=user_id)
    )
예제 #5
0
def get_hackathon_role(*, sdk: methods.Looker40SDK) -> models.Role:
    global HACKATHON_ROLE
    if HACKATHON_ROLE is not None:
        return HACKATHON_ROLE

    for role in sdk.all_roles(fields="name,id"):
        if role.name == "Hackathon":
            HACKATHON_ROLE = role
            assert HACKATHON_ROLE.id
            break
    else:
        raise RegisterError("Hackathon role needs to be created")

    return HACKATHON_ROLE
예제 #6
0
def disable_user(*, sdk: methods.Looker40SDK, user_id: int):
    sdk.update_user(user_id=user_id, body=models.WriteUser(is_disabled=True))
예제 #7
0
def create_api3_credentials(
    *, sdk: methods.Looker40SDK, user_id: int
) -> models.CredentialsApi3:
    return sdk.create_user_credentials_api3(
        user_id=user_id, body=models.CredentialsApi3()
    )
예제 #8
0
def create_email_credentials(*, sdk: methods.Looker40SDK, user_id: int, email: str):
    sdk.create_user_credentials_email(
        user_id=user_id, body=models.WriteCredentialsEmail(email=email)
    )
    sdk.send_user_credentials_email_password_reset(user_id)