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 = team_services.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 get_or_create_user_and_enterprise(self, oauth_user): try: user = user_repo.get_enterprise_user_by_username( oauth_user.enterprise_id, oauth_user.name) except Users.DoesNotExist: user_info = dict() user_info["email"] = oauth_user.email user_info["nick_name"] = oauth_user.name user_info["client_ip"] = oauth_user.client_ip user_info["phone"] = oauth_user.phone user_info["real_name"] = oauth_user.real_name user_info["is_active"] = 1 password = "******" user_info["enterprise_center_user_id"] = oauth_user.id user = Users(**user_info) user.set_password(password) user.save() enterprise = enterprise_services.get_enterprise_by_enterprise_id( oauth_user.enterprise_id) if not enterprise: enterprise = enterprise_services.create_oauth_enterprise( oauth_user.enterprise_domain, oauth_user.enterprise_name, oauth_user.enterprise_id) user_services.make_user_as_admin_for_enterprise( user.user_id, enterprise.enterprise_id) user.enterprise_id = enterprise.enterprise_id user.save() return user
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( enterprise_name=None, enterprise_alias=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)