def post(self, request, enterprise_id, *args, **kwargs): tenant_name = request.data.get("tenant_name", None) user_name = request.data.get("user_name", None) email = request.data.get("email", None) password = request.data.get("password", None) re_password = request.data.get("re_password", None) role_ids = request.data.get("role_ids", None) phone = request.data.get("phone", None) real_name = request.data.get("real_name", None) tenant = team_services.get_tenant_by_tenant_name(tenant_name) if len(password) < 8: result = general_message(400, "len error", "密码长度最少为8位") return Response(result) # check user info is_pass, msg = user_services.check_params(user_name, email, password, re_password, request.user.enterprise_id) if not is_pass: result = general_message(403, "user information is not passed", msg) return Response(result) client_ip = user_services.get_client_ip(request) enterprise = enterprise_services.get_enterprise_by_enterprise_id( enterprise_id) # create user oauth_instance, _ = user_services.check_user_is_enterprise_center_user( request.user.user_id) if oauth_instance: user = user_services.create_enterprise_center_user_set_password( user_name, email, password, "admin add", enterprise, client_ip, phone, real_name, oauth_instance) else: user = user_services.create_user_set_password( user_name, email, password, "admin add", enterprise, client_ip, phone, real_name) result = general_message(200, "success", "添加用户成功") if tenant: create_perm_param = { "user_id": user.user_id, "tenant_id": tenant.ID, "identity": "", "enterprise_id": enterprise.ID, } team_repo.create_team_perms(**create_perm_param) if role_ids: user_kind_role_service.update_user_roles( kind="team", kind_id=tenant.tenant_id, user=user, role_ids=role_ids) user.is_active = True user.save() result = general_message(200, "success", "添加用户成功") return Response(result)
def post(self, request, *args, **kwargs): """ 修改密码 --- parameters: - name: password description: 原密码 required: true type: string paramType: form - name: new_password description: 新密码 required: true type: string paramType: form - name: new_password2 description: 确认密码 required: true type: string paramType: form """ try: password = request.data.get("password", None) new_password = request.data.get("new_password", None) new_password2 = request.data.get("new_password2", None) u = request.user code = 400 if not user_services.check_user_password(user_id=u.user_id, password=password): result = general_message(400, "old password error", "旧密码错误") elif new_password != new_password2: result = general_message(400, "two password disagree", "两个密码不一致") elif password == new_password: result = general_message(400, "old and new password agree", "新旧密码一致") else: status, info = user_services.update_password(user_id=u.user_id, new_password=new_password) oauth_instance, _ = user_services.check_user_is_enterprise_center_user(request.user.user_id) if oauth_instance: data = { "password": new_password, "real_name": request.user.real_name, } oauth_instance.update_user(request.user.enterprise_id, request.user.enterprise_center_user_id, data) if status: code = 200 result = general_message(200, "change password success", "密码修改成功") else: result = general_message(400, "password change failed", "密码修改失败") return Response(result, status=code) except Exception as e: logger.exception(e) result = error_message(e.message) return Response(result, status=500)
def delete(self, request, enterprise_id, user_id, *args, **kwargs): user = user_repo.get_enterprise_user_by_id(enterprise_id, user_id) if not user: result = general_message(400, "fail", "未找到该用户") return Response(result, 403) user_services.delete_user(user_id) oauth_instance, oauth_user = user_services.check_user_is_enterprise_center_user(user_id) if oauth_instance: oauth_instance.delete_user(enterprise_id, user.enterprise_center_user_id) all_oauth_user = oauth_user_repo.get_all_user_oauth(user_id) all_oauth_user.delete() result = general_message(200, "success", "删除用户成功") return Response(result, status=200)
def put(self, request, *args, **kwargs): """ 修改密码 --- parameters: - name: user_id description: 用户id required: true type: string paramType: form - name: password description: 新密码 required: true type: string paramType: form - name: password1 description: 确认密码 required: true type: string paramType: form """ serializer = ChangePassWdUserSerializer(data=request.data) serializer.is_valid(raise_exception=True) user_id = serializer.data.get("user_id", None) new_password = serializer.data.get("password", None) new_password1 = serializer.data.get("password1", None) info = u"缺少参数" user = user_repo.get_enterprise_user_by_id(request.user.enterprise_id, user_id) if not user: raise ServiceHandleException(msg="no found user", msg_show=u"用户不存在", status_code=404) if new_password and new_password == new_password1: status, info = user_services.update_password( user_id=user_id, new_password=new_password) oauth_instance, _ = user_services.check_user_is_enterprise_center_user( user_id) if oauth_instance: data = { "password": new_password, "real_name": user.real_name, } oauth_instance.update_user(request.user.enterprise_id, user.enterprise_center_user_id, data) if status: return Response(None, status=200) logger.debug(info) return Response(None, status=400)
def put(self, request, enterprise_id, user_id, *args, **kwargs): password = request.data.get("password", None) real_name = request.data.get("real_name", None) phone = request.data.get("phone", None) user = user_services.update_user_set_password(enterprise_id, user_id, password, real_name, phone) user.save() oauth_instance, _ = user_services.check_user_is_enterprise_center_user(request.user.user_id) if oauth_instance: data = { "password": password, "real_name": real_name, "phone": phone, } oauth_instance.update_user(enterprise_id, user.enterprise_center_user_id, data) result = general_message(200, "success", "更新用户成功") return Response(result, status=200)
def put(self, request, *args, **kwargs): """ 修改密码 --- parameters: - name: password description: 新密码 required: true type: string paramType: form - name: password1 description: 确认密码 required: true type: string paramType: form """ serializer = ChangePassWdSerializer(data=request.data) serializer.is_valid(raise_exception=True) new_password = serializer.data.get("password", None) new_password1 = serializer.data.get("password1", None) info = u"缺少参数" if new_password and new_password == new_password1: status, info = user_services.update_password( user_id=request.user.user_id, new_password=new_password) oauth_instance, _ = user_services.check_user_is_enterprise_center_user( request.user.user_id) if oauth_instance: data = { "password": new_password, "real_name": request.user.real_name, } oauth_instance.update_user( request.user.enterprise_id, request.user.enterprise_center_user_id, data) if status: return Response(None, status=200) logger.debug(info) return Response(None, status=400)