This methods makes sure that:

        1.) All neccessary user groups exist.
        2.) All these groups have the right database permissions assigned.
        3.) Tutors have backend rights.
        4.) Course owners have backend rights.
        5.) Students have no backend rights.

        The permission objects were already generated by the Django database initialization,
        so we can assume them to be given.

        This method is idempotent and does not touch manually assigned permissions.

        The actitivities are wrapped in transactions, since they might be triggered
        by a signal handler in the middle of some other transaction. The latter - when
        some change happens - may crash the app on PostgreSQL.

def check_permission_system():
    '''
        This methods makes sure that:

        1.) All neccessary user groups exist.
        2.) All these groups have the right database permissions assigned.
        3.) Tutors have backend rights.
        4.) Course owners have backend rights.
        5.) Students have no backend rights.

        The permission objects were already generated by the Django database initialization,
        so we can assume them to be given.

        This method is idempotent and does not touch manually assigned permissions.

        The actitivities are wrapped in transactions, since they might be triggered
        by a signal handler in the middle of some other transaction. The latter - when
        some change happens - may crash the app on PostgreSQL.
    '''
    tutor_perms = ("change_submission",
                   "delete_submission",
                   "add_submissionfile",
                   "change_submissionfile",
                   "delete_submissionfile")
    owner_perms = ("add_assignment",
                   "change_assignment",
                   "delete_assignment",
                   "add_course",
                   "change_course",
                   "delete_course",
                   "add_grading",
                   "change_grading",
                   "delete_grading",
                   "add_gradingscheme",
                   "change_gradingscheme",
                   "delete_gradingscheme",
                   "add_studyprogram",
                   "change_studyprogram",
                   "delete_studyprogram",
                   "change_submission",
                   "delete_submission",
                   "add_submissionfile",
                   "change_submissionfile",
                   "delete_submissionfile")

    logger.debug("Starting check of permission system ...")

    try:
        with transaction.atomic():
            # Give all tutor users staff rights and add them to the tutors permission group
            tutors = User.objects.filter(courses_tutoring__isnull=False)
            tutors.update(is_staff=True)
    except Exception as e:
        logger.error("Error while checking tutor staff permissions: " + str(e))

    try:
        with transaction.atomic():
            # If the app crashes here, you may have duplicate group objects, which must be fixed manually in the DB.
            tutor_group, created = Group.objects.get_or_create(
                name=STUDENT_TUTORS_GROUP_NAME)
            # If the app crashes here, you may have duplicate permission objects, which must be fixed manually in the DB.
            tutor_group.permissions = [Permission.objects.get(
                codename=perm) for perm in tutor_perms]
            tutor_group.user_set.add(*tutors)
            tutor_group.save()
    except Exception as e:
        logger.error("Error while checking tutor group permissions: " + str(e))

    try:
        with transaction.atomic():
            # Give all course owner users staff rights and add them to the course owners permission group
            owners = User.objects.filter(courses__isnull=False)
            owners.update(is_staff=True)
    except Exception as e:
        logger.error("Error while checking course owner staff permissions: " + str(e))

    try:
        with transaction.atomic():
            # If the app crashes here, you may have duplicate group objects, which must be fixed manually in the DB.
            owner_group, created = Group.objects.get_or_create(
                name=COURSE_OWNERS_GROUP_NAME)
            # If the app crashes here, you may have duplicate permission objects, which must be fixed manually in the DB.
            owner_group.permissions = [Permission.objects.get(
                codename=perm) for perm in owner_perms]
            owner_group.user_set.add(*owners)
            owner_group.save()
    except Exception as e:
        logger.error("Error while checking course owner group permissions: " + str(e))

    try:
        with transaction.atomic():
            # Make sure that pure students (no tutor, no course owner, no superuser) have no backend access at all
            pure_students = User.objects.filter(
                courses__isnull=True, courses_tutoring__isnull=True, is_superuser=False)
            pure_students.update(is_staff=False)
    except Exception as e:
        logger.error("Error while checking student permissions: " + str(e))

    try:
        with transaction.atomic():
            # Read the admin email address from the configuration and make sure that he gets admin rights
            # This is mainly needed for fresh installation, so that the admin has full power after his first
            # regular (social) login
            conffile_admin = User.objects.get(email=settings.ADMIN_EMAIL)
            make_admin(conffile_admin)
    except ObjectDoesNotExist:
        pass
    except Exception as e:
        logger.error("Error while checking admin permissions: " + str(e))