<commit_before>import sys
import logging
import logging.config
import os
import yaml
import pprint

import consul
from pymongo import MongoClient
import requests
from pyftpdlib.authorizers import DummyAuthorizer, AuthenticationFailed
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer

from biomaj_user.user import BmajUser
from biomaj_core.utils import Utils
from biomaj_core.config import BiomajConfig


class BiomajAuthorizer(DummyAuthorizer):

    def set_config(self, cfg):
        self.cfg = cfg
        self.mongo = MongoClient(BiomajConfig.global_config.get('GENERAL', 'db.url'))
        self.db = self.mongo[BiomajConfig.global_config.get('GENERAL', 'db.name')]
        self.bank = None
        self.logger = logging

    def set_logger(self, logger):
        self.logger = logger

    def validate_authentication(self, username, apikey, handler):
        """Raises AuthenticationFailed if supplied username and
        password don't match the stored credentials, else return
        None.
        """
        # msg = "Authentication failed."
        if apikey == 'anonymous':
            bank = self.db.banks.find_one({'name': username})
            if not bank:
                self.logger.error('Bank not found: ' + username)
                raise AuthenticationFailed('Bank does not exists')
            if bank['properties']['visibility'] != 'public':
                raise AuthenticationFailed('Not allowed to access to this bank')
            if len(bank['production']) == 0:
                raise AuthenticationFailed('No production release available')
            self.bank = bank
            if not self.has_user(username):
                self.add_user(username,apikey,self.get_home_dir(username))
            return
        if apikey != 'anonymous':
            user = None
            if 'web' in self.cfg and 'local_endpoint' in self.cfg['web'] and self.cfg['web']['local_endpoint']:
                user_req = requests.get(self.cfg['web']['local_endpoint'] + '/api/user/info/apikey/' + apikey)
                if not user_req.status_code == 200:
                    raise AuthenticationFailed('Wrong or failed authentication')
                user = user_req.json()
            else:
                user = BmajUser.get_user_by_apikey(apikey)
            #Add user authentification CR
            if user['id'] == username :
                dict_bank = {}
                for db_entry in self.db.banks.find() :
                    home_dir = self.get_home_dir(username, db_entry)
                    dict_bank[home_dir] = [db_entry['properties']['visibility'], db_entry['properties']['owner']]
                self.bank = dict_bank
                #Create a new user for biomaj server with specific permission
                if not self.has_user(username):
                    self.add_user(username,apikey,self.get_home_dir(username))
                for directory in dict_bank :
                     #If the user is the bank's owner
                     if dict_bank[directory][0] == "public" :
                         perm = "elr"
                         self.override_perm(username, directory, perm, recursive=True)
                     if dict_bank[directory][1] == username :#and dict_bank[directory][0] != "public" :
                         perm = "elr"
                         self.override_perm(username, directory, perm, recursive=True)
                     else :
                         perm = "el"
                         self.override_perm(username, directory, perm, recursive=True)
                return
            bank = self.db.banks.find_one({'name': username})
            if not bank:
                self.logger.error('Bank not found: ' + username)
                raise AuthenticationFailed('Bank does not exists')
            if bank['properties']['visibility'] != 'public':
                if user['id'] != bank['properties']['owner']:
                    if 'members' not in bank['properties'] or user['id'] not in bank['properties']['members']:
                        raise AuthenticationFailed('Not allowed to access to this bank')
            if len(bank['production']) == 0:
                raise AuthenticationFailed('No production release available')
            self.bank = bank
            if not self.has_user(username):
                self.add_user(username,apikey,self.get_home_dir(bank))
           <commit_msg>add logging for anonymous user<commit_after>import sys
import logging
import logging.config
import os
import yaml
import pprint

import consul
from pymongo import MongoClient
import requests
from pyftpdlib.authorizers import DummyAuthorizer, AuthenticationFailed
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer

from biomaj_user.user import BmajUser
from biomaj_core.utils import Utils
from biomaj_core.config import BiomajConfig


class BiomajAuthorizer(DummyAuthorizer):

    def set_config(self, cfg):
        self.cfg = cfg
        self.mongo = MongoClient(BiomajConfig.global_config.get('GENERAL', 'db.url'))
        self.db = self.mongo[BiomajConfig.global_config.get('GENERAL', 'db.name')]
        self.bank = None
        self.logger = logging

    def set_logger(self, logger):
        self.logger = logger

    def validate_authentication(self, username, apikey, handler):
        """Raises AuthenticationFailed if supplied username and
        password don't match the stored credentials, else return
        None.
        """
        # msg = "Authentication failed."
        if apikey == 'anonymous':
            bank = self.db.banks.find_one({'name': username})
            if not bank:
                self.logger.error('Bank not found: ' + username)
                raise AuthenticationFailed('Bank does not exists')
            if bank['properties']['visibility'] != 'public':
                raise AuthenticationFailed('Not allowed to access to this bank')
            if len(bank['production']) == 0:
                raise AuthenticationFailed('No production release available')
            self.bank = bank
            if not self.has_user(username):
                self.add_user(username,apikey,self.get_home_dir(username))
            return
        if apikey != 'anonymous':
            user = None
            if 'web' in self.cfg and 'local_endpoint' in self.cfg['web'] and self.cfg['web']['local_endpoint']:
                user_req = requests.get(self.cfg['web']['local_endpoint'] + '/api/user/info/apikey/' + apikey)
                if not user_req.status_code == 200:
                    raise AuthenticationFailed('Wrong or failed authentication')
                user = user_req.json()
            else:
                user = BmajUser.get_user_by_apikey(apikey)
            #Add user authentification CR
            if user['id'] == username or username == "anonymous" :
                dict_bank = {}
                for db_entry in self.db.banks.find() :
                    home_dir = self.get_home_dir(username, db_entry)
                    dict_bank[home_dir] = [db_entry['properties']['visibility'], db_entry['properties']['owner']]
                self.bank = dict_bank
                #Create a new user for biomaj server with specific permission
                if not self.has_user(username):
                    self.add_user(username,apikey,self.get_home_dir(username))
                for directory in dict_bank :
                     #If the user is the bank's owner
                     if dict_bank[directory][0] == "public" :
                         perm = "elr"
                         self.override_perm(username, directory, perm, recursive=True)
                     if dict_bank[directory][1] == username and dict_bank[directory][0] != "public" :
                         perm = "elr"
                         self.override_perm(username, directory, perm, recursive=True)
                     else :
                         perm = "el"
                         self.override_perm(username, directory, perm, recursive=True)
                return
            bank = self.db.banks.find_one({'name': username})
            if not bank:
                self.logger.error('Bank not found: ' + username)
                raise AuthenticationFailed('Bank does not exists')
            if bank['properties']['visibility'] != 'public':
                if user['id'] != bank['properties']['owner']:
                    if 'members' not in bank['properties'] or user['id'] not in bank['properties']['members']:
                        raise AuthenticationFailed('Not allowed to access to this bank')
            if len(bank['production']) == 0:
                raise AuthenticationFailed('No production release available')
            self.bank = bank
            if not self.has_user(username):
                self.add_user(username,apikey,self.get_home_dir(bank))
           
{'fraction_non_alphanumeric': 0.09896302975653742, 'fraction_numerical': 0.0018034265103697023, 'mean_word_length': 2.0480934386808656, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2194822', 'n_tokens_mistral': 2340, 'n_tokens_neox': 2215, 'n_words': 595}