allianceauth/util/__init__.py
Joakim Strandberg b190b8e191 Adding "Paplink" feature (#401)
* Initial testing of paplink functionality. More fancy interfaces coming.

* Removed a invalid view reference.

* Added a link on the front page.

* Fixed some bad references and incorrect in game browser header usages.

* Started work on statistics-pages.

* Added an initial modify-paplink page where the pap itself can be deleted and characters removed.

* Added a very simple statistics page. Also some name change for ~reasons~.

* Small but crucial fix of syntax.

* Added personal statistics page.

* Corputils page now include fatlinkstats.

* Added link to the personal statistics page. Moved other buttons for clarity.

* Removed some unused code and imports

* Added more statistics, and all corps in alliance are now visible even if no paps are registered.

* Now requesting trust for the right domain. And some redundant imports and commented lines are removed.
2016-04-29 18:00:45 -05:00

78 lines
4.4 KiB
Python
Executable File

import uuid
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import User
from django.contrib.auth.models import Group
from django.contrib.auth.models import Permission
from django.conf import settings
import logging
logger = logging.getLogger(__name__)
def bootstrap_permissions():
ct = ContentType.objects.get_for_model(User)
Permission.objects.get_or_create(codename="member", content_type=ct, name="member")
Permission.objects.get_or_create(codename="group_management", content_type=ct, name="group_management")
Permission.objects.get_or_create(codename="jabber_broadcast", content_type=ct, name="jabber_broadcast")
Permission.objects.get_or_create(codename="jabber_broadcast_all", content_type=ct, name="jabber_broadcast_all")
Permission.objects.get_or_create(codename="fleetactivitytracking", content_type=ct, name="fleetactivitytracking")
Permission.objects.get_or_create(codename="fleetactivitytracking_statistics", content_type=ct, name="fleetactivitytracking_statistics")
Permission.objects.get_or_create(codename="human_resources", content_type=ct, name="human_resources")
Permission.objects.get_or_create(codename="blue_member", content_type=ct, name="blue_member")
Permission.objects.get_or_create(codename="alliance_apis", content_type=ct, name="alliance_apis")
Permission.objects.get_or_create(codename="corp_apis", content_type=ct, name="corp_apis")
Permission.objects.get_or_create(codename="timer_management", content_type=ct, name="timer_management")
Permission.objects.get_or_create(codename="timer_view", content_type=ct, name="timer_view")
Permission.objects.get_or_create(codename="srp_management", content_type=ct, name="srp_management")
Permission.objects.get_or_create(codename="signature_management", content_type=ct, name="signature_management")
Permission.objects.get_or_create(codename="signature_view", content_type=ct, name="signature_view")
Permission.objects.get_or_create(codename="optimer_management", content_type=ct, name="optimer_management")
Permission.objects.get_or_create(codename="optimer_view", content_type=ct, name="optimer_view")
Permission.objects.get_or_create(codename="logging_notifications", content_type=ct, name="logging_notifications")
Group.objects.get_or_create(name=settings.DEFAULT_AUTH_GROUP)
Group.objects.get_or_create(name=settings.DEFAULT_BLUE_GROUP)
logger.info("Bootstrapped permissions for auth and created default groups.")
def add_member_permission(user, permission):
logger.debug("Adding permission %s to member %s" % (permission, user))
ct = ContentType.objects.get_for_model(User)
stored_permission, created = Permission.objects.get_or_create(codename=permission,
content_type=ct, name=permission)
user = User.objects.get(username=user.username)
user.user_permissions.add(stored_permission)
logger.info("Added permission %s to user %s" % (permission, user))
user.save()
def remove_member_permission(user, permission):
logger.debug("Removing permission %s from member %s" % (permission, user))
ct = ContentType.objects.get_for_model(User)
stored_permission, created = Permission.objects.get_or_create(codename=permission,
content_type=ct, name=permission)
user = User.objects.get(username=user.username)
if user.has_perm('auth.' + permission):
user.user_permissions.remove(stored_permission)
user.save()
logger.info("Removed permission %s from member %s" % (permission, user))
else:
logger.warn("Attempting to remove permission user %s does not have: %s" % (user, permission))
def check_if_user_has_permission(user, permission):
ct = ContentType.objects.get_for_model(User)
stored_permission, created = Permission.objects.get_or_create(codename=permission,
content_type=ct, name=permission)
return user.has_perm('auth.' + permission)
def random_string(string_length=10):
"""Returns a random string of length string_length."""
random = str(uuid.uuid4()) # Convert UUID format to a Python string.
random = random.upper() # Make all characters uppercase.
random = random.replace("-", "") # Remove the UUID '-'.
return random[0:string_length] # Return the random string.