protected function getUserClassSelect($login_name, $organization)
    {
        $select = $this->db->select();

        /**
         * tolerance field:
         * 1 - login and organization match
         * 2 - login found in an organization with access to the requested organization
         * 3 - login found in another organization without rights to the requested organiation
         *     (could be allowed due to privilege with rights to ALL organizations)
         */
        $select->from('gems__user_logins', array("gul_user_class", 'gul_id_organization', 'gul_login'))
                ->where('gul_can_login = 1');

        if ($this->allowLoginOnWithoutOrganization && !$organization) {
            $select->columns(new \Zend_Db_Expr('1 AS tolerance'));
        } else {
            $select->from('gems__organizations', array())
                ->columns(new \Zend_Db_Expr(
                        "CASE
                            WHEN gor_id_organization = gul_id_organization THEN 1
                            WHEN gor_accessible_by LIKE CONCAT('%:', gul_id_organization, ':%') THEN 2
                            ELSE 3
                        END AS tolerance"))
                ->where('gor_active = 1')
                ->where('gor_id_organization = ?', $organization)
                ->order('tolerance');
        }
        $wheres[] = $this->db->quoteInto('gul_login = ?', $login_name);
        $isEmail  = \MUtil_String::contains($login_name, '@');

        if ($isEmail && $this->allowStaffEmailLogin) {
            $rows = $this->db->fetchAll(
                    "SELECT gsf_login, gsf_id_organization FROM gems__staff WHERE gsf_email = ?",
                    $login_name
                    );
            if ($rows) {
                foreach ($rows as $row) {
                    $wheres[] = $this->db->quoteInto('gul_login = ? AND ', $row['gsf_login'])
                            . $this->db->quoteInto('gul_id_organization = ?', $row['gsf_id_organization']);
                }
            }
        }
        if ($isEmail && $this->allowRespondentEmailLogin) {
            $rows = $this->db->fetchAll(
                    "SELECT gr2o_patient_nr, gr2o_id_organization FROM gems__respondent2org  "
                    . "INNER JOIN gems__respondents WHERE gr2o_id_user = grs_id_user AND gr2o_email = ?",
                    $login_name
                    );
            if ($rows) {
                foreach ($rows as $row) {
                    $wheres[] = $this->db->quoteInto('gul_login = ? AND ', $row['gr2o_patient_nr'])
                            . $this->db->quoteInto('gul_id_organization = ?', $row['gr2o_id_organization']);
                }
            }
        }
        // Add search fields
        $select->where(new \Zend_Db_Expr('(' . implode(') OR (', $wheres) . ')'));
        // \MUtil_Echo::track($select->__toString());

        return $select;
    }