Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adapter doesn't implement update extension #4

Closed
zy7y opened this issue Jun 20, 2022 · 4 comments
Closed

adapter doesn't implement update extension #4

zy7y opened this issue Jun 20, 2022 · 4 comments
Assignees
Labels
bug Something isn't working triaged

Comments

@zy7y
Copy link

zy7y commented Jun 20, 2022

Traceback (most recent call last):
  File "E:\WORK\system\venv\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 366, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "E:\WORK\system\venv\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 75, in __call__
    return await self.app(scope, receive, send)
  File "E:\WORK\system\venv\lib\site-packages\fastapi\applications.py", line 269, in __call__
    await super().__call__(scope, receive, send)
  File "E:\WORK\system\venv\lib\site-packages\starlette\applications.py", line 124, in __call__
    await self.middleware_stack(scope, receive, send)
  File "E:\WORK\system\venv\lib\site-packages\starlette\middleware\errors.py", line 184, in __call__
    raise exc
  File "E:\WORK\system\venv\lib\site-packages\starlette\middleware\errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "E:\WORK\system\venv\lib\site-packages\starlette\middleware\cors.py", line 84, in __call__
    await self.app(scope, receive, send)
  File "E:\WORK\system\venv\lib\site-packages\starlette\middleware\base.py", line 68, in __call__
    response = await self.dispatch_func(request, call_next)
  File "E:\WORK\system\core\middleware.py", line 16, in dispatch
    response = await call_next(request)
  File "E:\WORK\system\venv\lib\site-packages\starlette\middleware\base.py", line 46, in call_next
    raise app_exc
  File "E:\WORK\system\venv\lib\site-packages\starlette\middleware\base.py", line 36, in coro
    await self.app(scope, request.receive, send_stream.send)
  File "E:\WORK\system\venv\lib\site-packages\starlette\exceptions.py", line 93, in __call__
    raise exc
  File "E:\WORK\system\venv\lib\site-packages\starlette\exceptions.py", line 82, in __call__
    await self.app(scope, receive, sender)
  File "E:\WORK\system\venv\lib\site-packages\fastapi\middleware\asyncexitstack.py", line 21, in __call__
    raise e
  File "E:\WORK\system\venv\lib\site-packages\fastapi\middleware\asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "E:\WORK\system\venv\lib\site-packages\starlette\routing.py", line 670, in __call__
    await route.handle(scope, receive, send)
  File "E:\WORK\system\venv\lib\site-packages\starlette\routing.py", line 266, in handle
    await self.app(scope, receive, send)
  File "E:\WORK\system\venv\lib\site-packages\starlette\routing.py", line 65, in app
    response = await func(request)
  File "E:\WORK\system\venv\lib\site-packages\fastapi\routing.py", line 227, in app
    raw_response = await run_endpoint_function(
  File "E:\WORK\system\venv\lib\site-packages\fastapi\routing.py", line 160, in run_endpoint_function
    return await dependant.call(**values)
  File "E:\WORK\system\main.py", line 29, in add
    await e.update_policy(["883", "user/:id", "GET"], ["882", "user/:id", "GET"])
  File "E:\WORK\system\venv\lib\site-packages\casbin\management_enforcer.py", line 158, in update_policy
    return await self.update_named_policy("p", old_rule, new_rule)
  File "E:\WORK\system\venv\lib\site-packages\casbin\management_enforcer.py", line 168, in update_named_policy
    return await self._update_policy("p", ptype, old_rule, new_rule)
  File "E:\WORK\system\venv\lib\site-packages\casbin\internal_enforcer.py", line 53, in _update_policy
    await self.adapter.update_policy(
AttributeError: 'TortoiseAdapter' object has no attribute 'update_policy'

code

import casbin
import casbin_tortoise_adapter
from fastapi import FastAPI
from tortoise.contrib.fastapi import register_tortoise

app = FastAPI()
register_tortoise(
    app,
    db_url="sqlite://db.sqlite",
    modules={"models": ["casbin_tortoise_adapter"]},
    generate_schemas=True,
    add_exception_handlers=True,
)


# https://casbin.org/docs/zh-CN/management-api


adapter = casbin_tortoise_adapter.TortoiseAdapter()
e = casbin.Enforcer('model.conf', adapter)


@app.get("/")
async def add():
    # add  
    await e.add_policy("883", "user/:id", "GET")
    # update
    await e.update_policy(["883", "user/:id", "GET"], ["882", "user/:id", "GET"])
    return 1


if __name__ == '__main__':
    import uvicorn
    uvicorn.run("main:app", reload=True)

How do you update an existing policy? Looking forward to your reply. Thank you

@hsluoyz
Copy link

hsluoyz commented Jun 20, 2022

@thearchitector

@thearchitector
Copy link
Owner

thearchitector commented Jun 21, 2022

looks like the documentation totally skips mention of and that adapters need implementation of the update funcs --- in fact, looks like most adapters don't actually provide this at all...

there is a separate adapter interface for these (though it annoyingly is missing the plural policy update func). i'll add this functionality in with some tests

if this is blocking you, it also seems like you could delete (via remove policy) and add it again (add policy), but it's a bit ugly. alternatively impl it yourself until i get a chance to do it correctly on this end -- here's the sqlalchemy impl

@thearchitector thearchitector self-assigned this Jun 21, 2022
@thearchitector thearchitector added bug Something isn't working triaged labels Jun 21, 2022
@thearchitector thearchitector changed the title If the add_policy command is used, the database will not be written if the storage exists adapter doesn't implement update extension Jun 21, 2022
@hsluoyz
Copy link

hsluoyz commented Jun 21, 2022

@thearchitector the docs will be improved: casdoor/casdoor-website#223

@thearchitector
Copy link
Owner

@zy7y added enforcer.update_policy and enforcer.update_policies, following the reference implementation. they're available if you update to the new release v1.2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triaged
Projects
None yet
Development

No branches or pull requests

3 participants