diff --git a/celerytask/tasks.py b/celerytask/tasks.py index 571c5b77..6fc62ffa 100755 --- a/celerytask/tasks.py +++ b/celerytask/tasks.py @@ -2,7 +2,7 @@ from django.conf import settings from celery.task import periodic_task from django.contrib.auth.models import User from django.contrib.auth.models import Group - +from notifications import notify from models import SyncGroupCache from celery.task.schedules import crontab from services.managers.openfire_manager import OpenfireManager @@ -34,14 +34,18 @@ import logging logger = logging.getLogger(__name__) def disable_member(user): + change = False logger.debug("Disabling member %s" % user) if user.user_permissions.all().exists(): logger.info("Clearning user %s permission to deactivate user." % user) user.user_permissions.clear() + change = True if user.groups.all().exists(): logger.info("Clearing user %s groups to deactivate user." % user) user.groups.clear() + change = True deactivate_services(user) + return change def is_teamspeak3_active(): return settings.ENABLE_AUTH_TEAMSPEAK3 or settings.ENABLE_BLUE_TEAMSPEAK3 @@ -272,54 +276,68 @@ def assign_corp_group(auth): auth.user.groups.remove(g) def make_member(user): + change = False logger.debug("Ensuring user %s has member permissions and groups." % user) # ensure member is not blue right now if check_if_user_has_permission(user, 'blue_member'): logger.info("Removing user %s blue permission to transition to member" % user) remove_member_permission(user, 'blue_member') + change = True blue_group, c = Group.objects.get_or_create(name=settings.DEFAULT_BLUE_GROUP) if blue_group in user.groups.all(): logger.info("Removing user %s blue group" % user) user.groups.remove(blue_group) + change = True # make member if check_if_user_has_permission(user, 'member') is False: logger.info("Adding user %s member permission" % user) add_member_permission(user, 'member') + change = True member_group, c = Group.objects.get_or_create(name=settings.DEFAULT_AUTH_GROUP) if not member_group in user.groups.all(): logger.info("Adding user %s to member group" % user) user.groups.add(member_group) + change = True auth, c = AuthServicesInfo.objects.get_or_create(user=user) if auth.is_blue: logger.info("Marking user %s as non-blue" % user) auth.is_blue = False auth.save() + change = True assign_corp_group(auth) + return change def make_blue(user): + change = False logger.debug("Ensuring user %s has blue permissions and groups." % user) # ensure user is not a member if check_if_user_has_permission(user, 'member'): logger.info("Removing user %s member permission to transition to blue" % user) remove_member_permission(user, 'blue_member') + change = True member_group, c = Group.objects.get_or_create(name=settings.DEFAULT_AUTH_GROUP) if member_group in user.groups.all(): logger.info("Removing user %s member group" % user) user.groups.remove(member_group) + change = True # make blue if check_if_user_has_permission(user, 'blue_member') is False: logger.info("Adding user %s blue permission" % user) add_member_permission(user, 'blue_member') + change = True blue_group, c = Group.objects.get_or_create(name=settings.DEFAULT_BLUE_GROUP) if not blue_group in user.groups.all(): logger.info("Adding user %s to blue group" % user) user.groups.add(blue_group) + change = True auth, c = AuthServicesInfo.objects.get_or_create(user=user) if auth.is_blue is False: logger.info("Marking user %s as blue" % user) auth.is_blue = True auth.save() + change = True assign_corp_group(auth) + return change def determine_membership_by_character(char): if settings.IS_CORP: @@ -357,14 +375,17 @@ def determine_membership_by_user(user): return False def set_state(user): + change = False state = determine_membership_by_user(user) logger.debug("Assigning user %s to state %s" % (user, state)) if state == "MEMBER": - make_member(user) + change = make_member(user) elif state == "BLUE": - make_blue(user) + change = make_blue(user) else: - disable_member(user) + change = disable_member(user) + if change: + notify(user, "Membership State Change", message="You membership state has been changed to %s" % state) # Run every minute @periodic_task(run_every=crontab(minute="*/1")) @@ -414,6 +435,7 @@ def refresh_api(api_key_pair): elif type == False: logger.info("Determined api key %s for blue user %s is no longer type account as requred." % (api_key_pair.api_id, user)) still_valid = False + notify(user, "API Failed Validation", message="Your API key ID %s is not account-wide as required." % api_key_pair.api_id, level="danger") full = EveApiManager.check_blue_api_is_full(api_key_pair.api_id, api_key_pair.api_key) if full == None: api_key_pair.error_count += 1 @@ -423,6 +445,7 @@ def refresh_api(api_key_pair): elif full == False: logger.info("Determined api key %s for blue user %s no longer meets minimum access mask as required." % (api_key_pair.api_id, user)) still_valid = False + notify(user, "API Failed Validation", message="Your API key ID %s does not meet access mask requirements." % api_key_pair.api_id, level="danger") elif state == "MEMBER": if settings.MEMBER_API_ACCOUNT: type = EveApiManager.check_api_is_type_account(api_key_pair.api_id, api_key_pair.api_key) @@ -434,6 +457,7 @@ def refresh_api(api_key_pair): elif type == False: logger.info("Determined api key %s for user %s is no longer type account as required." % (api_key_pair.api_id, user)) still_valid = False + notify(user, "API Failed Validation", message="Your API key ID %s is not account-wide as required." % api_key_pair.api_id, level="danger") full = EveApiManager.check_api_is_full(api_key_pair.api_id, api_key_pair.api_key) if full == None: api_key_pair.error_count += 1 @@ -443,14 +467,17 @@ def refresh_api(api_key_pair): elif full == False: logger.info("Determined api key %s for user %s no longer meets minimum access mask as required." % (api_key_pair.api_id, user)) still_valid = False + notify(user, "API Failed Validation", message="Your API key ID %s does not meet access mask requirements." % api_key_pair.api_id, level="danger") if still_valid == None: if api_key_pair.error_count >= 3: logger.info("API key %s has incurred 3 or more errors. Assuming invalid." % api_key_pair.api_id) still_valid = False + notify(user, "API Failed Validation", message="Your API key ID %s has accumulated too many errors during refresh and is assumed to be invalid." % api_key_pair.api_id, level="danger") if still_valid == False: logger.debug("API key %s has failed validation; it and its characters will be deleted." % api_key_pair.api_id) EveManager.delete_characters_by_api_id(api_key_pair.api_id, user.id) EveManager.delete_api_key_pair(api_key_pair.api_id, user.id) + notify(user, "API Key Deleted", message="Your API key ID %s has failed validation. It and its associated characters have been deleted." % api_key_pair.api_id, level="danger") elif still_valid == True: if api_key_pair.error_count != 0: logger.info("Clearing error count for api %s as it passed validation" % api_key_pair.api_id) @@ -473,6 +500,7 @@ def refresh_api(api_key_pair): logger.debug("API key %s is no longer valid; it and its characters will be deleted." % api_key_pair.api_id) EveManager.delete_characters_by_api_id(api_key_pair.api_id, user.id) EveManager.delete_api_key_pair(api_key_pair.api_id, user.id) + notify(user, "API Key Deleted", message="Your API key ID %s is invalid. It and its associated characters have been deleted." % api_key_pair.api_id, level="danger") # Run every 3 hours @periodic_task(run_every=crontab(minute=0, hour="*/3")) @@ -495,6 +523,7 @@ def run_api_refresh(): logger.info("User %s main character id %s missing model. Clearning main character." % (user, authserviceinfo.main_char_id)) authserviceinfo.main_char_id = '' authserviceinfo.save() + notify(user, "Main Character Reset", message="Your specified main character no longer has a model.\nThis could be the result of an invalid API\nYour main character ID has been reset." % api_key_pair.api_id, level="warn") set_state(user) def populate_alliance(id, blue=False): diff --git a/hrapplications/views.py b/hrapplications/views.py index 903121c9..998be473 100755 --- a/hrapplications/views.py +++ b/hrapplications/views.py @@ -3,7 +3,7 @@ from django.shortcuts import render_to_response from django.contrib.auth.decorators import permission_required from django.contrib.auth.decorators import login_required from django.shortcuts import HttpResponseRedirect - +from notifications import notify from models import HRApplication from models import HRApplicationComment from forms import HRApplicationForm @@ -162,6 +162,7 @@ def hr_application_remove(request, app_id): application = HRApplication.objects.get(id=app_id) if application: logger.info("Deleted HRApplication id %s on behalf of user %s" % (app_id, request.user)) + notify(application.user, "Application Deleted", message="Your application to %s was deleted.") application.delete() else: logger.error("Unable to delete HRApplication with id %s on behalf of user %s: application is NoneType" % (app_id, request.user)) @@ -183,6 +184,7 @@ def hr_application_approve(request, app_id): application.reviewer_character = EveCharacter.objects.get(character_id=auth_info.main_char_id) application.save() logger.info("HRApplication for user %s to corp %s approved by %s" % (application.user, application.corp, request.user)) + notify(application.user, "Application Accepted", message="Your application to %s has been approved." % application.corp, level="success") else: logger.error("User %s unable to approve HRApplication id %s - hrapplication with that id not found." % (request.user, app_id)) @@ -201,6 +203,7 @@ def hr_application_reject(request, app_id): application.reviewer_character = EveCharacter.objects.get(character_id=auth_info.main_char_id) application.save() logger.info("HRApplication for user %s to corp %s rejected by %s" % (application.user, application.corp, request.user)) + notify(application.user, "Application Rejected", message="Your application to %s has been rejected." % application.corp, level="danger") else: logger.error("User %s unable to reject HRApplication id %s - hrapplication with that id not found." % (request.user, app_id)) @@ -251,6 +254,7 @@ def hr_application_mark_in_progress(request, app_id): application.reviewer_inprogress_character = EveCharacter.objects.get(character_id=auth_info.main_char_id) application.save() logger.info("Marked HRApplication for user %s to corp %s in progress by user %s" % (application.user, application.corp, request.user)) + notify(application.user, "Application In Progress", message="Your application to %s is being reviewed by %s" % (application.corp, application.reviewer_inprogress_character)) else: logger.error("Unable to mark HRApplication id %s in progress by user %s - hrapplication matching id not found." % (app_id, request.user)) diff --git a/notifications/__init__.py b/notifications/__init__.py index d12a961d..3e095793 100644 --- a/notifications/__init__.py +++ b/notifications/__init__.py @@ -5,8 +5,8 @@ def notify(user, title, message=None, level='info'): notif.user = user notif.title = title if not message: - message = Title + message = title notif.message = message notif.level = level notif.save() - logger.info("Created notification %s" % notif + logger.info("Created notification %s" % notif) diff --git a/util/common_task.py b/util/common_task.py index c29f5aee..0c98406f 100755 --- a/util/common_task.py +++ b/util/common_task.py @@ -1,6 +1,6 @@ from django.contrib.auth.models import Group from django.contrib.auth.models import User - +from notifications import notify from authentication.managers import AuthServicesInfoManager from services.managers.openfire_manager import OpenfireManager from services.managers.phpbb3_manager import Phpbb3Manager @@ -39,33 +39,41 @@ def remove_user_from_group(user, groupname): def deactivate_services(user): + change = False logger.debug("Deactivating services for user %s" % user) authinfo = AuthServicesInfoManager.get_auth_service_info(user) if authinfo.mumble_username and authinfo.mumble_username != "": logger.debug("User %s has mumble account %s. Deleting." % (user, authinfo.mumble_username)) MumbleManager.delete_user(authinfo.mumble_username) AuthServicesInfoManager.update_user_mumble_info("", "", user) + change = True if authinfo.jabber_username and authinfo.jabber_username != "": logger.debug("User %s has jabber account %s. Deleting." % (user, authinfo.jabber_username)) OpenfireManager.delete_user(authinfo.jabber_username) AuthServicesInfoManager.update_user_jabber_info("", "", user) + change = True if authinfo.forum_username and authinfo.forum_username != "": logger.debug("User %s has forum account %s. Deleting." % (user, authinfo.forum_username)) Phpbb3Manager.disable_user(authinfo.forum_username) AuthServicesInfoManager.update_user_forum_info("", "", user) + change = True if authinfo.ipboard_username and authinfo.ipboard_username != "": logger.debug("User %s has ipboard account %s. Deleting." % (user, authinfo.ipboard_username)) IPBoardManager.disable_user(authinfo.ipboard_username) AuthServicesInfoManager.update_user_forum_info("", "", user) + change = True if authinfo.teamspeak3_uid and authinfo.teamspeak3_uid != "": logger.debug("User %s has mumble account %s. Deleting." % (user, authinfo.teamspeak3_uid)) Teamspeak3Manager.delete_user(authinfo.teamspeak3_uid) AuthServicesInfoManager.update_user_teamspeak3_info("", "", user) + change = True if authinfo.discord_uid and authinfo.discord_uid != "": logger.debug("User %s has discord account %s. Deleting." % (user, authinfo.discord_uid)) DiscordManager.delete_user(authinfo.discord_uid) AuthServicesInfoManager.update_user_discord_info("", user) - + change = True + if change: + notify(user, "Services Disabled", message="Your services accounts have been disabled.", level="danger") def generate_corp_group_name(corpname): return 'Corp_' + corpname.replace(' ', '_')