Exemple #1
0
    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)
Exemple #2
0
 def create_team(self, user, enterprise, region_list=None, team_alias=None):
     team_name = self.random_tenant_name(enterprise=user.enterprise_id,
                                         length=8)
     is_public = settings.MODULES.get('SSO_LOGIN')
     if not is_public:
         pay_type = 'payed'
         pay_level = 'company'
     else:
         pay_type = 'free'
         pay_level = 'company'
     expired_day = 7
     if hasattr(settings, "TENANT_VALID_TIME"):
         expired_day = int(settings.TENANT_VALID_TIME)
     expire_time = datetime.datetime.now() + datetime.timedelta(
         days=expired_day)
     default_region = ""
     if region_list and len(region_list) > 0:
         default_region = region_list[0]
     if not team_alias:
         team_alias = "{0}的团队".format(user.nick_name)
     params = {
         "tenant_name": team_name,
         "pay_type": pay_type,
         "pay_level": pay_level,
         "creater": user.user_id,
         "region": default_region,
         "expired_time": expire_time,
         "tenant_alias": team_alias,
         "enterprise_id": enterprise.enterprise_id,
         "limit_memory": 0,
     }
     team = team_repo.create_tenant(**params)
     create_perm_param = {
         "user_id": user.user_id,
         "tenant_id": team.ID,
         "identity": "owner",
         "enterprise_id": enterprise.ID,
     }
     team_repo.create_team_perms(**create_perm_param)
     # init default roles
     role_kind_services.init_default_roles(kind="team",
                                           kind_id=team.tenant_id)
     admin_role = role_kind_services.get_role_by_name(
         kind="team", kind_id=team.tenant_id, name=u"管理员")
     user_kind_role_service.update_user_roles(kind="team",
                                              kind_id=team.tenant_id,
                                              user=user,
                                              role_ids=[admin_role.ID])
     return team
 def create_team(self, user, enterprise, region_list=None, team_alias=None):
     team_name = self.random_tenant_name(enterprise=user.enterprise_id,
                                         length=8)
     is_public = settings.MODULES.get('SSO_LOGIN')
     if not is_public:
         pay_type = 'payed'
         pay_level = 'company'
     else:
         pay_type = 'free'
         pay_level = 'company'
     expired_day = 7
     if hasattr(settings, "TENANT_VALID_TIME"):
         expired_day = int(settings.TENANT_VALID_TIME)
     expire_time = datetime.datetime.now() + datetime.timedelta(
         days=expired_day)
     if not region_list:
         region_list = [
             r.region_name for r in region_repo.get_usable_regions()
         ]
         if not region_list:
             return 404, "无可用数据中心", None
     default_region = region_list[0]
     if not team_alias:
         team_alias = "{0}的团队".format(user.nick_name)
     params = {
         "tenant_name": team_name,
         "pay_type": pay_type,
         "pay_level": pay_level,
         "creater": user.user_id,
         "region": default_region,
         "expired_time": expire_time,
         "tenant_alias": team_alias,
         "enterprise_id": enterprise.enterprise_id,
         "limit_memory": 0,
     }
     team = team_repo.create_tenant(**params)
     create_perm_param = {
         "user_id": user.user_id,
         "tenant_id": team.ID,
         "identity": "owner",
         "enterprise_id": enterprise.ID,
     }
     team_repo.create_team_perms(**create_perm_param)
     return 200, "success", team