Make new users properly auto-join default channels. Resolve #3239

This commit is contained in:
Griatch 2023-08-06 21:19:13 +02:00
parent 84f609fc6f
commit 9f08b6c9df
2 changed files with 60 additions and 37 deletions

View file

@ -14,13 +14,13 @@ import re
import time import time
from random import getrandbits from random import getrandbits
import evennia
from django.conf import settings from django.conf import settings
from django.contrib.auth import authenticate, password_validation from django.contrib.auth import authenticate, password_validation
from django.core.exceptions import ImproperlyConfigured, ValidationError from django.core.exceptions import ImproperlyConfigured, ValidationError
from django.utils import timezone from django.utils import timezone
from django.utils.module_loading import import_string from django.utils.module_loading import import_string
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
import evennia
from evennia.accounts.manager import AccountManager from evennia.accounts.manager import AccountManager
from evennia.accounts.models import AccountDB from evennia.accounts.models import AccountDB
from evennia.commands.cmdsethandler import CmdSetHandler from evennia.commands.cmdsethandler import CmdSetHandler
@ -854,13 +854,28 @@ class DefaultAccount(AccountDB, metaclass=TypeclassBase):
if ip: if ip:
account.db.creator_ip = ip account.db.creator_ip = ip
# join the new account to the public channel # join the new account to the public channels
pchannel = ChannelDB.objects.get_channel(settings.DEFAULT_CHANNELS[0]["key"]) pchannel = ChannelDB.objects.get_channel(settings.DEFAULT_CHANNELS[0]["key"])
if not pchannel or not pchannel.connect(account): if not pchannel or not pchannel.connect(account):
string = "New account '{account.key}' could not connect to public channel!" string = "New account '{account.key}' could not connect to public channel!"
errors.append(string) errors.append(string)
logger.log_err(string) logger.log_err(string)
# join the new account to the public channels
for chan_info in settings.DEFAULT_CHANNELS:
if chankey := chan_info.get("key"):
channel = ChannelDB.objects.get_channel(chankey)
if not channel or not (
channel.access(account, "listen") and channel.connect(account)
):
string = (
f"New account '{account.key}' could not connect to default channel"
f" '{chankey}'!"
)
logger.log_err(string)
else:
logger.log_err(f"Default channel '{chan_info}' is missing a 'key' field!")
if account and _AUTO_CREATE_CHARACTER_WITH_ACCOUNT: if account and _AUTO_CREATE_CHARACTER_WITH_ACCOUNT:
# Auto-create a character to go with this account # Auto-create a character to go with this account

View file

@ -6,9 +6,8 @@ import datetime
import re import re
from codecs import lookup as codecs_lookup from codecs import lookup as codecs_lookup
from django.conf import settings
import evennia import evennia
from django.conf import settings
from evennia.commands.cmdhandler import CMD_LOGINSTART from evennia.commands.cmdhandler import CMD_LOGINSTART
from evennia.comms.models import ChannelDB from evennia.comms.models import ChannelDB
from evennia.utils import class_from_module, create, gametime, logger, utils from evennia.utils import class_from_module, create, gametime, logger, utils
@ -490,40 +489,49 @@ def _create_account(session, accountname, password, permissions, typeclass=None,
# hooks during login later) # hooks during login later)
new_account.db.FIRST_LOGIN = True new_account.db.FIRST_LOGIN = True
# join the new account to the public channel # join the new account to the public channels
pchannel = ChannelDB.objects.get_channel(settings.DEFAULT_CHANNELS[0]["key"]) for chan_info in settings.DEFAULT_CHANNELS:
if not pchannel or not pchannel.connect(new_account): if chankey := chan_info.get("key"):
string = "New account '%s' could not connect to public channel!" % new_account.key channel = ChannelDB.objects.get_channel(chankey)
logger.log_err(string) if not channel or not (
channel.access(new_account, "listen") and channel.connect(new_account)
):
string = (
f"New account '{new_account.key}' could not connect to default channel"
f" '{chankey}'!"
)
logger.log_err(string)
else:
logger.log_err(f"Default channel '{chan_info}' is missing a 'key' field!")
return new_account return new_account
def _create_character(session, new_account, typeclass, home, permissions):
"""
Helper function, creates a character based on an account's name.
This is meant for Guest and AUTO_CREATRE_CHARACTER_WITH_ACCOUNT=True situations.
"""
try:
new_character = create.create_object(
typeclass, key=new_account.key, home=home, permissions=permissions
)
# set playable character list
new_account.db._playable_characters.append(new_character)
def _create_character(session, new_account, typeclass, home, permissions): # allow only the character itself and the account to puppet this character (and Developers).
""" new_character.locks.add(
Helper function, creates a character based on an account's name. "puppet:id(%i) or pid(%i) or perm(Developer) or pperm(Developer)"
This is meant for Guest and AUTO_CREATRE_CHARACTER_WITH_ACCOUNT=True situations. % (new_character.id, new_account.id)
""" )
try:
new_character = create.create_object(
typeclass, key=new_account.key, home=home, permissions=permissions
)
# set playable character list
new_account.db._playable_characters.append(new_character)
# allow only the character itself and the account to puppet this character (and Developers). # If no description is set, set a default description
new_character.locks.add( if not new_character.db.desc:
"puppet:id(%i) or pid(%i) or perm(Developer) or pperm(Developer)" new_character.db.desc = "This is a character."
% (new_character.id, new_account.id) # We need to set this to have ic auto-connect to this character
) new_account.db._last_puppet = new_character
except Exception as e:
# If no description is set, set a default description session.msg(
if not new_character.db.desc: "There was an error creating the Character:\n%s\n If this problem persists, contact"
new_character.db.desc = "This is a character." " an admin." % e
# We need to set this to have ic auto-connect to this character )
new_account.db._last_puppet = new_character logger.log_trace()
except Exception as e:
session.msg(
"There was an error creating the Character:\n%s\n If this problem persists, contact an"
" admin." % e
)
logger.log_trace()