def put(self, request, *args, **kwargs): """ 管理后台修改用户密码 :param request: :param args: :param kwargs: :return: """ try: new_password = request.data.get('new_password') re_password = request.data.get("re_password") username = request.data.get('username') if not username or not re_password or not new_password: logger.debug('===================') return Response(generate_result("1003", "params error", "参数错误")) if new_password != re_password: return Response(generate_result("1010", "two password disagree", "两个密码不一致")) user_obj = user_service.get_user_by_username(username) # 修改密码 status, info = user_services.update_password(user_id=user_obj.user_id, new_password=new_password) if status: result = generate_result("0000", "change password success", "密码修改成功") else: result = generate_result("1004", "password change failed", "密码修改失败") except Exception as e: logger.exception(e) result = generate_error_result() return Response(result)
def post(self, request, tenant_name, *args, **kwargs): """ 为团队添加用户 --- parameters: - name: tenant_name description: 团队名 required: true type: string paramType: path - name: user_name description: 用户名 required: true type: string paramType: form """ try: user_name = request.data.get("user_name", None) if not user_name: raise ParamsError("用户名为空") user = user_service.get_user_by_username(user_name) tenant = tenant_service.get_tenant(tenant_name) tenant_service.add_user_to_tenant(tenant, user) result = generate_result("0000", "success", "用户添加成功") except PermTenantsExistError as e: result = generate_result("1009", "permtenant exist", e.message) except UserNotExistError as e: result = generate_result("1008", "user not exist", e.message) except Tenants.DoesNotExist as e: logger.exception(e) result = generate_result("1001", "tenant not exist", "租户{}不存在".format(tenant_name)) except Exception as e: logger.exception(e) result = generate_result("9999", "system error", "系统异常") return Response(result)
def post(self, request, tenant_name, *args, **kwargs): """ 为团队添加用户 --- parameters: - name: tenant_name description: 团队名 required: true type: string paramType: path - name: user_name description: 用户名 required: true type: string paramType: form - name: identity description: 权限 required: true type: string paramType: form """ try: user_name = request.data.get("user_name", None) if not user_name: return Response( generate_result("1003", "username is null", "用户名不能为空")) identity = request.data.get("identity", "viewer") if not identity: return Response( generate_result("1003", "identity is null", "用户权限不能为空")) user = user_service.get_user_by_username(user_name) tenant = tenant_service.get_tenant(tenant_name) if not tenant: result = generate_result("1001", "tenant not exist", "租户{}不存在".format(tenant_name)) return Response(result) enterprise = enterprise_services.get_enterprise_by_id( tenant.enterprise_id) tenant_service.add_user_to_tenant(tenant, user, identity, enterprise) result = generate_result("0000", "success", "用户添加成功") except PermTenantsExistError as e: result = generate_result("1009", "permtenant exist", e.message) except UserNotExistError as e: result = generate_result("1008", "user not exist", e.message) except Tenants.DoesNotExist as e: logger.exception(e) result = generate_result("1001", "tenant not exist", "租户{}不存在".format(tenant_name)) except Exception as e: logger.exception(e) result = generate_result("9999", "system error", "系统异常") return Response(result)
def get(self, request, tenant_name, user_name, *args, **kwargs): """ 查询某团队下的某个用户 --- parameters: - name: tenant_name description: 团队名 required: true type: string paramType: path - name: user_name description: 用户名 required: true type: string paramType: path """ try: user = user_service.get_user_by_username(user_name) tenant = tenant_service.get_tenant(tenant_name) perm_tenants = PermRelTenant.objects.filter(tenant_id=tenant.ID, user_id=user.pk) if not perm_tenants: result = generate_result( "1010", "tenant user not exist", "租户{0}下不存在用户{1}".format(tenant_name, user_name)) else: code = "0000" msg = "success" list = [] res = { "tenant_id": tenant.tenant_id, "tenant_name": tenant.tenant_name, "user_id": user.user_id, "nick_name": user.nick_name, "email": user.email, "phone": user.phone } list.append(res) result = generate_result(code, msg, "查询成功", list=list) except UserNotExistError as e: result = generate_result("1008", "user not exist", e.message) except Tenants.DoesNotExist as e: logger.exception(e) result = generate_result("1001", "tenant not exist", "租户{}不存在".format(tenant_name)) except Exception as e: logger.exception(e) result = generate_result("9999", "system error", "系统异常") return Response(result)