def list_user_groups(keycloak: KeycloakClient, user_id): try: from rhub.api.auth.group import _group_href return [ group | { '_href': _group_href(group) } for group in keycloak.user_group_list(user_id) ] except KeycloakGetError as e: logger.exception(e) return problem_from_keycloak_error(e) except Exception as e: logger.exception(e) return problem(500, 'Unknown Error', str(e))
def list_product_regions(keycloak: KeycloakClient, product_id, user, filter_, page=0, limit=DEFAULT_PAGE_LIMIT): product = model.Product.query.get(product_id) if not product: return problem(404, 'Not Found', f'Product {product_id} does not exist') regions_relation = product.regions_relation if not keycloak.user_check_role(user, ADMIN_ROLE): user_groups = [group['id'] for group in keycloak.user_group_list(user)] regions_relation = regions_relation.filter( sqlalchemy.or_( model.Region.users_group.is_(None), model.Region.users_group.in_(user_groups), model.Region.owner_group.in_(user_groups), )) if 'name' in filter_: regions_relation = regions_relation.filter( model.Region.name.ilike(filter_['name']), ) if 'location' in filter_: regions_relation = regions_relation.filter( model.Region.location.ilike(filter_['location']), ) if 'enabled' in filter_: regions_relation = regions_relation.filter( sqlalchemy.and_( model.RegionProduct.enabled == filter_['enabled'], model.Region.enabled == filter_['enabled'], )) if 'reservations_enabled' in filter_: regions_relation = regions_relation.filter( model.Region.reservations_enabled == filter_['reservations_enabled'], ) return [{ 'id': r.region_id, 'region': r.region.to_dict(), 'enabled': r.enabled } for r in regions_relation]
def list_regions(keycloak: KeycloakClient, user, filter_, sort=None, page=0, limit=DEFAULT_PAGE_LIMIT): if keycloak.user_check_role(user, ADMIN_ROLE): regions = model.Region.query else: user_groups = [group['id'] for group in keycloak.user_group_list(user)] regions = model.Region.query.filter( sqlalchemy.or_( model.Region.users_group.is_(None), model.Region.users_group.in_(user_groups), model.Region.owner_group.in_(user_groups), )) if 'name' in filter_: regions = regions.filter(model.Region.name.ilike(filter_['name'])) if 'location' in filter_: regions = regions.filter( model.Region.location.ilike(filter_['location'])) if 'enabled' in filter_: regions = regions.filter(model.Region.enabled == filter_['enabled']) if 'reservations_enabled' in filter_: regions = regions.filter(model.Region.reservations_enabled == filter_['reservations_enabled']) if sort: regions = db_sort(regions, sort) return { 'data': [ region.to_dict() | { '_href': _region_href(region) } for region in regions.limit(limit).offset(page * limit) ], 'total': regions.count(), }