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

fixed the outdated docs for the Python SDK usage #515

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 27 additions & 29 deletions docs/getting-started/_quickstart-parts/_quickstart_python_sync.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ user = {
"lastName": "Smith", # optional
"email": "[email protected]", # optional
}
permit.write(permit.api.sync_user(user))
permit.api.users.create(user)
```

### Sync user with initial roles
Expand All @@ -145,7 +145,7 @@ user = {
# John will be assigned the admin role in the default tenant upon creation
"roles": [{"role":"admin", "tenant": "default"}]
}
permit.write(permit.api.sync_user(user))
permit.api.users.create(user)
```

:::note
Expand All @@ -161,7 +161,7 @@ the tenant cannot access the tenant's associated resources.

Every one of your customers is typically a tenant that contains only that tenant's users.

To create a tenant, use `permit.api.create_tenant()`:
To create a tenant, use `permit.api.tenants.create()`:

```python
cool_company_tenant = {
Expand All @@ -178,7 +178,7 @@ You can assign a role to a user in a given tenant:

```python
permit.write(
permit.api.assign_role("[email protected]", "viewer", "cool_company_inc")
permit.api.users.assign_role("[email protected]", "viewer", "cool_company_inc")
)
```

Expand All @@ -189,22 +189,20 @@ will run them according to the order you specify.

```python
# runs the mutations in order:
permit.write(
# first creates the user "john"
permit.api.sync_user({
# first creates the user "john"
permit.api.users.create(
{
"key": "[email protected]",
"firstName": "John",
"lastName": "Smith",
"first_name": "John",
"last_name": "Smith",
"email": "[email protected]",
}),
# then, creates the "cool_company_inc" tenant
permit.api.create_tenant({
"key": "cool_company_inc",
"name": "Cool Company Inc"
}),
# finally, assigns the role "admin" to user "john" on the tenant "cool_company_inc"
permit.api.assign_role("[email protected]", "admin", "cool_company_inc")
)
}
),
# then, creates the "cool_company_inc" tenant
permit.api.tenants.create({"key": "cool_company_inc", "name": "Cool Company Inc"}),
# finally, assigns the role "admin" to user "john" on the tenant "cool_company_inc"
permit.api.users.assign_role("[email protected]", "admin", "cool_company_inc")

``` -->

## Full app example
Expand All @@ -226,7 +224,7 @@ pyenv virtualenv permissions && pyenv activate permissions
Now install the Permit.io SDK. We will also install `flask` package.

```
pip install permit==1.0.0rc1 flask
pip install permit flask
```

Create a file called `test.py`.
Expand Down Expand Up @@ -259,31 +257,30 @@ permit = Permit(
def sync_objects():
# first let's create a user:
user = {
"key": "john@smith.com",
"firstName": "John",
"lastName": "Smith",
"key": "john3@smith.com",
"first_name": "John",
"last_name": "Smith",
"email": "[email protected]",
"roles": [{"role":"admin", "tenant": "default"}]
"role_assignments": [{"role":"admin", "tenant": "default"}]
}
permit.write(permit.api.sync_user(user))

permit.api.users.create(user)
tenant2 = {
"key": "tenant2",
"name": "Second Tenant"
}

# create tenant 2 and assign role viewer to user john
permit.write(
permit.api.roles.create(
permit.api.create_tenant(tenant2),
permit.api.assign_role("[email protected]", "viewer", "tenant2")
)

sync_objects()
# sync_objects()

@app.route("/")
def check_permissions():
# After we created this user in the previous step, we also synced the user's identifier
# to permit.io servers with permit.write(permit.api.syncUser(user)). The user identifier
# to permit.io servers with permit.api.users.create(user). The user identifier
# can be anything (email, db id, etc.) but must be unique for each user. Now that the
# user is synced, we can use its identifier to check permissions with `permit.check()`.
permitted = permit.check("[email protected]", "retrieve", "task") # default tenant is used
Expand All @@ -299,7 +296,7 @@ def check_permissions():
@app.route("/tenant2")
def check_permissions_tenant2():
# After we created this user in the previous step, we also synced the user's identifier
# to permit.io servers with permit.write(permit.api.syncUser(user)). The user identifier
# to permit.io servers with permit.api.users.create(user). The user identifier
# can be anything (email, db id, etc.) but must be unique for each user. Now that the
# user is synced, we can use its identifier to check permissions with `permit.check()`.
permitted = permit.check("[email protected]", "create", {"type": "task", "tenant": "tenant2"}) # tenant2 is used
Expand All @@ -312,6 +309,7 @@ def check_permissions_tenant2():
"result": f"John Smith is PERMITTED to create task (tenant 2)!"
}), status=200, mimetype='application/json')


```

Now that your application is ready, let's run it!
Expand Down