mirror of
https://github.com/lnbits/lnbits-legend.git
synced 2025-02-21 22:11:59 +01:00
Add files via upload
This commit is contained in:
parent
8dfdd15adb
commit
d72678972d
10 changed files with 838 additions and 0 deletions
9
lnbits/extensions/usermanager/README.md
Normal file
9
lnbits/extensions/usermanager/README.md
Normal file
|
@ -0,0 +1,9 @@
|
|||
<h1>User Manager</h1>
|
||||
<h2>Make and manager users/wallets</h2>
|
||||
To help developers use LNbits to manage their users, the User Manager extention allows the creation and management of users and wallets. For example, a games developer may be developing a game that needs each user to have their own wallet, LNbits can be included in the develpoers stack as the user and wallet manager.
|
||||
<img src="https://i.imgur.com/P1tvBSG.png">
|
||||
|
||||
|
||||
<h2>API endpoints</h2>
|
||||
|
||||
<code>curl -X GET http://YOUR-TOR-ADDRESS</code>
|
8
lnbits/extensions/usermanager/__init__.py
Normal file
8
lnbits/extensions/usermanager/__init__.py
Normal file
|
@ -0,0 +1,8 @@
|
|||
from flask import Blueprint
|
||||
|
||||
|
||||
usermanager_ext: Blueprint = Blueprint("usermanager", __name__, static_folder="static", template_folder="templates")
|
||||
|
||||
|
||||
from .views_api import * # noqa
|
||||
from .views import * # noqa
|
6
lnbits/extensions/usermanager/config.json
Normal file
6
lnbits/extensions/usermanager/config.json
Normal file
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"name": "User Manager",
|
||||
"short_description": "Generate users and wallets",
|
||||
"icon": "person_add",
|
||||
"contributors": ["benarc"]
|
||||
}
|
117
lnbits/extensions/usermanager/crud.py
Normal file
117
lnbits/extensions/usermanager/crud.py
Normal file
|
@ -0,0 +1,117 @@
|
|||
from lnbits.db import open_ext_db
|
||||
from lnbits.settings import WALLET
|
||||
from .models import Users, Wallets
|
||||
from typing import List, Optional, Union
|
||||
|
||||
from ...core.crud import (
|
||||
create_account,
|
||||
get_user,
|
||||
update_user_extension,
|
||||
get_wallet_payments,
|
||||
create_wallet,
|
||||
delete_wallet,
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
###Users
|
||||
|
||||
def create_usermanager_user(user_name: str, wallet_name: str, admin_id: str) -> Users:
|
||||
user = get_user(create_account().id)
|
||||
|
||||
wallet = create_wallet(user_id=user.id, wallet_name=wallet_name)
|
||||
|
||||
with open_ext_db("usermanager") as db:
|
||||
db.execute(
|
||||
"""
|
||||
INSERT INTO users (id, name, admin)
|
||||
VALUES (?, ?, ?)
|
||||
""",
|
||||
(user.id, user_name, admin_id),
|
||||
)
|
||||
|
||||
db.execute(
|
||||
"""
|
||||
INSERT INTO wallets (id, admin, name, user, adminkey, inkey)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(wallet.id, admin_id, wallet_name, user.id, wallet.adminkey, wallet.inkey)
|
||||
)
|
||||
|
||||
return get_usermanager_user(user.id)
|
||||
|
||||
|
||||
def get_usermanager_user(user_id: str) -> Users:
|
||||
with open_ext_db("usermanager") as db:
|
||||
|
||||
row = db.fetchone("SELECT * FROM users WHERE id = ?", (user_id,))
|
||||
|
||||
|
||||
return Users(**row) if row else None
|
||||
|
||||
|
||||
def get_usermanager_users(user_id: str) -> Users:
|
||||
|
||||
with open_ext_db("usermanager") as db:
|
||||
rows = db.fetchall("SELECT * FROM users WHERE admin = ?", (user_id,))
|
||||
|
||||
return [Users(**row) for row in rows]
|
||||
|
||||
|
||||
def delete_usermanager_user(user_id: str) -> None:
|
||||
row = get_usermanager_wallets(user_id)
|
||||
print("test")
|
||||
with open_ext_db("usermanager") as db:
|
||||
db.execute("DELETE FROM users WHERE id = ?", (user_id,))
|
||||
row
|
||||
for r in row:
|
||||
delete_wallet( user_id=user_id, wallet_id=r.id)
|
||||
with open_ext_db("usermanager") as dbb:
|
||||
dbb.execute("DELETE FROM wallets WHERE user = ?", (user_id,))
|
||||
|
||||
###Wallets
|
||||
|
||||
def create_usermanager_wallet(user_id: str, wallet_name: str, admin_id: str) -> Wallets:
|
||||
wallet = create_wallet(user_id=user_id, wallet_name=wallet_name)
|
||||
with open_ext_db("usermanager") as db:
|
||||
|
||||
db.execute(
|
||||
"""
|
||||
INSERT INTO wallets (id, admin, name, user, adminkey, inkey)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(wallet.id, admin_id, wallet_name, user_id, wallet.adminkey, wallet.inkey)
|
||||
)
|
||||
|
||||
return get_usermanager_wallet(wallet.id)
|
||||
|
||||
def get_usermanager_wallet(wallet_id: str) -> Optional[Wallets]:
|
||||
with open_ext_db("usermanager") as db:
|
||||
row = db.fetchone("SELECT * FROM wallets WHERE id = ?", (wallet_id,))
|
||||
|
||||
return Wallets(**row) if row else None
|
||||
|
||||
|
||||
def get_usermanager_wallets(user_id: str) -> Wallets:
|
||||
|
||||
with open_ext_db("usermanager") as db:
|
||||
rows = db.fetchall("SELECT * FROM wallets WHERE admin = ?", (user_id,))
|
||||
|
||||
return [Wallets(**row) for row in rows]
|
||||
|
||||
|
||||
def get_usermanager_wallet_transactions(wallet_id: str) -> Users:
|
||||
return get_wallet_payments(wallet_id=wallet_id,include_all_pending=False)
|
||||
|
||||
def get_usermanager_wallet_balances(user_id: str) -> Users:
|
||||
user = get_user(user_id)
|
||||
return (user.wallets)
|
||||
|
||||
|
||||
def delete_usermanager_wallet(wallet_id: str, user_id: str) -> None:
|
||||
delete_wallet( user_id=user_id, wallet_id=wallet_id)
|
||||
with open_ext_db("usermanager") as db:
|
||||
db.execute("DELETE FROM wallets WHERE id = ?", (wallet_id,))
|
||||
|
36
lnbits/extensions/usermanager/migrations.py
Normal file
36
lnbits/extensions/usermanager/migrations.py
Normal file
|
@ -0,0 +1,36 @@
|
|||
from lnbits.db import open_ext_db
|
||||
|
||||
|
||||
def m001_initial(db):
|
||||
"""
|
||||
Initial users table.
|
||||
"""
|
||||
db.execute("""
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
admin TEXT NOT NULL,
|
||||
email TEXT,
|
||||
password TEXT
|
||||
);
|
||||
""")
|
||||
|
||||
|
||||
"""
|
||||
Initial wallets table.
|
||||
"""
|
||||
db.execute("""
|
||||
CREATE TABLE IF NOT EXISTS wallets (
|
||||
id TEXT PRIMARY KEY,
|
||||
admin TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
user TEXT NOT NULL,
|
||||
adminkey TEXT NOT NULL,
|
||||
inkey TEXT NOT NULL
|
||||
);
|
||||
""")
|
||||
|
||||
def migrate():
|
||||
with open_ext_db("usermanager") as db:
|
||||
m001_initial(db)
|
||||
|
18
lnbits/extensions/usermanager/models.py
Normal file
18
lnbits/extensions/usermanager/models.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
from typing import NamedTuple
|
||||
|
||||
class Users(NamedTuple):
|
||||
id: str
|
||||
name: str
|
||||
admin: str
|
||||
email: str
|
||||
password: str
|
||||
|
||||
class Wallets(NamedTuple):
|
||||
id: str
|
||||
admin: str
|
||||
name: str
|
||||
user: str
|
||||
adminkey: str
|
||||
inkey: str
|
||||
|
||||
|
|
@ -0,0 +1,121 @@
|
|||
|
||||
<q-expansion-item
|
||||
group="extras"
|
||||
icon="swap_vertical_circle"
|
||||
label="Info"
|
||||
:content-inset-level="0.5"
|
||||
>
|
||||
<q-card>
|
||||
<q-card-section>
|
||||
<h5 class="text-subtitle1 q-my-none">User Manager: Make and manager users/wallets</h5>
|
||||
<p>To help developers use LNbits to manage their users, the User Manager extention allows the creation and management of users and wallets. <br/>For example, a games developer may be developing a game that needs each user to have their own wallet, LNbits can be included in the develpoers stack as the user and wallet manager.<br/>
|
||||
<small> Created by, <a href="https://github.com/benarc">Ben Arc</a></small></p>
|
||||
</q-card>
|
||||
</q-card-section>
|
||||
|
||||
</q-card-section>
|
||||
</q-expansion-item>
|
||||
<q-expansion-item
|
||||
group="extras"
|
||||
icon="swap_vertical_circle"
|
||||
label="API info"
|
||||
:content-inset-level="0.5"
|
||||
>
|
||||
|
||||
<q-expansion-item group="api" dense expand-separator label="GET users">
|
||||
<q-card>
|
||||
<q-card-section>
|
||||
<code><span class="text-light-blue">GET</span> /usermanager/api/v1/users</code>
|
||||
<h5 class="text-caption q-mt-sm q-mb-none">Body (application/json)</h5>
|
||||
<h5 class="text-caption q-mt-sm q-mb-none">Returns 201 CREATED (application/json)</h5>
|
||||
<code>JSON list of users</code>
|
||||
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
|
||||
<code>curl -X GET {{ request.url_root }}usermanager/api/v1/users -H "X-Api-Key: {{ g.user.wallets[0].inkey }}" </code>
|
||||
</q-card-section>
|
||||
</q-card>
|
||||
</q-expansion-item>
|
||||
<q-expansion-item group="api" dense expand-separator label="GET wallets">
|
||||
<q-card>
|
||||
<q-card-section>
|
||||
<code><span class="text-light-blue">GET</span> /usermanager/api/v1/wallets/<user_id></code>
|
||||
<h5 class="text-caption q-mt-sm q-mb-none">Headers</h5>
|
||||
<code>{"X-Api-Key": <string>}</code>
|
||||
<h5 class="text-caption q-mt-sm q-mb-none">Body (application/json)</h5>
|
||||
<h5 class="text-caption q-mt-sm q-mb-none">Returns 201 CREATED (application/json)</h5>
|
||||
<code>JSON wallet data</code>
|
||||
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
|
||||
<code>curl -X GET {{ request.url_root }}usermanager/api/v1/wallets/<user_id> -H "X-Api-Key: {{ g.user.wallets[0].inkey }}" </code>
|
||||
</q-card-section>
|
||||
</q-card>
|
||||
</q-expansion-item>
|
||||
<q-expansion-item group="api" dense expand-separator label="GET transactions">
|
||||
<q-card>
|
||||
<q-card-section>
|
||||
<code><span class="text-light-blue">GET</span> /usermanager/api/v1/wallets<wallet_id></code>
|
||||
<h5 class="text-caption q-mt-sm q-mb-none">Headers</h5>
|
||||
<code>{"X-Api-Key": <string>}</code>
|
||||
<h5 class="text-caption q-mt-sm q-mb-none">Body (application/json)</h5>
|
||||
<h5 class="text-caption q-mt-sm q-mb-none">Returns 201 CREATED (application/json)</h5>
|
||||
<code>JSON a wallets transactions</code>
|
||||
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
|
||||
<code>curl -X GET {{ request.url_root }}usermanager/api/v1/wallets<wallet_id> -H "X-Api-Key: {{ g.user.wallets[0].inkey }}" </code>
|
||||
</q-card-section>
|
||||
</q-card>
|
||||
</q-expansion-item>
|
||||
<q-expansion-item group="api" dense expand-separator label="POST user + initial wallet">
|
||||
<q-card>
|
||||
<q-card-section>
|
||||
<code><span class="text-light-green">POST</span> /usermanager/api/v1/users</code>
|
||||
<h5 class="text-caption q-mt-sm q-mb-none">Headers</h5>
|
||||
<code>{"X-Api-Key": <string>, "Content-type": "application/json"}</code>
|
||||
<h5 class="text-caption q-mt-sm q-mb-none">Body (application/json) - "admin_id" is a YOUR user ID</h5>
|
||||
<code>{"admin_id": <string>, "user_name": <string>, "wallet_name": <string>}</code>
|
||||
<h5 class="text-caption q-mt-sm q-mb-none">Returns 201 CREATED (application/json)</h5>
|
||||
<code>{"checking_id": <string>,"payment_request": <string>}</code>
|
||||
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
|
||||
<code>curl -X POST {{ request.url_root }}usermanager/api/v1/users -d '{"admin_id": "{{ g.user.id }}", "wallet_name": <string>, "user_name": <string>}' -H "X-Api-Key: {{ g.user.wallets[0].inkey }}" -H "Content-type: application/json"
|
||||
</code>
|
||||
</q-card-section>
|
||||
</q-card>
|
||||
</q-expansion-item>
|
||||
<q-expansion-item group="api" dense expand-separator label="POST wallet">
|
||||
<q-card>
|
||||
<q-card-section>
|
||||
<code><span class="text-light-green">POST</span> /usermanager/api/v1/wallets</code>
|
||||
<h5 class="text-caption q-mt-sm q-mb-none">Headers</h5>
|
||||
<code>{"X-Api-Key": <string>, "Content-type": "application/json"}</code>
|
||||
<h5 class="text-caption q-mt-sm q-mb-none">Body (application/json) - "admin_id" is a YOUR user ID</h5>
|
||||
<code>{"user_id": <string>, "wallet_name": <string>, "admin_id": <string>}</code>
|
||||
<h5 class="text-caption q-mt-sm q-mb-none">Returns 201 CREATED (application/json)</h5>
|
||||
<code>{"checking_id": <string>,"payment_request": <string>}</code>
|
||||
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
|
||||
<code>curl -X POST {{ request.url_root }}usermanager/api/v1/wallets -d '{"user_id": <string>, "wallet_name": <string>, "admin_id": "{{ g.user.id }}"}' -H "X-Api-Key: {{ g.user.wallets[0].inkey }}" -H "Content-type: application/json"
|
||||
</code>
|
||||
</q-card-section>
|
||||
</q-card>
|
||||
</q-expansion-item>
|
||||
<q-expansion-item group="api" dense expand-separator label="DELETE user and their wallets">
|
||||
<q-card>
|
||||
<q-card-section>
|
||||
<code><span class="text-red">DELETE</span> /usermanager/api/v1/users/<user_id></code>
|
||||
<h5 class="text-caption q-mt-sm q-mb-none">Headers</h5>
|
||||
<code>{"X-Api-Key": <string>}</code>
|
||||
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
|
||||
<code>curl -X DELETE {{ request.url_root }}usermanager/api/v1/users/<user_id> -H "X-Api-Key: {{ g.user.wallets[0].inkey }}" </code>
|
||||
</q-card-section>
|
||||
</q-card>
|
||||
</q-expansion-item>
|
||||
<q-expansion-item group="api" dense expand-separator label="DELETE wallet">
|
||||
<q-card>
|
||||
<q-card-section>
|
||||
<code><span class="text-red">DELETE</span> /usermanager/api/v1/wallets/<wallet_id></code>
|
||||
<h5 class="text-caption q-mt-sm q-mb-none">Headers</h5>
|
||||
<code>{"X-Api-Key": <string>}</code>
|
||||
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
|
||||
<code>curl -X DELETE {{ request.url_root }}usermanager/api/v1/wallets/<wallet_id> -H "X-Api-Key: {{ g.user.wallets[0].inkey }}" </code>
|
||||
</q-card-section>
|
||||
</q-card>
|
||||
</q-expansion-item>
|
||||
|
||||
|
||||
</q-expansion-item>
|
416
lnbits/extensions/usermanager/templates/usermanager/index.html
Normal file
416
lnbits/extensions/usermanager/templates/usermanager/index.html
Normal file
|
@ -0,0 +1,416 @@
|
|||
{% extends "base.html" %}
|
||||
|
||||
{% from "macros.jinja" import window_vars with context %}
|
||||
|
||||
|
||||
{% block page %}
|
||||
<div class="row q-col-gutter-md">
|
||||
<div class="col-12 col-md-8 col-lg-7 q-gutter-y-md">
|
||||
<q-card>
|
||||
<q-card-section>
|
||||
<q-btn unelevated color="deep-purple" @click="userDialog.show = true">New User</q-btn>
|
||||
<q-btn unelevated color="deep-purple" @click="walletDialog.show = true">New Wallet
|
||||
</q-btn>
|
||||
</q-card-section>
|
||||
</q-card>
|
||||
|
||||
<q-card>
|
||||
<q-card-section>
|
||||
<div class="row items-center no-wrap q-mb-md">
|
||||
<div class="col">
|
||||
<h5 class="text-subtitle1 q-my-none">Users</h5>
|
||||
</div>
|
||||
<div class="col-auto">
|
||||
<q-btn flat color="grey" @click="exportUsersCSV">Export to CSV</q-btn>
|
||||
</div>
|
||||
</div>
|
||||
<q-table dense flat
|
||||
:data="users"
|
||||
row-key="id"
|
||||
:columns="usersTable.columns"
|
||||
:pagination.sync="usersTable.pagination">
|
||||
{% raw %}
|
||||
<template v-slot:header="props">
|
||||
<q-tr :props="props">
|
||||
<q-th
|
||||
v-for="col in props.cols"
|
||||
:key="col.name"
|
||||
:props="props"
|
||||
>
|
||||
{{ col.label }}
|
||||
</q-th>
|
||||
<q-th auto-width></q-th>
|
||||
</q-tr>
|
||||
</template>
|
||||
<template v-slot:body="props">
|
||||
<q-tr :props="props">
|
||||
<q-td
|
||||
v-for="col in props.cols"
|
||||
:key="col.name"
|
||||
:props="props"
|
||||
>
|
||||
{{ col.value }}
|
||||
</q-td>
|
||||
<q-td auto-width>
|
||||
|
||||
<q-btn flat dense size="xs" @click="deleteUser(props.row.id)" icon="cancel" color="pink"></q-btn>
|
||||
</q-td>
|
||||
</q-tr>
|
||||
</template>
|
||||
{% endraw %}
|
||||
</q-table>
|
||||
</q-card-section>
|
||||
</q-card>
|
||||
|
||||
<q-card>
|
||||
<q-card-section>
|
||||
<div class="row items-center no-wrap q-mb-md">
|
||||
<div class="col">
|
||||
<h5 class="text-subtitle1 q-my-none">Wallets</h5>
|
||||
</div>
|
||||
<div class="col-auto">
|
||||
<q-btn flat color="grey" @click="exportWalletsCSV">Export to CSV</q-btn>
|
||||
</div>
|
||||
</div>
|
||||
<q-table dense flat
|
||||
:data="wallets"
|
||||
row-key="id"
|
||||
:columns="walletsTable.columns"
|
||||
:pagination.sync="walletsTable.pagination">
|
||||
{% raw %}
|
||||
<template v-slot:header="props">
|
||||
<q-tr :props="props">
|
||||
<q-th auto-width></q-th>
|
||||
<q-th
|
||||
v-for="col in props.cols"
|
||||
:key="col.name"
|
||||
:props="props"
|
||||
>
|
||||
{{ col.label }}
|
||||
</q-th>
|
||||
<q-th auto-width></q-th>
|
||||
</q-tr>
|
||||
</template>
|
||||
<template v-slot:body="props">
|
||||
<q-tr :props="props">
|
||||
<q-td auto-width>
|
||||
<q-btn unelevated dense size="xs" icon="add_shopping_cart" :color="($q.dark.isActive) ? 'grey-7' : 'grey-5'" type="a" :href="props.row.walllink" target="_blank"></q-btn>
|
||||
<q-tooltip>
|
||||
Link to wallet
|
||||
</q-tooltip>
|
||||
|
||||
</q-td>
|
||||
<q-td
|
||||
v-for="col in props.cols"
|
||||
:key="col.name"
|
||||
:props="props"
|
||||
>
|
||||
|
||||
{{ col.value }}
|
||||
</q-td>
|
||||
<q-td auto-width>
|
||||
|
||||
<q-btn flat dense size="xs" @click="deleteWallet(props.row.id)" icon="cancel" color="pink"></q-btn>
|
||||
</q-td>
|
||||
</q-tr>
|
||||
</template>
|
||||
{% endraw %}
|
||||
</q-table>
|
||||
</q-card-section>
|
||||
</q-card>
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
<div class="col-12 col-md-4 col-lg-5 q-gutter-y-md">
|
||||
<q-card>
|
||||
<q-card-section>
|
||||
<h6 class="text-subtitle1 q-my-none">LNbits User Manager Extension</h6>
|
||||
</q-card-section>
|
||||
<q-card-section class="q-pa-none">
|
||||
<q-separator></q-separator>
|
||||
<q-list>
|
||||
{% include "usermanager/_api_docs.html" %}
|
||||
</q-list>
|
||||
</q-card-section>
|
||||
</q-card>
|
||||
</div>
|
||||
|
||||
|
||||
<q-dialog v-model="userDialog.show" position="top">
|
||||
<q-card class="q-pa-lg q-pt-xl" style="width: 500px">
|
||||
<q-form @submit="sendUserFormData" class="q-gutter-md">
|
||||
<q-input filled dense
|
||||
v-model.trim="userDialog.data.usrname"
|
||||
label="Username"></q-input>
|
||||
<q-input filled dense
|
||||
v-model.trim="userDialog.data.walname"
|
||||
label="Initial wallet name"></q-input>
|
||||
|
||||
<q-btn unelevated
|
||||
color="deep-purple"
|
||||
:disable="userDialog.data.walname == null"
|
||||
type="submit">Create User</q-btn>
|
||||
<q-btn v-close-popup flat color="grey" class="q-ml-auto">Cancel</q-btn>
|
||||
</div>
|
||||
</q-form>
|
||||
</q-card>
|
||||
</q-dialog>
|
||||
|
||||
|
||||
|
||||
<q-dialog v-model="walletDialog.show" position="top">
|
||||
<q-card class="q-pa-lg q-pt-xl" style="width: 500px">
|
||||
<q-form @submit="sendWalletFormData" class="q-gutter-md">
|
||||
<q-select filled dense emit-value v-model="walletDialog.data.user" :options="userOptions" label="User *">
|
||||
</q-select>
|
||||
<q-input filled dense
|
||||
v-model.trim="walletDialog.data.walname"
|
||||
label="Wallet name"></q-input>
|
||||
<q-btn unelevated
|
||||
color="deep-purple"
|
||||
:disable="walletDialog.data.walname == null"
|
||||
type="submit">Create User</q-btn>
|
||||
<q-btn v-close-popup flat color="grey" class="q-ml-auto">Cancel</q-btn>
|
||||
</div>
|
||||
</q-form>
|
||||
</q-card>
|
||||
</q-dialog>
|
||||
|
||||
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
{% block scripts %}
|
||||
{{ window_vars(user) }}
|
||||
<script>
|
||||
|
||||
var mapUserManager = function (obj) {
|
||||
|
||||
obj.date = Quasar.utils.date.formatDate(new Date(obj.time * 1000), 'YYYY-MM-DD HH:mm');
|
||||
obj.fsat = new Intl.NumberFormat(LOCALE).format(obj.amount);
|
||||
obj.walllink = ['../wallet?usr=', obj.user, '&wal=', obj.id].join('');
|
||||
obj._data = _.clone(obj);
|
||||
return obj;
|
||||
}
|
||||
|
||||
new Vue({
|
||||
el: '#vue',
|
||||
mixins: [windowMixin],
|
||||
data: function () {
|
||||
return {
|
||||
|
||||
wallets: [],
|
||||
users: [],
|
||||
|
||||
usersTable: {
|
||||
columns: [
|
||||
{name: 'id', align: 'left', label: 'ID', field: 'id'},
|
||||
{name: 'name', align: 'left', label: 'Username', field: 'name'}
|
||||
],
|
||||
pagination: {
|
||||
rowsPerPage: 10
|
||||
}
|
||||
},
|
||||
walletsTable: {
|
||||
columns: [
|
||||
{name: 'id', align: 'left', label: 'ID', field: 'id'},
|
||||
{name: 'name', align: 'left', label: 'Name', field: 'name'},
|
||||
{name: 'user', align: 'left', label: 'User', field: 'user'},
|
||||
{name: 'adminkey', align: 'left', label: 'Admin Key', field: 'adminkey'},
|
||||
{name: 'inkey', align: 'left', label: 'Invoice Key', field: 'inkey'}
|
||||
],
|
||||
pagination: {
|
||||
rowsPerPage: 10
|
||||
}
|
||||
},
|
||||
walletDialog: {
|
||||
show: false,
|
||||
data: {}
|
||||
},
|
||||
userDialog: {
|
||||
show: false,
|
||||
data: {}
|
||||
},
|
||||
};
|
||||
},
|
||||
computed: {
|
||||
userOptions: function () {
|
||||
return this.users.map(function (obj) {
|
||||
console.log(obj.id)
|
||||
return {
|
||||
value: String(obj.id),
|
||||
label: String(obj.id)
|
||||
|
||||
};
|
||||
});
|
||||
},
|
||||
},
|
||||
methods: {
|
||||
|
||||
///////////////Users////////////////////////////
|
||||
|
||||
|
||||
getUsers: function () {
|
||||
|
||||
var self = this;
|
||||
|
||||
LNbits.api.request(
|
||||
'GET',
|
||||
'/usermanager/api/v1/users',
|
||||
this.g.user.wallets[0].inkey
|
||||
).then(function (response) {
|
||||
|
||||
|
||||
self.users = response.data.map(function (obj) {
|
||||
|
||||
return mapUserManager(obj);
|
||||
});
|
||||
|
||||
});
|
||||
},
|
||||
|
||||
openUserUpdateDialog: function (linkId) {
|
||||
var link = _.findWhere(this.users, {id: linkId});
|
||||
|
||||
this.userDialog.data = _.clone(link._data);
|
||||
this.userDialog.show = true;
|
||||
},
|
||||
sendUserFormData: function () {
|
||||
if (this.userDialog.data.id){}
|
||||
else{
|
||||
|
||||
var data = {
|
||||
|
||||
admin_id: this.g.user.id,
|
||||
user_name: this.userDialog.data.usrname,
|
||||
wallet_name: this.userDialog.data.walname
|
||||
};}
|
||||
|
||||
{ this.createUser(data); }
|
||||
},
|
||||
|
||||
createUser: function (data) {
|
||||
var self = this;
|
||||
LNbits.api.request(
|
||||
'POST',
|
||||
'/usermanager/api/v1/users',
|
||||
this.g.user.wallets[0].inkey,
|
||||
data
|
||||
).then(function (response) {
|
||||
|
||||
self.users.push(mapUserManager(response.data));
|
||||
self.userDialog.show = false;
|
||||
self.userDialog.data = {};
|
||||
data = {};
|
||||
}).catch(function (error) {
|
||||
LNbits.utils.notifyApiError(error);
|
||||
});
|
||||
},
|
||||
deleteUser: function (userId) {
|
||||
var self = this;
|
||||
|
||||
console.log(userId)
|
||||
LNbits.utils.confirmDialog(
|
||||
'Are you sure you want to delete this User link?'
|
||||
).onOk(function () {
|
||||
|
||||
LNbits.api.request(
|
||||
'DELETE',
|
||||
'/usermanager/api/v1/users/' + userId,
|
||||
self.g.user.wallets[0].inkey
|
||||
).then(function (response) {
|
||||
self.users = _.reject(self.users, function (obj) { return obj.id == userId; });
|
||||
}).catch(function (error) {
|
||||
LNbits.utils.notifyApiError(error);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
exportUsersCSV: function () {
|
||||
LNbits.utils.exportCSV(this.usersTable.columns, this.users);
|
||||
},
|
||||
|
||||
|
||||
///////////////Wallets////////////////////////////
|
||||
|
||||
getWallets: function () {
|
||||
var self = this;
|
||||
|
||||
LNbits.api.request(
|
||||
'GET',
|
||||
'/usermanager/api/v1/wallets',
|
||||
this.g.user.wallets[0].inkey
|
||||
).then(function (response) {
|
||||
|
||||
self.wallets = response.data.map(function (obj) {
|
||||
return mapUserManager(obj);
|
||||
});
|
||||
});
|
||||
},
|
||||
openWalletUpdateDialog: function (linkId) {
|
||||
var link = _.findWhere(this.users, {id: linkId});
|
||||
|
||||
this.walletDialog.data = _.clone(link._data);
|
||||
this.walletDialog.show = true;
|
||||
},
|
||||
sendWalletFormData: function () {
|
||||
if (this.walletDialog.data.id){}
|
||||
else{
|
||||
var data = {
|
||||
user_id: this.walletDialog.data.user,
|
||||
admin_id: this.g.user.id,
|
||||
wallet_name: this.walletDialog.data.walname
|
||||
};}
|
||||
|
||||
{ this.createWallet(data); }
|
||||
},
|
||||
|
||||
createWallet: function (data) {
|
||||
var self = this;
|
||||
LNbits.api.request(
|
||||
'POST',
|
||||
'/usermanager/api/v1/wallets',
|
||||
this.g.user.wallets[0].inkey,
|
||||
data
|
||||
).then(function (response) {
|
||||
self.wallets.push(mapUserManager(response.data));
|
||||
self.walletDialog.show = false;
|
||||
self.walletDialog.data = {};
|
||||
data = {};
|
||||
}).catch(function (error) {
|
||||
LNbits.utils.notifyApiError(error);
|
||||
});
|
||||
},
|
||||
deleteWallet: function (userId) {
|
||||
var self = this;
|
||||
|
||||
LNbits.utils.confirmDialog(
|
||||
'Are you sure you want to delete this wallet link?'
|
||||
).onOk(function () {
|
||||
LNbits.api.request(
|
||||
'DELETE',
|
||||
'/usermanager/api/v1/wallets/' + userId,
|
||||
self.g.user.wallets[0].inkey
|
||||
).then(function (response) {
|
||||
self.wallets = _.reject(self.wallets, function (obj) { return obj.id == userId; });
|
||||
}).catch(function (error) {
|
||||
LNbits.utils.notifyApiError(error);
|
||||
});
|
||||
});
|
||||
},
|
||||
exportWalletsCSV: function () {
|
||||
LNbits.utils.exportCSV(this.walletsTable.columns, this.wallets);
|
||||
}
|
||||
|
||||
},
|
||||
created: function () {
|
||||
if (this.g.user.wallets.length) {
|
||||
this.getUsers();
|
||||
this.getWallets();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
</script>
|
||||
{% endblock %}
|
14
lnbits/extensions/usermanager/views.py
Normal file
14
lnbits/extensions/usermanager/views.py
Normal file
|
@ -0,0 +1,14 @@
|
|||
from flask import g, abort, render_template, jsonify
|
||||
import json
|
||||
from lnbits.decorators import check_user_exists, validate_uuids
|
||||
from lnbits.extensions.usermanager import usermanager_ext
|
||||
from lnbits.helpers import Status
|
||||
from lnbits.db import open_ext_db
|
||||
|
||||
|
||||
@usermanager_ext.route("/")
|
||||
@validate_uuids(["usr"], required=True)
|
||||
@check_user_exists()
|
||||
def index():
|
||||
|
||||
return render_template("usermanager/index.html", user=g.user)
|
93
lnbits/extensions/usermanager/views_api.py
Normal file
93
lnbits/extensions/usermanager/views_api.py
Normal file
|
@ -0,0 +1,93 @@
|
|||
from flask import g, jsonify, request
|
||||
|
||||
from lnbits.core.crud import get_user
|
||||
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
|
||||
from lnbits.helpers import Status
|
||||
|
||||
from lnbits.extensions.usermanager import usermanager_ext
|
||||
from .crud import create_usermanager_user, get_usermanager_user, get_usermanager_users, get_usermanager_wallet_transactions, get_usermanager_wallet_balances, delete_usermanager_user, create_usermanager_wallet, get_usermanager_wallet, get_usermanager_wallets, delete_usermanager_wallet
|
||||
from lnbits.core.services import create_invoice
|
||||
from base64 import urlsafe_b64encode
|
||||
from uuid import uuid4
|
||||
from lnbits.db import open_ext_db
|
||||
|
||||
|
||||
|
||||
###Users
|
||||
|
||||
@usermanager_ext.route("/api/v1/users", methods=["GET"])
|
||||
@api_check_wallet_key(key_type="invoice")
|
||||
def api_usermanager_users():
|
||||
user_id = g.wallet.user
|
||||
return jsonify([user._asdict() for user in get_usermanager_users(user_id)]), Status.OK
|
||||
|
||||
|
||||
@usermanager_ext.route("/api/v1/users", methods=["POST"])
|
||||
@api_check_wallet_key(key_type="invoice")
|
||||
@api_validate_post_request(schema={
|
||||
"admin_id": {"type": "string", "empty": False, "required": True},
|
||||
"user_name": {"type": "string", "empty": False, "required": True},
|
||||
"wallet_name": {"type": "string", "empty": False, "required": True}
|
||||
})
|
||||
def api_usermanager_users_create():
|
||||
user = create_usermanager_user(g.data["user_name"], g.data["wallet_name"], g.data["admin_id"])
|
||||
return jsonify(user._asdict()), Status.CREATED
|
||||
|
||||
|
||||
@usermanager_ext.route("/api/v1/users/<user_id>", methods=["DELETE"])
|
||||
@api_check_wallet_key(key_type="invoice")
|
||||
def api_usermanager_users_delete(user_id):
|
||||
print("cunt")
|
||||
user = get_usermanager_user(user_id)
|
||||
if not user:
|
||||
return jsonify({"message": "User does not exist."}), Status.NOT_FOUND
|
||||
delete_usermanager_user(user_id)
|
||||
return "", Status.NO_CONTENT
|
||||
|
||||
|
||||
###Wallets
|
||||
|
||||
@usermanager_ext.route("/api/v1/wallets", methods=["GET"])
|
||||
@api_check_wallet_key(key_type="invoice")
|
||||
def api_usermanager_wallets():
|
||||
user_id = g.wallet.user
|
||||
return jsonify([wallet._asdict() for wallet in get_usermanager_wallets(user_id)]), Status.OK
|
||||
|
||||
|
||||
@usermanager_ext.route("/api/v1/wallets", methods=["POST"])
|
||||
@api_check_wallet_key(key_type="invoice")
|
||||
@api_validate_post_request(schema={
|
||||
"user_id": {"type": "string", "empty": False, "required": True},
|
||||
"wallet_name": {"type": "string", "empty": False, "required": True},
|
||||
"admin_id": {"type": "string", "empty": False, "required": True}
|
||||
|
||||
})
|
||||
def api_usermanager_wallets_create():
|
||||
user = create_usermanager_wallet(g.data["user_id"], g.data["wallet_name"], g.data["admin_id"])
|
||||
return jsonify(user._asdict()), Status.CREATED
|
||||
|
||||
|
||||
@usermanager_ext.route("/api/v1/wallets<wallet_id>", methods=["GET"])
|
||||
@api_check_wallet_key(key_type="invoice")
|
||||
def api_usermanager_wallet_transactions(wallet_id):
|
||||
|
||||
return jsonify(get_usermanager_wallet_transactions(wallet_id)), Status.OK
|
||||
|
||||
@usermanager_ext.route("/api/v1/wallets/<user_id>", methods=["GET"])
|
||||
@api_check_wallet_key(key_type="invoice")
|
||||
def api_usermanager_wallet_balances(user_id):
|
||||
return jsonify(get_usermanager_wallet_balances(user_id)), Status.OK
|
||||
|
||||
|
||||
@usermanager_ext.route("/api/v1/wallets/<wallet_id>", methods=["DELETE"])
|
||||
@api_check_wallet_key(key_type="invoice")
|
||||
def api_usermanager_wallets_delete(wallet_id):
|
||||
wallet = get_usermanager_wallet(wallet_id)
|
||||
print(wallet.id)
|
||||
if not wallet:
|
||||
return jsonify({"message": "Wallet does not exist."}), Status.NOT_FOUND
|
||||
|
||||
delete_usermanager_wallet(wallet_id, wallet.user)
|
||||
|
||||
return "", Status.NO_CONTENT
|
||||
|
Loading…
Add table
Reference in a new issue