Exemplo n.º 1
0
def get_profile(request):

    response = {}

    try:

        user_is_authenticated = request.user.is_authenticated()
        user_has_web_access = request.user.has_perm("auth.web_access")
        user_has_app_access = request.user.has_perm("auth.app_access")

        application = request.POST["application"]

        if user_is_authenticated:
            if (user_has_web_access and application == "web") or (user_has_app_access and application == "app"):
                user = User.objects.get(username__exact=request.user.username)
                if user:
                    response = {
                        "username": user.username,
                        "first_name": user.first_name,
                        "last_name": user.last_name,
                        "email": user.email,
                        "account_created": get_user_account_created(user),
                        "last_login": get_user_last_login(user),
                        "permissions": {
                            "has_application_management": user.has_perm("auth.application_management"),
                            "has_create_views": user.has_perm("auth.create_views"),
                            "has_display_views": user.has_perm("auth.display_views"),
                            "has_web_access": user.has_perm("auth.web_access"),
                            "has_app_access": user.has_perm("auth.app_access"),
                        },
                    }

    except Exception:

        logger.error(request._get_request)
        logger.exception("An error occured:")

    return HttpResponse(json.dumps(response, sort_keys=False), mimetype="application/json")
Exemplo n.º 2
0
def get_user_info(request):

    response = {}
    user_details = {}
    group_list = []
    user_permission_list = {}
    group_permission_list = {}

    try:

        user_is_authenticated = request.user.is_authenticated()
        user_has_web_access = request.user.has_perm("auth.web_access")
        user_has_application_management = request.user.has_perm("auth.application_management")

        post_user_id = request.POST["user_id"]

        if user_is_authenticated and user_has_web_access and user_has_application_management:
            user = User.objects.get(id=post_user_id)
            groups = Group.objects.all()

            # First populate user details
            user_details = {
                "username": user.username,
                "first_name": user.first_name,
                "last_name": user.last_name,
                "email": user.email,
                "is_active": user.is_active,
                "is_superuser": user.is_superuser,
                "account_created": get_user_account_created(user),
                "last_login": get_user_last_login(user),
            }

            # Populate groups
            for group in groups:
                users_in_group = group.user_set.all()
                group_details = {"id": group.id, "group_name": group.name, "member": user in users_in_group}
                group_list.append(group_details)

            # Populate permissions
            group_permission_list = {
                "application_management": user.has_perm("auth.application_management"),
                "display_views": user.has_perm("auth.display_views"),
                "create_views": user.has_perm("auth.create_views"),
                "web_access": user.has_perm("auth.web_access"),
                "app_access": user.has_perm("auth.app_access"),
            }

            user_permission_list = {
                "application_management": len(
                    user.user_permissions.filter(codename="application_management", content_type__app_label="auth")
                )
                > 0,
                "display_views": len(
                    user.user_permissions.filter(codename="display_views", content_type__app_label="auth")
                )
                > 0,
                "create_views": len(
                    user.user_permissions.filter(codename="create_views", content_type__app_label="auth")
                )
                > 0,
                "web_access": len(user.user_permissions.filter(codename="web_access", content_type__app_label="auth"))
                > 0,
                "app_access": len(user.user_permissions.filter(codename="app_access", content_type__app_label="auth"))
                > 0,
            }

            # TODO: views have to be added to the response
            response = {
                "user_details": user_details,
                "groups": group_list,
                "user_permissions": user_permission_list,
                "group_permissions": group_permission_list,
            }

    except Exception:

        logger.error(request._get_request)
        logger.exception("An error occured:")

    return HttpResponse(json.dumps(response, sort_keys=False), mimetype="application/json")
Exemplo n.º 3
0
def send_email(message_type, to_user, params=None):

    if to_user and to_user.email != '':

        footer = 'vique | ' + _('sturingsinformatie in de cloud') + ' | ' + settings.VIQUEWEB_URL
        html_footer = 'vique&nbsp;|&nbsp;' + _('sturingsinformatie in de cloud') + '&nbsp;|&nbsp;<a href="' + settings.VIQUEWEB_URL + '">' + settings.VIQUEWEB_URL + '</a>'

        subject = _('Vique')
        topic = _('Mail')
        text_message = ''

        if message_type == MessageType.ACCOUNT_INFO:

            subject = _('Vique: account informatie')
            topic = _('Account informatie')
            text_message =  _('Hieronder tref je je accountgegevens voor Vique aan') + ':\n\n'
            text_message += _('Gebruikersnaam') + ': ' + to_user.username + '\n'
            text_message += _('Voornaam') + ': ' + to_user.first_name + '\n'
            text_message += _('Achternaam') + ': ' + to_user.last_name + '\n'
            text_message += _('Email adres') + ': ' + to_user.email + '\n'
            text_message += _('Account aangemaakt') + ': ' + get_user_account_created(to_user) + '\n'
            text_message += _('Ingelogd sinds') + ': ' + get_user_last_login(to_user) + '\n'
            text_message += _('Account actief') + ': ' + (_('Ja') + '\n' if (to_user.is_active == True) else _('Nee') + '\n')
            text_message += '\n'
            text_message += _('Rechten aan je toegekend') + ':\n\n'
            text_message += _('Applicatiebeheerder') + ': ' + (_('Ja') + '\n' if (to_user.has_perm('auth.application_management') == True) else _('Nee') + '\n')
            text_message += _('Web toegang') + ': ' + (_('Ja') + '\n' if (to_user.has_perm('auth.web_access') == True) else _('Nee') + '\n')
            text_message += _('App toegang') + ': ' + (_('Ja') + '\n' if (to_user.has_perm('auth.app_access') == True) else _('Nee') + '\n')
            text_message += _('Views maken') + ': ' + (_('Ja') + '\n' if (to_user.has_perm('auth.create_views') == True) else _('Nee') + '\n')
            text_message += _('Views bekijken') + ': ' + (_('Ja') + '\n' if (to_user.has_perm('auth.display_views') == True) else _('Nee') + '\n')

        elif message_type == MessageType.NEW_PASSWORD:

            subject = _('Vique: nieuw wachtwoord')
            topic = _('Nieuw wachtwoord')

            text_message =  _('Je hebt een nieuw wachtwoord om in te loggen in Vique') + ':\n\n'
            text_message += _('Gebruikersnaam') + ': ' + to_user.username + '\n'
            text_message += _('Wachtwoord') + ': ' + params['password'] + '\n'

        template = loader.get_template('generic_mail.html')
        context = Context({
            'topic': topic,
            'message': text_message,
            'footer': mark_safe(html_footer),
        })

        html_tmpl = template.render(context)
        text_tmpl = text_message + '\n' + footer

        # Default text message
        msg = EmailMultiAlternatives(subject, text_tmpl, settings.EMAIL_FROM, [to_user.email])

        # Alternative HTML message
        msg.attach_alternative(html_tmpl, "text/html")

        # Add logo
        img_file = open(settings.EMAIL_LOGO, 'rb')
        img_data = img_file.read()
        img = MIMEImage(img_data)
        img_file.close()
        img.add_header('Content-ID', '<logo.png>')
        img.add_header('Content-Disposition', 'inline')
        img.add_header('Content-Type', 'image/png')
        msg.attach(img)

        # Send email
        msg.send()