Advertisement
Kovitikus

temp account new room

Jan 11th, 2020
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.78 KB | None | 0 0
  1.     @classmethod
  2.     def create(cls, *args, **kwargs):
  3.         """
  4.        Creates an Account (or Account/Character pair for MULTISESSION_MODE<2)
  5.        with default (or overridden) permissions and having joined them to the
  6.        appropriate default channels.
  7.        Kwargs:
  8.            username (str): Username of Account owner
  9.            password (str): Password of Account owner
  10.            email (str, optional): Email address of Account owner
  11.            ip (str, optional): IP address of requesting connection
  12.            guest (bool, optional): Whether or not this is to be a Guest account
  13.            permissions (str, optional): Default permissions for the Account
  14.            typeclass (str, optional): Typeclass to use for new Account
  15.            character_typeclass (str, optional): Typeclass to use for new char
  16.                when applicable.
  17.        Returns:
  18.            account (Account): Account if successfully created; None if not
  19.            errors (list): List of error messages in string form
  20.        """
  21.  
  22.         account = None
  23.         errors = []
  24.  
  25.         username = kwargs.get('username')
  26.         password = kwargs.get('password')
  27.         email = kwargs.get('email', '').strip()
  28.         guest = kwargs.get('guest', False)
  29.  
  30.         permissions = kwargs.get('permissions', settings.PERMISSION_ACCOUNT_DEFAULT)
  31.         typeclass = kwargs.get('typeclass', cls)
  32.  
  33.         ip = kwargs.get('ip', '')
  34.         if ip and CREATION_THROTTLE.check(ip):
  35.             errors.append("You are creating too many accounts. Please log into an existing account.")
  36.             return None, errors
  37.  
  38.         # Normalize username
  39.         username = cls.normalize_username(username)
  40.  
  41.         # Validate username
  42.         if not guest:
  43.             valid, errs = cls.validate_username(username)
  44.             if not valid:
  45.                 # this echoes the restrictions made by django's auth
  46.                 # module (except not allowing spaces, for convenience of
  47.                 # logging in).
  48.                 errors.extend(errs)
  49.                 return None, errors
  50.  
  51.         # Validate password
  52.         # Have to create a dummy Account object to check username similarity
  53.         valid, errs = cls.validate_password(password, account=cls(username=username))
  54.         if not valid:
  55.             errors.extend(errs)
  56.             return None, errors
  57.  
  58.         # Check IP and/or name bans
  59.         banned = cls.is_banned(username=username, ip=ip)
  60.         if banned:
  61.             # this is a banned IP or name!
  62.             string = "|rYou have been banned and cannot continue from here." \
  63.                      "\nIf you feel this ban is in error, please email an admin.|x"
  64.             errors.append(string)
  65.             return None, errors
  66.  
  67.         # everything's ok. Create the new account.
  68.         try:
  69.             try:
  70.                 account = create.create_account(username, email, password, permissions=permissions, typeclass=typeclass)
  71.                 logger.log_sec(f'Account Created: {account} (IP: {ip}).')
  72.  
  73.             except Exception as e:
  74.                 errors.append("There was an error creating the Account. If this problem persists, contact an admin.")
  75.                 logger.log_trace()
  76.                 return None, errors
  77.  
  78.             # This needs to be set so the engine knows this account is
  79.             # logging in for the first time. (so it knows to call the right
  80.             # hooks during login later)
  81.             account.db.FIRST_LOGIN = True
  82.  
  83.             # Record IP address of creation, if available
  84.             if ip:
  85.                 account.db.creator_ip = ip
  86.  
  87.             # join the new account to the public channel
  88.             pchannel = ChannelDB.objects.get_channel(settings.DEFAULT_CHANNELS[0]["key"])
  89.             if not pchannel or not pchannel.connect(account):
  90.                 string = f"New account '{account.key}' could not connect to public channel!"
  91.                 errors.append(string)
  92.                 logger.log_err(string)
  93.  
  94.             if account and settings.MULTISESSION_MODE < 2:
  95.                 # Load the appropriate Character class
  96.                 character_typeclass = kwargs.get('character_typeclass', settings.BASE_CHARACTER_TYPECLASS)
  97.                 Character = class_from_module(character_typeclass)
  98.                 name = account.key
  99.                 possessive = '\'' if name[-1] == 's' else '\'s'
  100.                 homeroom = create_object(typeclass='typeclasses.rooms.OOC_Quarters',
  101.                                 key=f"{name}{possessive} Quarters")
  102.                 # Create the character
  103.                 character, errs = Character.create(
  104.                     account.key, account, ip=ip, typeclass=character_typeclass,
  105.                     permissions=permissions)
  106.                 errors.extend(errs)
  107.  
  108.                 if character:
  109.                     # Update playable character list
  110.                     if character not in account.characters:
  111.                         account.db._playable_characters.append(character)
  112.  
  113.                     # We need to set this to have @ic auto-connect to this character
  114.                     account.db._last_puppet = character
  115.                     character.home = homeroom
  116.  
  117.         except Exception:
  118.             # We are in the middle between logged in and -not, so we have
  119.             # to handle tracebacks ourselves at this point. If we don't,
  120.             # we won't see any errors at all.
  121.             errors.append("An error occurred. Please e-mail an admin if the problem persists.")
  122.             logger.log_trace()
  123.  
  124.         # Update the throttle to indicate a new account was created from this IP
  125.         if ip and not guest:
  126.             CREATION_THROTTLE.update(ip, 'Too many accounts being created.')
  127.         SIGNAL_ACCOUNT_POST_CREATE.send(sender=account, ip=ip)
  128.         return account, errors
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement