def list_users_by_tenant_id(self, tenant_id, page=None, size=None, query=""): result = user_repo.list_users_by_tenant_id(tenant_id, query=query, page=page, size=size) users = [] for item in result: user = user_repo.get_by_user_id(item.get("user_id")) role_infos = user_kind_role_service.get_user_roles( kind="team", kind_id=tenant_id, user=user) users.append({ "user_id": item.get("user_id"), "nick_name": item.get("nick_name"), "email": item.get("email"), "phone": item.get("phone"), "is_active": item.get("is_active"), "enterprise_id": item.get("enterprise_id"), "role_infos": role_infos["roles"], }) total = user_repo.count_users_by_tenant_id(tenant_id, query=query) return users, total
def list_users_by_tenant_id(self, tenant_id, page=None, size=None, query=""): result = user_repo.list_users_by_tenant_id(tenant_id, query=query, page=page, size=size) users = [] for item in result: # 获取一个用户在一个团队中的身份列表 perms_identitys = team_services.get_user_perm_identitys_in_permtenant( user_id=item.get("user_id"), tenant_name=tenant_id) # 获取一个用户在一个团队中的角色ID列表 perms_role_list = team_services.get_user_perm_role_id_in_permtenant( user_id=item.get("user_id"), tenant_name=tenant_id) role_infos = [] for identity in perms_identitys: if identity == "access": role_infos.append({"role_name": identity, "role_id": None}) else: role_id = role_repo.get_role_id_by_role_name(identity) role_infos.append({"role_name": identity, "role_id": role_id}) for role in perms_role_list: role_name = role_repo.get_role_name_by_role_id(role) role_infos.append({"role_name": role_name, "role_id": role}) users.append({ "user_id": item.get("user_id"), "nick_name": item.get("nick_name"), "email": item.get("email"), "phone": item.get("phone"), "is_active": item.get("is_active"), "enterprise_id": item.get("enterprise_id"), "role_infos": role_infos, }) total = user_repo.count_users_by_tenant_id(tenant_id, query=query) return users, total
def test_list_users_by_tenant_id(): from console.services.user_services import user_services from console.repositories.team_repo import team_repo from console.repositories.perm_repo import perms_repo from console.repositories.user_repo import user_repo eid = "bb2f17abc58b328374351e9c92abd400" tenant_id = "374351e9c92abd400bb2f17abc58b328" params = { "tenant_id": tenant_id, "tenant_name": "xxxxxxxx", "creater": 1, "region": "rainbond", "tenant_alias": "foobar team", "enterprise_id": eid, } team = team_repo.create_tenant(**params) userinfo = [{ "nick_name": "foo", "email": "*****@*****.**", "password": "******", "eid": eid }, { "nick_name": "bar", "email": "*****@*****.**", "password": "******", "eid": eid }, { "nick_name": "dummy", "email": "*****@*****.**", "password": "******", "eid": eid }] for item in userinfo: user = user_services.create(item) perminfo = { "user_id": user.user_id, "tenant_id": team.ID, "identity": "owner", "enterprise_id": 1 } perms_repo.add_user_tenant_perm(perminfo) testcases = [ { "tenant_id": tenant_id, "query": "", "page": None, "size": None, "count": 3, "user_id": 1 }, { "tenant_id": tenant_id, "query": "bar", "page": None, "size": None, "count": 1, "user_id": 2 }, { "tenant_id": tenant_id, "query": "*****@*****.**", "page": None, "size": None, "count": 1, "user_id": 1 }, { "tenant_id": tenant_id, "query": "", "page": 2, "size": 2, "count": 1, "user_id": 3 }, { "tenant_id": tenant_id, "query": "nothing", "page": None, "size": None, "count": 0, "user_id": 0 }, { "tenant_id": tenant_id, "query": "", "page": -1, "size": None, "count": 3, "user_id": 1 }, ] for tc in testcases: result = user_repo.list_users_by_tenant_id(tc["tenant_id"], tc["query"], tc["page"], tc["size"]) print result assert len(result) == tc["count"] if len(result) > 0: assert result[0].get("user_id") == tc["user_id"]