Esempio n. 1
0
    def user_data(self, handler, instance):
        if handler.request.method == "POST":
            data = load_json(handler.request.POST.get('data', "{}"))

            for user in data.get('existing', []):
                state = user.get('state', '')

                if state == 'deleted':
                    try:
                        User.objects.get(pk=user['id']).delete()
                    except (ValueError, User.DoesNotExist):
                        pass
                elif state in ("added", "modified"):
                    try:
                        u = User.objects.get(pk=user['id'])
                        u.username = user.get('username').strip()
                        u.first_name = user.get('firstname', '')
                        u.last_name = user.get('lastname', '')
                        u.email = user.get('email', '')
                        ## active?
                    except (ValueError, User.DoesNotExist):
                        u = User(username=user.get('username').strip(),
                                 first_name=user.get('firstname', ''),
                                 last_name=user.get('lastname', ''),
                                 email=user.get('email', ''))
                        u.save()

                    password = user.get('password', '').strip()
                    if password:
                        u.set_password(password)

                    u.roles.all().delete()

                    for role, isset in user.get('roles', {}).iteritems():
                        if isset:
                            WheelRole(role=Role(role), user=u).save()

                    u.save()

                ## roles


        users = User.objects.all()
        data = {}
        data['existing'] = [dict(id=u.id,
                              username=u.username,
                              firstname=u.first_name,
                              lastname=u.last_name,
                              email=u.email,
                              active=u.is_active,
                              superuser=u.is_superuser,
                              roles=dict((role.role.id, True)
                                         for role in u.roles.all())
                              ) for u in users]
        data['roles'] = [dict(id=role.id,
                              name=role.name,
                              description=role.description)
                         for role in Role.all()]

        return data
Esempio n. 2
0
    def auth(self, handler, request, action):
        ##
        ## If post, handle/reset perm changes

        if request.method == "POST":
            existing = RolePermission.assignments(self.instance)
            assignments = request.POST.getlist('assignment')
            for e in existing:
                if "{0}/{1}".format(e.permission, e.role) not in assignments:
                    e.delete()

            for assignment in assignments:
                perm, role = assignment.split('/', 1)
                RolePermission.assign(self.instance, Role(role),
                                      Permission(perm)).save()

        ctx = {'spoke':self}


        roles = Role.all()
        permissions = []

        ## order roles, permissions (alphabetically?)
        for perm in Permission.all():
            d = dict(perm=perm, roles=[])
            perms_per_role = RolePermission.assignments(
                                        self.instance).filter(
                                        permission=perm.id,
                                        ).values_list('role', flat=True)
            r = []
            for role in roles:
                r.append(dict(role=role, checked=role.id in perms_per_role))

            d['roles'] = r

            permissions.append(d)

        ctx['roles'] = roles
        ctx['permissions'] = permissions
        return handler.template("wheelcms_axle/edit_permissions.html", **ctx)
Esempio n. 3
0
 def test_all(self):
     """ retrieve all created permissions """
     r1 = Role.create("r1")
     r2 = Role.create("r2")
     r3 = Role.create("r2") # duplicate!
     assert set(Role.all()) == set((r1, r2))
Esempio n. 4
0
 def test_all_empty(self):
     """ empty role collection """
     assert len(Role.all()) == 0