def update_character_associations(user): LOG.debug("Updating character ownerships for '%s'..." % user.username) eve_accounts = [] invalid_api_keys = [] new_characters = set() new_corps = set() skills = [] # get all the user's registered api credentials for api_key in user.eve_accounts.all(): try: conn = api.connect_user(api_key) for char in api.get_account_characters(api_key): try: member = Member.objects.get(characterID=char.characterID) except Member.DoesNotExist: member = Member(characterID=char.characterID, name=char.name) char_info = conn.eve.CharacterInfo( characterID=member.characterID) set_char_info_attributes(member, char_info) # This also sets employment history. # TODO 1 move employment history into separate function # TODO 2 optimizie; only check if corp has changed, and add history records in save_all below. sheet = conn.char.CharacterSheet( characterID=member.characterID) set_extended_char_attributes(member, sheet) skills.extend(get_character_skills(member, sheet)) if not member in new_characters: corp = get_corp(char.corporationID) if member.corp != corp: member.corp = corp new_corps.add(corp) new_characters.add(member) api_key.is_valid = True except api.AuthenticationError, e: LOG.warning("%s (user: '******' keyID: %d)" % (str(e), user.username, api_key.keyID)) api_key.is_valid = False api_key.error = str(e) invalid_api_keys.append(api_key) except api.Error, e: # for all other errors, we abort the operation so that # character associations are not deleted by mistake and # therefore, that users find themselves with no access :) LOG.error("%d: %s (user: '******' keyID: %d)" % (e.code, str(e), user.username, api_key.keyID)) raise
def clean(self): cleaned_data = self.cleaned_data keyID = cleaned_data.get("keyID") vCode = cleaned_data.get("vCode") if keyID is not None and vCode is not None: # test if API credentials are valid try: self.characters = api.get_account_characters(UserAPIKey(keyID=keyID, vCode=vCode)) except api.Error, e: self._errors["keyID"] = self.error_class([str(e)]) self._errors["vCode"] = self.error_class([str(e)]) del cleaned_data["keyID"] del cleaned_data["vCode"]
def update_character_associations(user): LOG.debug("Updating character ownerships for '%s'..." % user.username) eve_accounts = [] invalid_api_keys = [] new_characters = set() new_corps = set() skills = [] # get all the user's registered api credentials for api_key in user.eve_accounts.all(): try: conn = api.connect_user(api_key) for char in api.get_account_characters(api_key): try: member = Member.objects.get(characterID=char.characterID) except Member.DoesNotExist: member = Member(characterID=char.characterID, name=char.name) char_info = conn.eve.CharacterInfo(characterID=member.characterID) set_char_info_attributes(member, char_info) # This also sets employment history. # TODO 1 move employment history into separate function # TODO 2 optimizie; only check if corp has changed, and add history records in save_all below. sheet = conn.char.CharacterSheet(characterID=member.characterID) set_extended_char_attributes(member, sheet) skills.extend(get_character_skills(member, sheet)) if not member in new_characters: corp = get_corp(char.corporationID) if member.corp != corp: member.corp = corp new_corps.add(corp) new_characters.add(member) api_key.is_valid = True except api.AuthenticationError, e: LOG.warning("%s (user: '******' keyID: %d)" % (str(e), user.username, api_key.keyID)) api_key.is_valid = False api_key.error = str(e) invalid_api_keys.append(api_key) except api.Error, e: # for all other errors, we abort the operation so that # character associations are not deleted by mistake and # therefore, that users find themselves with no access :) LOG.error("%d: %s (user: '******' keyID: %d)" % (e.code, str(e), user.username, api_key.keyID)) raise
def clean(self): cleaned_data = self.cleaned_data keyID = cleaned_data.get("keyID") vCode = cleaned_data.get("vCode") username = cleaned_data.get("username") email = cleaned_data.get("email") password1 = cleaned_data.get("password1") password2 = cleaned_data.get("password2") if not None in (keyID, vCode, username, email, password1, password2): # test if both password fields match if not password1 == password2: self._errors["password2"] = self.error_class([_("Passwords don't match")]) del cleaned_data["password2"] # test if API credentials are valid try: self.characters = api.get_account_characters(UserAPIKey(keyID=keyID, vCode=vCode)) except api.Error, e: self._errors["keyID"] = self.error_class([str(e)]) self._errors["vCode"] = self.error_class([str(e)]) del cleaned_data["keyID"] del cleaned_data["vCode"]
def authenticate(self, username=None, password=None): """Authenticate user against phpBB3 database. Check if the user exists in Django users. If not, create it. Then authenticate.""" logging.debug("PhpbbBackend::authenticate()") user = None try: phpbb_user = PhpbbUser.objects.get(username=username) except PhpbbUser.DoesNotExist: # The user does not exist in phpBB. Bailing out. logging.info("User '%s' doesn't exist." % username) return None phpbb_checker = php_password.PhpbbPassword() if phpbb_checker.phpbb_check_hash(password, phpbb_user.user_password): logging.debug("User %s successfully authenticated " "with phpBB database." % username) else: # Invalid password logging.info("Wrong password for user %s" % username) return None # At this point we have successfully checked phpBB user password. # Now we're getting and returning Django user. If necessary, we're # creating the user on the fly. try: user = User.objects.get(username=username) except User.DoesNotExist: logging.info("Creating new Django user '%s'" % username) if username: user = User(username=username, password="") user.is_staff = False user.is_superuser = False user.email = phpbb_user.user_email user.save() # Do the initial update of the user's characters characters = api.get_account_characters( UserAPIKey(keyID=phpbb_user.eveapi_keyid, vCode=phpbb_user.eveapi_vcode)) members, corps = init_characters(user, characters) for corp in corps: corp.save() for member in members: member.save() # Give the new user roles/groups: update_user_accesses(user) else: logging.warning("User name empty. Not creating.") return None # In case the phpBB password has changed, we're updating user's # Django password. Django password is necessary when user wants to log # in to the admin interface. user.set_password(password) # Update the API information always to allow changes from phpBB user_api = UserAPIKey() user_api.keyID = phpbb_user.eveapi_keyid user_api.vCode = phpbb_user.eveapi_vcode user_api.user = user user_api.save() logging.debug("Returning user '%s'" % user) return user