Exemplo n.º 1
0
 def post(self, request, *args, **kwargs):
     enterprise_id = request.data.get("enterprise_id")
     tenant_id = request.data.get("tenant_id")
     if tenant_id and enterprise_id:
         teams = Tenants.objects.filter(tenant_id=tenant_id, enterprise_id=enterprise_id)
     elif tenant_id and not enterprise_id:
         teams = Tenants.objects.filter(tenant_id=tenant_id)
     elif not tenant_id and enterprise_id:
         teams = Tenants.objects.filter(enterprise_id=enterprise_id)
     else:
         teams = Tenants.objects.all()
     if not teams:
         print(u"未发现团队, 初始化结束")
         return
     for team in teams:
         role_kind_services.init_default_roles(kind="team", kind_id=team.tenant_id)
         users = team_repo.get_tenant_users_by_tenant_ID(team.ID)
         admin = role_kind_services.get_role_by_name(kind="team", kind_id=team.tenant_id, name=u"管理员")
         developer = role_kind_services.get_role_by_name(kind="team", kind_id=team.tenant_id, name=u"开发者")
         if not admin or not developer:
             raise ServiceHandleException(msg="init failed", msg_show=u"初始化失败")
         if users:
             for user in users:
                 if user.user_id == team.creater:
                     user_kind_role_service.update_user_roles(
                         kind="team", kind_id=team.tenant_id, user=user, role_ids=[admin.ID])
                 else:
                     user_kind_role_service.update_user_roles(
                         kind="team", kind_id=team.tenant_id, user=user, role_ids=[developer.ID])
     result = general_message(msg="success", msg_show=u"初始化权限分配成功", code=200)
     return Response(result, status=200)
Exemplo n.º 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
Exemplo n.º 3
0
 def handle(self, *args, **options):
     tenant_id = options['tenant_id']
     enterprise_id = options['enterprise_id']
     if tenant_id and enterprise_id:
         teams = Tenants.objects.filter(tenant_id=tenant_id,
                                        enterprise_id=enterprise_id)
     elif tenant_id and not enterprise_id:
         teams = Tenants.objects.filter(tenant_id=tenant_id)
     elif not tenant_id and enterprise_id:
         teams = Tenants.objects.filter(enterprise_id=enterprise_id)
     else:
         teams = Tenants.objects.all()
     if not teams:
         print(u"未发现团队, 初始化结束")
         return
     for team in teams:
         role_kind_services.init_default_roles(kind="team",
                                               kind_id=team.tenant_id)
         users = team_repo.get_tenant_users_by_tenant_ID(team.ID)
         admin = role_kind_services.get_role_by_name(kind="team",
                                                     kind_id=team.tenant_id,
                                                     name=u"管理员")
         developer = role_kind_services.get_role_by_name(
             kind="team", kind_id=team.tenant_id, name=u"开发者")
         if not admin or not developer:
             raise ServiceHandleException(msg="init failed",
                                          msg_show=u"初始化失败")
         if users:
             for user in users:
                 if user.user_id == team.creater:
                     user_kind_role_service.update_user_roles(
                         kind="team",
                         kind_id=team.tenant_id,
                         user=user,
                         role_ids=[admin.ID])
                 else:
                     user_kind_role_service.update_user_roles(
                         kind="team",
                         kind_id=team.tenant_id,
                         user=user,
                         role_ids=[developer.ID])