Showing posts with label user. Show all posts
Showing posts with label user. Show all posts

May 16, 2012

Allowing only super user login decorator

from django.contrib.auth.decorators import user_passes_test

@user_passes_test(lambda u: u.is_superuser)
def foo_view(request):
    ....

Feb 23, 2012

Run Wireshark as a Normal User on Ubuntu 11.10

Install everything you need:
sudo apt-get install wireshark tshark libcap2-bin
According to /usr/share/doc/wireshark/README.Debian you have to add your user to the group wireshark
# check group
cat /etc/group | grep wireshark

# create it if not exists
sudo groupadd wireshark

# add user to this new group
sudo usermod -a -G wireshark `whoami`
# check user groups
groups `whoami`
# yourusername : yourusergroup ..... wireshark
# or
cat /etc/group | grep wireshark
# wireshark:x:1002:yourusername

# log in to a new group
newgrp wireshark
Then lets add capture privileges http://wiki.wireshark.org/CaptureSetup/CapturePrivileges :
sudo chgrp wireshark /usr/bin/dumpcap
sudo chmod 754 /usr/bin/dumpcap
sudo setcap 'CAP_NET_RAW+eip CAP_NET_ADMIN+eip' /usr/bin/dumpcap

# ensure that everything ok:
getcap /usr/bin/dumpcap
# /usr/bin/dumpcap = cap_net_admin,cap_net_raw+eip
You shoud be able run tshark without sudo:
tshark -R 'ip.addr==178.159.244.20'

# amqp
tshark -i lo tcp port 5672

Dec 30, 2011

Extending the Django User model with inheritance

from django.contrib.auth.models import User, UserManager

class CustomUser(User):
    """User with app settings."""
    timezone = models.CharField(max_length=50, default='Europe/London')

    # Use UserManager to get the create_user method, etc.
    objects = UserManager()

...

user = CustomUser.objects.create(...)

# auth_backends.py
from django.conf import settings
from django.contrib.auth.backends import ModelBackend
from django.core.exceptions import ImproperlyConfigured
from django.db.models import get_model

class CustomUserModelBackend(ModelBackend):
    def authenticate(self, username=None, password=None):
        try:
            user = self.user_class.objects.get(username=username)
            if user.check_password(password):
                return user
        except self.user_class.DoesNotExist:
            return None

    def get_user(self, user_id):
        try:
            return self.user_class.objects.get(pk=user_id)
        except self.user_class.DoesNotExist:
            return None

    @property
    def user_class(self):
        if not hasattr(self, '_user_class'):
            self._user_class = get_model(*settings.CUSTOM_USER_MODEL.split('.', 2))
            if not self._user_class:
                raise ImproperlyConfigured('Could not get custom user model')
        return self._user_class


# settings.py
AUTHENTICATION_BACKENDS = (
    'myproject.auth_backends.CustomUserModelBackend',
)
...

CUSTOM_USER_MODEL = 'accounts.CustomUser'

http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/

Nov 3, 2011

geobaza - Geobaza is the software for geolocation by IP-address.