def unregisterUser(self, user):
        """
        Deletes user, disconnection

        @param user: channel-id (ip, port)
        @type user: tuple
        """
        userLogin = None
        for cur_user in self.usersConnected:
            if self.usersConnected[cur_user]['address'] == user:
                userLogin = self.usersConnected[cur_user]['profile']['login']
                break
        if userLogin is None:
            self.trace('client %s not connected' % str(user))
        else:
            user_removed = self.usersConnected.pop(userLogin)
            self.info("Conn id %s: User (%s,  %s) unregistered" %
                      (user_removed['connection-id'], user_removed['address'],
                       userLogin))

            # update db
            UsersManager.instance().setOnlineStatus(login=userLogin,
                                                    online=False)
            UsersManager.instance().addStats(
                user=userLogin,
                connId=user_removed['connection-id'],
                startTime=user_removed['connected-at'],
                duration=time.time() - user_removed['connected-at'])
    def registerUser(self, user):
        """
        Adds new user, new connection

        @param user: user description
        @type user: dict
        """
        connStart = time.time()
        connId = self.getUniqueId()
        # user = { 'address' : client, <user>:{},  'profile': <user profile> }
        self.usersConnected[user['profile']['login']] = user
        self.usersConnected[user['profile']
                            ['login']]['connected-at'] = connStart
        self.usersConnected[user['profile']['login']]['connection-id'] = connId
        self.info(
            "User Registered: ConnectionID=%s PrivateAddress=%s Login=%s" %
            (connId, user['address'], user['profile']['login']))

        # update db
        UsersManager.instance().setOnlineStatus(login=user['profile']['login'],
                                                online=True)
        UsersManager.instance().addStats(user=user['profile']['login'],
                                         connId=connId,
                                         startTime=connStart,
                                         duration=0)

        return True
    def apiAuthorizationV2(self, authorization):
        """
        Check authorization for rest api
        New version in v17
        """
        if authorization.startswith("Basic "):
            try:
                encoded = authorization.split("Basic ")[1].strip()

                decoded = base64.b64decode(encoded)
                apikey_id, apikey_secret = decoded.rsplit(":", 1)

                usersDb = UsersManager.instance().cache()
                userOk = None
                for user, profile in usersDb.items():
                    if profile["apikey_id"] == apikey_id and profile[
                            "apikey_secret"] == apikey_secret:
                        userOk = profile
                        break
                return userOk

            except Exception as e:
                self.error("unable to decode authorization: %s" % e)
            return None
        return None
    def unregisterChannelUser(self, login):
        """
        Force channel disconnection
        """
        self.info("Unregister user Login=%s" % login)
        UsersManager.instance().setOnlineStatus(login=login, online=False)
        if not login in self.usersConnected:
            self.trace("unregister user from api, user %s not found" % login)
            return self.CODE_NOT_FOUND
        else:
            userProfile = self.usersConnected[login]

            # close the network link with the client if exists
            if userProfile['address'] in ESI.instance().clients:
                ESI.instance().stopClient(client=userProfile['address'])
            else:
                user_removed = self.usersConnected.pop(login)
                del user_removed

        return self.CODE_OK
    def apiAuthorization(self, login, password):
        """
        Check authorization for rest api
        """
        self.trace('Rest authorization called for Login=%s' % (login))
        expires = ''

        # check if this login exists on the database
        usersDb = UsersManager.instance().cache()
        if not login in usersDb:
            self.trace("Login=%s account not found" % login)
            return (self.CODE_NOT_FOUND, expires)

        user_profile = usersDb[login]

        # account disable ?
        if not user_profile['active']:
            self.trace("%s account not active" % login)
            return (self.CODE_DISABLED, expires)

        # check password, create a sha1 hash with salt: sha1( salt + sha1(password) )
        sha1 = hashlib.sha1()
        _pwd = "%s%s" % (Settings.get('Misc', 'salt'), password)
        sha1.update(_pwd.encode('utf8'))
        if user_profile['password'] != sha1.hexdigest():
            self.trace("incorrect password for %s account" % login)
            return (self.CODE_FAILED, expires)

        session_id = self.generateSessionid()
        user_profile['last_activity'] = time.time()

        lease = int(Settings.get('Users_Session',
                                 'max-expiry-age'))  #in seconds
        end = time.gmtime(user_profile['last_activity'] + lease)
        expires = time.strftime("%a, %d-%b-%Y %T GMT", end)

        self.userSessions.update({session_id: user_profile})

        self.trace('Rest authorized for Login=%s SessionId=%s Expires=%s' %
                   (login, session_id, expires))
        return (session_id, expires)
    def getStats(self, b64=False):
        """
        Constructs some statistics on tas server
            - Space disk
            - Lines in database by table 

        @return: server statistics
        @rtype: dict
        """
        ret = {}
        nbLinesTableUsers = 0
        try:
            ret['disk-usage'] = self.diskUsage(p=Settings.getDirExec())
            ret['disk-usage-logs'] = self.getSize(
                folder="%s/%s" %
                (Settings.getDirExec(), Settings.get('Paths', 'logs')))
            ret['disk-usage-tmp'] = self.getSize(
                folder="%s/%s" %
                (Settings.getDirExec(), Settings.get('Paths', 'tmp')))
            ret['disk-usage-testresults'] = self.getSize(
                folder="%s/%s" %
                (Settings.getDirExec(), Settings.get('Paths', 'testsresults')))
            ret['disk-usage-adapters'] = self.getSize(
                folder="%s/%s" %
                (Settings.getDirExec(), Settings.get('Paths', 'adapters')))
            ret['disk-usage-libraries'] = self.getSize(
                folder="%s/%s" %
                (Settings.getDirExec(), Settings.get('Paths', 'libraries')))
            ret['disk-usage-backups'] = self.getSize(
                folder="%s/%s" %
                (Settings.getDirExec(), Settings.get('Paths', 'backups')))
            ret['disk-usage-tests'] = self.getSize(
                folder="%s/%s" %
                (Settings.getDirExec(), Settings.get('Paths', 'tests')))

            # nbLinesTableUsers = UsersManager.instance().getNbOfUsers()
            ret['nb-line-table-users'] = len(UsersManager.instance().cache())
            # self.trace( "Nb [usr=%s]" % nbLinesTableUsers)

            nbTests = StatsManager.instance().getNbTests()
            self.trace(
                "Nb tests executed [sc=%s], [tg=%s], [tp=%s], [ts=%s], [tu=%s], [ta=%s], [tc=%s]"
                % (int(nbTests[0]['nbsc']), int(nbTests[0]['nbtg']),
                   int(nbTests[0]['nbtp']), int(nbTests[0]['nbts']),
                   int(nbTests[0]['nbtu']), int(
                       nbTests[0]['nbta']), int(nbTests[0]['nbtc'])))

            ret['nb-line-table-scriptsstats'] = int(nbTests[0]['nbsc'])
            ret['nb-line-table-testplansstats'] = int(nbTests[0]['nbtp'])
            ret['nb-line-table-testsuitesstats'] = int(nbTests[0]['nbts'])
            ret['nb-line-table-testunitsstats'] = int(nbTests[0]['nbtu'])
            ret['nb-line-table-testabstractsstats'] = int(nbTests[0]['nbta'])
            ret['nb-line-table-testglobalsstats'] = int(nbTests[0]['nbtg'])
            ret['nb-line-table-testcasesstats'] = int(nbTests[0]['nbtc'])

            ret['nb-line-total'] =  nbLinesTableUsers + int(nbTests[0]['nbsc']) + int(nbTests[0]['nbtc']) \
                                    + int(nbTests[0]['nbts']) + int(nbTests[0]['nbtp']) + int(nbTests[0]['nbtu']) \
                                    + int(nbTests[0]['nbtg']) + int(nbTests[0]['nbta'])
        except Exception as e:
            self.error("unable get statistics server: %s" % e)

        return ret