Skip to content

Commit

Permalink
Alter User Schema
Browse files Browse the repository at this point in the history
  • Loading branch information
0xsirsaif committed Feb 2, 2023
1 parent d12437f commit 9cae313
Show file tree
Hide file tree
Showing 10 changed files with 237 additions and 151 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,6 @@ ENV/

# .idea
.idea/

# edgedb-py generated file
generated_async_edgeql.py
54 changes: 19 additions & 35 deletions dbschema/default.esdl
Original file line number Diff line number Diff line change
@@ -1,66 +1,50 @@
module default {
# User type that implements User Protocol
abstract type AbstractBaseUser {
property email -> str {
type EdgeBaseUser {
required property email -> str {
constraint exclusive;
constraint regexp(r"^[A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*$");
}
}

type EdgeBaseUser extending AbstractBaseUser {
required property first_name -> str;
required property last_name -> str;
property full_name := .first_name ++ ' ' ++ .last_name;

property hashed_password -> str;
property is_active -> bool;
property is_superuser -> bool;
property is_verified -> bool;

required property username -> str {
constraint exclusive;
};
required property hashed_password -> str;
required property is_active -> bool;
required property is_superuser -> bool;
required property is_verified -> bool;

multi link oauth_accounts -> EdgeBaseOAuthUser {
# ensures a one-to-many relationship
constraint exclusive;
# Deleting this Object (User) will unconditionally delete linked objects (oauth)
on source delete delete target;
}

multi link access_tokens -> EdgeAccessTokenUser {
# ensures a one-to-many relationship
constraint exclusive;
# Deleting this Object (User) will unconditionally delete linked objects (access)
on source delete delete target;
}

# required property birth_date -> cal::local_date;
# property age := <Age>();
# scalar type Age extending int16{
# constraint max_value(120);
# constraint min_value(0);
# };
}

type EdgeBaseOAuthUser extending AbstractBaseUser {
required property oauth_name -> str {
constraint exclusive;
}
required property account_id -> str {
constraint exclusive;
type EdgeBaseOAuthUser {
required property account_email -> str {
constraint regexp(r"^[A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*$");
}
required property oauth_name -> str;
required property account_id -> str;
required property access_token -> str;
property expires_at -> int32;
property refresh_token -> str;
required property access_token -> str;
required property account_email -> str;


}

type EdgeAccessTokenUser extending AbstractBaseUser {
type EdgeAccessTokenUser {
required property token -> str {
constraint exclusive;
}
property created_at -> datetime {
required property created_at -> datetime {
default := std::datetime_current();
};
property user_id -> str;
}

}
77 changes: 77 additions & 0 deletions dbschema/migrations/00025.edgeql
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
CREATE MIGRATION m1eadqf3euyx5tpaogx55ntofdhuxp2vamka25sqvgzhqj7t3fukya
ONTO m1nng42nf6jyq6eb2mnvcl7xfesbi5gfbrmmserku47th5pp55dqqq
{
ALTER TYPE default::AbstractBaseUser {
DROP PROPERTY email;
};
ALTER TYPE default::EdgeAccessTokenUser {
DROP PROPERTY user_id;
DROP EXTENDING default::AbstractBaseUser;
};
ALTER TYPE default::EdgeBaseOAuthUser DROP EXTENDING default::AbstractBaseUser;
ALTER TYPE default::EdgeBaseOAuthUser {
ALTER PROPERTY account_email {
CREATE CONSTRAINT std::regexp(r'^[A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*$');
};
ALTER PROPERTY account_id {
DROP CONSTRAINT std::exclusive;
};
ALTER PROPERTY oauth_name {
DROP CONSTRAINT std::exclusive;
};
};
ALTER TYPE default::EdgeBaseUser {
ALTER PROPERTY username {
RENAME TO email;
};
};
ALTER TYPE default::EdgeBaseUser {
DROP PROPERTY full_name;
};
ALTER TYPE default::EdgeBaseUser {
DROP PROPERTY first_name;
};
ALTER TYPE default::EdgeBaseUser {
DROP PROPERTY last_name;
DROP EXTENDING default::AbstractBaseUser;
};
DROP TYPE default::AbstractBaseUser;
ALTER TYPE default::EdgeAccessTokenUser {
ALTER PROPERTY created_at {
SET REQUIRED USING (std::datetime_current());
};
};
ALTER TYPE default::EdgeBaseUser {
ALTER LINK access_tokens {
ON SOURCE DELETE DELETE TARGET;
};
};
ALTER TYPE default::EdgeBaseUser {
ALTER LINK oauth_accounts {
ON SOURCE DELETE DELETE TARGET;
};
ALTER PROPERTY email {
CREATE CONSTRAINT std::regexp(r'^[A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*$');
};
};
ALTER TYPE default::EdgeBaseUser {
ALTER PROPERTY hashed_password {
SET REQUIRED USING ('');
};
};
ALTER TYPE default::EdgeBaseUser {
ALTER PROPERTY is_active {
SET REQUIRED USING (true);
};
};
ALTER TYPE default::EdgeBaseUser {
ALTER PROPERTY is_superuser {
SET REQUIRED USING (false);
};
};
ALTER TYPE default::EdgeBaseUser {
ALTER PROPERTY is_verified {
SET REQUIRED USING (false);
};
};
};
7 changes: 2 additions & 5 deletions fastapiusers_edgedb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async def update(self, user: UP, update_dict: Dict[str, Any]) -> UP:
**{
k: v
for k, v in user_dict.items()
if k not in ["id", "oauth_accounts"]
if k not in ["id", "oauth_accounts", "access_tokens"]
}
)
return user
Expand All @@ -75,7 +75,7 @@ async def add_oauth_account(self, user: UP, create_dict: Dict[str, Any]) -> UP:

async def get_by_oauth_account(self, oauth: str, account_id: str) -> Optional[UP]:
"""Get a single user by OAuth account id."""
oauth_account = await user_api.get_by_oauth_account(
oauth_account: UP = await user_api.get_by_oauth_account(
self.client, oauth_name=oauth, account_id=account_id
)
return oauth_account
Expand Down Expand Up @@ -110,7 +110,4 @@ async def update_oauth_account(
user = await user_api.get_user(
self.client, cast="uuid", key="id", value=user.id
)
# print("?????????????????????????????????????????")
# print(user)
# print("?????????????????????????????????????????")
return user
15 changes: 9 additions & 6 deletions fastapiusers_edgedb/access_token.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""FastAPI Users access token database adapter for EdgeDB."""
import dataclasses
from datetime import datetime
from typing import Any, Dict, Generic, Optional
from typing import Any, Dict, Generic, List, Optional

from edgedb import AsyncIOClient
from fastapi_users.authentication.strategy.db import AP, AccessTokenDatabase
Expand All @@ -19,23 +19,26 @@ async def get_by_token(
self, token: str, max_age: Optional[datetime] = None
) -> Optional[AP]:
"""Get a single access token by token."""
access_token = await user_api.get_access_token(
access_token: AP | None = await user_api.get_access_token(
self.client, cast="str", key="token", value=token, max_age=max_age
)
return access_token

async def create(self, create_dict: Dict[str, Any]) -> AP:
"""Create an access token."""
access_token = await user_api.create_access_token(self.client, **create_dict)
access_token: AP = await user_api.create_access_token(
self.client, **create_dict
)
return access_token

async def update(self, access_token: AP, update_dict: Dict[str, Any]) -> AP:
"""Update an access token."""
access_token = await user_api.get_access_token(
self.client, cast="str", key="token", value=access_token.token
user = await user_api.get_user_by_access_token(
self.client, access_token=access_token.token
)
if access_token:
if user:
update_dict.update(dataclasses.asdict(access_token))
update_dict["user_id"] = user[0].id
access_token = await user_api.update_token(
self.client, **{k: v for k, v in update_dict.items() if k not in ["id"]}
)
Expand Down
Loading

0 comments on commit 9cae313

Please sign in to comment.