Exemplo n.º 1
0
    def setUp(self):
        super().setUp()
        account_config = AccountConfig.get_current()
        account_config.allow_email = True
        account_config.allow_mobile = True
        account_config.allow_register = True
        account_config.save()

        mobile_config = SMSConfig.get_current()
        mobile_config.is_valid = True
        mobile_config.save()

        self.clear_sms_token_patcher = mock.patch(
            'infrastructure.serializers.sms.SMSClaimSerializer.clear_sms_token'
        )
        self.mock_clear_sms_token = self.clear_sms_token_patcher.start()
        self.mock_clear_sms_token.return_value = True
Exemplo n.º 2
0
    def test_block_by_account_config(self):
        mobile_config = SMSConfig.get_current()
        mobile_config.is_valid = False
        mobile_config.save()

        res = self.anonymous.json_post(reverse('infra:sms', args=('activate_user', )), data={
            'key': '*',
        })
        self.assertEqual({'mobile': ['unsupported']}, res.json())

        account_config = AccountConfig.get_current()
        account_config.allow_register = False
        account_config.save()
        res = self.anonymous.json_post(reverse('infra:email', args=('register', )), data={
            'email': '*',
        })
        self.assertEqual({'email': ['unsupported']}, res.json())
Exemplo n.º 3
0
    def setUp(self):
        super().setUp()
        self.user = User.objects.create(username='******', mobile=MOBILE)
        self.user_2 = User.objects.create(username='******', mobile=I18N_MOBILE)

        account_config = AccountConfig.get_current()
        account_config.allow_register = True
        account_config.allow_email = True
        account_config.allow_mobile = True
        account_config.save()

        email_config = EmailConfig.get_current()
        email_config.is_valid = True
        email_config.save()

        mobile_config = SMSConfig.get_current()
        mobile_config.is_valid = True
        mobile_config.save()
Exemplo n.º 4
0
    def validate(self, attrs):
        user = None
        username = attrs.get('username', '')
        private_email = attrs.get('private_email', '')
        mobile = attrs.get('mobile', '')
        password = attrs.get('password')

        if not password:
            msg = _('Must include "password".')
            raise serializers.ValidationError(msg, code='authorization')

        if not (username or mobile or password):
            msg = _('Must include "username" or "private_email" or "mobile".')
            raise serializers.ValidationError(msg, code='authorization')

        msg = _('Unable to log in with provided credentials.')
        if username:
            user = User.valid_objects.filter(username=username).first()
            if not user or not user.check_password(password):
                raise serializers.ValidationError(msg, code='authorization')

        account_config = AccountConfig.get_current()

        if private_email:
            if not account_config.support_email:
                raise serializers.ValidationError(
                    {'private_email': ['unsupport']})
            user = User.valid_objects.filter(
                private_email=private_email).first()
            if not user or not user.check_password(password):
                raise serializers.ValidationError(msg, code='authorization')

        if mobile:
            if not account_config.support_mobile:
                raise serializers.ValidationError({'mobile': ['unsupport']})
            user = User.valid_objects.filter(mobile=mobile).first()
            if not user or not user.check_password(password):
                raise serializers.ValidationError(msg, code='authorization')

        if user is None:
            raise serializers.ValidationError(msg, code='authorization')

        attrs['user'] = user
        return attrs
Exemplo n.º 5
0
    def setUp(self):
        super().setUp()
        account_config = AccountConfig.get_current()
        account_config.allow_email = True
        account_config.allow_mobile = True
        account_config.allow_register = True
        account_config.allow_ding_qr = True
        account_config.save()

        ding_config = DingConfig.get_current()
        ding_config.qr_app_valid = True
        ding_config.save()

        email_config = EmailConfig.get_current()
        email_config.is_valid = True
        email_config.save()

        mobile_config = SMSConfig.get_current()
        mobile_config.is_valid = True
        mobile_config.save()
Exemplo n.º 6
0
    def test_meta(self):
        account_config = AccountConfig.get_current()
        account_config.allow_ding_qr = True
        account_config.save()
        company_config = CompanyConfig.get_current()
        company_config.fullname_cn = "demo"
        company_config.save()
        ding_config = DingConfig.get_current()
        ding_config.corp_id = "corp_id"
        ding_config.qr_app_id = 'qr_app_id'
        ding_config.qr_app_valid = True
        ding_config.save()

        res = self.anonymous.get(reverse('siteapi:meta'))
        expect = {
            'company_config': {
                'name_cn': '',
                'fullname_cn': 'demo',
                'name_en': '',
                'fullname_en': '',
                'icon': '',
                'address': '',
                'domain': '',
                'display_name': 'demo',
                'color': '',
            },
            'ding_config': {
                'corp_id': 'corp_id',
                'app_key': '',
                'qr_app_id':'qr_app_id',
                'qr_callback_url':'http://localhost/siteapi/v1/ding/qr/callback/'
            },
            'account_config': {
                'support_email': False,
                'support_mobile': False,
                'support_email_register': False,
                'support_mobile_register': False,
                'support_ding_qr': True,
            },
        }
        self.assertEqual(res.json(), expect)
Exemplo n.º 7
0
    def create(self, request, *args, **kwargs):
        '''
        钉钉扫码加绑定
        '''
        if not AccountConfig.get_current().support_ding_qr_register:
            return Response({'err_msg': 'ding qr register not allowed'},
                            HTTP_403_FORBIDDEN)

        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.save()
        user.save()

        cli = CLI(user)
        cli.add_users_to_group([user], Group.get_extern_root())
        data = self.read_serializer_class(user).data
        data.update(token=user.token)
        ding_id = serializer.validated_data['ding_id']
        ding_user = DingUser.objects.create(ding_id=ding_id, user=user)
        ding_user.save()
        return Response(data, status=status.HTTP_201_CREATED)
Exemplo n.º 8
0
    def test_meta(self):
        account_config = AccountConfig.get_current()
        account_config.allow_ding_qr = True
        account_config.allow_alipay_qr = True
        account_config.allow_qq_qr = True
        account_config.allow_work_wechat_qr = True
        account_config.allow_wechat_qr = True
        account_config.save()
        company_config = CompanyConfig.get_current()
        company_config.fullname_cn = "demo"
        company_config.save()
        ding_config = DingConfig.get_current()
        ding_config.corp_id = "corp_id"
        ding_config.qr_app_id = 'qr_app_id'
        ding_config.qr_app_valid = True
        ding_config.save()

        alipay_config = AlipayConfig.get_current()
        alipay_config.app_id = 'test_app_id'
        alipay_config.qr_app_valid = True
        alipay_config.save()

        qq_config = QQConfig.get_current()
        qq_config.app_id = 'test_app_id'
        qq_config.redirect_uri = 'test_redirect_uri'
        qq_config.qr_app_valid = True
        qq_config.save()

        work_wechat_config = WorkWechatConfig.get_current()
        work_wechat_config.corp_id = 'test_corp_id'
        work_wechat_config.agent_id = 'test_agent_id'
        work_wechat_config.qr_app_valid = True
        work_wechat_config.save()

        wechat_config = WechatConfig.get_current()
        wechat_config.appid = 'test_appid'
        wechat_config.secret = 'test_secret'
        wechat_config.qr_app_valid = True
        wechat_config.save()

        contactsconfig = ContactsConfig.get_current()
        contactsconfig.is_show = True
        contactsconfig.save()

        res = self.anonymous.get(reverse('siteapi:meta'))
        expect = {
            'company_config': {
                'name_cn': '',
                'fullname_cn': 'demo',
                'name_en': '',
                'fullname_en': '',
                'icon': '',
                'address': '',
                'domain': '',
                'display_name': 'demo',
                'color': '',
            },
            'ding_config': {
                'corp_id': 'corp_id',
                'app_key': '',
                'qr_app_id': 'qr_app_id',
            },
            'account_config': {
                'support_email': False,
                'support_mobile': False,
                'support_email_register': False,
                'support_mobile_register': False,
                'support_ding_qr': True,
                'support_alipay_qr': True,
                'support_qq_qr': True,
                'support_work_wechat_qr': True,
                'support_wechat_qr': True,
            },
            'alipay_config': {
                'app_id': 'test_app_id',
            },
            'qq_config': {
                'app_id': 'test_app_id',
            },
            'work_wechat_config': {
                'corp_id': 'test_corp_id',
                'agent_id': 'test_agent_id'
            },
            'wechat_config': {
                'appid': 'test_appid'
            },
            "contacts_config": {
                "is_show": True
            }
        }
        self.assertEqual(res.json(), expect)
Exemplo n.º 9
0
    def update(self, instance, validated_data):  # pylint: disable=too-many-locals, too-many-statements, too-many-branches
        company_config_data = validated_data.pop('company_config', None)
        if company_config_data:
            if not Dept.valid_objects.filter(parent__uid='root').exists():
                uid = gen_uid(name=company_config_data.get('name_cn', ''),
                              cls=Dept)
                parent_dept = Dept.valid_objects.filter(uid='root').first()
                cli = CLI()
                dept_data = {
                    'parent_uid': 'root',
                    'name': company_config_data.get('name_cn', ''),
                    'uid': uid,
                }
                child_dept = cli.create_dept(dept_data)
                cli.add_dept_to_dept(child_dept, parent_dept)
            else:
                company_dept = Dept.valid_objects.filter(
                    parent__uid='root').first()
                company_dept.name = company_config_data.get('name_cn', '')
                company_dept.save()
            serializer = CompanyConfigSerializer(CompanyConfig.get_current(),
                                                 company_config_data)
            serializer.is_valid(raise_exception=True)
            serializer.save()

        account_config_data = validated_data.pop('account_config', None)
        if account_config_data:
            serializer = AccountConfigSerializer(AccountConfig.get_current(),
                                                 account_config_data,
                                                 partial=True)
            serializer.is_valid(raise_exception=True)
            serializer.save()

        ding_config_data = validated_data.pop('ding_config', None)
        if ding_config_data:
            serializer = DingConfigSerializer(DingConfig.get_current(),
                                              ding_config_data,
                                              partial=True)
            serializer.is_valid(raise_exception=True)
            serializer.save()

        sms_config_data = validated_data.pop('sms_config', None)
        if sms_config_data:
            access_secret = sms_config_data.pop('access_secret', '')
            config = SMSConfig.get_current()
            serializer = SMSConfigSerializer(config,
                                             sms_config_data,
                                             partial=True)
            serializer.is_valid(raise_exception=True)  # pylint: disable=not-callable
            config.__dict__.update(serializer.validated_data)

            if access_secret:
                config.access_secret = access_secret
            if not config.check_valid():
                raise ValidationError({'sms': ['invalid']})
            config.is_valid = True
            config.save()

        email_config_data = validated_data.pop('email_config', None)
        if email_config_data:
            access_secret = email_config_data.pop('access_secret', '')
            config = EmailConfig.get_current()
            serializer = EmailConfigSerializer(config,
                                               email_config_data,
                                               partial=True)
            serializer.is_valid(raise_exception=True)  # pylint: disable=not-callable
            config.__dict__.update(serializer.validated_data)

            if access_secret:
                config.access_secret = access_secret
            if not config.check_valid():
                raise ValidationError({'email': ['invalid']})
            config.is_valid = True
            config.save()

        alipay_config_data = validated_data.pop('alipay_config', None)
        if alipay_config_data:
            if alipay_config_data["app_id"] != '':
                serializer = AlipayConfigSerializer(AlipayConfig.get_current(),
                                                    alipay_config_data,
                                                    partial=True)
                serializer.is_valid(raise_exception=True)
                serializer.save()

        qq_config_data = validated_data.pop('qq_config', None)
        if qq_config_data:
            serializer = QQConfigSerializer(QQConfig.get_current(),
                                            qq_config_data,
                                            partial=True)
            serializer.is_valid(raise_exception=True)
            serializer.save()

        work_wechat_config_data = validated_data.pop('work_wechat_config',
                                                     None)
        if work_wechat_config_data:
            serializer = WorkWechatConfigSerializer(WorkWechatConfig.get_current(),\
                work_wechat_config_data, partial=True)
            serializer.is_valid(raise_exception=True)
            serializer.save()

        wechat_config_data = validated_data.pop('wechat_config', None)
        if wechat_config_data:
            serializer = WechatConfigSerializer(WechatConfig.get_current(),\
                wechat_config_data, partial=True)
            serializer.is_valid(raise_exception=True)
            serializer.save()

        password_config_data = validated_data.pop('password_config', None)
        if password_config_data:
            config = PasswordComplexityConfig.get_current()
            serializer = PasswordConfigSerializer(config,
                                                  password_config_data,
                                                  partial=True)
            serializer.is_valid(raise_exception=True)  # pylint: disable=not-callable
            config.__dict__.update(serializer.validated_data)
            config.save()

        github_config = validated_data.pop('github_config', None)
        if github_config:
            config = GithubConfig.get_current()
            serializer = GithubConfigSerializer(config,
                                                github_config,
                                                partial=True)
            serializer.is_valid(raise_exception=True)
            serializer.save()

        instance.refresh_from_db()

        return instance
Exemplo n.º 10
0
 def inner(self, request):
     return Response({'err_msg':'ding qr not allowed'}, HTTP_403_FORBIDDEN)\
         if not AccountConfig.get_current().support_ding_qr else func(self, request)
Exemplo n.º 11
0
    def update(self, instance, validated_data):
        company_config_data = validated_data.pop('company_config', None)
        if company_config_data:
            if not Dept.valid_objects.filter(parent__uid='root').exists():
                uid = gen_uid(name=company_config_data.get('name_cn', ''), cls=Dept)
                parent_dept = Dept.valid_objects.filter(uid='root').first()
                cli = CLI()
                dept_data = {
                    'parent_uid': 'root',
                    'name': company_config_data.get('name_cn', ''),
                    'uid': uid,
                }
                child_dept = cli.create_dept(dept_data)
                cli.add_dept_to_dept(child_dept, parent_dept)
            else:
                company_dept = Dept.valid_objects.filter(parent__uid='root').first()
                company_dept.name = company_config_data.get('name_cn', '')
                company_dept.save()
            serializer = CompanyConfigSerializer(CompanyConfig.get_current(), company_config_data)
            serializer.is_valid(raise_exception=True)
            serializer.save()

        ding_config_data = validated_data.pop('ding_config', None)
        if ding_config_data:
            serializer = DingConfigSerializer(DingConfig.get_current(), ding_config_data, partial=True)
            serializer.is_valid(raise_exception=True)
            serializer.save()

        account_config_data = validated_data.pop('account_config', None)
        if account_config_data:
            serializer = AccountConfigSerializer(AccountConfig.get_current(), account_config_data, partial=True)
            serializer.is_valid(raise_exception=True)
            serializer.save()

        sms_config_data = validated_data.pop('sms_config', None)
        if sms_config_data:
            access_secret = sms_config_data.pop('access_secret', '')
            config = SMSConfig.get_current()
            serializer = SMSConfigSerializer(config, sms_config_data, partial=True)
            serializer.is_valid(raise_exception=True)    # pylint: disable=not-callable
            config.__dict__.update(serializer.validated_data)

            if access_secret:
                config.access_secret = access_secret
            if not config.check_valid():
                raise ValidationError({'sms': ['invalid']})
            config.is_valid = True
            config.save()

        email_config_data = validated_data.pop('email_config', None)
        if email_config_data:
            access_secret = email_config_data.pop('access_secret', '')
            config = EmailConfig.get_current()
            serializer = EmailConfigSerializer(config, email_config_data, partial=True)
            serializer.is_valid(raise_exception=True)    # pylint: disable=not-callable
            config.__dict__.update(serializer.validated_data)

            if access_secret:
                config.access_secret = access_secret
            if not config.check_valid():
                raise ValidationError({'email': ['invalid']})
            config.is_valid = True
            config.save()

        instance.refresh_from_db()

        return instance
Exemplo n.º 12
0
    def update(self, instance, validated_data):
        company_config_data = validated_data.pop('company_config', None)
        if company_config_data:
            serializer = CompanyConfigSerializer(CompanyConfig.get_current(),
                                                 company_config_data)
            serializer.is_valid(raise_exception=True)
            serializer.save()

        ding_config_data = validated_data.pop('ding_config', None)
        if ding_config_data:
            serializer = DingConfigSerializer(DingConfig.get_current(),
                                              ding_config_data,
                                              partial=True)
            serializer.is_valid(raise_exception=True)
            serializer.save()

        account_config_data = validated_data.pop('account_config', None)
        if account_config_data:
            serializer = AccountConfigSerializer(AccountConfig.get_current(),
                                                 account_config_data,
                                                 partial=True)
            serializer.is_valid(raise_exception=True)
            serializer.save()

        sms_config_data = validated_data.pop('sms_config', None)
        if sms_config_data:
            access_secret = sms_config_data.pop('access_secret', '')
            config = SMSConfig.get_current()
            serializer = SMSConfigSerializer(config,
                                             sms_config_data,
                                             partial=True)
            serializer.is_valid(raise_exception=True)  # pylint: disable=not-callable
            config.__dict__.update(serializer.validated_data)

            if access_secret:
                config.access_secret = access_secret
            if not config.check_valid():
                raise ValidationError({'sms': ['invalid']})
            config.is_valid = True
            config.save()

        email_config_data = validated_data.pop('email_config', None)
        if email_config_data:
            access_secret = email_config_data.pop('access_secret', '')
            config = EmailConfig.get_current()
            serializer = EmailConfigSerializer(config,
                                               email_config_data,
                                               partial=True)
            serializer.is_valid(raise_exception=True)  # pylint: disable=not-callable
            config.__dict__.update(serializer.validated_data)

            if access_secret:
                config.access_secret = access_secret
            if not config.check_valid():
                raise ValidationError({'email': ['invalid']})
            config.is_valid = True
            config.save()

        instance.refresh_from_db()

        return instance