usermanager working

This commit is contained in:
Tiago vasconcelos 2021-10-12 17:04:49 +01:00
parent f4dfc1b689
commit 04462f337f
6 changed files with 40 additions and 37 deletions

View File

@ -10,31 +10,27 @@ from lnbits.core.crud import (
) )
from . import db from . import db
from .models import Users, Wallets from .models import Users, Wallets, CreateUserData
### Users ### Users
async def create_usermanager_user( async def create_usermanager_user(
user_name: str, data: CreateUserData
wallet_name: str,
admin_id: str,
email: Optional[str] = None,
password: Optional[str] = None,
) -> Users: ) -> Users:
account = await create_account() account = await create_account()
user = await get_user(account.id) user = await get_user(account.id)
assert user, "Newly created user couldn't be retrieved" assert user, "Newly created user couldn't be retrieved"
wallet = await create_wallet(user_id=user.id, wallet_name=wallet_name) wallet = await create_wallet(user_id=user.id, wallet_name=data.wallet_name)
await db.execute( await db.execute(
""" """
INSERT INTO usermanager.users (id, name, admin, email, password) INSERT INTO usermanager.users (id, name, admin, email, password)
VALUES (?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?)
""", """,
(user.id, user_name, admin_id, email, password), (user.id, data.user_name, data.admin_id, data.email, data.password),
) )
await db.execute( await db.execute(
@ -42,7 +38,7 @@ async def create_usermanager_user(
INSERT INTO usermanager.wallets (id, admin, name, "user", adminkey, inkey) INSERT INTO usermanager.wallets (id, admin, name, "user", adminkey, inkey)
VALUES (?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?)
""", """,
(wallet.id, admin_id, wallet_name, user.id, wallet.adminkey, wallet.inkey), (wallet.id, data.admin_id, data.wallet_name, user.id, wallet.adminkey, wallet.inkey),
) )
user_created = await get_usermanager_user(user.id) user_created = await get_usermanager_user(user.id)
@ -59,6 +55,7 @@ async def get_usermanager_users(user_id: str) -> List[Users]:
rows = await db.fetchall( rows = await db.fetchall(
"SELECT * FROM usermanager.users WHERE admin = ?", (user_id,) "SELECT * FROM usermanager.users WHERE admin = ?", (user_id,)
) )
return [Users(**row) for row in rows] return [Users(**row) for row in rows]

View File

@ -6,8 +6,8 @@ class CreateUserData(BaseModel):
user_name: str = Query(...) user_name: str = Query(...)
wallet_name: str = Query(...) wallet_name: str = Query(...)
admin_id: str = Query(...) admin_id: str = Query(...)
email: str = Query(None) email: str = Query("")
password: str = Query(None) password: str = Query("")
class Users(BaseModel): class Users(BaseModel):

View File

@ -42,8 +42,8 @@
<code>JSON list of users</code> <code>JSON list of users</code>
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5> <h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
<code <code
>curl -X GET {{ request.url_root }}usermanager/api/v1/users -H "X-Api-Key: {{ >curl -X GET {{ request.url_root }}usermanager/api/v1/users -H
g.user.wallets[0].inkey }}" "X-Api-Key: {{ user.wallets[0].inkey }}"
</code> </code>
</q-card-section> </q-card-section>
</q-card> </q-card>
@ -62,8 +62,9 @@
<code>JSON list of users</code> <code>JSON list of users</code>
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5> <h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
<code <code
>curl -X GET {{ request.url_root }}usermanager/api/v1/users/&lt;user_id&gt; -H >curl -X GET {{ request.url_root
"X-Api-Key: {{ g.user.wallets[0].inkey }}" }}usermanager/api/v1/users/&lt;user_id&gt; -H "X-Api-Key: {{
user.wallets[0].inkey }}"
</code> </code>
</q-card-section> </q-card-section>
</q-card> </q-card>
@ -84,8 +85,9 @@
<code>JSON wallet data</code> <code>JSON wallet data</code>
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5> <h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
<code <code
>curl -X GET {{ request.url_root }}usermanager/api/v1/wallets/&lt;user_id&gt; -H >curl -X GET {{ request.url_root
"X-Api-Key: {{ g.user.wallets[0].inkey }}" }}usermanager/api/v1/wallets/&lt;user_id&gt; -H "X-Api-Key: {{
user.wallets[0].inkey }}"
</code> </code>
</q-card-section> </q-card-section>
</q-card> </q-card>
@ -106,8 +108,9 @@
<code>JSON a wallets transactions</code> <code>JSON a wallets transactions</code>
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5> <h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
<code <code
>curl -X GET {{ request.url_root }}usermanager/api/v1/wallets&lt;wallet_id&gt; -H >curl -X GET {{ request.url_root
"X-Api-Key: {{ g.user.wallets[0].inkey }}" }}usermanager/api/v1/wallets&lt;wallet_id&gt; -H "X-Api-Key: {{
user.wallets[0].inkey }}"
</code> </code>
</q-card-section> </q-card-section>
</q-card> </q-card>
@ -147,11 +150,11 @@
> >
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5> <h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
<code <code
>curl -X POST {{ request.url_root }}usermanager/api/v1/users -d '{"admin_id": "{{ >curl -X POST {{ request.url_root }}usermanager/api/v1/users -d
g.user.id }}", "wallet_name": &lt;string&gt;, "user_name": '{"admin_id": "{{ user.id }}", "wallet_name": &lt;string&gt;,
&lt;string&gt;, "email": &lt;Optional string&gt;, "password": &lt; "user_name": &lt;string&gt;, "email": &lt;Optional string&gt;,
Optional string&gt;}' -H "X-Api-Key: {{ g.user.wallets[0].inkey }}" -H "password": &lt; Optional string&gt;}' -H "X-Api-Key: {{
"Content-type: application/json" user.wallets[0].inkey }}" -H "Content-type: application/json"
</code> </code>
</q-card-section> </q-card-section>
</q-card> </q-card>
@ -185,10 +188,10 @@
> >
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5> <h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
<code <code
>curl -X POST {{ request.url_root }}usermanager/api/v1/wallets -d '{"user_id": >curl -X POST {{ request.url_root }}usermanager/api/v1/wallets -d
&lt;string&gt;, "wallet_name": &lt;string&gt;, "admin_id": "{{ '{"user_id": &lt;string&gt;, "wallet_name": &lt;string&gt;,
g.user.id }}"}' -H "X-Api-Key: {{ g.user.wallets[0].inkey }}" -H "admin_id": "{{ user.id }}"}' -H "X-Api-Key: {{ user.wallets[0].inkey
"Content-type: application/json" }}" -H "Content-type: application/json"
</code> </code>
</q-card-section> </q-card-section>
</q-card> </q-card>
@ -209,8 +212,9 @@
<code>{"X-Api-Key": &lt;string&gt;}</code> <code>{"X-Api-Key": &lt;string&gt;}</code>
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5> <h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
<code <code
>curl -X DELETE {{ request.url_root }}usermanager/api/v1/users/&lt;user_id&gt; -H >curl -X DELETE {{ request.url_root
"X-Api-Key: {{ g.user.wallets[0].inkey }}" }}usermanager/api/v1/users/&lt;user_id&gt; -H "X-Api-Key: {{
user.wallets[0].inkey }}"
</code> </code>
</q-card-section> </q-card-section>
</q-card> </q-card>
@ -226,8 +230,9 @@
<code>{"X-Api-Key": &lt;string&gt;}</code> <code>{"X-Api-Key": &lt;string&gt;}</code>
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5> <h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
<code <code
>curl -X DELETE {{ request.url_root }}usermanager/api/v1/wallets/&lt;wallet_id&gt; >curl -X DELETE {{ request.url_root
-H "X-Api-Key: {{ g.user.wallets[0].inkey }}" }}usermanager/api/v1/wallets/&lt;wallet_id&gt; -H "X-Api-Key: {{
user.wallets[0].inkey }}"
</code> </code>
</q-card-section> </q-card-section>
</q-card> </q-card>
@ -248,9 +253,9 @@
<code>{"X-Api-Key": &lt;string&gt;}</code> <code>{"X-Api-Key": &lt;string&gt;}</code>
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5> <h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
<code <code
>curl -X POST {{ request.url_root }}usermanager/api/v1/extensions -d '{"userid": >curl -X POST {{ request.url_root }}usermanager/api/v1/extensions -d
&lt;string&gt;, "extension": &lt;string&gt;, "active": '{"userid": &lt;string&gt;, "extension": &lt;string&gt;, "active":
&lt;integer&gt;}' -H "X-Api-Key: {{ g.user.wallets[0].inkey }}" -H &lt;integer&gt;}' -H "X-Api-Key: {{ user.wallets[0].inkey }}" -H
"Content-type: application/json" "Content-type: application/json"
</code> </code>
</q-card-section> </q-card-section>

View File

@ -368,6 +368,7 @@
self.users = _.reject(self.users, function (obj) { self.users = _.reject(self.users, function (obj) {
return obj.id == userId return obj.id == userId
}) })
self.getWallets()
}) })
.catch(function (error) { .catch(function (error) {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)

View File

@ -10,4 +10,4 @@ from . import usermanager_ext, usermanager_renderer
@usermanager_ext.get("/", response_class=HTMLResponse) @usermanager_ext.get("/", response_class=HTMLResponse)
async def index(request: Request, user: User = Depends(check_user_exists)): async def index(request: Request, user: User = Depends(check_user_exists)):
return await render_template("usermanager/index.html", user=user.dict()) return usermanager_renderer().TemplateResponse("usermanager/index.html", {"request": request,"user": user.dict()})

View File

@ -50,7 +50,7 @@ async def api_usermanager_user(user_id, wallet: WalletTypeInfo = Depends(get_key
# } # }
# ) # )
async def api_usermanager_users_create(data: CreateUserData, wallet: WalletTypeInfo = Depends(get_key_type)): async def api_usermanager_users_create(data: CreateUserData, wallet: WalletTypeInfo = Depends(get_key_type)):
user = await create_usermanager_user(**data) user = await create_usermanager_user(data)
full = user.dict() full = user.dict()
full["wallets"] = [wallet.dict() for wallet in await get_usermanager_users_wallets(user.id)] full["wallets"] = [wallet.dict() for wallet in await get_usermanager_users_wallets(user.id)]
return full return full