Ejemplo n.º 1
0
def user_add(request):
    """Add a user"""

    if not request.user.is_staff and not request.user.org['is_staff']:
        raise Http404

    base_template = 'org_admin_base.html' if request.user.org else 'admin_base.html'

    content_type = 'application/json; charset=utf-8'
    if request.method == 'POST':
        post_values = request.POST.copy()
        post_email = request.POST.get('email', '')
        post_values.update({'email': post_email.lower()})

        form = AddUserForm(post_values)
        if form.is_valid():
            email = form.cleaned_data['email']
            password = form.cleaned_data['password1']

            user = User.objects.create_user(email,
                                            password,
                                            is_staff=False,
                                            is_active=True)
            if request.user.org:
                org_id = request.user.org['org_id']
                url_prefix = request.user.org['url_prefix']
                ccnet_threaded_rpc.add_org_user(org_id, email, 0)
                if hasattr(settings, 'EMAIL_HOST'):
                    send_user_add_mail(request, email, password)

                return HttpResponseRedirect(
                    reverse('org_useradmin', args=[url_prefix]))
            else:
                if IS_EMAIL_CONFIGURED:
                    if SEND_EMAIL_ON_ADDING_SYSTEM_MEMBER:
                        try:
                            send_user_add_mail(request, email, password)
                            messages.success(
                                request,
                                _(u'Successfully added user %s. An email notification has been sent.'
                                  ) % email)
                        except Exception, e:
                            logger.error(str(e))
                            messages.success(
                                request,
                                _(u'Successfully added user %s. An error accurs when sending email notification, please check your email configuration.'
                                  ) % email)
                    else:
                        messages.success(
                            request,
                            _(u'Successfully added user %s.') % email)
                else:
                    messages.success(
                        request,
                        _(u'Successfully added user %s. But email notification can not be sent, because Email service is not properly configured.'
                          ) % email)

                return HttpResponse(json.dumps({'success': True}),
                                    content_type=content_type)
Ejemplo n.º 2
0
def user_add(request):
    """Add a user"""

    if not request.user.is_staff or request.method != 'POST':
        raise Http404

    content_type = 'application/json; charset=utf-8'

    post_values = request.POST.copy()
    post_email = request.POST.get('email', '')
    post_role = request.POST.get('role', DEFAULT_USER)
    post_values.update({
                        'email': post_email.lower(),
                        'role': post_role,
                      })

    form = AddUserForm(post_values)
    if form.is_valid():
        email = form.cleaned_data['email']
        role = form.cleaned_data['role']
        password = form.cleaned_data['password1']

        user = User.objects.create_user(email, password, is_staff=False,
                                        is_active=True)
        if user:
            User.objects.update_role(email, role)

        if request.user.org:
            org_id = request.user.org.org_id
            url_prefix = request.user.org.url_prefix
            ccnet_threaded_rpc.add_org_user(org_id, email, 0)
            if IS_EMAIL_CONFIGURED:
                try:
                    send_user_add_mail(request, email, password)
                    messages.success(request, _(u'Successfully added user %s. An email notification has been sent.') % email)
                except Exception, e:
                    logger.error(str(e))
                    messages.success(request, _(u'Successfully added user %s. An error accurs when sending email notification, please check your email configuration.') % email)
            else:
                messages.success(request, _(u'Successfully added user %s.') % email)

            return HttpResponse(json.dumps({'success': True}), content_type=content_type)
        else:
            if IS_EMAIL_CONFIGURED:
                if SEND_EMAIL_ON_ADDING_SYSTEM_MEMBER:
                    try:
                        send_user_add_mail(request, email, password)
                        messages.success(request, _(u'Successfully added user %s. An email notification has been sent.') % email)
                    except Exception, e:
                        logger.error(str(e))
                        messages.success(request, _(u'Successfully added user %s. An error accurs when sending email notification, please check your email configuration.') % email)
                else:
                    messages.success(request, _(u'Successfully added user %s.') % email)
            else:
Ejemplo n.º 3
0
def user_add(request):
    """Add a user"""

    if not request.user.is_staff and not request.user.org['is_staff']:
        raise Http404

    base_template = 'org_admin_base.html' if request.user.org else 'admin_base.html'
    
    content_type = 'application/json; charset=utf-8'
    if request.method == 'POST':
        post_values = request.POST.copy()
        post_email = request.POST.get('email', '')
        post_values.update({'email': post_email.lower()})

        form = AddUserForm(post_values)
        if form.is_valid():
            email = form.cleaned_data['email']
            password = form.cleaned_data['password1']

            user = User.objects.create_user(email, password, is_staff=False,
                                            is_active=True)
            if request.user.org:
                org_id = request.user.org['org_id']
                url_prefix = request.user.org['url_prefix']
                ccnet_threaded_rpc.add_org_user(org_id, email, 0)
                if hasattr(settings, 'EMAIL_HOST'):
                    send_user_add_mail(request, email, password)
                    
                return HttpResponseRedirect(reverse('org_useradmin',
                                                    args=[url_prefix]))
            else:
                if IS_EMAIL_CONFIGURED:
                    if SEND_EMAIL_ON_ADDING_SYSTEM_MEMBER:
                        try:
                            send_user_add_mail(request, email, password)
                            messages.success(request, _(u'Successfully added user %s. An email notification has been sent.') % email)
                        except Exception, e:
                            logger.error(str(e))
                            messages.success(request, _(u'Successfully added user %s. An error accurs when sending email notification, please check your email configuration.') % email)
                    else:
                        messages.success(request, _(u'Successfully added user %s.') % email)
                else:
                    messages.success(request, _(u'Successfully added user %s. But email notification can not be sent, because Email service is not properly configured.') % email)

                return HttpResponse(json.dumps({'success': True}), content_type=content_type)
Ejemplo n.º 4
0
def user_add(request):
    """Add a user"""

    if not request.user.is_staff or request.method != 'POST':
        raise Http404

    content_type = 'application/json; charset=utf-8'

    post_values = request.POST.copy()
    post_email = request.POST.get('email', '')
    post_role = request.POST.get('role', DEFAULT_USER)
    post_values.update({
        'email': post_email.lower(),
        'role': post_role,
    })

    form = AddUserForm(post_values)
    if form.is_valid():
        email = form.cleaned_data['email']
        role = form.cleaned_data['role']
        password = form.cleaned_data['password1']

        user = User.objects.create_user(email,
                                        password,
                                        is_staff=False,
                                        is_active=True)
        if user:
            User.objects.update_role(email, role)

        if request.user.org:
            org_id = request.user.org.org_id
            url_prefix = request.user.org.url_prefix
            ccnet_threaded_rpc.add_org_user(org_id, email, 0)
            if IS_EMAIL_CONFIGURED:
                try:
                    send_user_add_mail(request, email, password)
                    messages.success(
                        request,
                        _(u'Successfully added user %s. An email notification has been sent.'
                          ) % email)
                except Exception, e:
                    logger.error(str(e))
                    messages.success(
                        request,
                        _(u'Successfully added user %s. An error accurs when sending email notification, please check your email configuration.'
                          ) % email)
            else:
                messages.success(request,
                                 _(u'Successfully added user %s.') % email)

            return HttpResponse(json.dumps({'success': True}),
                                content_type=content_type)
        else:
            if IS_EMAIL_CONFIGURED:
                if SEND_EMAIL_ON_ADDING_SYSTEM_MEMBER:
                    try:
                        send_user_add_mail(request, email, password)
                        messages.success(
                            request,
                            _(u'Successfully added user %s. An email notification has been sent.'
                              ) % email)
                    except Exception, e:
                        logger.error(str(e))
                        messages.success(
                            request,
                            _(u'Successfully added user %s. An error accurs when sending email notification, please check your email configuration.'
                              ) % email)
                else:
                    messages.success(request,
                                     _(u'Successfully added user %s.') % email)
            else:
Ejemplo n.º 5
0
def user_add(request):
    """Add a user"""

    if not request.user.is_staff or request.method != 'POST':
        raise Http404

    content_type = 'application/json; charset=utf-8'

    post_values = request.POST.copy()
    post_email = request.POST.get('email', '')
    post_role = request.POST.get('role', DEFAULT_USER)
    post_values.update({
        'email': post_email.lower(),
        'role': post_role,
    })

    form = AddUserForm(post_values)
    if form.is_valid():
        email = form.cleaned_data['email']
        name = form.cleaned_data['name']
        department = form.cleaned_data['department']
        role = form.cleaned_data['role']
        password = form.cleaned_data['password1']

        try:
            user = User.objects.create_user(email,
                                            password,
                                            is_staff=False,
                                            is_active=True)
        except User.DoesNotExist as e:
            logger.error(e)
            err_msg = _('Fail to add user %s.') % email
            return HttpResponse(json.dumps({'error': err_msg}),
                                status=403,
                                content_type=content_type)

        # send admin operation log signal
        admin_op_detail = {
            "email": email,
        }
        admin_operation.send(sender=None,
                             admin_name=request.user.username,
                             operation=USER_ADD,
                             detail=admin_op_detail)

        if user:
            User.objects.update_role(email, role)
            if config.FORCE_PASSWORD_CHANGE:
                UserOptions.objects.set_force_passwd_change(email)
            if name:
                Profile.objects.add_or_update(email, name, '')
            if department:
                DetailedProfile.objects.add_or_update(email, department, '')

        if request.user.org:
            org_id = request.user.org.org_id
            ccnet_threaded_rpc.add_org_user(org_id, email, 0)
            if IS_EMAIL_CONFIGURED:
                try:
                    send_user_add_mail(request, email, password)
                    messages.success(
                        request,
                        _('Successfully added user %s. An email notification has been sent.'
                          ) % email)
                except Exception as e:
                    logger.error(str(e))
                    messages.success(
                        request,
                        _('Successfully added user %s. An error accurs when sending email notification, please check your email configuration.'
                          ) % email)
            else:
                messages.success(request,
                                 _('Successfully added user %s.') % email)

            return HttpResponse(json.dumps({'success': True}),
                                content_type=content_type)
        else:
            if IS_EMAIL_CONFIGURED:
                if SEND_EMAIL_ON_ADDING_SYSTEM_MEMBER:
                    try:
                        send_user_add_mail(request, email, password)
                        messages.success(
                            request,
                            _('Successfully added user %s. An email notification has been sent.'
                              ) % email)
                    except Exception as e:
                        logger.error(str(e))
                        messages.success(
                            request,
                            _('Successfully added user %s. An error accurs when sending email notification, please check your email configuration.'
                              ) % email)
                else:
                    messages.success(request,
                                     _('Successfully added user %s.') % email)
            else:
                messages.success(
                    request,
                    _('Successfully added user %s. But email notification can not be sent, because Email service is not properly configured.'
                      ) % email)

            return HttpResponse(json.dumps({'success': True}),
                                content_type=content_type)
    else:
        return HttpResponse(json.dumps(
            {'error': str(list(form.errors.values())[0])}),
                            status=400,
                            content_type=content_type)