def edit_permission(req, id): ctx = Context() found = False p = ctx.get_permission(id) if p is None: raise Http404 p.description = req.POST['description'] p.type = req.POST['type'] constraint_types = req.POST["constraint_types"].strip() and \ req.POST['constraint_types'].split("\n") or [] constraints = req.POST["constraints"].strip() and \ req.POST['constraints'].split("\n") or [] p.constraints = [] for i, t in enumerate(constraint_types): p.add_constraint(t, constraints[i]) for i in xrange(len(ctx.permissions)): if ctx.permissions[i].id == id: ctx.permissions[i] = p break ctx.write_xml() return HttpResponse("{'result': 'OK'}")
def delete_user(req, id): ctx = Context() found = False for i in xrange(len(ctx.users)): if ctx.users[i].login == id: del ctx.users[i] found = True break if not found: raise Http404 ctx.write_xml() return HttpResponse("{'result': 'OK'}")
def add_role(req): ctx = Context() if not req.POST: raise HttpError(400, "Invalid Request") id = req.POST['id'] description = req.POST['description'] permissions = req.POST['permissions'].strip() and req.POST['permissions'].split(",") or [] if id in [p.id for p in ctx.roles]: return HttpResponse("{'result': 'FAIL', 'error': 'This id already exists. Please, choose other.'}") ctx.add_role(id, description, permissions) ctx.write_xml() return HttpResponse("{'result': 'OK'}")
def delete_permission(req, id): ctx = Context() found = False for i in xrange(len(ctx.permissions)): if ctx.permissions[i].id == id: del ctx.permissions[i] found = True break if not found: raise Http404 for i in xrange(len(ctx.roles)): for j in xrange(len(ctx.roles[i].permissions)): if ctx.roles[i].permissions[j].id == id: del ctx.roles[i].permissions[j] ctx.write_xml() return HttpResponse("{'result': 'OK'}")
def add_user(req): ctx = Context() if not req.POST: raise HttpError(400, "Invalid Request") login = req.POST['login'] name = req.POST['name'] roles = req.POST['roles'].strip() and req.POST['roles'].split(",") or [] superuser = (req.POST['superuser'] == "yes") password = req.POST['password'] if login in [u.login for u in ctx.users]: return HttpResponse("{'result': 'FAIL', 'error': 'This login name already exists. Please, choose other.'}") ctx.add_user(name, login, password, superuser, roles) ctx.write_xml() return HttpResponse("{'result': 'OK'}")
def edit_role(req, id): ctx = Context() role = ctx.get_role(id=id) if not role: raise Http404 role.description = req.POST['description'] role.permissions = [] for id in (req.POST['permissions'].strip() and req.POST['permissions'].split(",") or []): role.permissions.append(ctx.get_permission(id)) for i in xrange(len(ctx.roles)): if ctx.roles[i].id == id: ctx.roles[i] = role break ctx.write_xml() return HttpResponse("{'result': 'OK'}")
def add_role(req): ctx = Context() if not req.POST: raise HttpError(400, "Invalid Request") id = req.POST['id'] description = req.POST['description'] permissions = req.POST['permissions'].strip( ) and req.POST['permissions'].split(",") or [] if id in [p.id for p in ctx.roles]: return HttpResponse( "{'result': 'FAIL', 'error': 'This id already exists. Please, choose other.'}" ) ctx.add_role(id, description, permissions) ctx.write_xml() return HttpResponse("{'result': 'OK'}")
def add_user(req): ctx = Context() if not req.POST: raise HttpError(400, "Invalid Request") login = req.POST['login'] name = req.POST['name'] roles = req.POST['roles'].strip() and req.POST['roles'].split(",") or [] superuser = (req.POST['superuser'] == "yes") password = req.POST['password'] if login in [u.login for u in ctx.users]: return HttpResponse( "{'result': 'FAIL', 'error': 'This login name already exists. Please, choose other.'}" ) ctx.add_user(name, login, password, superuser, roles) ctx.write_xml() return HttpResponse("{'result': 'OK'}")
def add_permission(req): ctx = Context() id = req.POST["id"] description = req.POST["description"] type = req.POST["type"] constraint_types = req.POST["constraint_types"].strip() and \ req.POST['constraint_types'].split("\n") or [] constraints = req.POST["constraints"].strip() and \ req.POST['constraints'].split("\n") or [] if id in [p.id for p in ctx.permissions]: return HttpResponse("{'result': 'FAIL', 'error': 'This id already exists. Please, choose other.'}") p = Permission(id, type) p.description = description for i, t in enumerate(constraint_types): p.add_constraint(t, constraints[i]) ctx.permissions.append(p) ctx.write_xml() return HttpResponse("{'result': 'OK'}")
def edit_user(req, id): ctx = Context() user = ctx.get_user(id=id) if not user: raise Http404 user.name = req.POST['name'] user.roles = [] for id in (req.POST['roles'].strip() and req.POST['roles'].split(",") or []): user.roles.append(ctx.get_role(id)) user.superuser = (req.POST['superuser'] == "yes") if req.POST['password'].strip(): password = req.POST['password'] for i in xrange(len(ctx.users)): if ctx.users[i].login == id: ctx.users[i] = user break ctx.write_xml() return HttpResponse("{'result': 'OK'}")
def add_permission(req): ctx = Context() id = req.POST["id"] description = req.POST["description"] type = req.POST["type"] constraint_types = req.POST["constraint_types"].strip() and \ req.POST['constraint_types'].split("\n") or [] constraints = req.POST["constraints"].strip() and \ req.POST['constraints'].split("\n") or [] if id in [p.id for p in ctx.permissions]: return HttpResponse( "{'result': 'FAIL', 'error': 'This id already exists. Please, choose other.'}" ) p = Permission(id, type) p.description = description for i, t in enumerate(constraint_types): p.add_constraint(t, constraints[i]) ctx.permissions.append(p) ctx.write_xml() return HttpResponse("{'result': 'OK'}")