Esempio n. 1
0
 def is_TFA_required(self):
     for tier in current_app.config['TFA_REQUIRED_ROLES']:
         if AccessControl.has_exact_role(self.roles, 'ADMIN', tier):
             return True
     else:
         return False
Esempio n. 2
0
    def post(self, organisation_id):
        post_data = request.get_json()

        organisation_name = post_data.get('organisation_name')
        custom_welcome_message_key = post_data.get(
            'custom_welcome_message_key')
        timezone = post_data.get('timezone')

        country_code = post_data.get('country_code')
        default_disbursement = post_data.get('default_disbursement')
        minimum_vendor_payout_withdrawal = post_data.get(
            'minimum_vendor_payout_withdrawal')
        require_transfer_card = post_data.get('require_transfer_card')
        default_lat = post_data.get('default_lat')
        default_lng = post_data.get('default_lng')
        account_types = post_data.get('account_types', [])

        token_id = post_data.get('token_id')
        deploy_cic = post_data.get('deploy_cic', False)

        for at in account_types:
            if at not in ASSIGNABLE_TIERS.keys():
                raise Exception(f'{at} not an assignable role')

        if organisation_name is None or country_code is None:
            return make_response(
                jsonify({
                    'message':
                    'Must provide name and ISO 2 country_code to create organisation.'
                })), 400

        existing_organisation = Organisation.query.filter_by(
            name=organisation_name).execution_options(show_all=True).first()
        if existing_organisation is not None:
            return make_response(
                jsonify({
                    'message':
                    'Must be unique name. Organisation already exists for name: {}'
                    .format(organisation_name),
                    'data': {
                        'organisation':
                        organisation_schema.dump(existing_organisation).data
                    }
                })), 400

        try:
            new_organisation = Organisation(
                name=organisation_name,
                custom_welcome_message_key=custom_welcome_message_key,
                timezone=timezone,
                country_code=country_code,
                default_disbursement=default_disbursement,
                minimum_vendor_payout_withdrawal=
                minimum_vendor_payout_withdrawal,
                require_transfer_card=require_transfer_card,
                default_lat=default_lat,
                default_lng=default_lng,
                valid_roles=account_types)
        except Exception as e:
            response_object = {
                'message': str(e),
            }
            return make_response(jsonify(response_object)), 400

        db.session.add(new_organisation)
        db.session.flush()

        response_object = {
            'message': 'Created Organisation',
            'data': {
                'organisation': organisation_schema.dump(new_organisation).data
            },
        }

        if token_id:
            token = Token.query.get(token_id)
            if token is None:
                return make_response(jsonify({'message':
                                              'Token not found'})), 404
            new_organisation.bind_token(token)

        elif deploy_cic:

            cic_response_object, cic_response_code = deploy_cic_token(
                post_data, new_organisation)
            if cic_response_code == 201:
                response_object['data']['token_id'] = cic_response_object[
                    'data']['token_id']
            else:
                return make_response(
                    jsonify(cic_response_object)), cic_response_code

        if AccessControl.has_exact_role(g.user.roles, 'ADMIN', 'superadmin'):
            g.user.add_user_to_organisation(new_organisation, is_admin=True)

        return make_response(jsonify(response_object)), 201