def add_user(self, request, tenant_name): phone = request.data.get("phone", None) user_name = request.data.get("user_name", None) email = request.data.get("email", None) password = request.data.get("password", None) tenant = tenantService.get_tenant(tenant_name) if Users.objects.filter(nick_name=user_name).exists(): raise UserExistError("用户名已存在") if Users.objects.filter(email=email).exists(): raise EmailExistError("邮箱已存在") if Users.objects.filter(phone=phone).exists(): raise PhoneExistError("手机号已存在") user = Users(email=email, nick_name=user_name, phone=phone, client_ip=self.get_client_ip(request), rf="backend") user.set_password(password) user.save() PermRelTenant.objects.create(user_id=user.pk, tenant_id=tenant.pk, identity='admin') codeRepositoriesService.createUser(user, email, password, user_name, user_name)
def init_region_tenant(self, region, tenant_name, tenant_id, nick_name): user = Users(nick_name=nick_name) for num in range(0, 3): result = tenant_svc.init_for_region(region, tenant_name, tenant_id, user) if result: logger.debug("openapi.cloudservice", "init tenant region success!") return result else: logger.error( "openapi.cloudservice", "init tenant region failed! try again!num:{0}".format(num)) return False
def post(self, request, *args, **kwargs): """ 注册用户、需要先访问captcha路由来获取验证码 --- parameters: - name: user_name description: 用户名 required: true type: string paramType: body - name: email description: 邮箱 required: true type: string paramType: body - name: password description: 密码,最少八位 required: true type: string paramType: body - name: password_repeat description: 确认密码 required: true type: string paramType: body - name: captcha_code description: 验证码 required: true type: string paramType: body - name: register_type description: 注册方式 暂: 邀请注册 invitation 其它方式暂无 有拓展再修改 required: false type: string paramType: body - name: value description: 数值 此处需要 team_id required: false type: string paramType: body - name: enter_name description: 企业名称 required: false type: string paramType: body """ try: import copy querydict = copy.copy(request.data) captcha_code = request.session.get("captcha_code") querydict.update({u'real_captcha_code': captcha_code}) client_ip = request.META.get("REMOTE_ADDR", None) register_form = RegisterForm(querydict) if register_form.is_valid(): nick_name = register_form.cleaned_data["user_name"] email = register_form.cleaned_data["email"] password = register_form.cleaned_data["password"] # 创建一个用户 user_info = dict() user_info["email"] = email user_info["nick_name"] = nick_name user_info["client_ip"] = client_ip user_info["is_active"] = 1 user = Users(**user_info) user.set_password(password) user.save() enterprise = enterprise_services.get_enterprise_first() if not enterprise: enter_name = request.data.get("enter_name", None) enterprise = enterprise_services.create_enterprise( enter_name, enter_name) # 创建用户在企业的权限 user_services.make_user_as_admin_for_enterprise( user.user_id, enterprise.enterprise_id) user.enterprise_id = enterprise.enterprise_id user.save() if Users.objects.count() == 1: SuperAdminUser.objects.create(user_id=user.user_id) enterprise = enterprise_services.get_enterprise_first() register_type = request.data.get("register_type", None) value = request.data.get("value", None) if register_type == "invitation": perm = perms_repo.add_user_tenant_perm( perm_info={ "user_id": user.user_id, "tenant_id": value, "identity": "viewer", "enterprise_id": enterprise.ID }) if not perm: result = general_message(400, "invited failed", "团队关联失败,注册失败") return Response(result, status=400) data = dict() data["user_id"] = user.user_id data["nick_name"] = user.nick_name data["email"] = user.email data["enterprise_id"] = user.enterprise_id payload = jwt_payload_handler(user) token = jwt_encode_handler(payload) data["token"] = token result = general_message(200, "register success", "注册成功", bean=data) response = Response(result, status=200) return response else: error = { "error": list(json.loads( register_form.errors.as_json()).values())[0][0].get( "message", "参数错误") } result = general_message(400, "failed", "{}".format(error["error"])) return Response(result, status=400) except Exception as e: logger.exception(e) result = error_message(e.message) return Response(result, status=500)
def post(self, request, *args, **kwargs): """ 注册用户租户 --- parameters: - name: username description: 用户名 required: true type: int paramType: form - name: password description: 密码 required: true type: string paramType: form - name: tenant_name description: 租户名称 required: true type: string paramType: form - name: region description: 数据中心 required: true type: string paramType: form """ # 数据中心 if sn.instance.is_private(): return Response(status=501, data={ "success": False, "msg": u"不允许创建用户!" }) region = request.data.get("region") username = request.data.get("username") password = request.data.get("password") tenant_name = request.data.get("tenant_name") if region is None: return Response(status=405, data={ "success": False, "msg": u"数据中心名称为空" }) if username is None: return Response(status=406, data={ "success": False, "msg": u"用户名不能为空" }) if tenant_name is None: return Response(status=407, data={ "success": False, "msg": u"租户名称不能为空!" }) # 校验username try: is_standard_word(username) is_sensitive(username) except Exception as e: return Response(status=408, data={ "success": False, "msg": u"用户名不合法!" }) try: is_standard_word(tenant_name) is_sensitive(tenant_name) except Exception as e: return Response(status=408, data={ "success": False, "msg": u"租户名称不合法!" }) # 参数log logger.debug( "openapi.services", "now create user tenant: tenant_name:{0}, region:{1}, username:{2}" .format(tenant_name, region, username)) # 创建用户 user_exists = True try: curr_user = Users.objects.get(nick_name=username) except Users.DoesNotExist: user_exists = False rf = "openapi" # 用户不存在,检查password if password is None: return Response(status=410, data={ "success": False, "msg": u"密码不能为空" }) # 新增用户 curr_user = Users(nick_name=username, client_ip=self.get_client_ip(request), rf=rf) if password.endswith("#"): return Response(status=411, data={ "success": False, "msg": u"密码不能以#结尾" }) # 设置密码 curr_user.set_password(password) curr_user.save() logger.debug("openapi.services", "now create user success") # 添加auth_user tmpname = username + "_token" oauth_user = OAuthUser.objects.create(username=tmpname) oauth_user.set_password(password) oauth_user.is_staff = True oauth_user.save() # 处理租户逻辑 try: tenant = Tenants.objects.get(tenant_name=tenant_name) except Tenants.DoesNotExist: logger.debug( "openapi.services", "Tenant {0} is not exists, now create...".format(tenant_name)) # 创建tenant tenant = manager.create_tenant(tenant_name, region, curr_user.user_id, username) if tenant: # 添加user-tenant关系 if not user_exists: try: PermRelTenant.objects.create(user_id=curr_user.pk, tenant_id=tenant.pk, identity='admin') except Exception as e: logger.exception("openapi.services", e) return Response(status=200, data={ "success": True, "tenant": { "tenant_id": tenant.tenant_id, "tenant_name": tenant.tenant_name, "region": tenant.region }, "user": { "user_id": curr_user.user_id, "nick_name": curr_user.nick_name, "email": curr_user.email } }) else: return Response(status=500, data={ "success": False, "msg": "操作失败!" })