def get_user_groups(current_account, accounts, user, user_groups):
    """
        :param current_account: The account for this request
        :param accounts: All accounts this user has access to
        :param user: The username of the user
        :param user_groups All the groups this user is part of
        :return: a comma separated string of group names. The group names are
                 as follows: account,account_user,groups...
                 If .admin is in the groups, this is replaced by all the
                 possible account ids. For example, for user joe, account acct
                 and resellers AUTH_, OTHER_, the returned string is as
                 follows: acct,acct:joe,AUTH_acct,OTHER_acct
        """
    groups = [current_account, user]
    groups.extend(user_groups)
    if '.admin' in groups:
        groups.remove('.admin')
        for account in accounts:
            groups.append(account)
    groups = ','.join(groups)
    return groups