def test_no_profile(self): """ Check for warning when there is no profile """ # TODO: Dirty! Currently we check for the warning by getting a 100% # test coverage, meaning that it dit output some warning. user = UserenaSignup.objects.create_user(**self.user_info) # remove the profile of this user get_profile_model().objects.get(user=user).delete() # run the command to check for the warning. call_command('check_permissions', test=True)
def create_user(self, username, email, password, active=False, send_email=True): """ A simple wrapper that creates a new :class:`User`. :param username: String containing the username of the new user. :param email: String containing the email address of the new user. :param password: String containing the password for the new user. :param active: Boolean that defines if the user requires activation by clicking on a link in an e-mail. Defaults to ``False``. :param send_email: Boolean that defines if the user should be sent an email. You could set this to ``False`` when you want to create a user in your own code, but don't want the user to activate through email. :return: :class:`User` instance representing the new user. """ now = get_datetime_now() new_user = get_user_model().objects.create_user( username, email, password) new_user.is_active = active new_user.save() userena_profile = self.create_userena_profile(new_user) # All users have an empty profile profile_model = get_profile_model() try: new_profile = new_user.get_profile() except profile_model.DoesNotExist: new_profile = profile_model(user=new_user) new_profile.save(using=self._db) # Give permissions to view and change profile for perm in ASSIGNED_PERMISSIONS['profile']: assign(perm[0], new_user, new_profile) # Give permissions to view and change itself for perm in ASSIGNED_PERMISSIONS['user']: assign(perm[0], new_user, new_user) if send_email: userena_profile.send_activation_email() return new_user
def check_permissions(self): """ Checks that all permissions are set correctly for the users. :return: A set of users whose permissions was wrong. """ # Variable to supply some feedback changed_permissions = [] changed_users = [] warnings = [] # Check that all the permissions are available. for model, perms in ASSIGNED_PERMISSIONS.items(): if model == 'profile': model_obj = get_profile_model() else: model_obj = get_user_model() model_content_type = ContentType.objects.get_for_model(model_obj) for perm in perms: try: Permission.objects.get(codename=perm[0], content_type=model_content_type) except Permission.DoesNotExist: changed_permissions.append(perm[1]) Permission.objects.create(name=perm[1], codename=perm[0], content_type=model_content_type) # it is safe to rely on settings.ANONYMOUS_USER_ID since it is a # requirement of django-guardian for user in get_user_model().objects.exclude(id=settings.ANONYMOUS_USER_ID): try: user_profile = user.get_profile() except ObjectDoesNotExist: warnings.append(_("No profile found for %(username)s") \ % {'username': user.username}) else: all_permissions = get_perms(user, user_profile) + get_perms(user, user) for model, perms in ASSIGNED_PERMISSIONS.items(): if model == 'profile': perm_object = user.get_profile() else: perm_object = user for perm in perms: if perm[0] not in all_permissions: assign(perm[0], user, perm_object) changed_users.append(user) return (changed_permissions, changed_users, warnings)
def test_incomplete_permissions(self): # Delete the neccesary permissions profile_model_obj = get_profile_model() content_type_profile = ContentType.objects.get_for_model(profile_model_obj) content_type_user = ContentType.objects.get_for_model(User) for model, perms in ASSIGNED_PERMISSIONS.items(): if model == "profile": content_type = content_type_profile else: content_type = content_type_user for perm in perms: Permission.objects.get(name=perm[1], content_type=content_type).delete() # Check if they are they are back for model, perms in ASSIGNED_PERMISSIONS.items(): if model == "profile": content_type = content_type_profile else: content_type = content_type_user for perm in perms: try: perm = Permission.objects.get(name=perm[1], content_type=content_type) except Permission.DoesNotExist: pass else: self.fail("Found %s: " % perm) # Repair them call_command('check_permissions', test=True) # Check if they are they are back for model, perms in ASSIGNED_PERMISSIONS.items(): if model == "profile": content_type = content_type_profile else: content_type = content_type_user for perm in perms: try: perm = Permission.objects.get(name=perm[1], content_type=content_type) except Permission.DoesNotExist: self.fail()
from django.contrib import admin from django.contrib.auth.admin import UserAdmin from django.utils.translation import ugettext as _ from guardian.admin import GuardedModelAdmin from apps.userena.models import UserenaSignup from apps.userena.utils import get_profile_model, get_user_model class UserenaSignupInline(admin.StackedInline): model = UserenaSignup max_num = 1 class UserenaAdmin(UserAdmin, GuardedModelAdmin): inlines = [UserenaSignupInline, ] list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff', 'is_active', 'date_joined') list_filter = ('is_staff', 'is_superuser', 'is_active') admin.site.unregister(get_user_model()) admin.site.register(get_user_model(), UserenaAdmin) admin.site.register(get_profile_model())