def test_clean_perms(self): # assign obj perms target_perms = { self.target_user1: ["change_%s" % user_module_name], self.target_group1: ["delete_group"], self.target_obj1: ["change_contenttype", "delete_contenttype"], self.target_obj2: ["change_contenttype"], } obj_perms_count = sum([len(val) for key, val in target_perms.items()]) for target, perms in target_perms.items(): target.__old_pk = target.pk # Store pkeys for perm in perms: assign_perm(perm, self.user, target) # Remove targets for target, perms in target_perms.items(): target.delete() # Clean orphans removed = clean_orphan_obj_perms() self.assertEqual(removed, obj_perms_count) # Recreate targets and check if user has no permissions for target, perms in target_perms.items(): target.pk = target.__old_pk target.save() for perm in perms: self.assertFalse(self.user.has_perm(perm, target))
def deleteclass(request): if request.user.is_staff: if request.method == "POST": form = DeleteClassForm(request.POST) if form.is_valid(): cd = form.cleaned_data if not ClassRoom.objects.filter(code="%s" % cd['code']): messages.add_message(request, messages.ERROR, 'There is no class with that code.') return HttpResponseRedirect(reverse(profile,)) classroom = ClassRoom.objects.get(code="%s" % cd['code']) students = User.objects.filter(userprofile__classroom__code=cd['code']) for student in students: old_testscores = TestScore.objects.filter(user=student) old_userprofiles = UserProfile.objects.filter(user=student) old_userprofiles.delete() old_testscores.delete() student.delete() classroom.delete() permission_removed = clean_orphan_obj_perms() messages.add_message(request, messages.SUCCESS, 'Class has been successfully removed.') return HttpResponseRedirect(reverse(profile,)) else: form = DeleteClassForm() return render_to_response('deleteclass.html', {'form': form,}, context_instance=RequestContext(request)) else: return HttpResponseForbidden
def deletePhase(request, id_fase): """ *Vista para la eliminación de una fase dentro del sistema. Opción válida para usuarios con los roles correspondientes.* :param request: HttpRequest necesario para eliminar fases de un proyectos, es la solicitud de la acción. :param id_fase: Identificador de la fase dentro del sistema la cual se desea eliminar. :return: Elimina la fase especifica y luego regresa al menu de fases. """ phase = Fase.objects.get(pk=id_fase) tiposItem = TipoItem.objects.filter(fase=phase) for ti in tiposItem: attrs = Atributo.objects.filter(tipoDeItem=ti) for attr in attrs: attr.delete() ti.delete() project = Proyecto.objects.get(pk=phase.proyecto.id) fasesProyecto = Fase.objects.filter(proyecto=project).order_by('nro_orden') ordenEliminado = phase.nro_orden for fase in fasesProyecto: if fase.nro_orden > ordenEliminado: fase.nro_orden = fase.nro_orden - 1 fase.save() phase_nombre = phase.nombre phase.delete() clean_orphan_obj_perms() mensaje = 'Fase: ' + phase_nombre + ', eliminada exitosamente' mensajes = [] mensajes.append(mensaje) request.session['messages'] = mensajes request.session['error'] = 0 return HttpResponseRedirect(reverse('administrarProyectos.views.workProject', kwargs={'id_proyecto': project.id, }))
def unassigntest(request, classcode): if request.user.is_staff and request.method == "POST": if request.POST["unassignedtest"] == "----": return HttpResponseRedirect(reverse(classprofile, args=(classcode,))) test = Test.objects.get(name="%s" % request.POST["unassignedtest"]) students = User.objects.filter(userprofile__classroom__code=classcode) for student in students: old_testscore = TestScore.objects.filter(test=test).filter(user=student) old_testscore.delete() remove_perm('testtracker.take_test', student, test) remove_perm('testtracker.view_test', student, test) permission_removed = clean_orphan_obj_perms() messages.add_message(request, messages.SUCCESS, 'Test has been successfully unassigned.') return HttpResponseRedirect(reverse(classprofile, args=(classcode,))) else: return HttpResponseForbidden
def deleteuser(request, classcode): if request.user.is_staff: classroom = ClassRoom.objects.get(code="%s" % classcode) if request.method == "POST": form = DeleteUserForm(request.POST) if form.is_valid(): cd = form.cleaned_data if not User.objects.filter(username=cd['username']): messages.add_message(request, messages.ERROR, 'There is no student with that username.') return HttpResponseRedirect(reverse(classprofile, args=(classcode,))) #Need to change for error message old_user = User.objects.get(username=cd['username']) old_testscores = TestScore.objects.filter(user=old_user) old_userprofiles = UserProfile.objects.filter(user=old_user) old_userprofiles.delete() old_testscores.delete() old_user.delete() permission_removed = clean_orphan_obj_perms() messages.add_message(request, messages.SUCCESS, 'Student has been successfully removed.') return HttpResponseRedirect(reverse(classprofile, args=(classcode,))) else: form = DeleteUserForm() return render_to_response('deleteuser.html', {'form': form, 'classroom':classroom}, context_instance=RequestContext(request)) else: return HttpResponseForbidden
def delete_model(self, request, obj): super(GlossaryAdmin, self).delete_model(request, obj) # Remove all orphaned per object permissions after deleting the # glossary instance. clean_orphan_obj_perms()
def clean_orphan_obj_perms(): utils.clean_orphan_obj_perms()
def handle_noargs(self, **options): removed = clean_orphan_obj_perms() if options['verbosity'] > 0: print("Removed %d object permission entries with no targets" % removed)
def delete(self, using=None): super(Document, self).delete(using=using) #TODO: Refactor clean_orphan_obj_perms()
def handle_noargs(self, **options): removed = clean_orphan_obj_perms() if options['verbosity'] > 0: print(("Removed %d object permission entries with no targets" % removed))
def cleanup(task): clean_orphan_obj_perms()
def cleanup_permissions(): "A Celery task that cleans up old django-guardian object permissions." clean_orphan_obj_perms()