'Check if this credential has already been added to the database, if not add it in.'

def add_credential(self, credtype, domain, username, password, groupid=None, pillaged_from=None):

    domain = domain.split('.')[0].upper()
    user_rowid = None
    cur = self.conn.cursor()
    if (groupid and (not self.is_group_valid(groupid))):
        cur.close()
        return
    if (pillaged_from and (not self.is_computer_valid(pillaged_from))):
        cur.close()
        return
    cur.execute('SELECT    *    FROM    users    WHERE    LOWER(domain)=LOWER(?)    AND    LOWER(username)=LOWER(?)', [domain, username])
    results = cur.fetchall()
    if (not len(results)):
        cur.execute('INSERT    INTO    users    (domain,    username,    password,    credtype,    pillaged_from_computerid)    VALUES    (?,?,?,?,?)', [domain, username, password, credtype, pillaged_from])
        user_rowid = cur.lastrowid
        if groupid:
            cur.execute('INSERT    INTO    group_relations    (userid,    groupid)    VALUES    (?,?)', [user_rowid, groupid])
    else:
        for user in results:
            if ((not user[3]) and (not user[4]) and (not user[5])):
                cur.execute('UPDATE    users    SET    password=?,    credtype=?,    pillaged_from_computerid=?    WHERE    id=?', [password, credtype, pillaged_from, user[0]])
                user_rowid = cur.lastrowid
                if (groupid and (not len(self.get_group_relations(user_rowid, groupid)))):
                    cur.execute('INSERT    INTO    group_relations    (userid,    groupid)    VALUES    (?,?)', [user_rowid, groupid])
    cur.close()
    logging.debug('add_credential(credtype={},    domain={},    username={},    password={},    groupid={},    pillaged_from={})    =>    {}'.format(credtype, domain, username, password, groupid, pillaged_from, user_rowid))
    return user_rowid
